container icon indicating copy to clipboard operation
container copied to clipboard

[Bug] `container cp` follows symlinks at the copy root, so a copy can land outside the directory the user named

Open Badranh opened this issue 1 month ago • 2 comments

I have done the following

  • [x] I have searched the existing issues
  • [ ] If possible, I've reproduced the issue using the 'main' branch of this project

Steps to reproduce

All output below is from a live run on container 1.5.0 (d265d66), macOS 27.2 (26B5101f), Apple silicon.

Setup

A container is run with a volume mount, which is the only directory shared with it. /tmp/demo stands in for a directory the user cares about and /tmp/demo/project for the project directory they share.

mkdir -p /tmp/demo/project
echo ORIGINAL        > /tmp/demo/.zshrc
echo SECRET-HOST-DATA > /tmp/demo/.secret_host_file

container run -d --name demo -v /tmp/demo/project:/work alpine sleep 600

# the container creates two relative symlinks inside its own mount, plus some content.
# no knowledge of any host path is needed — /proc/mounts inside the guest shows only "virtiofs".
container exec demo sh -c '
  mkdir -p /app
  echo FROM_CONTAINER > /app/.zshrc
  echo marker > /app/README
  ln -sfn .. /work/out
  ln -sfn .. /work/in'

The links are now visible on the host, inside the user's project directory:

$ ls -la /tmp/demo/project
lrwxr-xr-x  in  -> ..
lrwxr-xr-x  out -> ..

A. Copying out lands outside the named directory and replaces an existing file

container cp demo:/app/. /tmp/demo/project/out

Observed:

/tmp/demo/project/out/.        <- the CLI prints the un-normalised path

$ ls -A /tmp/demo/project      # nothing was written inside the named directory
in out

$ cat /tmp/demo/.zshrc         # written one level up, and the existing file was replaced
FROM_CONTAINER

$ ls -A /tmp/demo              # README was also created outside the named directory
.secret_host_file .zshrc README project

B. Copying in reads from outside the named directory

container cp /tmp/demo/project/in/. demo:/stolen
container exec demo ls -A /stolen
container exec demo cat /stolen/.secret_host_file

Observed — the container receives the contents of the directory above the one named:

.secret_host_file
.zshrc
README
project

SECRET-HOST-DATA

Note on the source form

Both forms resolve the symlink and land outside the named directory. The /. form above makes the link target itself the copy root, so members are written directly into it and existing files are replaced. A plain source (container cp demo:/app /tmp/demo/project/out) makes the copy root <link target>/app, so a new app/ directory is created outside the named directory instead — the escape still happens, but nothing pre-existing is replaced.

Cleanup

container rm -f demo && rm -rf /tmp/demo

Problem description

container cp resolves the directory it copies to or from with symlinks followed, and never checks that the resolved copy root is still inside the path the user named. A copy aimed at a path inside the user's own project directory can therefore read from, or write to, a different directory entirely — replacing files that are already there, with no warning and a zero exit status.

What makes this reachable in practice is the volume mount. Anything a container writes into its mount appears on the host inside the user's directory, so the container can create the symlink itself (ln -sfn .. /work/out). A later ordinary container cp aimed at that path then lands outside the shared directory, in both directions: files the container chooses are written outside it, and files from outside it are read into the container. The user types nothing unusual — a destination inside their own project — and gets no indication that the copy went somewhere else.

#1957 ("Do not follow destination symlink when copying user configuration") took the position that following a destination symlink is worth refusing elsewhere in this project, which is why I think the same stance may be worth applying here.

Expected behaviour

A copy should land inside the directory the user named, or fail with a clear error — not silently resolve to a different directory and replace files there.

Where this happens

  • ContainerCopy.swift:70 decides whether the destination is a directory with FileManager.default.fileExists(atPath:isDirectory:), which follows symlinks, then line 77 builds finalDestPath = destPath.appending(lastComponent). The result is not normalised or re-checked against the directory the user named.
  • ArchiveReader.extractContents opens the copy root with FileDescriptor.open(rootFilePath, .readOnly) — no O_NOFOLLOW and no check that the path is not a symlink. ArchiveReader.swift:271 in containerization 0.47.0 (shipped in 1.5.0) and 0.49.0; :275 on main.
  • LinuxContainer.copyIn (LinuxContainer.swift:1348) makes the same link-following directory test, and derives isArchive from the symlink target's type.

Suggested direction

  1. Open the copy root with O_NOFOLLOW (or O_DIRECTORY | O_NOFOLLOW plus an fstat) and refuse a root that is a symlink.
  2. Normalise finalDestPath and verify it is still inside the directory the user named. The resolve-then-verify-containment pattern already exists in this codebase at ContainerBuild/BuildFSSync.swift:118-121.
  3. Answer "is this a directory?" from an already-open O_NOFOLLOW descriptor rather than FileManager.fileExists(atPath:isDirectory:). For copyIn, derive isArchive from an lstat of the source itself.

Happy to test a fix.

Environment

- OS: macOS 27.2 (26B5101f), Apple silicon
- Xcode: Version 27.0 (27A266a)
- Container: container CLI version 1.5.0 (build: release, commit: d265d66)

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

Badranh avatar Oct 11 '26 14:10 Badranh

Hi @dkovba, I was able to reproduce this on 1.5.0, both directions behave as described.

The CLI checks the local path with fileExists(atPath:isDirectory:), which follows symlinks, so out, out/ and out/. all end up at the link target. I'd like to fix it by checking the local path with lstat (after dropping a trailing /.) and refusing it if it's a symlink, for both copy in and copy out. This is a bit stricter than Docker, which follows host symlinks, but it matches what was done in #1957.

If that sounds okay, I'd like to work on this.

nandan-varma avatar Oct 13 '26 06:10 nandan-varma

Hi,

Thanks @nandan-varma .

@dkovba should the issue be treated as security issue or the severity is not high enough?

Thanks.

Badranh avatar Oct 15 '26 15:10 Badranh