Skip to content

The agent that grows with you

fix: treat a group shared via another account's primary gid as non-private

Teknium8924db564796
diff --git a/hermes_cli/gateway_runtime_discovery.py b/hermes_cli/gateway_runtime_discovery.py
index 3cac0ebedef..54f5f2fda99 100644
--- a/hermes_cli/gateway_runtime_discovery.py
+++ b/hermes_cli/gateway_runtime_discovery.py
@@ -27,14 +27,17 @@ def home_mode_unsafe(node: os.stat_result) -> bool:
return True
if not mode & 0o020:
return False
+ # ``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:
import grp
import pwd
user, group = pwd.getpwuid(node.st_uid), grp.getgrgid(node.st_gid)
- except (ImportError, KeyError):
+ primary_members = {p.pw_uid for p in pwd.getpwall() if p.pw_gid == group.gr_gid}
+ except (ImportError, KeyError, OSError):
return True
return not (group.gr_gid == user.pw_gid and group.gr_name == user.pw_name
- and set(group.gr_mem) <= {user.pw_name})
+ and set(group.gr_mem) <= {user.pw_name} and primary_members <= {node.st_uid})
def _private_node(path: Path, *, kind: str, home: bool = False) -> os.stat_result:
diff --git a/tests/hermes_cli/test_gateway_runtime_discovery.py b/tests/hermes_cli/test_gateway_runtime_discovery.py
index cd1fa6d20c6..c43f3ff6226 100644
--- a/tests/hermes_cli/test_gateway_runtime_discovery.py
+++ b/tests/hermes_cli/test_gateway_runtime_discovery.py
@@ -63,6 +63,7 @@ def test_home_mode_refuses_only_writes_another_user_can_make(tmp_path, monkeypat
home = tmp_path / 'home'
home.mkdir()
user = pwd.getpwuid(os.getuid())
+ monkeypatch.setattr(pwd, 'getpwall', lambda: [user]) # no other account on this gid
private = grp.struct_group((user.pw_name, 'x', user.pw_gid, []))
shared = grp.struct_group(('staff', 'x', user.pw_gid, [user.pw_name, 'someone-else']))
for mode, group, unsafe in ((0o700, private, False), (0o755, shared, False), (0o757, private, True),
@@ -70,3 +71,27 @@ def test_home_mode_refuses_only_writes_another_user_can_make(tmp_path, monkeypat
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)
+
+
[email protected]("linux", "macos")
+def test_group_write_is_private_only_when_no_other_account_has_that_primary_group(tmp_path, monkeypatch):
+ import grp
+ import os
+ import pwd
+ home = tmp_path / 'home'
+ home.mkdir()
+ home.chmod(0o775)
+ user = pwd.getpwuid(os.getuid())
+ # gr_mem never lists accounts whose PRIMARY gid is the group: an empty member list
+ # with another passwd record on that gid is a shared group, not a private one.
+ 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
+ monkeypatch.setattr(pwd, 'getpwall', lambda: [user])
+ assert home_mode_unsafe(home.lstat()) is False
+
+ def unreadable():
+ raise OSError('passwd database unavailable')
+ monkeypatch.setattr(pwd, 'getpwall', unreadable)
+ assert home_mode_unsafe(home.lstat()) is True