Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit ca3b806

Browse files
committed
Fix addition group ids.
Signed-off-by: Lantao Liu <[email protected]>
1 parent f267f21 commit ca3b806

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

pkg/containerd/opts/spec.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2018 The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package opts
18+
19+
import (
20+
"context"
21+
22+
"github.com/containerd/containerd/containers"
23+
"github.com/containerd/containerd/oci"
24+
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
25+
)
26+
27+
// WithAdditionalGIDs adds any additional groups listed for a particular user in the
28+
// /etc/groups file of the image's root filesystem to the OCI spec's additionalGids array.
29+
func WithAdditionalGIDs(userstr string) oci.SpecOpts {
30+
return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) (err error) {
31+
gids := s.Process.User.AdditionalGids
32+
if err := oci.WithAdditionalGIDs(userstr)(ctx, client, c, s); err != nil {
33+
return err
34+
}
35+
// Merge existing gids and new gids.
36+
s.Process.User.AdditionalGids = mergeGids(s.Process.User.AdditionalGids, gids)
37+
return nil
38+
}
39+
}
40+
41+
func mergeGids(gids1, gids2 []uint32) []uint32 {
42+
for _, gid1 := range gids1 {
43+
for i, gid2 := range gids2 {
44+
if gid1 == gid2 {
45+
gids2 = append(gids2[:i], gids2[i+1:]...)
46+
break
47+
}
48+
}
49+
}
50+
return append(gids1, gids2...)
51+
}

pkg/containerd/opts/spec_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2018 The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package opts
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestMergeGids(t *testing.T) {
26+
gids1 := []uint32{3, 2, 1}
27+
gids2 := []uint32{2, 3, 4}
28+
assert.Equal(t, []uint32{3, 2, 1, 4}, mergeGids(gids1, gids2))
29+
}

pkg/server/container_create.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
229229
specOpts = append(specOpts, oci.WithUser(userstr))
230230
}
231231

232+
if securityContext.GetRunAsUsername() != "" {
233+
userstr = securityContext.GetRunAsUsername()
234+
} else {
235+
// Even if RunAsUser is not set, we still call `GetValue` to get uid 0.
236+
// Because it is still useful to get additional gids for uid 0.
237+
userstr = strconv.FormatInt(securityContext.GetRunAsUser().GetValue(), 10)
238+
}
239+
specOpts = append(specOpts, customopts.WithAdditionalGIDs(userstr))
240+
232241
apparmorSpecOpts, err := generateApparmorSpecOpts(
233242
securityContext.GetApparmorProfile(),
234243
securityContext.GetPrivileged(),

0 commit comments

Comments
 (0)