@yahyahammad/venastine 0.1.0 → 0.3.8
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 -14
- package/CONFIG_ARCHITECTURE.md +2002 -0
- package/NOTICE +1 -1
- package/README.md +249 -93
- package/agents/builtin/build.md +73 -0
- package/agents/builtin/general.md +80 -0
- package/agents/builtin/plan.md +23 -5
- package/agents/builtin/test.md +65 -0
- package/agents/builtin/writer.md +67 -0
- package/agents/manager.py +261 -195
- package/agents/subagent_tool.py +280 -23
- package/agents/tui_commands.py +31 -21
- package/bin/venastine.mjs +528 -382
- package/bin/venastine.py +71 -0
- package/cli_commands.py +835 -0
- package/config.py +95 -1198
- package/config.yaml +1090 -0
- package/config_edit.py +1301 -0
- package/config_schema.py +966 -0
- package/config_update.py +749 -0
- package/core/agent_activity.py +256 -0
- package/core/ansi.py +452 -0
- package/core/approval.py +27 -5
- package/core/call_view.py +364 -0
- package/core/cancel.py +121 -0
- package/core/chatgpt_auth.py +747 -0
- package/core/claims_view.py +107 -0
- package/core/client.py +1249 -82
- package/core/commands.py +454 -0
- package/core/compaction.py +1735 -959
- package/core/config_loader.py +516 -97
- package/core/connect.py +686 -0
- package/core/events.py +68 -6
- package/core/file_mentions.py +280 -0
- package/core/interaction.py +87 -4
- package/core/line_pattern.py +128 -0
- package/core/loop.py +2566 -1353
- package/core/memory.py +482 -370
- package/core/pipeline_models.py +23 -9
- package/core/provider_errors.py +234 -0
- package/core/reasoning/confidence_scoring.py +70 -54
- package/core/reasoning/json_retry.py +19 -1
- package/core/reasoning/orchestrator.py +232 -53
- package/core/reasoning/output_writer.py +90 -31
- package/core/reasoning/payload_validation.py +133 -7
- package/core/reasoning/pipeline_storage.py +427 -361
- package/core/reasoning/review.py +659 -615
- package/core/reasoning/scholar.py +38 -14
- package/core/reasoning/source_corpus.py +110 -4
- package/core/replay.py +315 -167
- package/core/session.py +36 -12
- package/core/session_view.py +259 -0
- package/core/session_wake.py +259 -0
- package/core/shell_sessions.py +1553 -0
- package/core/workspace_trust.py +1 -1
- package/credentials.py +308 -180
- package/database.py +82 -1
- package/{PRIVACY.md → docs/PRIVACY.md} +19 -7
- package/docs/SECURITY.md +108 -0
- package/docs/THIRD_PARTY_NOTICES.md +148 -0
- package/env_secrets.py +118 -77
- package/json_store.py +236 -16
- package/logging_setup.py +121 -11
- package/main.py +1672 -198
- package/mcp_client/client.py +257 -23
- package/mcp_client/config.py +484 -379
- package/mcp_client/registration.py +163 -2
- package/memories/manager.py +15 -0
- package/memories/tui_commands.py +21 -14
- package/package.json +303 -73
- package/project_init/config_files.py +4 -0
- package/project_init/generator.py +201 -37
- package/project_init/manifest.py +418 -406
- package/project_init/tui_commands.py +7 -4
- package/prompts/revalidate.md +1 -1
- package/prompts/revise.md +17 -15
- package/prompts/system_prompts.py +64 -10
- package/providers.json.example +94 -92
- package/pyproject.toml +149 -100
- package/requirements.txt +252 -29
- package/safety/policy_enforcement.py +1082 -682
- package/security/askpass.py +419 -0
- package/security/capability.py +37 -7
- package/security/confine.py +484 -0
- package/security/hosted.py +182 -0
- package/security/permissions.py +54 -7
- package/security/posture.py +124 -12
- package/security/protected_paths.py +198 -6
- package/security/sandbox.py +3262 -139
- package/security/secrets.py +855 -0
- package/skills/tui_commands.py +3 -4
- package/storage.py +1338 -1005
- package/tools/base.py +465 -305
- package/tools/builtin/_math_common.py +306 -16
- package/tools/builtin/_net_common.py +267 -61
- package/tools/builtin/arxiv.py +681 -223
- package/tools/builtin/ask_user.py +231 -213
- package/tools/builtin/discrete_math.py +35 -4
- package/tools/builtin/fetch_url.py +239 -21
- package/tools/builtin/file_ops.py +341 -33
- package/tools/builtin/geometry.py +184 -148
- package/tools/builtin/get_time.py +2 -2
- package/tools/builtin/linear_algebra.py +66 -23
- package/tools/builtin/load_skill.py +10 -0
- package/tools/builtin/logic.py +171 -12
- package/tools/builtin/pin.py +46 -12
- package/tools/builtin/probability_stats.py +224 -228
- package/tools/builtin/project_docs.py +80 -21
- package/tools/builtin/remember.py +21 -4
- package/tools/builtin/shell.py +586 -67
- package/tools/builtin/shell_sessions.py +613 -0
- package/tools/builtin/symbolic_math.py +80 -10
- package/tools/builtin/todo.py +5 -3
- package/tools/builtin/web_search.py +329 -151
- package/tools/context.py +23 -0
- package/tools/isolation.py +387 -314
- package/tools/registry.py +374 -29
- package/tui/app.py +5281 -397
- package/tui/app.tcss +290 -9
- package/tui/commands.py +26 -61
- package/tui/diffs.py +286 -265
- package/tui/history.py +128 -0
- package/tui/hosted_driver.py +102 -0
- package/tui/markdown.py +1329 -0
- package/tui/meters.py +364 -0
- package/tui/screens.py +346 -92
- package/tui/themes.py +599 -366
- package/tui/widgets.py +2673 -116
- package/SECURITY.md +0 -88
- package/THIRD_PARTY_NOTICES.md +0 -100
|
@@ -0,0 +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)
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""
|
|
2
|
+
security/hosted.py
|
|
3
|
+
|
|
4
|
+
ROADMAP_v3 §79 (HD4, HD6, HD13). What a hosted deployment changes inside the
|
|
5
|
+
harness, and nothing else: the mode it runs in, the tools a shared deployment
|
|
6
|
+
refuses whatever `config.yaml` says, the sign-ins its bridge completes, and
|
|
7
|
+
the hardening calls a hosted process makes about itself.
|
|
8
|
+
|
|
9
|
+
THE MODE COMES FROM THE REAL ENVIRONMENT, ONCE, AT IMPORT. The web bridge
|
|
10
|
+
(`deploy/hosted/`) starts every session's process with `VENASTINE_HOSTED`
|
|
11
|
+
set, and nothing else does. A `.env` cannot supply it (A70: `VENASTINE_*`
|
|
12
|
+
names are never taken from that file, and the file is never loaded into the
|
|
13
|
+
environment at all). It is bound at import for `security/posture.py`'s
|
|
14
|
+
reason: a value read at call time follows a mutation, and this one decides
|
|
15
|
+
what a visitor may do.
|
|
16
|
+
|
|
17
|
+
AN ENVIRONMENT VARIABLE CAN ONLY TAKE CAPABILITY AWAY HERE, which is why
|
|
18
|
+
having one does not contradict `posture.py`'s "there is none, by design".
|
|
19
|
+
Unset is a local install. `single-user` is a local install that says it is
|
|
20
|
+
hosted. `shared` removes things. And anything the parser does not recognise
|
|
21
|
+
is `shared`: a typo must land on the restrictive side, because the bridge
|
|
22
|
+
that sets this is the only thing standing between a visitor and the
|
|
23
|
+
container.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
import ctypes
|
|
27
|
+
import os
|
|
28
|
+
import signal
|
|
29
|
+
import sys
|
|
30
|
+
|
|
31
|
+
OFF = ""
|
|
32
|
+
SHARED = "shared"
|
|
33
|
+
SINGLE_USER = "single-user"
|
|
34
|
+
|
|
35
|
+
# HD4. The tools that reach the container itself -- its files and its
|
|
36
|
+
# processes. A shared deployment runs every visitor in one container as one
|
|
37
|
+
# Unix user, so any of these would let one visitor read another's session.
|
|
38
|
+
# They ship disabled anyway; this is the second layer, for the deployment
|
|
39
|
+
# whose config.yaml says otherwise.
|
|
40
|
+
SHARED_REFUSED_TOOLS = frozenset({
|
|
41
|
+
"read", "write", "edit",
|
|
42
|
+
# Batch 174: an image file, read under `read`'s rules (§81, IM1).
|
|
43
|
+
"view_image",
|
|
44
|
+
"shell", "shell_background", "shell_monitor", "shell_interactive",
|
|
45
|
+
"shell_input", "shell_sessions", "shell_output", "shell_kill",
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# HD11. The tools whose child evaluates what the model sent: SymPy's parser
|
|
50
|
+
# ends in `eval`. A shared deployment runs them only inside the walls
|
|
51
|
+
# security/confine.py puts up, and only once the bridge's self-test has
|
|
52
|
+
# shown those walls standing on this host -- `VENASTINE_CONFINEMENT` is set
|
|
53
|
+
# to `verified` by the bridge after a clean pass, and by nothing else.
|
|
54
|
+
# Unset, they are refused like the tools above. (These are exactly the
|
|
55
|
+
# registry's BUDGET_COMPUTE tools; a test pins that.)
|
|
56
|
+
CONFINED_TOOLS = frozenset({
|
|
57
|
+
"symbolic_math", "linear_algebra", "probability_stats",
|
|
58
|
+
"discrete_math", "logic", "geometry",
|
|
59
|
+
})
|
|
60
|
+
CONFINEMENT_VERIFIED = "verified"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _mode_from_env() -> str:
|
|
64
|
+
raw = os.environ.get("VENASTINE_HOSTED", "").strip().lower()
|
|
65
|
+
if not raw:
|
|
66
|
+
return OFF
|
|
67
|
+
if raw == SINGLE_USER:
|
|
68
|
+
return SINGLE_USER
|
|
69
|
+
return SHARED
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
MODE = _mode_from_env()
|
|
73
|
+
# Bound at import for MODE's reason. Only a value the bridge set counts.
|
|
74
|
+
CONFINEMENT = os.environ.get("VENASTINE_CONFINEMENT", "").strip()
|
|
75
|
+
|
|
76
|
+
# HD13. The sign-ins the BRIDGE completes for this session -- ModelScope's,
|
|
77
|
+
# on modelscope.ai and on modelscope.cn (batch 176), whose apps each need a
|
|
78
|
+
# client secret that only the bridge holds. The names are
|
|
79
|
+
# deploy/hosted/sign_in.py's SITES, which this layer does not import;
|
|
80
|
+
# test_hosted_mode.py holds the two together. Bound at import for MODE's
|
|
81
|
+
# reason, and honoured only in a hosted session: the bridge sets it when it
|
|
82
|
+
# can finish the sign-in, and a local install has no bridge to finish it,
|
|
83
|
+
# however this variable reads.
|
|
84
|
+
BRIDGE_SIGN_IN_NAMES = frozenset({"modelscope", "modelscope-cn"})
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _bridge_sign_ins_from_env() -> frozenset:
|
|
88
|
+
if MODE == OFF:
|
|
89
|
+
return frozenset()
|
|
90
|
+
raw = os.environ.get("VENASTINE_BRIDGE_SIGN_IN", "")
|
|
91
|
+
return frozenset(name.strip().lower() for name in raw.split(",")) \
|
|
92
|
+
& BRIDGE_SIGN_IN_NAMES
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
BRIDGE_SIGN_INS = _bridge_sign_ins_from_env()
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def mode() -> str:
|
|
99
|
+
return MODE
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def refused_tools() -> frozenset:
|
|
103
|
+
"""What this deployment refuses whatever config.yaml says: nothing
|
|
104
|
+
unless it is shared; then the container tools always, and the math
|
|
105
|
+
tools unless their confinement was verified on this host (HD4, HD11)."""
|
|
106
|
+
if MODE != SHARED:
|
|
107
|
+
return frozenset()
|
|
108
|
+
if CONFINEMENT == CONFINEMENT_VERIFIED:
|
|
109
|
+
return SHARED_REFUSED_TOOLS
|
|
110
|
+
return SHARED_REFUSED_TOOLS | CONFINED_TOOLS
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def is_hosted() -> bool:
|
|
114
|
+
return MODE != OFF
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def is_shared() -> bool:
|
|
118
|
+
return MODE == SHARED
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def bridge_signs_in(name: str) -> bool:
|
|
122
|
+
"""Whether this session's bridge completes `name`'s sign-in (HD13)."""
|
|
123
|
+
return name in BRIDGE_SIGN_INS
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def make_undumpable() -> bool:
|
|
127
|
+
"""Mark this process non-dumpable, so other processes of the same user
|
|
128
|
+
cannot read its memory or environment (HD6). True when it took effect.
|
|
129
|
+
|
|
130
|
+
`prctl(PR_SET_DUMPABLE, 0)`: the kernel then hands `/proc/<pid>/mem`,
|
|
131
|
+
`/proc/<pid>/environ` and the rest to root and refuses a debugger
|
|
132
|
+
attach without CAP_SYS_PTRACE. Measured under WSL before it was relied
|
|
133
|
+
on: a sibling process read an ordinary process's environ, secret
|
|
134
|
+
included, and was refused the same read once the target had made this
|
|
135
|
+
call. It is per PROCESS and an exec resets it, which is why the session
|
|
136
|
+
makes it itself, first thing, rather than the bridge making it for the
|
|
137
|
+
child.
|
|
138
|
+
|
|
139
|
+
Linux only, and never raises: off Linux there is no shared container to
|
|
140
|
+
protect, and a hosted session that cannot harden itself still runs --
|
|
141
|
+
the bridge's own isolation is the first line, and this is the second.
|
|
142
|
+
"""
|
|
143
|
+
if not sys.platform.startswith("linux"):
|
|
144
|
+
return False
|
|
145
|
+
try:
|
|
146
|
+
libc = ctypes.CDLL(None, use_errno=True)
|
|
147
|
+
return libc.prctl(4, 0, 0, 0, 0) == 0 # PR_SET_DUMPABLE == 4
|
|
148
|
+
except (OSError, AttributeError):
|
|
149
|
+
return False
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def die_with_parent() -> bool:
|
|
153
|
+
"""Ask the kernel for SIGTERM when the bridge that started this session
|
|
154
|
+
goes away (HD8). True when it took effect.
|
|
155
|
+
|
|
156
|
+
The bridge starts every session in a NEW process group, so that stopping
|
|
157
|
+
one reaches what it started; the cost is that a signal to the bridge
|
|
158
|
+
reaches no session. Measured 2026-10-20 under WSL: a SIGTERM to the
|
|
159
|
+
bridge left the open session running, orphaned, with its directory, and
|
|
160
|
+
nothing would ever have stopped it -- it holds a visitor's key in memory
|
|
161
|
+
and counts against the container's. `prctl(PR_SET_PDEATHSIG)` closes
|
|
162
|
+
that, in the session, because like PR_SET_DUMPABLE it does not survive
|
|
163
|
+
the exec that starts one.
|
|
164
|
+
|
|
165
|
+
The signal comes when the THREAD that forked this process exits; the
|
|
166
|
+
bridge spawns from its event loop's thread, which lives as long as the
|
|
167
|
+
server does. If the bridge died before this call, the signal is already
|
|
168
|
+
missed, so a changed parent pid ends the session here instead.
|
|
169
|
+
|
|
170
|
+
Linux only, and never raises, for make_undumpable's reasons.
|
|
171
|
+
"""
|
|
172
|
+
if not sys.platform.startswith("linux"):
|
|
173
|
+
return False
|
|
174
|
+
parent = os.getppid()
|
|
175
|
+
try:
|
|
176
|
+
libc = ctypes.CDLL(None, use_errno=True)
|
|
177
|
+
ok = libc.prctl(1, int(signal.SIGTERM), 0, 0, 0) == 0 # PR_SET_PDEATHSIG == 1
|
|
178
|
+
except (OSError, AttributeError):
|
|
179
|
+
return False
|
|
180
|
+
if ok and os.getppid() != parent:
|
|
181
|
+
os.kill(os.getpid(), signal.SIGTERM)
|
|
182
|
+
return ok
|