Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generator: Handle symlinks when copying from a container #90

Merged
merged 1 commit into from Mar 19, 2024

Conversation

euanh
Copy link
Contributor

@euanh euanh commented Mar 19, 2024

docker cp tries to avoid creating simlinks which would break out of the directory it is creating. In #88, when copying /usr/lib/x86_64-linux-gnu from a running container, a symlink pointing back 4 layers is rejected:

invalid symlink: <dest>/usr/lib/x86_64-linux-gnu/hdf5/serial/include" -> "../../../../include/hdf5/serial"

The intended target of the symlink is in <dest>/usr/include which is outside the path being copied. The error message is generated here:

https://github.com/moby/moby/blob/dd146571ea458082c499c647ba36580bb7277131/pkg/archive/archive.go#L753

Symlinks which point inside the directory tree being copied do not cause errors:

<dest>/usr/lib/x86_64-linux-gnu/libxcb-render.so.0 -> libxcb-render.so.0.0.0

The "hdf5 include" error does not occur if we copy the whole of the /usr directory from the container, making the symlink no longer break out of the copied directory:

% docker cp <container>:/usr <dest>
Successfully copied 3.24GB to <dest>

This commit avoids the error by asking docker cp' to print a 'tar' stream to standard output and piping it to a local tarprocess. The localtar` does not complain about creating potential dangling symlinks.

The performance of piping to 'tar' should be similar to using "normal" docker cp because dockerd always sends a tar stream to the docker CLI. In the normal case, docker cp unarchives the tar stream, using the library which checks for symlinks which break out of the tree. In the "tar stream" case, docker cp just prints the stream to standard output.

Co-authored-by: @t089
Fixes #88

`docker cp` tries to avoid creating simlinks which would break out
of the directory it is creating.   In apple#88, when copying
`/usr/lib/x86_64-linux-gnu` from a running container, a symlink
pointing back 4 layers is rejected:

    invalid symlink: <dest>/usr/lib/x86_64-linux-gnu/hdf5/serial/include" -> "../../../../include/hdf5/serial"

The intended target of the symlink is in `<dest>/usr/include` which is
outside the path being copied.    The error message is generated
here:

    https://github.com/moby/moby/blob/dd146571ea458082c499c647ba36580bb7277131/pkg/archive/archive.go#L753

Symlinks which point inside the directory tree being copied
do not cause errors:

    <dest>/usr/lib/x86_64-linux-gnu/libxcb-render.so.0 -> libxcb-render.so.0.0.0

The "hdf5 include" error does not occur if we copy the whole of the
`/usr` directory from the container, making the symlink no longer break
out of the copied directory:

    % docker cp <container>:/usr <dest>
    Successfully copied 3.24GB to <dest>

This commit avoids the error by asking `docker cp' to print a 'tar'
stream to standard output and piping it to a local `tar` process.
The local `tar` does not complain about creating potential dangling
symlinks.

The performance of piping to 'tar' should be similar to using
"normal" `docker cp` because `dockerd` always sends a `tar` stream
to the `docker` CLI.   In the normal case, `docker cp` unarchives
the `tar` stream, using the library which checks for symlinks which
break out of the tree.  In the "tar stream" case, `docker cp` just
prints the stream to standard output.

Co-authored-by: @t089
Fixes apple#88
@euanh euanh requested a review from MaxDesiatov as a code owner March 19, 2024 11:12
Copy link
Member

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Thanks!

@MaxDesiatov
Copy link
Member

@swift-ci test

@euanh euanh added the bug Something isn't working label Mar 19, 2024
@t089
Copy link

t089 commented Mar 19, 2024

Thanks for tackling this!

@euanh euanh merged commit aa28983 into apple:main Mar 19, 2024
3 checks passed
@euanh euanh deleted the docker-cp-tar-symlinks branch March 19, 2024 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error building with docker
3 participants