Skip to content

Commit 1a81e9a

Browse files
authored
Merge pull request #958 from dubstack/skip-devices
Skip updates on parent Devices cgroup
2 parents 5226749 + d4c6719 commit 1a81e9a

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

libcontainer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ config := &configs.Config{
7777
Parent: "system",
7878
Resources: &configs.Resources{
7979
MemorySwappiness: nil,
80-
AllowAllDevices: false,
80+
AllowAllDevices: nil,
8181
AllowedDevices: configs.DefaultAllowedDevices,
8282
},
8383
},

libcontainer/cgroups/fs/devices.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,23 @@ func (s *DevicesGroup) Set(path string, cgroup *configs.Cgroup) error {
4343
}
4444
return nil
4545
}
46-
if !cgroup.Resources.AllowAllDevices {
47-
if err := writeFile(path, "devices.deny", "a"); err != nil {
48-
return err
49-
}
50-
51-
for _, dev := range cgroup.Resources.AllowedDevices {
52-
if err := writeFile(path, "devices.allow", dev.CgroupString()); err != nil {
46+
if cgroup.Resources.AllowAllDevices != nil {
47+
if *cgroup.Resources.AllowAllDevices == false {
48+
if err := writeFile(path, "devices.deny", "a"); err != nil {
5349
return err
5450
}
51+
52+
for _, dev := range cgroup.Resources.AllowedDevices {
53+
if err := writeFile(path, "devices.allow", dev.CgroupString()); err != nil {
54+
return err
55+
}
56+
}
57+
return nil
5558
}
56-
return nil
57-
}
5859

59-
if err := writeFile(path, "devices.allow", "a"); err != nil {
60-
return err
60+
if err := writeFile(path, "devices.allow", "a"); err != nil {
61+
return err
62+
}
6163
}
6264

6365
for _, dev := range cgroup.Resources.DeniedDevices {

libcontainer/cgroups/fs/devices_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func TestDevicesSetAllow(t *testing.T) {
4040
helper.writeFileContents(map[string]string{
4141
"devices.deny": "a",
4242
})
43-
44-
helper.CgroupData.config.Resources.AllowAllDevices = false
43+
allowAllDevices := false
44+
helper.CgroupData.config.Resources.AllowAllDevices = &allowAllDevices
4545
helper.CgroupData.config.Resources.AllowedDevices = allowedDevices
4646
devices := &DevicesGroup{}
4747
if err := devices.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
@@ -56,6 +56,19 @@ func TestDevicesSetAllow(t *testing.T) {
5656
if value != allowedList {
5757
t.Fatal("Got the wrong value, set devices.allow failed.")
5858
}
59+
60+
// When AllowAllDevices is nil, devices.allow file should not be modified.
61+
helper.CgroupData.config.Resources.AllowAllDevices = nil
62+
if err := devices.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
63+
t.Fatal(err)
64+
}
65+
value, err = getCgroupParamString(helper.CgroupPath, "devices.allow")
66+
if err != nil {
67+
t.Fatalf("Failed to parse devices.allow - %s", err)
68+
}
69+
if value != allowedList {
70+
t.Fatal("devices policy shouldn't have changed on AllowedAllDevices=nil.")
71+
}
5972
}
6073

6174
func TestDevicesSetDeny(t *testing.T) {
@@ -66,7 +79,8 @@ func TestDevicesSetDeny(t *testing.T) {
6679
"devices.allow": "a",
6780
})
6881

69-
helper.CgroupData.config.Resources.AllowAllDevices = true
82+
allowAllDevices := true
83+
helper.CgroupData.config.Resources.AllowAllDevices = &allowAllDevices
7084
helper.CgroupData.config.Resources.DeniedDevices = deniedDevices
7185
devices := &DevicesGroup{}
7286
if err := devices.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {

libcontainer/configs/cgroup_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Cgroup struct {
3636
type Resources struct {
3737
// If this is true allow access to any kind of device within the container. If false, allow access only to devices explicitly listed in the allowed_devices list.
3838
// Deprecated
39-
AllowAllDevices bool `json:"allow_all_devices,omitempty"`
39+
AllowAllDevices *bool `json:"allow_all_devices,omitempty"`
4040
// Deprecated
4141
AllowedDevices []*Device `json:"allowed_devices,omitempty"`
4242
// Deprecated

libcontainer/integration/template_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NOD
2020
// it uses a network strategy of just setting a loopback interface
2121
// and the default setup for devices
2222
func newTemplateConfig(rootfs string) *configs.Config {
23+
allowAllDevices := false
2324
return &configs.Config{
2425
Rootfs: rootfs,
2526
Capabilities: []string{
@@ -49,7 +50,7 @@ func newTemplateConfig(rootfs string) *configs.Config {
4950
Path: "integration/test",
5051
Resources: &configs.Resources{
5152
MemorySwappiness: nil,
52-
AllowAllDevices: false,
53+
AllowAllDevices: &allowAllDevices,
5354
AllowedDevices: configs.DefaultAllowedDevices,
5455
},
5556
},

0 commit comments

Comments
 (0)