X Tutup
Skip to content

Commit f2a20ea

Browse files
Merge pull request containerd#3137 from Random-Liu/fix-race-and-panic
Fix capability option race and panic.
2 parents 90a7da8 + 808b223 commit f2a20ea

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

oci/spec_opts.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,9 @@ func WithCapabilities(caps []string) SpecOpts {
741741
}
742742

743743
// WithAllCapabilities sets all linux capabilities for the process
744-
var WithAllCapabilities = WithCapabilities(GetAllCapabilities())
744+
var WithAllCapabilities = func(ctx context.Context, client Client, c *containers.Container, s *Spec) error {
745+
return WithCapabilities(GetAllCapabilities())(ctx, client, c, s)
746+
}
745747

746748
// GetAllCapabilities returns all caps up to CAP_LAST_CAP
747749
// or CAP_BLOCK_SUSPEND on RHEL6
@@ -771,12 +773,14 @@ func capsContain(caps []string, s string) bool {
771773
}
772774

773775
func removeCap(caps *[]string, s string) {
774-
for i, c := range *caps {
776+
var newcaps []string
777+
for _, c := range *caps {
775778
if c == s {
776-
*caps = append((*caps)[:i], (*caps)[i+1:]...)
777-
return
779+
continue
778780
}
781+
newcaps = append(newcaps, c)
779782
}
783+
*caps = newcaps
780784
}
781785

782786
// WithAddedCapabilities adds the provided capabilities

oci/spec_opts_test.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,25 @@ func TestWithEnv(t *testing.T) {
166166
Env: []string{"DEFAULT=test"},
167167
}
168168

169-
WithEnv([]string{"env=1"})(nil, nil, nil, &s)
169+
WithEnv([]string{"env=1"})(context.Background(), nil, nil, &s)
170170

171171
if len(s.Process.Env) != 2 {
172172
t.Fatal("didn't append")
173173
}
174174

175-
WithEnv([]string{"env2=1"})(nil, nil, nil, &s)
175+
WithEnv([]string{"env2=1"})(context.Background(), nil, nil, &s)
176176

177177
if len(s.Process.Env) != 3 {
178178
t.Fatal("didn't append")
179179
}
180180

181-
WithEnv([]string{"env2=2"})(nil, nil, nil, &s)
181+
WithEnv([]string{"env2=2"})(context.Background(), nil, nil, &s)
182182

183183
if s.Process.Env[2] != "env2=2" {
184184
t.Fatal("couldn't update")
185185
}
186186

187-
WithEnv([]string{"env2"})(nil, nil, nil, &s)
187+
WithEnv([]string{"env2"})(context.Background(), nil, nil, &s)
188188

189189
if len(s.Process.Env) != 2 {
190190
t.Fatal("couldn't unset")
@@ -428,7 +428,7 @@ func TestAddCaps(t *testing.T) {
428428

429429
var s specs.Spec
430430

431-
if err := WithAddedCapabilities([]string{"CAP_CHOWN"})(nil, nil, nil, &s); err != nil {
431+
if err := WithAddedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
432432
t.Fatal(err)
433433
}
434434
for i, cl := range [][]string{
@@ -448,10 +448,10 @@ func TestDropCaps(t *testing.T) {
448448

449449
var s specs.Spec
450450

451-
if err := WithAllCapabilities(nil, nil, nil, &s); err != nil {
451+
if err := WithAllCapabilities(context.Background(), nil, nil, &s); err != nil {
452452
t.Fatal(err)
453453
}
454-
if err := WithDroppedCapabilities([]string{"CAP_CHOWN"})(nil, nil, nil, &s); err != nil {
454+
if err := WithDroppedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
455455
t.Fatal(err)
456456
}
457457

@@ -465,4 +465,44 @@ func TestDropCaps(t *testing.T) {
465465
t.Errorf("cap list %d contains dropped cap", i)
466466
}
467467
}
468+
469+
// Add all capabilities back and drop a different cap.
470+
if err := WithAllCapabilities(context.Background(), nil, nil, &s); err != nil {
471+
t.Fatal(err)
472+
}
473+
if err := WithDroppedCapabilities([]string{"CAP_FOWNER"})(context.Background(), nil, nil, &s); err != nil {
474+
t.Fatal(err)
475+
}
476+
477+
for i, cl := range [][]string{
478+
s.Process.Capabilities.Bounding,
479+
s.Process.Capabilities.Effective,
480+
s.Process.Capabilities.Permitted,
481+
s.Process.Capabilities.Inheritable,
482+
} {
483+
if capsContain(cl, "CAP_FOWNER") {
484+
t.Errorf("cap list %d contains dropped cap", i)
485+
}
486+
if !capsContain(cl, "CAP_CHOWN") {
487+
t.Errorf("cap list %d doesn't contain non-dropped cap", i)
488+
}
489+
}
490+
491+
// Drop all duplicated caps.
492+
if err := WithCapabilities([]string{"CAP_CHOWN", "CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
493+
t.Fatal(err)
494+
}
495+
if err := WithDroppedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
496+
t.Fatal(err)
497+
}
498+
for i, cl := range [][]string{
499+
s.Process.Capabilities.Bounding,
500+
s.Process.Capabilities.Effective,
501+
s.Process.Capabilities.Permitted,
502+
s.Process.Capabilities.Inheritable,
503+
} {
504+
if len(cl) != 0 {
505+
t.Errorf("cap list %d is not empty", i)
506+
}
507+
}
468508
}

0 commit comments

Comments
 (0)
X Tutup