Skip to content

The agent that grows with you

fix: a POSIX ACL on the profile home makes group write unsafe

Teknium3c5ea91b147a
diff --git a/gateway/control_socket.py b/gateway/control_socket.py
index fa30b7df515..52899caca00 100644
--- a/gateway/control_socket.py
+++ b/gateway/control_socket.py
@@ -288,7 +288,7 @@ class GatewayControlServer:
home = self._home
info = home.lstat()
if (home.absolute() != home.resolve() or not stat.S_ISDIR(info.st_mode)
- or info.st_uid != os.getuid() or home_mode_unsafe(info)): # windows-footgun: ok — POSIX-only helper
+ or info.st_uid != os.getuid() or home_mode_unsafe(info, home)): # windows-footgun: ok — POSIX-only helper
return None
sock = writer.get_extra_info("socket")
if hasattr(socket, "SO_PEERCRED"):
diff --git a/hermes_cli/gateway_runtime_discovery.py b/hermes_cli/gateway_runtime_discovery.py
index 54f5f2fda99..33d57ebe0d0 100644
--- a/hermes_cli/gateway_runtime_discovery.py
+++ b/hermes_cli/gateway_runtime_discovery.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+import errno
import json
import os
from pathlib import Path
@@ -17,7 +18,20 @@ class DiscoveryError(ValueError):
self.reason = reason
-def home_mode_unsafe(node: os.stat_result) -> bool:
+def _may_have_access_acl(path: Path) -> bool:
+ """A Linux POSIX access ACL is present, or its absence cannot be established. Python has no
+ os.listxattr on macOS (whose chmod +a ACLs are separate); mode bits stay the policy there."""
+ if not hasattr(os, "listxattr"):
+ return False
+ try:
+ return "system.posix_acl_access" in os.listxattr(path, follow_symlinks=False)
+ except OSError as exc:
+ if exc.errno in {errno.ENOTSUP, errno.EOPNOTSUPP}:
+ return False # filesystem without xattrs cannot carry an ACL
+ return True
+
+
+def home_mode_unsafe(node: os.stat_result, path: Path) -> bool:
"""The profile home keeps the operator's mode (main's home policy: symlinked homes,
HERMES_HOME_MODE 0701/0750). Only write by another user lets them swap the socket; read/search
bits grant nothing against our 0600 socket, and group-write is harmless when the group is the
@@ -27,6 +41,10 @@ def home_mode_unsafe(node: os.stat_result) -> bool:
return True
if not mode & 0o020:
return False
+ # With an access ACL the group bits are the mask: a named user (setfacl u:other:rwx) may
+ # hold write even though the owning group is private.
+ if _may_have_access_acl(path):
+ return True
# ``gr_mem`` never lists accounts whose PRIMARY gid is the group, so privacy also needs the
# passwd scan; membership we cannot establish is treated as shared.
try:
@@ -45,7 +63,7 @@ def _private_node(path: Path, *, kind: str, home: bool = False) -> os.stat_resul
predicates = {"socket": stat.S_ISSOCK, "file": stat.S_ISREG, "directory": stat.S_ISDIR}
if not predicates[kind](node.st_mode) or node.st_uid != os.getuid(): # windows-footgun: ok — POSIX socket path only
raise DiscoveryError("unsafe_control_path")
- if home_mode_unsafe(node) if home else stat.S_IMODE(node.st_mode) & 0o077:
+ if home_mode_unsafe(node, path) if home else stat.S_IMODE(node.st_mode) & 0o077:
raise DiscoveryError("unsafe_control_permissions")
return node
diff --git a/tests/gateway/test_control_socket_peer_security.py b/tests/gateway/test_control_socket_peer_security.py
index 011e9645593..070e8429bc5 100644
--- a/tests/gateway/test_control_socket_peer_security.py
+++ b/tests/gateway/test_control_socket_peer_security.py
@@ -38,3 +38,27 @@ def test_restart_heals_a_world_readable_pointer_left_by_an_older_gateway(tmp_pat
finally:
await server.stop()
asyncio.run(run())
+
+
+def test_group_bits_that_are_an_acl_mask_never_count_as_a_private_group(tmp_path, monkeypatch):
+ # With a POSIX ACL (setfacl u:nobody:rwx) the stat group bits are the mask: a 0770 home whose
+ # group is the owner's private one may still grant another named user write.
+ import grp
+ import pwd
+ from gateway.control_socket import GatewayControlServer
+ from hermes_cli.gateway_runtime_discovery import DiscoveryError, _socket_path
+ home = tmp_path / "home"
+ home.mkdir()
+ home.chmod(0o770)
+ user = pwd.getpwuid(os.getuid())
+ monkeypatch.setattr(pwd, "getpwall", lambda: [user])
+ monkeypatch.setattr(grp, "getgrgid", lambda _gid: grp.struct_group((user.pw_name, "x", user.pw_gid, [])))
+ monkeypatch.setattr(os, "listxattr", lambda *_a, **_k: ["system.posix_acl_access"])
+ with pytest.raises(DiscoveryError, match="unsafe_control_permissions"):
+ _socket_path(home)
+ ours, _theirs = socket.socketpair(socket.AF_UNIX)
+
+ class Writer:
+ def get_extra_info(self, _key):
+ return ours
+ assert GatewayControlServer(home)._posix_peer_subject(Writer()) is None
diff --git a/tests/hermes_cli/test_gateway_runtime_discovery.py b/tests/hermes_cli/test_gateway_runtime_discovery.py
index c43f3ff6226..f6826ae28ac 100644
--- a/tests/hermes_cli/test_gateway_runtime_discovery.py
+++ b/tests/hermes_cli/test_gateway_runtime_discovery.py
@@ -70,7 +70,7 @@ def test_home_mode_refuses_only_writes_another_user_can_make(tmp_path, monkeypat
(0o775, private, False), (0o775, shared, True)):
home.chmod(mode)
monkeypatch.setattr(grp, 'getgrgid', lambda _gid, g=group: g)
- assert home_mode_unsafe(home.lstat()) is unsafe, (oct(mode), group.gr_name)
+ assert home_mode_unsafe(home.lstat(), home) is unsafe, (oct(mode), group.gr_name)
@pytest.mark.platforms("linux", "macos")
@@ -87,11 +87,11 @@ def test_group_write_is_private_only_when_no_other_account_has_that_primary_grou
monkeypatch.setattr(grp, 'getgrgid', lambda _gid: grp.struct_group((user.pw_name, 'x', user.pw_gid, [])))
other = pwd.struct_passwd(('intruder', 'x', user.pw_uid + 1, user.pw_gid, '', '/', '/bin/sh'))
monkeypatch.setattr(pwd, 'getpwall', lambda: [user, other])
- assert home_mode_unsafe(home.lstat()) is True
+ assert home_mode_unsafe(home.lstat(), home) is True
monkeypatch.setattr(pwd, 'getpwall', lambda: [user])
- assert home_mode_unsafe(home.lstat()) is False
+ assert home_mode_unsafe(home.lstat(), home) is False
def unreadable():
raise OSError('passwd database unavailable')
monkeypatch.setattr(pwd, 'getpwall', unreadable)
- assert home_mode_unsafe(home.lstat()) is True
+ assert home_mode_unsafe(home.lstat(), home) is True