Skip to content

The agent that grows with you

fix(gateway): refuse a hardlinked gateway.lock whatever its mode

Teknium3efc7264fff2
diff --git a/gateway/runtime_ownership.py b/gateway/runtime_ownership.py
index a6757f84db4..2e035513590 100644
--- a/gateway/runtime_ownership.py
+++ b/gateway/runtime_ownership.py
@@ -31,15 +31,11 @@ def tighten_lock_mode(fd: int) -> None:
chmodded, through the already-open NOFOLLOW descriptor (no path race); anything else is refused.
"""
info = os.fstat(fd)
- if os.name == 'nt':
- if not stat.S_ISREG(info.st_mode):
- raise PermissionError('unsafe gateway lock owner or type')
- return
- if not stat.S_ISREG(info.st_mode) or info.st_uid != os.getuid(): # windows-footgun: ok — POSIX branch
+ if not stat.S_ISREG(info.st_mode) or (os.name != 'nt' and info.st_uid != os.getuid()): # windows-footgun: ok — POSIX only
raise PermissionError('unsafe gateway lock owner or type')
- if stat.S_IMODE(info.st_mode) & 0o077:
- if info.st_nlink != 1:
- raise PermissionError('unsafe gateway lock link count')
+ if info.st_nlink != 1: # a second name (cp -al / rsync --link-dest clone) shares the record
+ raise PermissionError('unsafe gateway lock link count')
+ if os.name != 'nt' and stat.S_IMODE(info.st_mode) & 0o077:
os.fchmod(fd, 0o600) # windows-footgun: ok — POSIX branch
diff --git a/tests/gateway/test_gateway_lock_hardlink.py b/tests/gateway/test_gateway_lock_hardlink.py
new file mode 100644
index 00000000000..63d427668f4
--- /dev/null
+++ b/tests/gateway/test_gateway_lock_hardlink.py
@@ -0,0 +1,19 @@
+"""A hardlinked gateway.lock (a cp -al / rsync --link-dest profile clone) is never written."""
+import os
+
+import pytest
+
+
+def test_private_hardlinked_gateway_lock_is_refused_unwritten(tmp_path):
+ from gateway import runtime_ownership
+ clone, home = tmp_path / 'clone', tmp_path / 'home'
+ for path in (clone, home):
+ path.mkdir(mode=0o700)
+ sentinel = clone / 'gateway.lock'
+ sentinel.write_text('sentinel', encoding='utf-8')
+ sentinel.chmod(0o600) # already private: the chmod branch never runs
+ os.link(sentinel, home / 'gateway.lock')
+ with pytest.raises(PermissionError, match='link count'):
+ runtime_ownership.ProfileOwnership().reserve([home])
+ assert sentinel.read_text(encoding='utf-8') == 'sentinel'