@yahyahammad/venastine 0.3.8 → 0.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +19 -19
- package/CONFIG_ARCHITECTURE.md +2030 -2002
- package/LICENSE +201 -201
- package/README.md +1034 -1034
- package/agents/builtin/compactor.md +75 -75
- package/agents/builtin/explore.md +77 -77
- package/agents/builtin/pipeline-reviewer.md +100 -100
- package/agents/builtin/plan.md +106 -106
- package/agents/builtin/review.md +74 -74
- package/agents/manager.py +261 -261
- package/agents/subagent_tool.py +472 -472
- package/agents/tui_commands.py +238 -146
- package/bin/venastine.mjs +528 -528
- package/bin/venastine.py +71 -71
- package/cli_commands.py +838 -835
- package/config.yaml +13 -0
- package/config_edit.py +1301 -1301
- package/config_schema.py +971 -966
- package/config_update.py +749 -749
- package/core/agent_activity.py +256 -256
- package/core/ansi.py +452 -452
- package/core/approval.py +137 -137
- package/core/call_view.py +364 -364
- package/core/cancel.py +121 -121
- package/core/chatgpt_auth.py +747 -747
- package/core/claims_view.py +107 -107
- package/core/client.py +2715 -2715
- package/core/commands.py +460 -454
- package/core/compaction.py +1735 -1735
- package/core/config_loader.py +1982 -1982
- package/core/connect.py +686 -686
- package/core/events.py +123 -123
- package/core/file_mentions.py +280 -280
- package/core/interaction.py +411 -411
- package/core/loop.py +2566 -2566
- package/core/memory.py +482 -482
- package/core/pipeline_models.py +190 -190
- package/core/provider_errors.py +234 -234
- package/core/reasoning/authorization.py +130 -130
- package/core/reasoning/base.py +219 -219
- package/core/reasoning/confidence_scoring.py +484 -484
- package/core/reasoning/events.py +149 -149
- package/core/reasoning/orchestrator.py +1561 -1561
- package/core/reasoning/output_writer.py +345 -345
- package/core/reasoning/payload_validation.py +524 -524
- package/core/reasoning/pipeline_storage.py +427 -427
- package/core/reasoning/scholar.py +458 -458
- package/core/reasoning/source_corpus.py +433 -433
- package/core/reasoning/source_scoring.py +855 -855
- package/core/replay.py +315 -315
- package/core/shell_sessions.py +1553 -1553
- package/credentials.py +307 -307
- package/database.py +333 -333
- package/docs/PRIVACY.md +78 -78
- package/docs/SECURITY.md +108 -108
- package/docs/THIRD_PARTY_NOTICES.md +148 -148
- package/env_secrets.py +118 -118
- package/json_store.py +343 -343
- package/logging_setup.py +295 -295
- package/main.py +3433 -3433
- package/mcp_client/client.py +733 -733
- package/mcp_client/config.py +484 -484
- package/mcp_client/registration.py +343 -343
- package/memories/manager.py +147 -147
- package/memories/tui_commands.py +319 -239
- package/package.json +303 -303
- package/project_init/config_files.py +282 -282
- package/project_init/generator.py +728 -728
- package/project_init/manifest.py +418 -418
- package/project_init/tui_commands.py +147 -147
- package/prompts/critic_pass.md +24 -24
- package/prompts/revalidate.md +25 -25
- package/prompts/revise.md +17 -17
- package/prompts/source_grounding.md +66 -66
- package/prompts/system_prompts.py +316 -316
- package/providers.json.example +94 -94
- package/pyproject.toml +149 -149
- package/requirements.txt +295 -295
- package/safety/policy_enforcement.py +1082 -1082
- package/security/askpass.py +419 -419
- package/security/capability.py +207 -207
- package/security/confine.py +484 -484
- package/security/hosted.py +182 -182
- package/security/permissions.py +237 -237
- package/security/protected_paths.py +501 -501
- package/security/sandbox.py +4108 -4108
- package/skills/builtin/math/formal-specification.md +67 -67
- package/skills/builtin/math/numerical-methods.md +66 -66
- package/skills/builtin/research/experiment-design.md +62 -62
- package/skills/builtin/research/reproducibility.md +55 -55
- package/skills/builtin/research/source-evaluation.md +70 -70
- package/skills/builtin/research/statistical-inference.md +63 -63
- package/skills/builtin/research/technical-writing.md +59 -59
- package/skills/builtin/software/code-review.md +61 -61
- package/skills/builtin/software/debugging.md +61 -61
- package/skills/builtin/software/test-design.md +67 -67
- package/skills/tui_commands.py +110 -110
- package/storage.py +1338 -1338
- package/tools/base.py +465 -465
- package/tools/builtin/_math_common.py +667 -667
- package/tools/builtin/_net_common.py +267 -267
- package/tools/builtin/arxiv.py +681 -681
- package/tools/builtin/fetch_url.py +371 -371
- package/tools/builtin/file_ops.py +720 -720
- package/tools/builtin/geometry.py +184 -184
- package/tools/builtin/load_skill.py +76 -76
- package/tools/builtin/pin.py +227 -227
- package/tools/builtin/probability_stats.py +224 -224
- package/tools/builtin/project_docs.py +464 -464
- package/tools/builtin/remember.py +175 -175
- package/tools/builtin/shell.py +980 -980
- package/tools/builtin/todo.py +149 -149
- package/tools/builtin/web_search.py +328 -328
- package/tools/context.py +159 -159
- package/tools/isolation.py +387 -387
- package/tools/registry.py +1242 -1242
- package/tui/app.py +8318 -8307
- package/tui/app.tcss +747 -739
- package/tui/commands.py +26 -26
- package/tui/diffs.py +286 -286
- package/tui/history.py +128 -128
- package/tui/hosted_driver.py +102 -102
- package/tui/markdown.py +1329 -1329
- package/tui/meters.py +364 -364
- package/tui/ravens.py +146 -146
- package/tui/screens.py +1255 -1197
- package/tui/themes.py +599 -599
- package/tui/widgets.py +3813 -3796
package/security/confine.py
CHANGED
|
@@ -1,484 +1,484 @@
|
|
|
1
|
-
"""
|
|
2
|
-
security/confine.py
|
|
3
|
-
|
|
4
|
-
ROADMAP_v3 §79 (HD11). The walls a math tool's child puts up around itself in
|
|
5
|
-
a shared hosted deployment: after it has imported SymPy, and before it reads
|
|
6
|
-
the expression a visitor's model sent.
|
|
7
|
-
|
|
8
|
-
WHY THE MATH CHILD. SymPy's parser ends in `eval` -- `_math_common.py`'s
|
|
9
|
-
docstring is the long account of how far that goes -- and in a shared demo
|
|
10
|
-
the visitor connects and prompts their OWN model, so they choose every
|
|
11
|
-
expression the six math tools are handed. `_validate_ast` is the lock. This
|
|
12
|
-
is what stands behind it if that lock ever opens: a child that cannot read
|
|
13
|
-
another visitor's files, write anywhere, open a socket, start a process,
|
|
14
|
-
signal or trace one, or keep root. `tools/isolation.py` already ran each
|
|
15
|
-
call in its own short-lived child for the CPU bound (H2, H3); this module
|
|
16
|
-
is what that child does to itself first, so the boundary already existed
|
|
17
|
-
and only its walls are new.
|
|
18
|
-
|
|
19
|
-
FOUR LAYERS, in the order they go up. Each was measured on the Space before
|
|
20
|
-
it was relied on (2026-10-20: Amazon Linux kernel 6.18, x86_64, sessions
|
|
21
|
-
running as root with Docker's default capabilities less NET_RAW, Landlock
|
|
22
|
-
ABI 7, seccomp filters installable):
|
|
23
|
-
|
|
24
|
-
1. LIMITS. No file may grow past the cap already in force (RLIMIT_FSIZE:
|
|
25
|
-
zero, or the cap isolation.py puts on the two output files it hands
|
|
26
|
-
a math child), few descriptors, no core dump, and the process is
|
|
27
|
-
non-dumpable. CPU and memory are already bounded by isolation.py's
|
|
28
|
-
preexec_fn.
|
|
29
|
-
2. IDENTITY. A child started as root becomes `nobody`, and loses every
|
|
30
|
-
capability with it -- DAC_OVERRIDE above all, which is what lets root
|
|
31
|
-
read a 0700 directory it does not own. Why the session environment
|
|
32
|
-
lives in /opt when the bridge runs as root: `nobody` must be able to
|
|
33
|
-
import SymPy from it (session_env.py).
|
|
34
|
-
3. LANDLOCK. Read-only beneath the interpreter's own import roots and
|
|
35
|
-
nothing else -- no other visitor's session directory, no /tmp, no
|
|
36
|
-
/proc, no write anywhere. ABI 4 adds no TCP; ABI 6 adds no signals or
|
|
37
|
-
abstract sockets outside the child.
|
|
38
|
-
4. SECCOMP. A denylist answered with EPERM, so SymPy sees an ordinary
|
|
39
|
-
OSError and the tool reports it, rather than a killed process. Last,
|
|
40
|
-
because it refuses the calls the layers before it are made of.
|
|
41
|
-
|
|
42
|
-
A DENYLIST, NOT AN ALLOWLIST, and on purpose. Which syscalls CPython, glibc
|
|
43
|
-
and SymPy's lazy imports need is a property of their versions, not of this
|
|
44
|
-
code, and an allowlist that misses one presents as a math tool that fails
|
|
45
|
-
for no reason the model can act on. Landlock carries the filesystem, so
|
|
46
|
-
the denylist only has to carry what Landlock does not: processes,
|
|
47
|
-
networking on a kernel older than ABI 4, cross-process access, privilege,
|
|
48
|
-
and the kernel's own control surfaces.
|
|
49
|
-
|
|
50
|
-
ALL OR NOTHING. `lock_down()` returns None only when every layer is in
|
|
51
|
-
place; anything else is the reason, and the caller does not run the tool.
|
|
52
|
-
A half-walled child that ran anyway would be the "reports a bound that did
|
|
53
|
-
not happen" this project keeps refusing (isolation.py says it first).
|
|
54
|
-
Linux on x86_64 only; anywhere else every call is refused, because the
|
|
55
|
-
syscall numbers below are that architecture's.
|
|
56
|
-
|
|
57
|
-
THE SELF-TEST is how a deployment knows, rather than hopes. The bridge runs
|
|
58
|
-
`python -m security.confine --self-test` on the session interpreter at
|
|
59
|
-
startup: the child locks itself down exactly as a math call's child does,
|
|
60
|
-
computes an integral, and then tries every escape this module claims to
|
|
61
|
-
prevent. Only a clean pass lets the bridge tell its sessions
|
|
62
|
-
`VENASTINE_CONFINEMENT=verified`; without it a shared deployment refuses
|
|
63
|
-
the six math tools outright (security/hosted.py).
|
|
64
|
-
"""
|
|
65
|
-
|
|
66
|
-
from __future__ import annotations
|
|
67
|
-
|
|
68
|
-
import ctypes
|
|
69
|
-
import errno
|
|
70
|
-
import json
|
|
71
|
-
import os
|
|
72
|
-
import platform
|
|
73
|
-
import subprocess # nosec B404 - runs this module on the session interpreter, never a shell
|
|
74
|
-
import sys
|
|
75
|
-
import sysconfig
|
|
76
|
-
from typing import Optional
|
|
77
|
-
|
|
78
|
-
MARKER = "@@VENASTINE_CONFINE@@"
|
|
79
|
-
|
|
80
|
-
# --- Landlock -----------------------------------------------------------------
|
|
81
|
-
# One syscall number on every architecture: they postdate the unified table.
|
|
82
|
-
_LANDLOCK_CREATE_RULESET = 444
|
|
83
|
-
_LANDLOCK_ADD_RULE = 445
|
|
84
|
-
_LANDLOCK_RESTRICT_SELF = 446
|
|
85
|
-
_LANDLOCK_CREATE_RULESET_VERSION = 1 << 0
|
|
86
|
-
_LANDLOCK_RULE_PATH_BENEATH = 1
|
|
87
|
-
_FS_EXECUTE = 1 << 0
|
|
88
|
-
_FS_READ_FILE = 1 << 2
|
|
89
|
-
_FS_READ_DIR = 1 << 3
|
|
90
|
-
_FS_REFER = 1 << 13 # ABI 2
|
|
91
|
-
_FS_TRUNCATE = 1 << 14 # ABI 3
|
|
92
|
-
_FS_IOCTL_DEV = 1 << 15 # ABI 5
|
|
93
|
-
_NET_BIND_TCP = 1 << 0 # ABI 4
|
|
94
|
-
_NET_CONNECT_TCP = 1 << 1 # ABI 4
|
|
95
|
-
_SCOPE_ABSTRACT_UNIX_SOCKET = 1 << 0 # ABI 6
|
|
96
|
-
_SCOPE_SIGNAL = 1 << 1 # ABI 6
|
|
97
|
-
# What the child may do beneath an import root: read, and map a C
|
|
98
|
-
# extension. EXECUTE grants execve, which seccomp refuses regardless.
|
|
99
|
-
_READ_ONLY = _FS_EXECUTE | _FS_READ_FILE | _FS_READ_DIR
|
|
100
|
-
|
|
101
|
-
# Never a read root, whatever sys.path says: a jail with `/` or `/tmp` in it
|
|
102
|
-
# is no jail, and /proc is where another process's environment lives.
|
|
103
|
-
_NEVER_ROOTS = ("/proc", "/sys", "/dev", "/tmp", "/var/tmp", "/run") # nosec B108 - places the child may NOT read
|
|
104
|
-
|
|
105
|
-
# --- prctl and seccomp ----------------------------------------------------------
|
|
106
|
-
_PR_SET_DUMPABLE = 4
|
|
107
|
-
_PR_SET_SECCOMP = 22
|
|
108
|
-
_PR_SET_NO_NEW_PRIVS = 38
|
|
109
|
-
_SECCOMP_MODE_FILTER = 2
|
|
110
|
-
_AUDIT_ARCH_X86_64 = 0xC000003E
|
|
111
|
-
_RET_KILL_PROCESS = 0x80000000
|
|
112
|
-
_RET_ERRNO = 0x00050000
|
|
113
|
-
_RET_ALLOW = 0x7FFF0000
|
|
114
|
-
_BPF_LD_W_ABS = 0x20
|
|
115
|
-
_BPF_JEQ_K = 0x15
|
|
116
|
-
_BPF_JGE_K = 0x35
|
|
117
|
-
_BPF_RET_K = 0x06
|
|
118
|
-
_X32_SYSCALL_BIT = 0x40000000
|
|
119
|
-
|
|
120
|
-
# x86_64 numbers, grouped by what each group would let an escaped child do.
|
|
121
|
-
DENIED_SYSCALLS = {
|
|
122
|
-
# Start a process, or become a different program.
|
|
123
|
-
"clone": 56, "fork": 57, "vfork": 58, "execve": 59, "execveat": 322,
|
|
124
|
-
"clone3": 435,
|
|
125
|
-
# Reach the network, or another process through a socket.
|
|
126
|
-
"socket": 41, "connect": 42, "accept": 43, "bind": 49, "listen": 50,
|
|
127
|
-
"socketpair": 53, "accept4": 288,
|
|
128
|
-
# Signal, trace or read another process.
|
|
129
|
-
"kill": 62, "tkill": 200, "tgkill": 234, "rt_sigqueueinfo": 129,
|
|
130
|
-
"rt_tgsigqueueinfo": 297, "pidfd_send_signal": 424, "pidfd_open": 434,
|
|
131
|
-
"pidfd_getfd": 438, "ptrace": 101, "process_vm_readv": 310,
|
|
132
|
-
"process_vm_writev": 311, "kcmp": 312, "perf_event_open": 298,
|
|
133
|
-
# Change who it is, or undo the walls.
|
|
134
|
-
"setuid": 105, "setgid": 106, "setreuid": 113, "setregid": 114,
|
|
135
|
-
"setgroups": 116, "setresuid": 117, "setresgid": 119, "setfsuid": 122,
|
|
136
|
-
"setfsgid": 123, "capset": 126, "prctl": 157, "personality": 135,
|
|
137
|
-
# Rearrange the filesystem it sees.
|
|
138
|
-
"chroot": 161, "pivot_root": 155, "mount": 165, "umount2": 166,
|
|
139
|
-
"unshare": 272, "setns": 308, "open_tree": 428, "move_mount": 429,
|
|
140
|
-
"fsopen": 430, "fsconfig": 431, "fsmount": 432, "fspick": 433,
|
|
141
|
-
"mount_setattr": 442, "name_to_handle_at": 303, "open_by_handle_at": 304,
|
|
142
|
-
# The kernel's own control surfaces.
|
|
143
|
-
"bpf": 321, "userfaultfd": 323, "io_uring_setup": 425,
|
|
144
|
-
"io_uring_enter": 426, "io_uring_register": 427, "keyctl": 250,
|
|
145
|
-
"add_key": 248, "request_key": 249, "init_module": 175,
|
|
146
|
-
"finit_module": 313, "delete_module": 176, "kexec_load": 246,
|
|
147
|
-
"kexec_file_load": 320, "reboot": 169, "swapon": 167, "swapoff": 168,
|
|
148
|
-
"acct": 163, "quotactl": 179, "syslog": 103, "iopl": 172, "ioperm": 173,
|
|
149
|
-
"sethostname": 170, "setdomainname": 171, "settimeofday": 164,
|
|
150
|
-
"clock_settime": 227, "adjtimex": 159, "clock_adjtime": 305,
|
|
151
|
-
"vhangup": 153,
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
_OPEN_FILES = 64
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
def supported() -> Optional[str]:
|
|
158
|
-
"""None where this module can confine a child, else why it cannot."""
|
|
159
|
-
if not sys.platform.startswith("linux"):
|
|
160
|
-
return f"{sys.platform} has no Landlock or seccomp"
|
|
161
|
-
if platform.machine() != "x86_64":
|
|
162
|
-
return f"the seccomp filter is x86_64's, not {platform.machine()}'s"
|
|
163
|
-
return None
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
def _libc():
|
|
167
|
-
libc = ctypes.CDLL(None, use_errno=True)
|
|
168
|
-
libc.syscall.restype = ctypes.c_long
|
|
169
|
-
return libc
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
def _errno_name() -> str:
|
|
173
|
-
code = ctypes.get_errno()
|
|
174
|
-
return errno.errorcode.get(code, str(code))
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
def landlock_abi() -> int:
|
|
178
|
-
"""Landlock's ABI version, or -1 when the kernel offers none."""
|
|
179
|
-
if not sys.platform.startswith("linux"):
|
|
180
|
-
return -1
|
|
181
|
-
abi = _libc().syscall(_LANDLOCK_CREATE_RULESET, None, 0,
|
|
182
|
-
_LANDLOCK_CREATE_RULESET_VERSION)
|
|
183
|
-
return int(abi) if abi >= 0 else -1
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
def read_roots() -> list:
|
|
187
|
-
"""The directories the child may read beneath: where this interpreter
|
|
188
|
-
imports from, and nothing that holds another process's data."""
|
|
189
|
-
candidates = {sysconfig.get_paths()[key]
|
|
190
|
-
for key in ("stdlib", "platstdlib", "purelib", "platlib")}
|
|
191
|
-
candidates.update(p for p in sys.path if p)
|
|
192
|
-
candidates.add(sys.base_prefix)
|
|
193
|
-
roots = set()
|
|
194
|
-
for candidate in candidates:
|
|
195
|
-
real = os.path.realpath(candidate)
|
|
196
|
-
if not os.path.isdir(real) or real == "/":
|
|
197
|
-
continue
|
|
198
|
-
if any(real == never or real.startswith(never + "/")
|
|
199
|
-
for never in _NEVER_ROOTS):
|
|
200
|
-
continue
|
|
201
|
-
roots.add(real)
|
|
202
|
-
return sorted(roots)
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
# --- the layers ---------------------------------------------------------------
|
|
206
|
-
|
|
207
|
-
def _apply_limits() -> Optional[str]:
|
|
208
|
-
import resource
|
|
209
|
-
|
|
210
|
-
try:
|
|
211
|
-
# THE LIMIT ALREADY IN FORCE, or zero where there is none. Since
|
|
212
|
-
# batch 221 (#204) isolation.py hands a math child two FILES for
|
|
213
|
-
# stdout and stderr and caps them, so zero here refused the child's
|
|
214
|
-
# own result: every confined call exited 120 with "no usable
|
|
215
|
-
# result", while the self-test, whose child writes to pipes, still
|
|
216
|
-
# passed. Landlock is what refuses opening any file for writing; the
|
|
217
|
-
# only files this child can grow are those two, and their cap is
|
|
218
|
-
# the transport's. Never raised, so a caller with no cap gets zero.
|
|
219
|
-
soft, _hard = resource.getrlimit(resource.RLIMIT_FSIZE)
|
|
220
|
-
grow = 0 if soft == resource.RLIM_INFINITY else soft
|
|
221
|
-
resource.setrlimit(resource.RLIMIT_FSIZE, (grow, grow))
|
|
222
|
-
resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
|
|
223
|
-
resource.setrlimit(resource.RLIMIT_NOFILE, (_OPEN_FILES, _OPEN_FILES))
|
|
224
|
-
except (OSError, ValueError) as exc:
|
|
225
|
-
return f"limits: {exc}"
|
|
226
|
-
if _libc().prctl(_PR_SET_DUMPABLE, 0, 0, 0, 0) != 0:
|
|
227
|
-
return f"non-dumpable: {_errno_name()}"
|
|
228
|
-
return None
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
def _drop_root() -> Optional[str]:
|
|
232
|
-
"""Become `nobody` when started as root; nothing to do otherwise."""
|
|
233
|
-
if os.geteuid() != 0:
|
|
234
|
-
return None
|
|
235
|
-
import pwd
|
|
236
|
-
|
|
237
|
-
try:
|
|
238
|
-
entry = pwd.getpwnam("nobody")
|
|
239
|
-
os.setgroups([])
|
|
240
|
-
os.setgid(entry.pw_gid)
|
|
241
|
-
os.setuid(entry.pw_uid)
|
|
242
|
-
except (KeyError, OSError) as exc:
|
|
243
|
-
return f"dropping root: {exc}"
|
|
244
|
-
if os.geteuid() == 0 or os.getegid() == 0:
|
|
245
|
-
return "dropping root: still root afterwards"
|
|
246
|
-
return None
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
def _apply_landlock(roots) -> Optional[str]:
|
|
250
|
-
abi = landlock_abi()
|
|
251
|
-
if abi < 1:
|
|
252
|
-
return "Landlock is not available on this kernel"
|
|
253
|
-
|
|
254
|
-
handled_fs = (1 << 13) - 1 # every ABI 1 right
|
|
255
|
-
if abi >= 2:
|
|
256
|
-
handled_fs |= _FS_REFER
|
|
257
|
-
if abi >= 3:
|
|
258
|
-
handled_fs |= _FS_TRUNCATE
|
|
259
|
-
if abi >= 5:
|
|
260
|
-
handled_fs |= _FS_IOCTL_DEV
|
|
261
|
-
fields = [("handled_access_fs", ctypes.c_uint64)]
|
|
262
|
-
values = [handled_fs]
|
|
263
|
-
if abi >= 4:
|
|
264
|
-
fields.append(("handled_access_net", ctypes.c_uint64))
|
|
265
|
-
values.append(_NET_BIND_TCP | _NET_CONNECT_TCP)
|
|
266
|
-
if abi >= 6:
|
|
267
|
-
fields.append(("scoped", ctypes.c_uint64))
|
|
268
|
-
values.append(_SCOPE_ABSTRACT_UNIX_SOCKET | _SCOPE_SIGNAL)
|
|
269
|
-
|
|
270
|
-
class RulesetAttr(ctypes.Structure):
|
|
271
|
-
_fields_ = fields
|
|
272
|
-
|
|
273
|
-
class PathBeneath(ctypes.Structure):
|
|
274
|
-
_pack_ = 1
|
|
275
|
-
_fields_ = [("allowed_access", ctypes.c_uint64),
|
|
276
|
-
("parent_fd", ctypes.c_int32)]
|
|
277
|
-
|
|
278
|
-
libc = _libc()
|
|
279
|
-
attr = RulesetAttr(*values)
|
|
280
|
-
ruleset = libc.syscall(_LANDLOCK_CREATE_RULESET, ctypes.byref(attr),
|
|
281
|
-
ctypes.sizeof(attr), 0)
|
|
282
|
-
if ruleset < 0:
|
|
283
|
-
return f"Landlock ruleset: {_errno_name()}"
|
|
284
|
-
try:
|
|
285
|
-
for root in roots:
|
|
286
|
-
try:
|
|
287
|
-
dirfd = os.open(root, os.O_PATH | os.O_CLOEXEC)
|
|
288
|
-
except OSError as exc:
|
|
289
|
-
return f"Landlock root {root}: {exc.strerror}"
|
|
290
|
-
try:
|
|
291
|
-
rule = PathBeneath(_READ_ONLY & handled_fs, dirfd)
|
|
292
|
-
if libc.syscall(_LANDLOCK_ADD_RULE, ruleset,
|
|
293
|
-
_LANDLOCK_RULE_PATH_BENEATH,
|
|
294
|
-
ctypes.byref(rule), 0) != 0:
|
|
295
|
-
return f"Landlock rule {root}: {_errno_name()}"
|
|
296
|
-
finally:
|
|
297
|
-
os.close(dirfd)
|
|
298
|
-
if libc.prctl(_PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0:
|
|
299
|
-
return f"no_new_privs: {_errno_name()}"
|
|
300
|
-
if libc.syscall(_LANDLOCK_RESTRICT_SELF, ruleset, 0) != 0:
|
|
301
|
-
return f"Landlock restrict: {_errno_name()}"
|
|
302
|
-
finally:
|
|
303
|
-
os.close(ruleset)
|
|
304
|
-
return None
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
def seccomp_program() -> list:
|
|
308
|
-
"""The filter as (code, jt, jf, k) rows: the architecture checked first,
|
|
309
|
-
an x32 number refused, every DENIED_SYSCALLS entry answered EPERM, and
|
|
310
|
-
everything else allowed."""
|
|
311
|
-
denied = sorted(set(DENIED_SYSCALLS.values()))
|
|
312
|
-
count = len(denied)
|
|
313
|
-
rows = [
|
|
314
|
-
(_BPF_LD_W_ABS, 0, 0, 4), # seccomp_data.arch
|
|
315
|
-
(_BPF_JEQ_K, 1, 0, _AUDIT_ARCH_X86_64),
|
|
316
|
-
(_BPF_RET_K, 0, 0, _RET_KILL_PROCESS),
|
|
317
|
-
(_BPF_LD_W_ABS, 0, 0, 0), # seccomp_data.nr
|
|
318
|
-
(_BPF_JGE_K, count + 1, 0, _X32_SYSCALL_BIT),
|
|
319
|
-
]
|
|
320
|
-
for index, number in enumerate(denied):
|
|
321
|
-
rows.append((_BPF_JEQ_K, count - index, 0, number))
|
|
322
|
-
rows.append((_BPF_RET_K, 0, 0, _RET_ALLOW))
|
|
323
|
-
rows.append((_BPF_RET_K, 0, 0, _RET_ERRNO | errno.EPERM))
|
|
324
|
-
return rows
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
def _apply_seccomp() -> Optional[str]:
|
|
328
|
-
class Filter(ctypes.Structure):
|
|
329
|
-
_fields_ = [("code", ctypes.c_ushort), ("jt", ctypes.c_ubyte),
|
|
330
|
-
("jf", ctypes.c_ubyte), ("k", ctypes.c_uint32)]
|
|
331
|
-
|
|
332
|
-
class Program(ctypes.Structure):
|
|
333
|
-
_fields_ = [("len", ctypes.c_ushort),
|
|
334
|
-
("filter", ctypes.POINTER(Filter))]
|
|
335
|
-
|
|
336
|
-
rows = seccomp_program()
|
|
337
|
-
array = (Filter * len(rows))(*[Filter(*row) for row in rows])
|
|
338
|
-
program = Program(len(rows), array)
|
|
339
|
-
libc = _libc()
|
|
340
|
-
if libc.prctl(_PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0:
|
|
341
|
-
return f"no_new_privs: {_errno_name()}"
|
|
342
|
-
if libc.prctl(_PR_SET_SECCOMP, _SECCOMP_MODE_FILTER,
|
|
343
|
-
ctypes.byref(program), 0, 0) != 0:
|
|
344
|
-
return f"seccomp: {_errno_name()}"
|
|
345
|
-
return None
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
def lock_down(roots: Optional[list] = None) -> Optional[str]:
|
|
349
|
-
"""Put every wall up around THIS process. None when all are up; else
|
|
350
|
-
the reason, and the caller must not run the tool (see the docstring).
|
|
351
|
-
Irreversible: call it in a process that exists for one tool call."""
|
|
352
|
-
reason = supported()
|
|
353
|
-
if reason:
|
|
354
|
-
return reason
|
|
355
|
-
roots = read_roots() if roots is None else roots
|
|
356
|
-
for layer in (_apply_limits, _drop_root, lambda: _apply_landlock(roots),
|
|
357
|
-
_apply_seccomp):
|
|
358
|
-
reason = layer()
|
|
359
|
-
if reason:
|
|
360
|
-
return reason
|
|
361
|
-
return None
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
# --- the self-test --------------------------------------------------------------
|
|
365
|
-
|
|
366
|
-
def _attempt(fn) -> str:
|
|
367
|
-
try:
|
|
368
|
-
fn()
|
|
369
|
-
except OSError as exc:
|
|
370
|
-
return f"refused ({errno.errorcode.get(exc.errno, exc.errno)})"
|
|
371
|
-
except Exception as exc: # noqa: BLE001 -- reported; the verdict reads it
|
|
372
|
-
return f"failed ({type(exc).__name__})"
|
|
373
|
-
return "ALLOWED"
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
def _ptrace_parent() -> None:
|
|
377
|
-
libc = _libc()
|
|
378
|
-
libc.ptrace.restype = ctypes.c_long
|
|
379
|
-
if libc.ptrace(16, os.getppid(), None, None) != 0: # PTRACE_ATTACH
|
|
380
|
-
code = ctypes.get_errno()
|
|
381
|
-
raise OSError(code, os.strerror(code))
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
def _read_one(path: str) -> None:
|
|
385
|
-
"""The probe is whether the open is refused. One that is not still
|
|
386
|
-
closes what it opened, rather than leaving that to the collector."""
|
|
387
|
-
with open(path, "rb") as fh:
|
|
388
|
-
fh.read(1)
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
def _create(path: str) -> None:
|
|
392
|
-
with open(path, "wb"):
|
|
393
|
-
pass # creating it is the escape being probed; nothing is written
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
def _escapes(app_root: str) -> dict:
|
|
397
|
-
"""Every escape this module claims to prevent, by name."""
|
|
398
|
-
parent = os.getppid()
|
|
399
|
-
planted = os.path.join(app_root, f".confine-probe-{os.getpid()}")
|
|
400
|
-
return {
|
|
401
|
-
"read /etc/hostname": lambda: _read_one("/etc/hostname"),
|
|
402
|
-
"list /tmp": lambda: os.listdir("/tmp"), # nosec B108 - asserting it is refused
|
|
403
|
-
"write /tmp": lambda: _create(f"/tmp/.confine-probe-{os.getpid()}"), # nosec B108 - asserting it is refused
|
|
404
|
-
"write into the app": lambda: _create(planted),
|
|
405
|
-
"read the parent's environment":
|
|
406
|
-
lambda: _read_one(f"/proc/{parent}/environ"),
|
|
407
|
-
"open a socket": lambda: __import__("socket").socket().close(),
|
|
408
|
-
"fork": os.fork,
|
|
409
|
-
"start a program": lambda: subprocess.run(["true"], check=False), # nosec B603 B607 - asserting it is refused
|
|
410
|
-
"signal the parent": lambda: os.kill(parent, 0),
|
|
411
|
-
"trace the parent": _ptrace_parent,
|
|
412
|
-
"become root again": lambda: os.setuid(0),
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
def self_test() -> dict:
|
|
417
|
-
"""In THIS process: load the math tool, lock down, compute, try every
|
|
418
|
-
escape. `ok` only when the walls went up, the math still works and
|
|
419
|
-
every escape was refused."""
|
|
420
|
-
app_root = os.getcwd()
|
|
421
|
-
try:
|
|
422
|
-
from tools.builtin import symbolic_math
|
|
423
|
-
except Exception as exc: # noqa: BLE001 -- the verdict says why
|
|
424
|
-
return {"ok": False, "reason": f"the math tool would not load: {exc}"}
|
|
425
|
-
started_as_root = os.geteuid() == 0
|
|
426
|
-
reason = lock_down()
|
|
427
|
-
if reason:
|
|
428
|
-
return {"ok": False, "reason": reason}
|
|
429
|
-
answer = symbolic_math.run({"operation": "integrate",
|
|
430
|
-
"expression": "sin(x)**2"})
|
|
431
|
-
checks = {"math": answer.get("result") or f"failed: {answer.get('error')}"}
|
|
432
|
-
if started_as_root:
|
|
433
|
-
# Landlock and seccomp would still hold, but root's capabilities
|
|
434
|
-
# are a wall this module claims to remove, so it is checked too.
|
|
435
|
-
checks["still root"] = ("ALLOWED" if os.geteuid() == 0
|
|
436
|
-
else f"refused (uid {os.geteuid()})")
|
|
437
|
-
try:
|
|
438
|
-
__import__("sympy.physics.units")
|
|
439
|
-
checks["a lazy import"] = "ok"
|
|
440
|
-
except Exception as exc: # noqa: BLE001 -- the verdict says why
|
|
441
|
-
checks["a lazy import"] = f"failed ({type(exc).__name__})"
|
|
442
|
-
for label, fn in _escapes(app_root).items():
|
|
443
|
-
checks[label] = _attempt(fn)
|
|
444
|
-
escaped = [label for label, verdict in checks.items()
|
|
445
|
-
if verdict == "ALLOWED"]
|
|
446
|
-
broken = [label for label in ("math", "a lazy import")
|
|
447
|
-
if str(checks[label]).startswith("failed")]
|
|
448
|
-
ok = not escaped and not broken
|
|
449
|
-
return {"ok": ok, "uid": os.getuid(), "landlock_abi": landlock_abi(),
|
|
450
|
-
"reason": None if ok else
|
|
451
|
-
f"allowed: {escaped}; broken: {broken}", "checks": checks}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
def verify(python, root, *, run=subprocess.run, timeout: float = 120) -> dict:
|
|
455
|
-
"""Run the self-test in a child on `python`, from `root`. The verdict,
|
|
456
|
-
or `ok: False` with why the child gave none."""
|
|
457
|
-
env = {"PATH": os.environ.get("PATH", "/usr/bin:/bin"),
|
|
458
|
-
"PYTHONDONTWRITEBYTECODE": "1", "PYTHONIOENCODING": "utf-8"}
|
|
459
|
-
try:
|
|
460
|
-
done = run([str(python), "-m", "security.confine", "--self-test"],
|
|
461
|
-
cwd=str(root), env=env, capture_output=True, text=True,
|
|
462
|
-
timeout=timeout, check=False) # nosec B603 - fixed arguments, no shell
|
|
463
|
-
except (OSError, subprocess.TimeoutExpired) as exc:
|
|
464
|
-
return {"ok": False, "reason": f"the self-test did not run: "
|
|
465
|
-
f"{type(exc).__name__}"}
|
|
466
|
-
for line in (done.stdout or "").splitlines():
|
|
467
|
-
if line.startswith(MARKER):
|
|
468
|
-
try:
|
|
469
|
-
return json.loads(line[len(MARKER):])
|
|
470
|
-
except ValueError:
|
|
471
|
-
break
|
|
472
|
-
tail = (done.stderr or "").strip()[-400:]
|
|
473
|
-
return {"ok": False, "reason": f"the self-test gave no verdict (exit "
|
|
474
|
-
f"{done.returncode}): {tail}"}
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
if __name__ == "__main__":
|
|
478
|
-
if sys.argv[1:] != ["--self-test"]:
|
|
479
|
-
print("usage: python -m security.confine --self-test", file=sys.stderr)
|
|
480
|
-
sys.exit(2)
|
|
481
|
-
verdict = self_test()
|
|
482
|
-
sys.stdout.write(MARKER + json.dumps(verdict) + "\n")
|
|
483
|
-
sys.stdout.flush()
|
|
484
|
-
sys.exit(0)
|
|
1
|
+
"""
|
|
2
|
+
security/confine.py
|
|
3
|
+
|
|
4
|
+
ROADMAP_v3 §79 (HD11). The walls a math tool's child puts up around itself in
|
|
5
|
+
a shared hosted deployment: after it has imported SymPy, and before it reads
|
|
6
|
+
the expression a visitor's model sent.
|
|
7
|
+
|
|
8
|
+
WHY THE MATH CHILD. SymPy's parser ends in `eval` -- `_math_common.py`'s
|
|
9
|
+
docstring is the long account of how far that goes -- and in a shared demo
|
|
10
|
+
the visitor connects and prompts their OWN model, so they choose every
|
|
11
|
+
expression the six math tools are handed. `_validate_ast` is the lock. This
|
|
12
|
+
is what stands behind it if that lock ever opens: a child that cannot read
|
|
13
|
+
another visitor's files, write anywhere, open a socket, start a process,
|
|
14
|
+
signal or trace one, or keep root. `tools/isolation.py` already ran each
|
|
15
|
+
call in its own short-lived child for the CPU bound (H2, H3); this module
|
|
16
|
+
is what that child does to itself first, so the boundary already existed
|
|
17
|
+
and only its walls are new.
|
|
18
|
+
|
|
19
|
+
FOUR LAYERS, in the order they go up. Each was measured on the Space before
|
|
20
|
+
it was relied on (2026-10-20: Amazon Linux kernel 6.18, x86_64, sessions
|
|
21
|
+
running as root with Docker's default capabilities less NET_RAW, Landlock
|
|
22
|
+
ABI 7, seccomp filters installable):
|
|
23
|
+
|
|
24
|
+
1. LIMITS. No file may grow past the cap already in force (RLIMIT_FSIZE:
|
|
25
|
+
zero, or the cap isolation.py puts on the two output files it hands
|
|
26
|
+
a math child), few descriptors, no core dump, and the process is
|
|
27
|
+
non-dumpable. CPU and memory are already bounded by isolation.py's
|
|
28
|
+
preexec_fn.
|
|
29
|
+
2. IDENTITY. A child started as root becomes `nobody`, and loses every
|
|
30
|
+
capability with it -- DAC_OVERRIDE above all, which is what lets root
|
|
31
|
+
read a 0700 directory it does not own. Why the session environment
|
|
32
|
+
lives in /opt when the bridge runs as root: `nobody` must be able to
|
|
33
|
+
import SymPy from it (session_env.py).
|
|
34
|
+
3. LANDLOCK. Read-only beneath the interpreter's own import roots and
|
|
35
|
+
nothing else -- no other visitor's session directory, no /tmp, no
|
|
36
|
+
/proc, no write anywhere. ABI 4 adds no TCP; ABI 6 adds no signals or
|
|
37
|
+
abstract sockets outside the child.
|
|
38
|
+
4. SECCOMP. A denylist answered with EPERM, so SymPy sees an ordinary
|
|
39
|
+
OSError and the tool reports it, rather than a killed process. Last,
|
|
40
|
+
because it refuses the calls the layers before it are made of.
|
|
41
|
+
|
|
42
|
+
A DENYLIST, NOT AN ALLOWLIST, and on purpose. Which syscalls CPython, glibc
|
|
43
|
+
and SymPy's lazy imports need is a property of their versions, not of this
|
|
44
|
+
code, and an allowlist that misses one presents as a math tool that fails
|
|
45
|
+
for no reason the model can act on. Landlock carries the filesystem, so
|
|
46
|
+
the denylist only has to carry what Landlock does not: processes,
|
|
47
|
+
networking on a kernel older than ABI 4, cross-process access, privilege,
|
|
48
|
+
and the kernel's own control surfaces.
|
|
49
|
+
|
|
50
|
+
ALL OR NOTHING. `lock_down()` returns None only when every layer is in
|
|
51
|
+
place; anything else is the reason, and the caller does not run the tool.
|
|
52
|
+
A half-walled child that ran anyway would be the "reports a bound that did
|
|
53
|
+
not happen" this project keeps refusing (isolation.py says it first).
|
|
54
|
+
Linux on x86_64 only; anywhere else every call is refused, because the
|
|
55
|
+
syscall numbers below are that architecture's.
|
|
56
|
+
|
|
57
|
+
THE SELF-TEST is how a deployment knows, rather than hopes. The bridge runs
|
|
58
|
+
`python -m security.confine --self-test` on the session interpreter at
|
|
59
|
+
startup: the child locks itself down exactly as a math call's child does,
|
|
60
|
+
computes an integral, and then tries every escape this module claims to
|
|
61
|
+
prevent. Only a clean pass lets the bridge tell its sessions
|
|
62
|
+
`VENASTINE_CONFINEMENT=verified`; without it a shared deployment refuses
|
|
63
|
+
the six math tools outright (security/hosted.py).
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
from __future__ import annotations
|
|
67
|
+
|
|
68
|
+
import ctypes
|
|
69
|
+
import errno
|
|
70
|
+
import json
|
|
71
|
+
import os
|
|
72
|
+
import platform
|
|
73
|
+
import subprocess # nosec B404 - runs this module on the session interpreter, never a shell
|
|
74
|
+
import sys
|
|
75
|
+
import sysconfig
|
|
76
|
+
from typing import Optional
|
|
77
|
+
|
|
78
|
+
MARKER = "@@VENASTINE_CONFINE@@"
|
|
79
|
+
|
|
80
|
+
# --- Landlock -----------------------------------------------------------------
|
|
81
|
+
# One syscall number on every architecture: they postdate the unified table.
|
|
82
|
+
_LANDLOCK_CREATE_RULESET = 444
|
|
83
|
+
_LANDLOCK_ADD_RULE = 445
|
|
84
|
+
_LANDLOCK_RESTRICT_SELF = 446
|
|
85
|
+
_LANDLOCK_CREATE_RULESET_VERSION = 1 << 0
|
|
86
|
+
_LANDLOCK_RULE_PATH_BENEATH = 1
|
|
87
|
+
_FS_EXECUTE = 1 << 0
|
|
88
|
+
_FS_READ_FILE = 1 << 2
|
|
89
|
+
_FS_READ_DIR = 1 << 3
|
|
90
|
+
_FS_REFER = 1 << 13 # ABI 2
|
|
91
|
+
_FS_TRUNCATE = 1 << 14 # ABI 3
|
|
92
|
+
_FS_IOCTL_DEV = 1 << 15 # ABI 5
|
|
93
|
+
_NET_BIND_TCP = 1 << 0 # ABI 4
|
|
94
|
+
_NET_CONNECT_TCP = 1 << 1 # ABI 4
|
|
95
|
+
_SCOPE_ABSTRACT_UNIX_SOCKET = 1 << 0 # ABI 6
|
|
96
|
+
_SCOPE_SIGNAL = 1 << 1 # ABI 6
|
|
97
|
+
# What the child may do beneath an import root: read, and map a C
|
|
98
|
+
# extension. EXECUTE grants execve, which seccomp refuses regardless.
|
|
99
|
+
_READ_ONLY = _FS_EXECUTE | _FS_READ_FILE | _FS_READ_DIR
|
|
100
|
+
|
|
101
|
+
# Never a read root, whatever sys.path says: a jail with `/` or `/tmp` in it
|
|
102
|
+
# is no jail, and /proc is where another process's environment lives.
|
|
103
|
+
_NEVER_ROOTS = ("/proc", "/sys", "/dev", "/tmp", "/var/tmp", "/run") # nosec B108 - places the child may NOT read
|
|
104
|
+
|
|
105
|
+
# --- prctl and seccomp ----------------------------------------------------------
|
|
106
|
+
_PR_SET_DUMPABLE = 4
|
|
107
|
+
_PR_SET_SECCOMP = 22
|
|
108
|
+
_PR_SET_NO_NEW_PRIVS = 38
|
|
109
|
+
_SECCOMP_MODE_FILTER = 2
|
|
110
|
+
_AUDIT_ARCH_X86_64 = 0xC000003E
|
|
111
|
+
_RET_KILL_PROCESS = 0x80000000
|
|
112
|
+
_RET_ERRNO = 0x00050000
|
|
113
|
+
_RET_ALLOW = 0x7FFF0000
|
|
114
|
+
_BPF_LD_W_ABS = 0x20
|
|
115
|
+
_BPF_JEQ_K = 0x15
|
|
116
|
+
_BPF_JGE_K = 0x35
|
|
117
|
+
_BPF_RET_K = 0x06
|
|
118
|
+
_X32_SYSCALL_BIT = 0x40000000
|
|
119
|
+
|
|
120
|
+
# x86_64 numbers, grouped by what each group would let an escaped child do.
|
|
121
|
+
DENIED_SYSCALLS = {
|
|
122
|
+
# Start a process, or become a different program.
|
|
123
|
+
"clone": 56, "fork": 57, "vfork": 58, "execve": 59, "execveat": 322,
|
|
124
|
+
"clone3": 435,
|
|
125
|
+
# Reach the network, or another process through a socket.
|
|
126
|
+
"socket": 41, "connect": 42, "accept": 43, "bind": 49, "listen": 50,
|
|
127
|
+
"socketpair": 53, "accept4": 288,
|
|
128
|
+
# Signal, trace or read another process.
|
|
129
|
+
"kill": 62, "tkill": 200, "tgkill": 234, "rt_sigqueueinfo": 129,
|
|
130
|
+
"rt_tgsigqueueinfo": 297, "pidfd_send_signal": 424, "pidfd_open": 434,
|
|
131
|
+
"pidfd_getfd": 438, "ptrace": 101, "process_vm_readv": 310,
|
|
132
|
+
"process_vm_writev": 311, "kcmp": 312, "perf_event_open": 298,
|
|
133
|
+
# Change who it is, or undo the walls.
|
|
134
|
+
"setuid": 105, "setgid": 106, "setreuid": 113, "setregid": 114,
|
|
135
|
+
"setgroups": 116, "setresuid": 117, "setresgid": 119, "setfsuid": 122,
|
|
136
|
+
"setfsgid": 123, "capset": 126, "prctl": 157, "personality": 135,
|
|
137
|
+
# Rearrange the filesystem it sees.
|
|
138
|
+
"chroot": 161, "pivot_root": 155, "mount": 165, "umount2": 166,
|
|
139
|
+
"unshare": 272, "setns": 308, "open_tree": 428, "move_mount": 429,
|
|
140
|
+
"fsopen": 430, "fsconfig": 431, "fsmount": 432, "fspick": 433,
|
|
141
|
+
"mount_setattr": 442, "name_to_handle_at": 303, "open_by_handle_at": 304,
|
|
142
|
+
# The kernel's own control surfaces.
|
|
143
|
+
"bpf": 321, "userfaultfd": 323, "io_uring_setup": 425,
|
|
144
|
+
"io_uring_enter": 426, "io_uring_register": 427, "keyctl": 250,
|
|
145
|
+
"add_key": 248, "request_key": 249, "init_module": 175,
|
|
146
|
+
"finit_module": 313, "delete_module": 176, "kexec_load": 246,
|
|
147
|
+
"kexec_file_load": 320, "reboot": 169, "swapon": 167, "swapoff": 168,
|
|
148
|
+
"acct": 163, "quotactl": 179, "syslog": 103, "iopl": 172, "ioperm": 173,
|
|
149
|
+
"sethostname": 170, "setdomainname": 171, "settimeofday": 164,
|
|
150
|
+
"clock_settime": 227, "adjtimex": 159, "clock_adjtime": 305,
|
|
151
|
+
"vhangup": 153,
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
_OPEN_FILES = 64
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def supported() -> Optional[str]:
|
|
158
|
+
"""None where this module can confine a child, else why it cannot."""
|
|
159
|
+
if not sys.platform.startswith("linux"):
|
|
160
|
+
return f"{sys.platform} has no Landlock or seccomp"
|
|
161
|
+
if platform.machine() != "x86_64":
|
|
162
|
+
return f"the seccomp filter is x86_64's, not {platform.machine()}'s"
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _libc():
|
|
167
|
+
libc = ctypes.CDLL(None, use_errno=True)
|
|
168
|
+
libc.syscall.restype = ctypes.c_long
|
|
169
|
+
return libc
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _errno_name() -> str:
|
|
173
|
+
code = ctypes.get_errno()
|
|
174
|
+
return errno.errorcode.get(code, str(code))
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def landlock_abi() -> int:
|
|
178
|
+
"""Landlock's ABI version, or -1 when the kernel offers none."""
|
|
179
|
+
if not sys.platform.startswith("linux"):
|
|
180
|
+
return -1
|
|
181
|
+
abi = _libc().syscall(_LANDLOCK_CREATE_RULESET, None, 0,
|
|
182
|
+
_LANDLOCK_CREATE_RULESET_VERSION)
|
|
183
|
+
return int(abi) if abi >= 0 else -1
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def read_roots() -> list:
|
|
187
|
+
"""The directories the child may read beneath: where this interpreter
|
|
188
|
+
imports from, and nothing that holds another process's data."""
|
|
189
|
+
candidates = {sysconfig.get_paths()[key]
|
|
190
|
+
for key in ("stdlib", "platstdlib", "purelib", "platlib")}
|
|
191
|
+
candidates.update(p for p in sys.path if p)
|
|
192
|
+
candidates.add(sys.base_prefix)
|
|
193
|
+
roots = set()
|
|
194
|
+
for candidate in candidates:
|
|
195
|
+
real = os.path.realpath(candidate)
|
|
196
|
+
if not os.path.isdir(real) or real == "/":
|
|
197
|
+
continue
|
|
198
|
+
if any(real == never or real.startswith(never + "/")
|
|
199
|
+
for never in _NEVER_ROOTS):
|
|
200
|
+
continue
|
|
201
|
+
roots.add(real)
|
|
202
|
+
return sorted(roots)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
# --- the layers ---------------------------------------------------------------
|
|
206
|
+
|
|
207
|
+
def _apply_limits() -> Optional[str]:
|
|
208
|
+
import resource
|
|
209
|
+
|
|
210
|
+
try:
|
|
211
|
+
# THE LIMIT ALREADY IN FORCE, or zero where there is none. Since
|
|
212
|
+
# batch 221 (#204) isolation.py hands a math child two FILES for
|
|
213
|
+
# stdout and stderr and caps them, so zero here refused the child's
|
|
214
|
+
# own result: every confined call exited 120 with "no usable
|
|
215
|
+
# result", while the self-test, whose child writes to pipes, still
|
|
216
|
+
# passed. Landlock is what refuses opening any file for writing; the
|
|
217
|
+
# only files this child can grow are those two, and their cap is
|
|
218
|
+
# the transport's. Never raised, so a caller with no cap gets zero.
|
|
219
|
+
soft, _hard = resource.getrlimit(resource.RLIMIT_FSIZE)
|
|
220
|
+
grow = 0 if soft == resource.RLIM_INFINITY else soft
|
|
221
|
+
resource.setrlimit(resource.RLIMIT_FSIZE, (grow, grow))
|
|
222
|
+
resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
|
|
223
|
+
resource.setrlimit(resource.RLIMIT_NOFILE, (_OPEN_FILES, _OPEN_FILES))
|
|
224
|
+
except (OSError, ValueError) as exc:
|
|
225
|
+
return f"limits: {exc}"
|
|
226
|
+
if _libc().prctl(_PR_SET_DUMPABLE, 0, 0, 0, 0) != 0:
|
|
227
|
+
return f"non-dumpable: {_errno_name()}"
|
|
228
|
+
return None
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _drop_root() -> Optional[str]:
|
|
232
|
+
"""Become `nobody` when started as root; nothing to do otherwise."""
|
|
233
|
+
if os.geteuid() != 0:
|
|
234
|
+
return None
|
|
235
|
+
import pwd
|
|
236
|
+
|
|
237
|
+
try:
|
|
238
|
+
entry = pwd.getpwnam("nobody")
|
|
239
|
+
os.setgroups([])
|
|
240
|
+
os.setgid(entry.pw_gid)
|
|
241
|
+
os.setuid(entry.pw_uid)
|
|
242
|
+
except (KeyError, OSError) as exc:
|
|
243
|
+
return f"dropping root: {exc}"
|
|
244
|
+
if os.geteuid() == 0 or os.getegid() == 0:
|
|
245
|
+
return "dropping root: still root afterwards"
|
|
246
|
+
return None
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _apply_landlock(roots) -> Optional[str]:
|
|
250
|
+
abi = landlock_abi()
|
|
251
|
+
if abi < 1:
|
|
252
|
+
return "Landlock is not available on this kernel"
|
|
253
|
+
|
|
254
|
+
handled_fs = (1 << 13) - 1 # every ABI 1 right
|
|
255
|
+
if abi >= 2:
|
|
256
|
+
handled_fs |= _FS_REFER
|
|
257
|
+
if abi >= 3:
|
|
258
|
+
handled_fs |= _FS_TRUNCATE
|
|
259
|
+
if abi >= 5:
|
|
260
|
+
handled_fs |= _FS_IOCTL_DEV
|
|
261
|
+
fields = [("handled_access_fs", ctypes.c_uint64)]
|
|
262
|
+
values = [handled_fs]
|
|
263
|
+
if abi >= 4:
|
|
264
|
+
fields.append(("handled_access_net", ctypes.c_uint64))
|
|
265
|
+
values.append(_NET_BIND_TCP | _NET_CONNECT_TCP)
|
|
266
|
+
if abi >= 6:
|
|
267
|
+
fields.append(("scoped", ctypes.c_uint64))
|
|
268
|
+
values.append(_SCOPE_ABSTRACT_UNIX_SOCKET | _SCOPE_SIGNAL)
|
|
269
|
+
|
|
270
|
+
class RulesetAttr(ctypes.Structure):
|
|
271
|
+
_fields_ = fields
|
|
272
|
+
|
|
273
|
+
class PathBeneath(ctypes.Structure):
|
|
274
|
+
_pack_ = 1
|
|
275
|
+
_fields_ = [("allowed_access", ctypes.c_uint64),
|
|
276
|
+
("parent_fd", ctypes.c_int32)]
|
|
277
|
+
|
|
278
|
+
libc = _libc()
|
|
279
|
+
attr = RulesetAttr(*values)
|
|
280
|
+
ruleset = libc.syscall(_LANDLOCK_CREATE_RULESET, ctypes.byref(attr),
|
|
281
|
+
ctypes.sizeof(attr), 0)
|
|
282
|
+
if ruleset < 0:
|
|
283
|
+
return f"Landlock ruleset: {_errno_name()}"
|
|
284
|
+
try:
|
|
285
|
+
for root in roots:
|
|
286
|
+
try:
|
|
287
|
+
dirfd = os.open(root, os.O_PATH | os.O_CLOEXEC)
|
|
288
|
+
except OSError as exc:
|
|
289
|
+
return f"Landlock root {root}: {exc.strerror}"
|
|
290
|
+
try:
|
|
291
|
+
rule = PathBeneath(_READ_ONLY & handled_fs, dirfd)
|
|
292
|
+
if libc.syscall(_LANDLOCK_ADD_RULE, ruleset,
|
|
293
|
+
_LANDLOCK_RULE_PATH_BENEATH,
|
|
294
|
+
ctypes.byref(rule), 0) != 0:
|
|
295
|
+
return f"Landlock rule {root}: {_errno_name()}"
|
|
296
|
+
finally:
|
|
297
|
+
os.close(dirfd)
|
|
298
|
+
if libc.prctl(_PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0:
|
|
299
|
+
return f"no_new_privs: {_errno_name()}"
|
|
300
|
+
if libc.syscall(_LANDLOCK_RESTRICT_SELF, ruleset, 0) != 0:
|
|
301
|
+
return f"Landlock restrict: {_errno_name()}"
|
|
302
|
+
finally:
|
|
303
|
+
os.close(ruleset)
|
|
304
|
+
return None
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def seccomp_program() -> list:
|
|
308
|
+
"""The filter as (code, jt, jf, k) rows: the architecture checked first,
|
|
309
|
+
an x32 number refused, every DENIED_SYSCALLS entry answered EPERM, and
|
|
310
|
+
everything else allowed."""
|
|
311
|
+
denied = sorted(set(DENIED_SYSCALLS.values()))
|
|
312
|
+
count = len(denied)
|
|
313
|
+
rows = [
|
|
314
|
+
(_BPF_LD_W_ABS, 0, 0, 4), # seccomp_data.arch
|
|
315
|
+
(_BPF_JEQ_K, 1, 0, _AUDIT_ARCH_X86_64),
|
|
316
|
+
(_BPF_RET_K, 0, 0, _RET_KILL_PROCESS),
|
|
317
|
+
(_BPF_LD_W_ABS, 0, 0, 0), # seccomp_data.nr
|
|
318
|
+
(_BPF_JGE_K, count + 1, 0, _X32_SYSCALL_BIT),
|
|
319
|
+
]
|
|
320
|
+
for index, number in enumerate(denied):
|
|
321
|
+
rows.append((_BPF_JEQ_K, count - index, 0, number))
|
|
322
|
+
rows.append((_BPF_RET_K, 0, 0, _RET_ALLOW))
|
|
323
|
+
rows.append((_BPF_RET_K, 0, 0, _RET_ERRNO | errno.EPERM))
|
|
324
|
+
return rows
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def _apply_seccomp() -> Optional[str]:
|
|
328
|
+
class Filter(ctypes.Structure):
|
|
329
|
+
_fields_ = [("code", ctypes.c_ushort), ("jt", ctypes.c_ubyte),
|
|
330
|
+
("jf", ctypes.c_ubyte), ("k", ctypes.c_uint32)]
|
|
331
|
+
|
|
332
|
+
class Program(ctypes.Structure):
|
|
333
|
+
_fields_ = [("len", ctypes.c_ushort),
|
|
334
|
+
("filter", ctypes.POINTER(Filter))]
|
|
335
|
+
|
|
336
|
+
rows = seccomp_program()
|
|
337
|
+
array = (Filter * len(rows))(*[Filter(*row) for row in rows])
|
|
338
|
+
program = Program(len(rows), array)
|
|
339
|
+
libc = _libc()
|
|
340
|
+
if libc.prctl(_PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0:
|
|
341
|
+
return f"no_new_privs: {_errno_name()}"
|
|
342
|
+
if libc.prctl(_PR_SET_SECCOMP, _SECCOMP_MODE_FILTER,
|
|
343
|
+
ctypes.byref(program), 0, 0) != 0:
|
|
344
|
+
return f"seccomp: {_errno_name()}"
|
|
345
|
+
return None
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def lock_down(roots: Optional[list] = None) -> Optional[str]:
|
|
349
|
+
"""Put every wall up around THIS process. None when all are up; else
|
|
350
|
+
the reason, and the caller must not run the tool (see the docstring).
|
|
351
|
+
Irreversible: call it in a process that exists for one tool call."""
|
|
352
|
+
reason = supported()
|
|
353
|
+
if reason:
|
|
354
|
+
return reason
|
|
355
|
+
roots = read_roots() if roots is None else roots
|
|
356
|
+
for layer in (_apply_limits, _drop_root, lambda: _apply_landlock(roots),
|
|
357
|
+
_apply_seccomp):
|
|
358
|
+
reason = layer()
|
|
359
|
+
if reason:
|
|
360
|
+
return reason
|
|
361
|
+
return None
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
# --- the self-test --------------------------------------------------------------
|
|
365
|
+
|
|
366
|
+
def _attempt(fn) -> str:
|
|
367
|
+
try:
|
|
368
|
+
fn()
|
|
369
|
+
except OSError as exc:
|
|
370
|
+
return f"refused ({errno.errorcode.get(exc.errno, exc.errno)})"
|
|
371
|
+
except Exception as exc: # noqa: BLE001 -- reported; the verdict reads it
|
|
372
|
+
return f"failed ({type(exc).__name__})"
|
|
373
|
+
return "ALLOWED"
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def _ptrace_parent() -> None:
|
|
377
|
+
libc = _libc()
|
|
378
|
+
libc.ptrace.restype = ctypes.c_long
|
|
379
|
+
if libc.ptrace(16, os.getppid(), None, None) != 0: # PTRACE_ATTACH
|
|
380
|
+
code = ctypes.get_errno()
|
|
381
|
+
raise OSError(code, os.strerror(code))
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def _read_one(path: str) -> None:
|
|
385
|
+
"""The probe is whether the open is refused. One that is not still
|
|
386
|
+
closes what it opened, rather than leaving that to the collector."""
|
|
387
|
+
with open(path, "rb") as fh:
|
|
388
|
+
fh.read(1)
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def _create(path: str) -> None:
|
|
392
|
+
with open(path, "wb"):
|
|
393
|
+
pass # creating it is the escape being probed; nothing is written
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def _escapes(app_root: str) -> dict:
|
|
397
|
+
"""Every escape this module claims to prevent, by name."""
|
|
398
|
+
parent = os.getppid()
|
|
399
|
+
planted = os.path.join(app_root, f".confine-probe-{os.getpid()}")
|
|
400
|
+
return {
|
|
401
|
+
"read /etc/hostname": lambda: _read_one("/etc/hostname"),
|
|
402
|
+
"list /tmp": lambda: os.listdir("/tmp"), # nosec B108 - asserting it is refused
|
|
403
|
+
"write /tmp": lambda: _create(f"/tmp/.confine-probe-{os.getpid()}"), # nosec B108 - asserting it is refused
|
|
404
|
+
"write into the app": lambda: _create(planted),
|
|
405
|
+
"read the parent's environment":
|
|
406
|
+
lambda: _read_one(f"/proc/{parent}/environ"),
|
|
407
|
+
"open a socket": lambda: __import__("socket").socket().close(),
|
|
408
|
+
"fork": os.fork,
|
|
409
|
+
"start a program": lambda: subprocess.run(["true"], check=False), # nosec B603 B607 - asserting it is refused
|
|
410
|
+
"signal the parent": lambda: os.kill(parent, 0),
|
|
411
|
+
"trace the parent": _ptrace_parent,
|
|
412
|
+
"become root again": lambda: os.setuid(0),
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def self_test() -> dict:
|
|
417
|
+
"""In THIS process: load the math tool, lock down, compute, try every
|
|
418
|
+
escape. `ok` only when the walls went up, the math still works and
|
|
419
|
+
every escape was refused."""
|
|
420
|
+
app_root = os.getcwd()
|
|
421
|
+
try:
|
|
422
|
+
from tools.builtin import symbolic_math
|
|
423
|
+
except Exception as exc: # noqa: BLE001 -- the verdict says why
|
|
424
|
+
return {"ok": False, "reason": f"the math tool would not load: {exc}"}
|
|
425
|
+
started_as_root = os.geteuid() == 0
|
|
426
|
+
reason = lock_down()
|
|
427
|
+
if reason:
|
|
428
|
+
return {"ok": False, "reason": reason}
|
|
429
|
+
answer = symbolic_math.run({"operation": "integrate",
|
|
430
|
+
"expression": "sin(x)**2"})
|
|
431
|
+
checks = {"math": answer.get("result") or f"failed: {answer.get('error')}"}
|
|
432
|
+
if started_as_root:
|
|
433
|
+
# Landlock and seccomp would still hold, but root's capabilities
|
|
434
|
+
# are a wall this module claims to remove, so it is checked too.
|
|
435
|
+
checks["still root"] = ("ALLOWED" if os.geteuid() == 0
|
|
436
|
+
else f"refused (uid {os.geteuid()})")
|
|
437
|
+
try:
|
|
438
|
+
__import__("sympy.physics.units")
|
|
439
|
+
checks["a lazy import"] = "ok"
|
|
440
|
+
except Exception as exc: # noqa: BLE001 -- the verdict says why
|
|
441
|
+
checks["a lazy import"] = f"failed ({type(exc).__name__})"
|
|
442
|
+
for label, fn in _escapes(app_root).items():
|
|
443
|
+
checks[label] = _attempt(fn)
|
|
444
|
+
escaped = [label for label, verdict in checks.items()
|
|
445
|
+
if verdict == "ALLOWED"]
|
|
446
|
+
broken = [label for label in ("math", "a lazy import")
|
|
447
|
+
if str(checks[label]).startswith("failed")]
|
|
448
|
+
ok = not escaped and not broken
|
|
449
|
+
return {"ok": ok, "uid": os.getuid(), "landlock_abi": landlock_abi(),
|
|
450
|
+
"reason": None if ok else
|
|
451
|
+
f"allowed: {escaped}; broken: {broken}", "checks": checks}
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
def verify(python, root, *, run=subprocess.run, timeout: float = 120) -> dict:
|
|
455
|
+
"""Run the self-test in a child on `python`, from `root`. The verdict,
|
|
456
|
+
or `ok: False` with why the child gave none."""
|
|
457
|
+
env = {"PATH": os.environ.get("PATH", "/usr/bin:/bin"),
|
|
458
|
+
"PYTHONDONTWRITEBYTECODE": "1", "PYTHONIOENCODING": "utf-8"}
|
|
459
|
+
try:
|
|
460
|
+
done = run([str(python), "-m", "security.confine", "--self-test"],
|
|
461
|
+
cwd=str(root), env=env, capture_output=True, text=True,
|
|
462
|
+
timeout=timeout, check=False) # nosec B603 - fixed arguments, no shell
|
|
463
|
+
except (OSError, subprocess.TimeoutExpired) as exc:
|
|
464
|
+
return {"ok": False, "reason": f"the self-test did not run: "
|
|
465
|
+
f"{type(exc).__name__}"}
|
|
466
|
+
for line in (done.stdout or "").splitlines():
|
|
467
|
+
if line.startswith(MARKER):
|
|
468
|
+
try:
|
|
469
|
+
return json.loads(line[len(MARKER):])
|
|
470
|
+
except ValueError:
|
|
471
|
+
break
|
|
472
|
+
tail = (done.stderr or "").strip()[-400:]
|
|
473
|
+
return {"ok": False, "reason": f"the self-test gave no verdict (exit "
|
|
474
|
+
f"{done.returncode}): {tail}"}
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
if __name__ == "__main__":
|
|
478
|
+
if sys.argv[1:] != ["--self-test"]:
|
|
479
|
+
print("usage: python -m security.confine --self-test", file=sys.stderr)
|
|
480
|
+
sys.exit(2)
|
|
481
|
+
verdict = self_test()
|
|
482
|
+
sys.stdout.write(MARKER + json.dumps(verdict) + "\n")
|
|
483
|
+
sys.stdout.flush()
|
|
484
|
+
sys.exit(0)
|