Skip to content

fix(jni): verify the remote native library JAR and cache it per user - #465

Merged
manusa merged 1 commit into
mainfrom
fix/remote-jar-verification
Nov 22, 2026
Merged

manusa merged 1 commit into
mainfrom
fix/remote-jar-verification

Conversation

@manusa

@manusa manusa commented Nov 22, 2026

Copy link
Copy Markdown
Owner

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

  • Integrity.
    • The JAR's .sha1 is 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.
    • A missing or malformed .sha1 fails the download. Maven Central and the Sonatype snapshot repository always publish one, and the repository URLs are fixed.
    • The checksum replaces fix: isolate tests from the user's Helm config, harden JAR download #464's ZIP check, which it makes redundant.
  • Per-user cache directory.
    • The default is $XDG_CACHE_HOME/helm-java or ~/.cache/helm-java on Linux and macOS, and %LOCALAPPDATA%\helm-java\cache on Windows. The new com.marcnuri.jkube-helm.cacheDir system property overrides it.
    • On POSIX file systems, the directory is created rwx------, and the JAR and its record rw------- with O_EXCL, whatever the umask.
    • An existing directory is refused if it's a symbolic link, belongs to another user, or is group- or world-writable. Windows relies on the user profile's permissions.
    • The current user is never looked up with a file in the directory being checked, since another user able to rename files there could swap it. It's the owner of /proc/self where available (Linux), which needs no file at all. Otherwise it's the owner of a probe file created in java.io.tmpdir, which is sticky, or private to the user (as on macOS), and deleted right away. Either is more reliable than user.name in containers with an arbitrary user id.
  • Fallback.
    • If the default directory can't be used, a new private temporary directory is used. Examples: no home directory (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).
    • Each JVM then downloads the JAR again, and a java.util.logging warning says why.
    • A cacheDir that can't be used, and any unsafe directory, fail with an error naming the property.
  • Cheap cache hits.
    • When a JAR is verified and cached, its SHA-1 and size are recorded next to it in <jar>.verified, written atomically and owner-only.
    • A cached JAR is used if the JAR and its record are private files and the JAR still has the recorded size. A private file here is a regular file (not a symbolic link) that, on POSIX, belongs to the current user and isn't writable by others.
    • Otherwise, including when the record is corrupt, the JAR is downloaded and verified again. Cached JARs aren't re-hashed on every JVM start.
  • Housekeeping.
    • Stale files that helm-java creates in the cache (*.jar.<uuid>.part, *.jar.verified.<uuid>.part) are deleted before the next download once they haven't been modified for an hour.
    • Snapshot metadata without a <snapshot> element, or with an invalid timestamp or build number, fails with a clear error naming the metadata URL.
    • Invalid metadata no longer prints [Fatal Error] … to stderr.
  • Tests.
    • 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.
    • The cache directory, verification and cache hits are covered against a local HttpServer, with a controlled environment: no test depends on the real LOCALAPPDATA, XDG_CACHE_HOME, user.home or umask.
  • Build: maven-jar-plugin is 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 location: the native library JAR is now cached in ~/.cache/helm-java (Linux, macOS) or %LOCALAPPDATA%\helm-java\cache (Windows), not in the temporary directory.
    • The first run after upgrading downloads it once more.
    • JARs cached by earlier versions in the temporary directory (<os>-<arch>-<version>.jar) are no longer used and can be deleted.
    • The new com.marcnuri.jkube-helm.cacheDir system property sets another directory.
  • Verification:
    • Downloads are verified against the repository's SHA-1 checksum. A missing or invalid checksum fails the download.
    • The cached files are readable and writable only by their owner.
    • A cached JAR is used only if other users can't write it and it still has its recorded size; otherwise it's downloaded again.
  • Fallback: if the default directory can't be used (no home directory, a read-only file system such as some containers or AWS Lambda), the JAR is downloaded to a private temporary directory on each start, and a java.util.logging warning says so. Set com.marcnuri.jkube-helm.cacheDir to keep a persistent cache.
  • Refused directories: a cache directory that is group- or world-writable, a symbolic link, or another user's is refused with an error naming the property. So is a configured cacheDir that can't be created or written.

