Skip to content

Layer tar files contain entry for root (/) #1375

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

Open
neggert opened this issue Aug 7, 2020 · 7 comments · May be fixed by #3425
Open

Layer tar files contain entry for root (/) #1375

neggert opened this issue Aug 7, 2020 · 7 comments · May be fixed by #3425
Labels
area/layers differs-from-docker more-information-needed priority/p3 agreed that this would be good to have, but no one is available at the moment. uncategorized works-with-docker

Comments

@neggert
Copy link

neggert commented Aug 7, 2020

Actual behavior
Layer tar files contain a header for the path /, which looks like an absolute path name and creates problems for the internal non-Docker container runtime we use. Images created using Docker work just fine.

Expected behavior
Tar files should not contain absolute paths. GNU tar drops the leading / from absolute paths, so this mostly works, but other tools fail to extract images created by Kaniko.

To Reproduce
Use this Dockerfile:

FROM debian:buster

RUN apt-get update && \
    apt-get install -y --no-install-recommends vim && \
    rm -rf /var/lib/apt/lists/*

Build with kaniko, saving the image locally so that we can inspect it.

docker run -v $PWD:/workspace gcr.io/kaniko-project/executor@sha256:d60705cb55460f32cee586570d7b14a0e8a5f23030a0532230aaf707ad05cecd \
  --dockerfile /workspace/Dockerfile --context dir:///workspace/ --tarPath /workspace/image.tar \
  --no-push --destination=image

Extract and inspect the image. Observe the first entry: /.

$ tar xf image.tar
$ tar --list --verbose --file d201a7839a4aa3a639e61c9782ac73e714b1b2acd3919a6774c29536cdd395c7.tar.gz | head
drwxr-xr-x  0 0      0           0 Aug  7 13:52 /
drwxr-xr-x  0 0      0           0 Aug  7 13:52 etc/
drwxr-xr-x  0 0      0           0 Aug  7 13:52 etc/alternatives/
lrwxrwxrwx  0 0      0           0 Aug  7 13:52 etc/alternatives/editor -> /usr/bin/vim.basic
lrwxrwxrwx  0 0      0           0 Aug  7 13:52 etc/alternatives/editor.1.gz -> /usr/share/man/man1/vim.1.gz
lrwxrwxrwx  0 0      0           0 Aug  7 13:52 etc/alternatives/editor.da.1.gz -> /usr/share/man/da/man1/vim.1.gz
lrwxrwxrwx  0 0      0           0 Aug  7 13:52 etc/alternatives/editor.de.1.gz -> /usr/share/man/de/man1/vim.1.gz
lrwxrwxrwx  0 0      0           0 Aug  7 13:52 etc/alternatives/editor.fr.1.gz -> /usr/share/man/fr/man1/vim.1.gz
lrwxrwxrwx  0 0      0           0 Aug  7 13:52 etc/alternatives/editor.it.1.gz -> /usr/share/man/it/man1/vim.1.gz
lrwxrwxrwx  0 0      0           0 Aug  7 13:52 etc/alternatives/editor.ja.1.gz -> /usr/share/man/ja/man1/vim.1.gz

Triage Notes for the Maintainers

Description Yes/No
Please check if this a new feature you are proposing
Please check if the build works in docker but not in kaniko
Please check if this error is seen when you use --cache flag
Please check if your dockerfile is a multistage dockerfile
@tejal29
Copy link
Contributor

tejal29 commented Aug 12, 2020

@neggert Adding "/" does will create files "/etc" when exploded using tar -xvf. this layer.tar should work when you extract it at path "/"

@tejal29 tejal29 added more-information-needed priority/p3 agreed that this would be good to have, but no one is available at the moment. labels Aug 12, 2020
@neggert
Copy link
Author

neggert commented Aug 12, 2020

But there aren't any other absolute paths in the tar. Notice there is no leading slash for the paths in etc in the output I pasted.

Absolute paths in tars are usually a bad idea, to the point where GNU tar ignores them by default.

@neggert
Copy link
Author

neggert commented Aug 12, 2020

Actually, there's a pretty good explanation of the issue in the page I linked.

When tar extracts archive members from an archive, it strips any leading slashes ('/') from the member name. This causes absolute member names in the archive to be treated as relative file names. This allows you to have such members extracted wherever you want, instead of being restricted to extracting the member in the exact directory named in the archive. For example, if the archive member has the name '/etc/passwd', tar will extract it as if the name were really 'etc/passwd'.

File names containing '..' can cause problems when extracting, so tar normally warns you about such files when creating an archive, and rejects attempts to extracts such files.

Other tar programs do not do this. As a result, if you create an archive whose member names start with a slash, they will be difficult for other people with a non-GNU tar program to use. Therefore, GNU tar also strips leading slashes from member names when putting members into the archive. For example, if you ask tar to add the file '/bin/ls' to an archive, it will do so, but the member name will be 'bin/ls'(19).

@neggert
Copy link
Author

neggert commented Sep 1, 2020

@tejal29 Do you need anymore information on this? We're forced to revert to Docker because Kaniko-built images don't work in our system.

@neggert
Copy link
Author

neggert commented Sep 3, 2020

This appears to be the offending line of code.

@colindean
Copy link

I'm on Nic's team now. We're looking at Makisu as an alternative to Kaniko for building containers to be used in the environment in question. It does not build with / in the layers as Kaniko does with the above Dockerfile:

docker run -i --rm --net host -v /var/run/docker.sock:/docker.sock -e DOCKER_HOST=unix:///docker.sock -v /Users/colin/Downloads/test-delete-me:/makisu-context -v /tmp/makisu-storage:/makisu-storage gcr.io/uber-container-tools/makisu:latest build --commit=explicit --modifyfs=true --load --dest /makisu-context/deleteme.tar -t deleteme /makisu-context
$ tar xf deleteme.tar
$ tar --list --verbose --file 25a485*/layer.tar | head
drwxr-xr-x  0 0      0           0 Mar 10 19:00 bin/
-rwxr-xr-x  0 0      0        2345 Jan  5  2019 bin/uncompress
drwxr-xr-x  0 0      0           0 Mar 24 12:17 etc/
drwxr-xr-x  0 0      0           0 Mar 24 12:17 etc/alternatives/
<snip>
$ tar --list --verbose --file e2212*/layer.tar | head
drwxr-xr-x  0 0      0           0 Mar 10 19:00 bin/
-rwxr-xr-x  0 0      0     1168776 Apr 18  2019 bin/bash
-rwxr-xr-x  0 0      0       43744 Feb 28  2019 bin/cat
<snip>
$ find . -iname 'layer.tar' -exec tar --list --verbose --file {}  \; | grep ' /$'
$

@colindean
Copy link

colindean commented Apr 29, 2021

We've learned today via uber-archive/makisu#365 that Makisu is deprecated and will no longer be updated after May 4, 2021. That leaves Kaniko as our only currently available option in the long term so we're looking at how we can expedite this change or changing how our internal container runtime handles Kaniko-produced containers with this / entry.

zhiyuan-chn pushed a commit to zhiyuan-chn/kaniko that referenced this issue Mar 19, 2025
zhiyuan-chn pushed a commit to zhiyuan-chn/kaniko that referenced this issue Mar 19, 2025
zhiyuan-chn added a commit to zhiyuan-chn/kaniko that referenced this issue Mar 19, 2025
zhiyuan-chn added a commit to zhiyuan-chn/kaniko that referenced this issue Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/layers differs-from-docker more-information-needed priority/p3 agreed that this would be good to have, but no one is available at the moment. uncategorized works-with-docker
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants