Skip to content

Commit a954834

Browse files
committed
libcontainer: cgroups: loudly fail with Set
It is vital to loudly fail when a user attempts to set a cgroup limit (rather than using the system default). Otherwise the user will assume they have security they do not actually have. This mirrors the original Apply() (that would set cgroup configs) semantics. Signed-off-by: Aleksa Sarai <[email protected]>
1 parent f36ed4b commit a954834

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

libcontainer/cgroups/fs/apply_raw.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,24 @@ func (m *Manager) GetStats() (*cgroups.Stats, error) {
171171
}
172172

173173
func (m *Manager) Set(container *configs.Config) error {
174-
for name, path := range m.Paths {
174+
for _, sys := range subsystems {
175175
// We can't set this here, because after being applied, memcg doesn't
176176
// allow a non-empty cgroup from having its limits changed.
177-
if name == "memory" {
177+
if sys.Name() == "memory" {
178178
continue
179179
}
180-
sys, err := subsystems.Get(name)
181-
if err == errSubsystemDoesNotExist || !cgroups.PathExists(path) {
182-
continue
180+
181+
// Generate fake cgroup data.
182+
d, err := getCgroupData(container.Cgroups, -1)
183+
if err != nil {
184+
return err
183185
}
186+
// Get the path, but don't error out if the cgroup wasn't found.
187+
path, err := d.path(sys.Name())
188+
if err != nil && !cgroups.IsNotFound(err) {
189+
return err
190+
}
191+
184192
if err := sys.Set(path, container.Cgroups); err != nil {
185193
return err
186194
}

libcontainer/cgroups/systemd/apply_systemd.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,16 +431,19 @@ func (m *Manager) GetStats() (*cgroups.Stats, error) {
431431
}
432432

433433
func (m *Manager) Set(container *configs.Config) error {
434-
for name, path := range m.Paths {
434+
for _, sys := range subsystems {
435435
// We can't set this here, because after being applied, memcg doesn't
436436
// allow a non-empty cgroup from having its limits changed.
437-
if name == "memory" {
437+
if sys.Name() == "memory" {
438438
continue
439439
}
440-
sys, err := subsystems.Get(name)
441-
if err == errSubsystemDoesNotExist || !cgroups.PathExists(path) {
442-
continue
440+
441+
// Get the subsystem path, but don't error out for not found cgroups.
442+
path, err := getSubsystemPath(container.Cgroups, sys.Name())
443+
if err != nil && !cgroups.IsNotFound(err) {
444+
return err
443445
}
446+
444447
if err := sys.Set(path, container.Cgroups); err != nil {
445448
return err
446449
}

0 commit comments

Comments
 (0)