fix(apparmor): lift the unprivileged-unconfined restriction in teardown

On runner image 20261101 kernel.apparmor_restrict_unprivileged_unconfined
is on, so the kernel turns runc's change_onexec into the stack
docker-default//&unconfined. docker-default grants no signal to an
unconfined peer, and every in-container kill() is denied; run 41743807934
lost four package builds that way (journal: apparmor="DENIED"
operation="signal" comm="rsync" signal=usr2).

teardown.sh now writes 0 to the sysctl and records the value it found
under /run/infinito; restore.sh writes that value back and drops the
record. A missing sysctl or one already at 0 is left alone, and a second
teardown keeps the first record. CI runs teardown before every deploy,
workspace and dns job; dev hosts through make environment-bootstrap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-11-04 17:35:08 +01:00
1 parent fd05f85d44
commit 330b85f9e7
4 files changed
+120

No files matched your search

@@ -8,7 +8,9 @@ profile, and BuildKit then failed each node image RUN step with
from __future__ import annotations
import subprocess
import tempfile
import unittest
from pathlib import Path
from utils.cache.files import PROJECT_ROOT
@@ -44,5 +46,74 @@ class TestRemovableProfiles(unittest.TestCase):
)
def _call(function: str, sysctl: Path, state: Path) -> subprocess.CompletedProcess:
return subprocess.run(
[
"bash",
"-c",
f'source "{COMMON}"; {function} "$1" "$2"',
"-",
str(sysctl),
str(state),
],
capture_output=True,
text=True,
check=False,
)
def _value(path: Path) -> str:
return path.read_text().strip() # nocheck: cache-read - per-test temp file
class TestUnconfinedRestriction(unittest.TestCase):
def setUp(self) -> None:
self._tmp = tempfile.TemporaryDirectory()
self.addCleanup(self._tmp.cleanup)
self.sysctl = Path(self._tmp.name) / "apparmor_restrict_unprivileged_unconfined"
self.state = Path(self._tmp.name) / "run" / "infinito" / "restriction"
def test_lifting_turns_the_restriction_off(self) -> None:
self.sysctl.write_text("1\n")
result = _call("apparmor_lift_unconfined_restriction", self.sysctl, self.state)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(_value(self.sysctl), "0")
def test_lifting_records_the_previous_value(self) -> None:
self.sysctl.write_text("1\n")
_call("apparmor_lift_unconfined_restriction", self.sysctl, self.state)
self.assertEqual(_value(self.state), "1")
def test_lifting_an_already_lifted_restriction_records_nothing(self) -> None:
self.sysctl.write_text("0\n")
result = _call("apparmor_lift_unconfined_restriction", self.sysctl, self.state)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertFalse(self.state.exists())
def test_a_kernel_without_the_sysctl_is_left_alone(self) -> None:
result = _call("apparmor_lift_unconfined_restriction", self.sysctl, self.state)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertFalse(self.sysctl.exists())
self.assertFalse(self.state.exists())
def test_reinstating_restores_the_recorded_value(self) -> None:
self.sysctl.write_text("1\n")
_call("apparmor_lift_unconfined_restriction", self.sysctl, self.state)
result = _call(
"apparmor_reinstate_unconfined_restriction", self.sysctl, self.state
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(_value(self.sysctl), "1")
self.assertFalse(self.state.exists())
def test_reinstating_without_a_record_changes_nothing(self) -> None:
self.sysctl.write_text("0\n")
result = _call(
"apparmor_reinstate_unconfined_restriction", self.sysctl, self.state
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(_value(self.sysctl), "0")
if __name__ == "__main__":
unittest.main()