Skip to content

The agent that grows with you

fix(desktop): a failed AppArmor profile write keeps the installed profile

Teknium9467a34fae95
diff --git a/hermes_cli/main_desktop_apparmor.py b/hermes_cli/main_desktop_apparmor.py
index ba70822bfd4..23216a83490 100644
--- a/hermes_cli/main_desktop_apparmor.py
+++ b/hermes_cli/main_desktop_apparmor.py
@@ -148,6 +148,30 @@ def _profile_parses(parser: str, text: str) -> bool:
return True
+def _write_and_load(sudo: str, parser: str, target: Path, text: str) -> bool:
+ """Stage the profile beside ``target``, load it, and only then rename it into place, so a
+ failed write or load leaves an already-installed profile intact. The stage is a dotfile,
+ which apparmor_parser skips when it loads the directory at boot."""
+ staged = target.with_name(f".{target.name}.hermes-new")
+ failure = ""
+ written = _run([sudo, "tee", str(staged)], text)
+ if written is None or written.returncode != 0:
+ failure = f"✗ Could not write {target}."
+ else:
+ loaded = _run([sudo, parser, "-r", str(staged)])
+ if loaded is None or loaded.returncode != 0:
+ failure = f"✗ apparmor_parser could not load the profile for {target}."
+ else:
+ moved = _run([sudo, "mv", "-f", str(staged), str(target)])
+ if moved is not None and moved.returncode == 0:
+ return True
+ failure = f"✗ The profile is loaded until reboot, but moving it to {target} failed."
+ removed = _run([sudo, "rm", "-f", str(staged)])
+ print(failure + (" Any profile already there is unchanged."
+ if removed is not None and removed.returncode == 0 else f" Remove {staged} with sudo rm."))
+ return False
+
+
def install_profile(packaged_executable: Path) -> bool:
"""Write and load the profile through sudo (prompting on a terminal). Prints the outcome."""
target = profile_path(packaged_executable)
@@ -175,17 +199,7 @@ def install_profile(packaged_executable: Path) -> bool:
if not _profile_parses(parser, text):
return False
print(f"→ Installing AppArmor profile {target} (sudo required)...")
- written = _run([sudo, "tee", str(target)], text)
- if written is None or written.returncode != 0:
- print(f"✗ Could not write {target}.")
- return False
- loaded = _run([sudo, parser, "-r", str(target)])
- if loaded is None or loaded.returncode != 0:
- removed = _run([sudo, "rm", "-f", str(target)])
- if removed is not None and removed.returncode == 0:
- print(f"✗ apparmor_parser could not load {target}; removed it again.")
- else:
- print(f"✗ apparmor_parser could not load {target}, and removing it failed: run sudo rm {target}")
+ if not _write_and_load(sudo, parser, target, text):
return False
verdict = chromium_userns_sandbox_verdict(packaged_executable)
if verdict is False:
diff --git a/tests/hermes_cli/test_desktop_apparmor_sandbox.py b/tests/hermes_cli/test_desktop_apparmor_sandbox.py
index 465c48c2de0..2ca9fecbad5 100644
--- a/tests/hermes_cli/test_desktop_apparmor_sandbox.py
+++ b/tests/hermes_cli/test_desktop_apparmor_sandbox.py
@@ -107,3 +107,20 @@ def test_profile_that_fails_to_parse_or_load_leaves_nothing_in_the_profile_dir(t
# A parse failure never reaches sudo; a load failure wrote the file, then removed it again.
expected = ["tee", "apparmor_parser", "rm"] if failing_step == "-r" else []
assert [Path(call.split()[0]).name for call in sudo_calls] == expected
+
+
+def test_a_failed_profile_write_keeps_the_installed_profile(tmp_path, monkeypatch):
+ """A reinstall whose privileged write fails (disk full, I/O error) leaves the profile that was
+ already installed byte-for-byte, and no partial file in the profile directory."""
+ profile_dir, sudo_log = _fake_root_tools(tmp_path, monkeypatch)
+ # The write dies at its first byte, the way a full disk or a file-size limit stops ``tee``.
+ (tmp_path / "fakebin" / "sudo").write_text(
+ f'#!/bin/sh\necho "$*" >> "{sudo_log}"\n[ "$1" = tee ] && ulimit -f 0\nexec "$@"\n', encoding="utf-8")
+ exe = _packaged_app(tmp_path, _ALLOWED)
+ installed = main_desktop_apparmor.profile_path(exe)
+ installed.write_text(main_desktop_apparmor.profile_text(exe), encoding="utf-8")
+ before = installed.read_bytes()
+
+ assert main_desktop_apparmor.install_profile(exe) is False
+ assert installed.read_bytes() == before
+ assert list(profile_dir.iterdir()) == [installed]