[Bug] `container cp` follows symlinks at the copy root, so a copy can land outside the directory the user named
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:70decides whether the destination is a directory withFileManager.default.fileExists(atPath:isDirectory:), which follows symlinks, then line 77 buildsfinalDestPath = destPath.appending(lastComponent). The result is not normalised or re-checked against the directory the user named.ArchiveReader.extractContentsopens the copy root withFileDescriptor.open(rootFilePath, .readOnly)— noO_NOFOLLOWand 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 onmain.LinuxContainer.copyIn(LinuxContainer.swift:1348) makes the same link-following directory test, and derivesisArchivefrom the symlink target's type.
Suggested direction
- Open the copy root with
O_NOFOLLOW(orO_DIRECTORY | O_NOFOLLOWplus anfstat) and refuse a root that is a symlink. - Normalise
finalDestPathand verify it is still inside the directory the user named. The resolve-then-verify-containment pattern already exists in this codebase atContainerBuild/BuildFSSync.swift:118-121. - Answer "is this a directory?" from an already-open
O_NOFOLLOWdescriptor rather thanFileManager.fileExists(atPath:isDirectory:). ForcopyIn, deriveisArchivefrom anlstatof 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
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.
Hi,
Thanks @nandan-varma .
@dkovba should the issue be treated as security issue or the severity is not high enough?
Thanks.