Threat model and residual risk

  • Protected against: another local user planting or replacing the cached JAR or its record. They can't write the directory, which is checked on every use, or the files, which are created owner-only and checked on every cache hit. Nothing is written to a directory before it has passed these checks, and the user it must belong to is determined without writing a file there. The .part files and the stale cleanup live only in that private directory.
  • Same-user processes: not protected against. Anything running as the same user can replace the JAR, with another of the same size or together with its record. It can also do so between the checks and the URLClassLoader opening 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.
  • The checksum's origin: the .sha1 comes 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.
  • Not checked: the cache directory's parent directories. Where there's no /proc (macOS), the current user comes from a probe file in java.io.tmpdir, so a java.io.tmpdir deliberately 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 custom cacheDir there is trusted as given.
  • Root-owned directories: treated as unusable rather than unsafe (root isn't the adversary). Under user namespaces or NFS root squash, root-owned files show another user id, so they're refused as another user's instead, which fails safe. A read-only root-owned cache baked into an image isn't supported. For images, adding the platform artifact to the classpath avoids the download altogether.
  • Windows fallback leak: in the fallback temporary directory, files are deleted when the JVM exits. On Windows the loaded JAR is still open then (its class loader is never closed), so about 22 MB stays in %TEMP% per JVM that falls back.

Testing

On Java 8 (linux/amd64), in a sandbox where the home directory's .cache is read-only:

  • lib/api: 102 tests pass, with 1 skipped (CacheDirectoryTest 34, RemoteJarLoaderTest 34). They run against a local HttpServer, with no mocks.
    • The skipped test needs a directory with uid 0, and the sandbox maps root to nobody.
    • The same run passes under umask 002 with XDG_CACHE_HOME and LOCALAPPDATA set, 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.
  • Missing java.io.tmpdir, checked with a separate JVM started with -Djava.io.tmpdir=<missing>:
    • a configured cacheDir and a default under a writable home both work (on Linux the current user comes from /proc/self; elsewhere a missing java.io.tmpdir fails with an error naming the property);
    • an unusable default fails with an error naming the property.
  • Build: mvn validate over the reactor no longer warns about the missing maven-jar-plugin version.
  • Repositories: checked with curl. Maven Central (linux-amd64-0.0.1.jar.sha1) and the Sonatype snapshot repository (the timestamped .sha1) both serve 40-hex checksums, and the release one matches sha1sum of the JAR. .sha256 and .sha512 aren't published.

Mutation checks: each of these fails a test.

  • not comparing the download's checksum;
  • files created with the umask;
  • a cache hit that ignores the size, accepts files writable by others, follows a symbolic link, or lets a malformed record escape;
  • an unsafe default falling back;
  • an unusable default not falling back;
  • a configured directory falling back;
  • no fallback warning;
  • the directory not created owner-only, or accepted when writable by others or a symbolic link;
  • looking up the current user with a file in the directory being checked (it would change the directory's modification time);
  • the stale cleanup matching any .part file, or not running;
  • the snapshot version not validated, or a missing <snapshot> not detected;
  • the default XML error handler;
  • the Windows move fallback accepting whatever is at the JAR path.

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:

  • Windows: CI runs the platform-specific paths: LOCALAPPDATA, the remote tests' cache directory cleanup with the open JAR, and the temporary-directory fallback.
  • Other users and root: another user's directory or file can't be created as a single non-root user, so the mutation that drops the owner check on a cache hit survives. The root-by-uid test runs on CI's Linux and macOS runners.
  • Not testable without mocks:
    • the Windows move fallback's use of a JAR another JVM holds open (only its rethrow branch is tested), and its SHA-1 comparison;
    • the registration of the fallback's files for deletion on exit, which only happens at JVM exit.

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 manusa left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.tmpdir deliberately pointed at a non-sticky shared directory.

Fixed during review

  • Round 1
    • Critical: a CacheDirectoryTest case read the real LOCALAPPDATA/XDG_CACHE_HOME and 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 cacheDir or an unsafe directory still fails loudly.
      • NativeLibraryTest wrote 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.
  • Round 2
    • Important: the remote NativeLibraryTest cases would have failed @TempDir cleanup 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 .verified record escaping as IllegalArgumentException;
      • root detected by name rather than uid;
      • docs not stating the same-size limitation.
  • 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 and hidepid), or from a probe in a sticky or per-user java.io.tmpdir.
  • 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 under umask 002 with XDG_CACHE_HOME and LOCALAPPDATA set, and nothing is written to those directories.
  • Missing java.io.tmpdir (a separate JVM):
    • a configured cacheDir works;
    • a default under a writable home works;
    • an unusable default fails with an error naming the property.
  • 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 .part file;
    • unvalidated snapshot versions;
    • the default XML error handler.
  • Repositories: checked with curl. Maven Central and the Sonatype snapshot repository both serve 40-hex .sha1 files, and the release one matches the JAR. Neither publishes .sha256 or .sha512.

Covered only by CI, or not at all

  • Windows: the LOCALAPPDATA path, 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.

@manusa
manusa merged commit c11dca5 into main Nov 22, 2026
3 checks passed
@manusa
manusa deleted the fix/remote-jar-verification branch November 22, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant