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

Use securejoin.SecureJoin when forming userns paths #2134

Merged
merged 1 commit into from
Oct 14, 2024

Conversation

mheon
Copy link
Member

@mheon mheon commented Oct 14, 2024

We need to read /etc/passwd and /etc/group in the container to get an idea of how many UIDs and GIDs we need to allocate for a user namespace when --userns=auto is specified. We were forming paths for these using filepath.Join, which is not safe for paths within a container, resulting in this CVE allowing crafted symlinks in the container to access paths on the host instead.

Addresses CVE-2024-9676

We need to read /etc/passwd and /etc/group in the container to
get an idea of how many UIDs and GIDs we need to allocate for a
user namespace when `--userns=auto` is specified. We were forming
paths for these using filepath.Join, which is not safe for paths
within a container, resulting in this CVE allowing crafted
symlinks in the container to access paths on the host instead.

Addresses CVE-2024-9676

Signed-off-by: Matt Heon <[email protected]>
@mheon mheon force-pushed the fix_CVE-2024-9676 branch from fba61fc to 491d72e Compare October 14, 2024 16:36
Copy link
Collaborator

@mtrmac mtrmac left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

just some minor nit, the fix itself looks good to me though I haven't tried to reproduce it

Comment on lines +1 to +2
//go:build linux

Copy link
Member

Choose a reason for hiding this comment

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

Just to make sure does this still build the freebsd build in podman?

Another thing if we now that this will only ever be used on linux we can rename userns.go to userns_linux.go which I think is a bit better, also for the test userns_linux_test.go

Copy link
Member Author

Choose a reason for hiding this comment

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

Should be fine, FreeBSD doesn't support user namespaces last I checked.

Copy link
Member

@kwilczynski kwilczynski Jan 1, 2025

Choose a reason for hiding this comment

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

@mheon
Copy link
Member Author

mheon commented Oct 14, 2024

Cross builds:

env CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -compiler gc -tags " " ./...
env CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -compiler gc -tags " " -o containers-storage.freebsd.amd64 ./cmd/containers-storage
env CGO_ENABLED=0 GOOS=freebsd GOARCH=arm64 go build -compiler gc -tags " " ./...
env CGO_ENABLED=0 GOOS=freebsd GOARCH=arm64 go build -compiler gc -tags " " -o containers-storage.freebsd.arm64 ./cmd/containers-storage

So I think we're fine

Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

yes this looks good then, thanks

LGTM

@nalind
Copy link
Member

nalind commented Oct 14, 2024

LGTM

@mheon
Copy link
Member Author

mheon commented Oct 14, 2024

@rhatdan PTAL and merge

@mtrmac
Copy link
Collaborator

mtrmac commented Oct 14, 2024

/lgtm

Three reviewers should be enough :)

@mtrmac
Copy link
Collaborator

mtrmac commented Oct 14, 2024

/approve

Copy link
Contributor

openshift-ci bot commented Oct 14, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mheon, mtrmac

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-bot openshift-merge-bot bot merged commit 935c58f into containers:main Oct 14, 2024
19 checks passed
@mheon
Copy link
Member Author

mheon commented Oct 14, 2024

/cherry-pick release-1.55

@openshift-cherrypick-robot

@mheon: #2134 failed to apply on top of branch "release-1.55":

Applying: Use securejoin.SecureJoin when forming userns paths
Using index info to reconstruct a base tree...
M	userns.go
M	userns_test.go
Falling back to patching base and 3-way merge...
Auto-merging userns_test.go
Auto-merging userns.go
CONFLICT (content): Merge conflict in userns.go
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config advice.mergeConflict false"
Patch failed at 0001 Use securejoin.SecureJoin when forming userns paths

In response to this:

/cherry-pick release-1.55

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@nalind
Copy link
Member

nalind commented Oct 14, 2024

This cherry-picks cleanly onto the release-1.55 branch if #2105 and #2109 are cherry-picked first.


// Securely open (read-only) a file in a container mount.
func secureOpen(containerMount, file string) (*os.File, error) {
tmpFile, err := securejoin.OpenInRoot(containerMount, file)
Copy link
Member

@kwilczynski kwilczynski Oct 16, 2024

Choose a reason for hiding this comment

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

@mheon, would it be possible to use the older API here?

Why? Using filepath-securejoin 0.3.x would bring the internal use of the slices package, which requires Go 1.21 or newer. Which is something we don't have for when building images for older releases of OpenShift.

@mheon
Copy link
Member Author

mheon commented Oct 16, 2024 via email

@kwilczynski
Copy link
Member

I like having the new version on main, but replacing the new API with a securejoin and os.Open for older versions should be fine

@mheon, sounds good!

I will made the necessary changes and then ask you for a quick review, to make sure everything checks out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants