Repository navigation
fix(jni): verify the remote native library JAR and cache it per user - #465
Merged
Merged
Conversation
The JAR downloaded when no platform artifact is on the classpath holds native code, yet it was cached at a predictable path in the shared temporary directory and loaded from there without any check, so another local user could plant one first. - the download is verified against the repository's .sha1 (fail closed), and cached in a per-user directory (~/.cache/helm-java, %LOCALAPPDATA%\helm-java\cache, or the new com.marcnuri.jkube-helm.cacheDir system property) that is created owner-only and refused if unsafe; the files are owner-only too - a cache hit is a private file of the size recorded when verified - an unusable default directory falls back to a private temporary one, with a warning - stale .part files are cleaned up, snapshot metadata is validated, and invalid metadata no longer prints to stderr - NativeLibrary.getInstance() reporting the cause is tested, and the remote tests use their own cache directory - maven-jar-plugin is pinned, so builds no longer resolve its version from Maven Central Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Marc Nuri <marc@marcnuri.com>
manusa
commented
Nov 22, 2026
manusa
left a comment
Owner
Author
There was a problem hiding this comment.
Review of #465
Verdict: approve. This is posted as a comment only because it's my own PR. The change went through four review rounds before it was pushed, and every Critical and Important finding is fixed in dd5edbc.
Threat model
The threat model is sound for the stated adversary: another local user planting or replacing the cached JAR.
- Cache directory: private, created
rwx------. An existing directory is refused if it's a symlink, owned by another user, or group- or world-writable. - Cached files: owner-only (
O_EXCL,rw-------) whatever the umask. - Downloads: verified against the repository's
.sha1, failing closed. - Cache hits: accept only private regular files of the size recorded at verification.
The residual risks are documented honestly in the description:
- same-user processes, including a same-size swap and the TOCTOU window before
URLClassLoader; - a checksum served from the same HTTPS origin, which gives integrity only;
- unchecked parent directories;
- trusted custom directories on Windows;
- a
java.io.tmpdirdeliberately pointed at a non-sticky shared directory.
Fixed during review
- Round 1
- Critical: a
CacheDirectoryTestcase read the realLOCALAPPDATA/XDG_CACHE_HOMEand would have failed on every Windows run. Selection now takes the environment as a parameter. - Important:
- Cached files inherited the umask.
- An unusable default cache directory failed hard. It now falls back to a private per-JVM temporary directory with a warning. An explicit
cacheDiror an unsafe directory still fails loudly. NativeLibraryTestwrote to the real~/.cache.- The threat model overstated what a re-hash on every start buys. A cache hit is now a cheap privacy and size check, and the SHA-1 is computed only on download.
- Critical: a
- Round 2
- Important: the remote
NativeLibraryTestcases would have failed@TempDircleanup on Windows, which can't delete the open JAR. - Minor:
- a hard dependency on
java.io.tmpdir; - a silent fallback, and its cleanup registration;
- a malformed
.verifiedrecord escaping asIllegalArgumentException; - root detected by name rather than uid;
- docs not stating the same-size limitation.
- a hard dependency on
- Important: the remote
- Round 3
- Important: the owner probe ran inside the directory being checked, before it was checked. Under a root JVM pointed at another user's directory, that user could race the probe and become the reference "current user". Now nothing is written to a directory before it passes the checks. The current user comes from the owner of
/proc/self(the effective uid, including under setuid, setcap,gosu/su, user namespaces andhidepid), or from a probe in a sticky or per-userjava.io.tmpdir.
- Important: the owner probe ran inside the directory being checked, before it was checked. Under a root JVM pointed at another user's directory, that user could race the probe and become the reference "current user". Now nothing is written to a directory before it passes the checks. The current user comes from the owner of
- Round 4: ready to commit. Two wording and test-timestamp nits were applied.
Verified by running
All runs used Java 8, in a sandbox where the home directory's .cache is read-only.
lib/api: 102 tests pass with 1 skipped. The skipped one needs a uid-0 directory and runs on the Linux and macOS runners. The suite also passes underumask 002withXDG_CACHE_HOMEandLOCALAPPDATAset, and nothing is written to those directories.- Missing
java.io.tmpdir(a separate JVM):- a configured
cacheDirworks; - a default under a writable home works;
- an unusable default fails with an error naming the property.
- a configured
- Mutations: each of these makes a test fail:
- not comparing the checksum;
- files created with the umask;
- a cache hit that ignores the size, accepts files writable by others, follows a symlink, or lets a malformed record escape;
- unsafe and unusable directories classified the wrong way round;
- a probe written into the checked directory;
- the stale cleanup matching any
.partfile; - unvalidated snapshot versions;
- the default XML error handler.
- Repositories: checked with curl. Maven Central and the Sonatype snapshot repository both serve 40-hex
.sha1files, and the release one matches the JAR. Neither publishes.sha256or.sha512.
Covered only by CI, or not at all
- Windows: the
LOCALAPPDATApath, the remote tests' cleanup with the open JAR, and the fallback. - Real downloads: the downloads from Maven Central and Sonatype through the new verification.
- Not testable without mocks or a second uid:
- a cache hit owned by another user;
- the Windows move fallback's use of a JAR another JVM holds open;
- deletion-on-exit registration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #464 (context: #409) in the remote native JAR fallback:
RemoteJarLoader, used when no platform artifact is on the classpath, e.g. with Gradle.Until now the downloaded JAR, which contains native code, was cached at a predictable path in the shared
java.io.tmpdir, and a cache hit was loaded without any check. Another local user could plant a JAR there first.Changes
.sha1is fetched from the same repository, and the download is hashed while it's written. The JAR is moved into the cache only if they match..sha1fails the download. Maven Central and the Sonatype snapshot repository always publish one, and the repository URLs are fixed.$XDG_CACHE_HOME/helm-javaor~/.cache/helm-javaon Linux and macOS, and%LOCALAPPDATA%\helm-java\cacheon Windows. The newcom.marcnuri.jkube-helm.cacheDirsystem property overrides it.rwx------, and the JAR and its recordrw-------withO_EXCL, whatever the umask./proc/selfwhere available (Linux), which needs no file at all. Otherwise it's the owner of a probe file created injava.io.tmpdir, which is sticky, or private to the user (as on macOS), and deleted right away. Either is more reliable thanuser.namein containers with an arbitrary user id.user.home=?,HOME=/nonexistent), a read-only file system, a parent owned by someone else, or a root-owned directory (detected by user id where available).java.util.loggingwarning says why.cacheDirthat can't be used, and any unsafe directory, fail with an error naming the property.<jar>.verified, written atomically and owner-only.*.jar.<uuid>.part,*.jar.verified.<uuid>.part) are deleted before the next download once they haven't been modified for an hour.<snapshot>element, or with an invalid timestamp or build number, fails with a clear error naming the metadata URL.[Fatal Error] …to stderr.NativeLibrary.getInstance()reporting the download failure as its cause is now tested.NativeLibraryTest's remote cases use their own temporary cache directory, deleted best-effort; on Windows the open JAR is left behind.HttpServer, with a controlled environment: no test depends on the realLOCALAPPDATA,XDG_CACHE_HOME,user.homeor umask.maven-jar-pluginis pinned to 3.5.1, the version Maven resolved. Without a version, every build looked it up on Maven Central, which failed a CI run of fix: isolate tests from the user's Helm config, harden JAR download #464.Release notes
For applications that rely on the download from Maven Central (no platform artifact on the classpath):
~/.cache/helm-java(Linux, macOS) or%LOCALAPPDATA%\helm-java\cache(Windows), not in the temporary directory.<os>-<arch>-<version>.jar) are no longer used and can be deleted.com.marcnuri.jkube-helm.cacheDirsystem property sets another directory.java.util.loggingwarning says so. Setcom.marcnuri.jkube-helm.cacheDirto keep a persistent cache.cacheDirthat can't be created or written.Threat model and residual risk
.partfiles and the stale cleanup live only in that private directory.URLClassLoaderopening the JAR (TOCTOU). Such a process can already run any code as that user. The size check on a cache hit catches an incomplete or corrupt replacement, not a deliberate one..sha1comes from the same repository over HTTPS, so it doesn't protect against a compromised or spoofed repository; that would need signature (.asc) verification. SHA-1 is broken for collisions, not for second preimages./proc(macOS), the current user comes from a probe file injava.io.tmpdir, so ajava.io.tmpdirdeliberately set to a non-sticky directory that other users can write isn't protected. On non-POSIX file systems (Windows) the permission checks are skipped, and a customcacheDirthere is trusted as given.%TEMP%per JVM that falls back.Testing
On Java 8 (linux/amd64), in a sandbox where the home directory's
.cacheis read-only:lib/api: 102 tests pass, with 1 skipped (CacheDirectoryTest34,RemoteJarLoaderTest34). They run against a localHttpServer, with no mocks.nobody.umask 002withXDG_CACHE_HOMEandLOCALAPPDATAset, and nothing is written to those directories.NativeLibraryTest: the offline cases pass, including the new cause test. The two remote cases can't reach Maven Central from the sandbox. They get past their own cache directory and fail on the network (UnknownHostException), so CI covers them.java.io.tmpdir, checked with a separate JVM started with-Djava.io.tmpdir=<missing>:cacheDirand a default under a writable home both work (on Linux the current user comes from/proc/self; elsewhere a missingjava.io.tmpdirfails with an error naming the property);mvn validateover the reactor no longer warns about the missingmaven-jar-pluginversion.linux-amd64-0.0.1.jar.sha1) and the Sonatype snapshot repository (the timestamped.sha1) both serve 40-hex checksums, and the release one matchessha1sumof the JAR..sha256and.sha512aren't published.Mutation checks: each of these fails a test.
.partfile, or not running;<snapshot>not detected;Moving the current-user lookup before the writable-by-others check survives. That's harmless now, because the lookup no longer writes to the directory.
Verified only by CI, or not at all:
LOCALAPPDATA, the remote tests' cache directory cleanup with the open JAR, and the temporary-directory fallback.