Skip to content

Commit 166ea55

Browse files
committed
[feature] enable --security-opt writable-cgroups=true|false as an option
Signed-off-by: ningmingxiao <[email protected]>
1 parent 3c8411b commit 166ea55

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

cmd/nerdctl/container/container_run_cgroup_linux_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ func TestRunCgroupV2(t *testing.T) {
134134
base.Cmd("exec", testutil.Identifier(t)+"-testUpdate2",
135135
"cat", "cpu.max", "memory.max", "memory.swap.max", "memory.low",
136136
"pids.max", "cpu.weight", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected2)
137-
137+
base.Cmd("run", "--rm", "--security-opt", "writable-cgroups=true", testutil.AlpineImage, "mkdir", "/sys/fs/cgroup/foo").AssertOK()
138+
base.Cmd("run", "--rm", "--security-opt", "writable-cgroups=false", testutil.AlpineImage, "mkdir", "/sys/fs/cgroup/foo").AssertFail()
139+
base.Cmd("run", "--rm", testutil.AlpineImage, "mkdir", "/sys/fs/cgroup/foo").AssertFail()
138140
}
139141

140142
func TestRunCgroupV1(t *testing.T) {
@@ -176,6 +178,9 @@ func TestRunCgroupV1(t *testing.T) {
176178
const expected = "42000\n100000\n0\n44040192\n6291456\n104857600\n0\n42\n2000\n0-1\n"
177179
base.Cmd("run", "--rm", "--cpus", "0.42", "--cpuset-mems", "0", "--memory", "42m", "--memory-reservation", "6m", "--memory-swap", "100m", "--memory-swappiness", "0", "--pids-limit", "42", "--cpu-shares", "2000", "--cpuset-cpus", "0-1", testutil.AlpineImage, "cat", quota, period, cpusetMems, memoryLimit, memoryReservation, memorySwap, memorySwappiness, pidsLimit, cpuShare, cpusetCpus).AssertOutExactly(expected)
178180
base.Cmd("run", "--rm", "--cpu-quota", "42000", "--cpu-period", "100000", "--cpuset-mems", "0", "--memory", "42m", "--memory-reservation", "6m", "--memory-swap", "100m", "--memory-swappiness", "0", "--pids-limit", "42", "--cpu-shares", "2000", "--cpuset-cpus", "0-1", testutil.AlpineImage, "cat", quota, period, cpusetMems, memoryLimit, memoryReservation, memorySwap, memorySwappiness, pidsLimit, cpuShare, cpusetCpus).AssertOutExactly(expected)
181+
base.Cmd("run", "--rm", "--security-opt", "writable-cgroups=true", testutil.AlpineImage, "mkdir", "/sys/fs/cgroup/pids/foo").AssertOK()
182+
base.Cmd("run", "--rm", "--security-opt", "writable-cgroups=false", testutil.AlpineImage, "mkdir", "/sys/fs/cgroup/pids/foo").AssertFail()
183+
base.Cmd("run", "--rm", testutil.AlpineImage, "mkdir", "/sys/fs/cgroup/pids/foo").AssertFail()
179184
}
180185

181186
// TestIssue3781 tests https://github.com/containerd/nerdctl/issues/3781

docs/command-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ Security flags:
244244
- :whale: `--security-opt apparmor=<PROFILE>`: specify custom AppArmor profile
245245
- :whale: `--security-opt no-new-privileges`: disallow privilege escalation, e.g., setuid and file capabilities
246246
- :whale: `--security-opt systempaths=unconfined`: Turn off confinement for system paths (masked paths, read-only paths) for the container
247+
- :whale: `--security-opt writable-cgroups`: making the cgroups writeable
247248
- :nerd_face: `--security-opt privileged-without-host-devices`: Don't pass host devices to privileged containers
248249
- :whale: `--cap-add=<CAP>`: Add Linux capabilities
249250
- :whale: `--cap-drop=<CAP>`: Drop Linux capabilities

pkg/cmd/container/run_security_linux.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package container
1818

1919
import (
2020
"errors"
21+
"fmt"
22+
"strconv"
2123
"strings"
2224
"sync"
2325

@@ -52,7 +54,7 @@ const (
5254
func generateSecurityOpts(privileged bool, securityOptsMap map[string]string) ([]oci.SpecOpts, error) {
5355
for k := range securityOptsMap {
5456
switch k {
55-
case "seccomp", "apparmor", "no-new-privileges", "systempaths", "privileged-without-host-devices":
57+
case "seccomp", "apparmor", "no-new-privileges", "systempaths", "privileged-without-host-devices", "writable-cgroups":
5658
default:
5759
log.L.Warnf("unknown security-opt: %q", k)
5860
}
@@ -118,6 +120,15 @@ func generateSecurityOpts(privileged bool, securityOptsMap map[string]string) ([
118120
if privilegedWithoutHostDevices && !privileged {
119121
return nil, errors.New("flag `--security-opt privileged-without-host-devices` can't be used without `--privileged` enabled")
120122
}
123+
if value, ok := securityOptsMap["writable-cgroups"]; ok {
124+
writable, err := strconv.ParseBool(value)
125+
if err != nil {
126+
return nil, fmt.Errorf("invalid \"writable-cgroups\" value: %q", value)
127+
}
128+
if writable {
129+
opts = append(opts, oci.WithWriteableCgroupfs)
130+
}
131+
}
121132

122133
if privileged {
123134
if privilegedWithoutHostDevices {

0 commit comments

Comments
 (0)