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

[release-1.51] Backport CVE-2024-9676 fix #2146

Merged

Conversation

mheon
Copy link
Member

@mheon mheon commented Oct 23, 2024

This backports #2134 to release-1.51 (with some bugfix patches from Giuseppe to make it apply cleanly). release-1.51 is used by Podman 4.9.x, which we are still supported on F39 for a couple more months.

fix the detection for the maximum userns size from an image.

If the maximum ID used in an image is X, we need to use a user
namespace with size X+1 to include UID=X.

Closes: containers#2104

Signed-off-by: Giuseppe Scrivano <[email protected]>
the alpine image defines a "nogroup":

$ podman run --rm alpine grep nogroup /etc/group
nogroup:x:65533:

ignore it as we are already doing for the "nobody" user.

Signed-off-by: Giuseppe Scrivano <[email protected]>
@mheon
Copy link
Member Author

mheon commented Oct 23, 2024

Too old securejoin to have the new API, reverting to the old one

@mheon mheon force-pushed the backport_2024_9676_release151 branch from 872d5b6 to 67270e0 Compare October 23, 2024 13:03
userns.go Outdated
Comment on lines 336 to 349
func secureOpen(containerMount, file string) (*os.File, error) {
finalPath, err := securejoin.SecureJoin(containerMount, file)
if err != nil {
return nil, err
}

return os.Open(finalPath)
}
Copy link
Member

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 a similar code to the one from:

Just to make things consistent.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, this is not sufficient if there is a risk of a racing process changing the filesystem from underneath you. You should instead do something like (untested):

func somewhatSafeOpen(root, unsafePath string, mode int) (_ *os.File, Err error) {
	if mode&(os.O_CREAT|os.O_TRUNC) != 0 {
		return nil, fmt.Errorf("O_CREAT|O_TRUNC cannot be used safely")
	}
	path, err := securejoin.SecureJoin(root, unsafePath)
	if err != nil {
		return nil, err
	}
	handle, err := os.OpenFile(path, mode)
	if err != nil {
		return nil, err
	}
	defer func() {
		if Err != nil {
			_ = handle.Close()
		}
	}()
	// At this point, we aren't sure that the handle we grabbed is the same
	// path we expected. So double-check it using procfs.
	actualPath, err := os.Readlink(fmt.Sprintf("/proc/self/fd/%d", handle.Fd()))
	if err != nil {
		return nil, fmt.Errorf("failed to check handle path for %q: %w", path, err)
	}
	if path != actualPath {
		return nil, fmt.Errorf("container breakout detected: tried to open %q but instead opened %q", path, actualPath)
	}
	return handle, nil
}

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.

Cherry-pick conflict fixed for v1.51 branch, and converted to use
the old securejoin API (securejoin.SecureJoin and then os.Open)
as this branch is too old to have the new API.

Addresses CVE-2024-9676

Signed-off-by: Matt Heon <[email protected]>
@mheon mheon force-pushed the backport_2024_9676_release151 branch from 67270e0 to 0dc4fc9 Compare October 23, 2024 13:11
@kwilczynski
Copy link
Member

/approve
/lgtm

Copy link
Contributor

openshift-ci bot commented Oct 23, 2024

@kwilczynski: changing LGTM is restricted to collaborators

In response to this:

/approve
/lgtm

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.

@TomSweeneyRedHat
Copy link
Member

LGTM

@TomSweeneyRedHat
Copy link
Member

/lgtm

@TomSweeneyRedHat
Copy link
Member

/approve

Copy link
Contributor

openshift-ci bot commented Oct 23, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: kwilczynski, mheon, TomSweeneyRedHat

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

@TomSweeneyRedHat TomSweeneyRedHat merged commit e7a5bf7 into containers:release-1.51 Oct 23, 2024
18 checks passed
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.

5 participants