Skip to content

Fix host UID/GID leakage in copyFileToContainer TAR entries - #11929

Open
Gautam-aman wants to merge 1 commit into
testcontainers:mainfrom
Gautam-aman:fix-rootless-copyfile
Open

Gautam-aman wants to merge 1 commit into
testcontainers:mainfrom
Gautam-aman:fix-rootless-copyfile

Conversation

@Gautam-aman

Copy link
Copy Markdown

Fixes #11487

When copyFileToContainer copies a MountableFile,
TAR entries may carry host UID/GID values into
the container.

On rootless Docker setups these IDs may be
unmapped, causing permission and ownership issues.

This change normalizes TAR entry ownership to
uid=0 and gid=0 and adds a regression test.

@Gautam-aman
Gautam-aman requested a review from a team as a code owner July 16, 2026 09:43
@Gautam-aman

Copy link
Copy Markdown
Author

This revives the approach from #11488, which was
previously approved but later closed after the
source repository was deleted.

@myroch

myroch commented Dec 3, 2026

Copy link
Copy Markdown

We hit the same bug on rootless Podman, where it fails harder than on rootless Docker: the copy itself is rejected and the container never starts.

Setup: Testcontainers 2.0.5, commons-compress 1.28.0, Podman 5.8.2 rootless (API socket), RHEL 9.9. The host user is a directory (AD) account with a large UID (9-digit), far outside the 65536 IDs mapped into the user namespace.

Reproduction: withCopyFileToContainer(MountableFile.forHostPath(file), "/policy/bundle.tar.gz") on a plain GenericContainer (Open Policy Agent image, in our case a Quarkus Dev Service):

lchown /policy/bundle.tar.gz: invalid argument

new TarArchiveEntry(File, name) in MountableFile reads unix:uid/unix:gid from the host file, and Podman cannot lchown to an ID that isn't mapped.

Workaround: copying the same bytes with Transferable.of(bytes, mode) works, since it writes owner 0:0 (root in a rootless container is the host user anyway). With that change the same container starts fine. So normalizing the ownership to 0:0 as this PR does is the right fix for us. It would remove the need for that workaround in every Dev Service and test resource that copies host files.

Happy to test a snapshot build.

@myroch myroch left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for picking this up again. The change itself looks right to me: recursiveTar is the only place MountableFile creates entries, so files, nested files and directory entries are all covered, and resetting the owner after new TarArchiveEntry(File, …) is the right order, since that constructor is what reads unix:uid/unix:gid.

A few points that might help get it merged:

  1. Behaviour change on rootful Docker. Today the host UID is kept. When it happens to match the container user (often 1000), a 0600 file copied in stays readable for that user. With root ownership it no longer is, for any image that doesn't run as root. That's probably the main thing to decide, and worth a note in the PR description and changelog. Whether it needs an opt-out is for the maintainers, but MountableFile.forHostPath(path, mode) already lets users widen the mode, which could be the suggested migration.

  2. Test:

    • Please drop the System.out.println lines.
    • The assertion also passes without the fix when the build runs as root (UID 0). It's fine on the GitHub runners, but checking that the source file's unix:uid is non-zero first (or assumeTrue on it) would make the test meaningful everywhere.
    • A case for a copied directory (nested file plus directory entry) would cover the recursive path.
    • Optionally assert getUserName()/getGroupName() too, since the PR sets them.
  3. Branch: it's quite a bit behind main. A rebase would let the full CI run once a maintainer approves the workflow.

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.

[Bug]: copyFileToContainer retention of uid/gid breaks rootless Docker

2 participants