Skip to content

Commit a0ef27c

Browse files
committed
Ensures that the primary GID is also included in the additional GIDs
Apply `ensureAdditionalGids()` from containerd/containerd@3eda46a (CVE-2023-25173, GHSA-hmfx-3pcx-653p) Signed-off-by: Akihiro Suda <[email protected]>
1 parent 37d54eb commit a0ef27c

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

executor/oci/user.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func parseUID(str string) (uint32, error) {
9191
// once the PR in containerd is merged we should remove this function.
9292
func WithUIDGID(uid, gid uint32, sgids []uint32) containerdoci.SpecOpts {
9393
return func(_ context.Context, _ containerdoci.Client, _ *containers.Container, s *containerdoci.Spec) error {
94+
defer ensureAdditionalGids(s)
9495
setProcess(s)
9596
s.Process.User.UID = uid
9697
s.Process.User.GID = gid
@@ -106,3 +107,15 @@ func setProcess(s *containerdoci.Spec) {
106107
s.Process = &specs.Process{}
107108
}
108109
}
110+
111+
// ensureAdditionalGids ensures that the primary GID is also included in the additional GID list.
112+
// From https://github.com/containerd/containerd/blob/v1.7.0-beta.4/oci/spec_opts.go#L124-L133
113+
func ensureAdditionalGids(s *containerdoci.Spec) {
114+
setProcess(s)
115+
for _, f := range s.Process.User.AdditionalGids {
116+
if f == s.Process.User.GID {
117+
return
118+
}
119+
}
120+
s.Process.User.AdditionalGids = append([]uint32{s.Process.User.GID}, s.Process.User.AdditionalGids...)
121+
}

frontend/dockerfile/dockerfile_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ var allTests = integration.TestFuncs(
7171
testExportedHistory,
7272
testExposeExpansion,
7373
testUser,
74+
testUserAdditionalGids,
7475
testCacheReleased,
7576
testDockerignore,
7677
testDockerignoreInvalid,
@@ -3004,6 +3005,43 @@ USER nobody
30043005
require.Equal(t, "nobody", ociimg.Config.User)
30053006
}
30063007

3008+
// testUserAdditionalGids ensures that that the primary GID is also included in the additional GID list.
3009+
// CVE-2023-25173: https://github.com/advisories/GHSA-hmfx-3pcx-653p
3010+
func testUserAdditionalGids(t *testing.T, sb integration.Sandbox) {
3011+
f := getFrontend(t, sb)
3012+
3013+
dockerfile := []byte(`
3014+
# Mimics the tests in https://github.com/containerd/containerd/commit/3eda46af12b1deedab3d0802adb2e81cb3521950
3015+
FROM busybox
3016+
SHELL ["/bin/sh", "-euxc"]
3017+
RUN [ "$(id)" = "uid=0(root) gid=0(root) groups=0(root),10(wheel)" ]
3018+
USER 1234
3019+
RUN [ "$(id)" = "uid=1234 gid=0(root) groups=0(root)" ]
3020+
USER 1234:1234
3021+
RUN [ "$(id)" = "uid=1234 gid=1234 groups=1234" ]
3022+
USER daemon
3023+
RUN [ "$(id)" = "uid=1(daemon) gid=1(daemon) groups=1(daemon)" ]
3024+
`)
3025+
3026+
dir, err := integration.Tmpdir(
3027+
t,
3028+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
3029+
)
3030+
require.NoError(t, err)
3031+
3032+
c, err := client.New(sb.Context(), sb.Address())
3033+
require.NoError(t, err)
3034+
defer c.Close()
3035+
3036+
_, err = f.Solve(sb.Context(), c, client.SolveOpt{
3037+
LocalDirs: map[string]string{
3038+
dockerui.DefaultLocalNameDockerfile: dir,
3039+
dockerui.DefaultLocalNameContext: dir,
3040+
},
3041+
}, nil)
3042+
require.NoError(t, err)
3043+
}
3044+
30073045
func testCopyChown(t *testing.T, sb integration.Sandbox) {
30083046
f := getFrontend(t, sb)
30093047

0 commit comments

Comments
 (0)