Skip to content

Commit d04ed72

Browse files
committed
runc run: refuse a frozen cgroup
Sometimes a container cgroup already exists but is frozen. When this happens, runc init hangs, and it's not clear what is going on. Refuse to run in a frozen cgroup; add a test case. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 8f71ccf commit d04ed72

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

libcontainer/factory_linux.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
182182
}
183183
}
184184

185+
// Check that cgroup is not frozen. Do it even if Exists() above returned
186+
// false, since in cgroup v1 it only checks "devices" controller.
187+
st, err := cm.GetFreezerState()
188+
if err != nil {
189+
return nil, fmt.Errorf("unable to get cgroup freezer state: %w", err)
190+
}
191+
if st == configs.Frozen {
192+
return nil, errors.New("container's cgroup unexpectedly frozen")
193+
}
194+
185195
if err := os.MkdirAll(containerRoot, 0o711); err != nil {
186196
return nil, err
187197
}

tests/integration/cgroups.bats

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,44 @@ function setup() {
347347
[ "$status" -ne 0 ]
348348
[[ "$output" == *"container's cgroup is not empty"* ]]
349349
}
350+
351+
@test "runc run/create should refuse pre-existing frozen cgroup" {
352+
requires cgroups_freezer
353+
if [[ "$ROOTLESS" -ne 0 ]]; then
354+
requires rootless_cgroup
355+
fi
356+
357+
set_cgroups_path
358+
359+
case $CGROUP_UNIFIED in
360+
no)
361+
FREEZER_DIR="${CGROUP_FREEZER_BASE_PATH}/${REL_CGROUPS_PATH}"
362+
FREEZER="${FREEZER_DIR}/freezer.state"
363+
STATE="FROZEN"
364+
;;
365+
yes)
366+
FREEZER_DIR="${CGROUP_PATH}"
367+
FREEZER="${FREEZER_DIR}/cgroup.freeze"
368+
STATE="1"
369+
;;
370+
esac
371+
372+
# Create and freeze the cgroup.
373+
mkdir -p "$FREEZER_DIR"
374+
echo "$STATE" >"$FREEZER"
375+
376+
# Start a container.
377+
runc run -d --console-socket "$CONSOLE_SOCKET" ct1
378+
[ "$status" -eq 1 ]
379+
# A warning should be printed.
380+
[[ "$output" == *"container's cgroup unexpectedly frozen"* ]]
381+
382+
# Same check for runc create.
383+
runc create --console-socket "$CONSOLE_SOCKET" ct2
384+
[ "$status" -eq 1 ]
385+
# A warning should be printed.
386+
[[ "$output" == *"container's cgroup unexpectedly frozen"* ]]
387+
388+
# Cleanup.
389+
rmdir "$FREEZER_DIR"
390+
}

0 commit comments

Comments
 (0)