paths
paths
¶
Per-dispatch socket paths for the memory gateway endpoint (BE1.1/1.2).
The path is <run_dir>/s/s_<hash16>.sock where hash16 is the first 16
hex digits of a SHA-256 over job id, dispatch id and attachment id. The whole
path must fit in 70 bytes (the kernel's sun_path limit is 108); a longer
one is refused with a typed error naming the length, never truncated. The
caller supplies run_dir; there is no default location.
Directory safety (security review)¶
Path-based mkdir/stat/chmod follow symlinks and race. So:
run_dirmust already exist as a real directory (lstat, no symlink), owned by this UID and not writable by group or others; it is never created or repaired here.sis created withos.mkdir(mode=0o700)(onlyFileExistsErroris tolerated), then openedO_RDONLY|O_DIRECTORY|O_NOFOLLOW. Every check and the mode fix go through that descriptor (fstat,fchmod). A symlink, a non-directory or a foreign owner is a typed refusal and is never repaired.- The socket is bound through the descriptor, at
/proc/self/fd/<dirfd>/<name>, so the bind lands in the directory that was verified even if the path is swapped afterwards. (Chosen over re-verification alone because it removes the window instead of detecting it; it needs Linux/proc.) After the bind the directory path islstat-ed and compared (st_dev, st_ino) with the held descriptor as a second check; on a mismatch the socket is unlinked through the descriptor and the bind is refused. - A pre-existing entry at the socket name is unlinked (through the
descriptor) only if
lstatsays it is a socket owned by this UID; anything else is refused untouched. - Socket mode: the bind runs under
umask(0o177)held under a module lock for the duration of thebindcall only (umask is process-wide; other threads that create files in that instant get a stricter mode, never a looser one), then the mode is set to 0600 viachmod(dir_fd=)and re-checked withlstat.
Classes¶
SocketDir
dataclass
¶
A verified <run_dir>/s held open by descriptor, plus the socket name inside it.
Methods:¶
verify_same_directory
¶
Refuse unless the path still names the directory this descriptor holds.
Source code in src/marianne/memory/gateway/paths.py
clear_stale_socket
¶
Unlink an existing socket of ours at the name; refuse any other entry.
Source code in src/marianne/memory/gateway/paths.py
bind_socket
¶
Bind a fresh AF_UNIX socket inside the verified directory; returns it (not listening).
Source code in src/marianne/memory/gateway/paths.py
Functions:¶
socket_hash
¶
16 hex digits, stable for one (job, dispatch, attachment).
Source code in src/marianne/memory/gateway/paths.py
planned_socket_path
¶
The per-dispatch socket path as pure computation: no I/O, nothing created or checked.
Source code in src/marianne/memory/gateway/paths.py
socket_path_for
¶
The per-dispatch socket path, with its directory verified (and created 0700).
Source code in src/marianne/memory/gateway/paths.py
prepare_socket_dir
¶
Verify run_dir, create/open s safely and yield a :class:SocketDir.