-
Notifications
You must be signed in to change notification settings - Fork 919
Reject symlinked Containerfile and ignore files escaping build context #6886
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,12 @@ package util //nolint:revive,nolintlint | |
|
|
||
| import ( | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| securejoin "github.com/cyphar/filepath-securejoin" | ||
| "go.podman.io/buildah/pkg/parse" | ||
| ) | ||
|
|
||
|
|
@@ -41,42 +43,76 @@ func MirrorToTempFileIfPathIsDescriptor(file string) (string, bool) { | |
| } | ||
|
|
||
| // DiscoverContainerfile tries to find a Containerfile or a Dockerfile within the provided `path`. | ||
| // The path may be a directory (in which case Containerfile/Dockerfile is searched inside it) | ||
| // or a direct path to a container file. | ||
| // | ||
| // Symlinked Containerfile/Dockerfile entries are only used when their real | ||
| // target stays inside the build context directory. Symlinks that resolve | ||
| // outside the context or are dangling are skipped. | ||
| func DiscoverContainerfile(path string) (foundCtrFile string, err error) { | ||
| // Test for existence of the file | ||
| target, err := os.Stat(path) | ||
| path, err = filepath.Abs(path) | ||
| if err != nil { | ||
| return "", fmt.Errorf("discovering Containerfile: %w", err) | ||
| } | ||
|
|
||
| switch mode := target.Mode(); { | ||
| case mode.IsDir(): | ||
| // If the path is a real directory, we assume a Containerfile or a Dockerfile within it | ||
| ctrfile := filepath.Join(path, "Containerfile") | ||
| target, err := os.Lstat(path) | ||
| if err != nil { | ||
| return "", fmt.Errorf("discovering Containerfile: %w", err) | ||
| } | ||
|
|
||
| // Test for existence of the Containerfile file | ||
| file, err := os.Stat(ctrfile) | ||
| if err != nil { | ||
| // See if we have a Dockerfile within it | ||
| ctrfile = filepath.Join(path, "Dockerfile") | ||
| // If path is a symlink to a directory (e.g. the build context itself is | ||
| // a symlink), follow it so the IsDir() branch handles it. | ||
| if target.Mode()&os.ModeSymlink != 0 { | ||
| if realInfo, err := os.Stat(path); err == nil && realInfo.IsDir() { | ||
| target = realInfo | ||
| } | ||
| } | ||
|
|
||
| // Test for existence of the Dockerfile file | ||
| file, err = os.Stat(ctrfile) | ||
| if err != nil { | ||
| return "", fmt.Errorf("cannot find Containerfile or Dockerfile in context directory: %w", err) | ||
| switch { | ||
| case target.IsDir(): | ||
| for _, name := range []string{"Containerfile", "Dockerfile"} { | ||
| ctrfile := filepath.Join(path, name) | ||
| if resolved, ok := isRegularFileInContext(path, ctrfile); ok { | ||
| return resolved, nil | ||
| } | ||
| } | ||
| return "", fmt.Errorf("cannot find Containerfile or Dockerfile in context directory: %w", fs.ErrNotExist) | ||
|
|
||
| case target.Mode().IsRegular(): | ||
| return path, nil | ||
|
|
||
| // The file exists, now verify the correct mode | ||
| if mode := file.Mode(); mode.IsRegular() { | ||
| foundCtrFile = ctrfile | ||
| } else { | ||
| return "", fmt.Errorf("assumed Containerfile %q is not a file", ctrfile) | ||
| case target.Mode()&os.ModeSymlink != 0: | ||
| if resolved, ok := isRegularFileInContext(filepath.Dir(path), path); ok { | ||
| return resolved, nil | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what we gain by ensuring that the last component in a pathname is not a symbolic link to somewhere outside of its parent directory when that directory can already be outside of any known build context directory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But that would be a breaking change, no? Currently we do support file as an argument in the place of
But it does not seem to work together with $ buildah build -t demo -f test-repository6/Dockerfile test-repository6/Dockerfile
Error: mounting an overlay over build context directory: creating overlay scaffolding for build context directory: mount overlay:/var/tmp/buildah-context-231279313/overlay/388852530/merge, data: lowerdir=/home/sbrauner/Desktop/cve-work/buildah-build/test-repository6/Dockerfile,upperdir=/var/tmp/buildah-context-231279313/overlay/388852530/upper,workdir=/var/tmp/buildah-context-231279313/overlay/388852530/work,context="system_u:object_r:container_file_t:s0:c231,c966",userxattr: invalid argument |
||
| return "", fmt.Errorf("assumed Containerfile %q is not a file", path) | ||
|
|
||
| case mode.IsRegular(): | ||
| // If the context dir is a file, we assume this as Containerfile | ||
| foundCtrFile = path | ||
| default: | ||
| return "", fmt.Errorf("assumed Containerfile %q is not a file", path) | ||
| } | ||
| } | ||
|
|
||
| return foundCtrFile, nil | ||
| // isRegularFileInContext checks whether path resolves to a regular file | ||
| // inside contextDir using RESOLVE_IN_ROOT semantics (securejoin.SecureJoin): | ||
| // ".." components are clamped to the root and absolute symlink targets are | ||
| // re-rooted under contextDir. This matches Docker BuildKit's behavior. | ||
| // | ||
| // On success it returns the resolved host path. | ||
| func isRegularFileInContext(contextDir, path string) (string, bool) { | ||
| name, err := filepath.Rel(contextDir, path) | ||
| if err != nil { | ||
| return "", false | ||
| } | ||
| resolved, err := securejoin.SecureJoin(contextDir, name) | ||
| if err != nil { | ||
| return "", false | ||
| } | ||
| fi, err := os.Stat(resolved) | ||
| if err != nil { | ||
| return "", false | ||
| } | ||
| if !fi.Mode().IsRegular() { | ||
| return "", false | ||
| } | ||
| return resolved, true | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
lstatis ran there, it seems to cause trouble when the symlink is to a directory.I would propose either a special case for directories, or delaying the
lstatcall after we know that it is not a directory. Because building in a directory which is a symlink is a valid use case, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that seems right. I will take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now