sandlock 0.1.0__tar.gz

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.
Files changed (45) hide show
  1. sandlock-0.1.0/LICENSE +191 -0
  2. sandlock-0.1.0/PKG-INFO +318 -0
  3. sandlock-0.1.0/README.md +303 -0
  4. sandlock-0.1.0/pyproject.toml +31 -0
  5. sandlock-0.1.0/setup.cfg +4 -0
  6. sandlock-0.1.0/src/sandlock/__init__.py +65 -0
  7. sandlock-0.1.0/src/sandlock/_branchfs.py +222 -0
  8. sandlock-0.1.0/src/sandlock/_cgroup.py +176 -0
  9. sandlock-0.1.0/src/sandlock/_checkpoint.py +437 -0
  10. sandlock-0.1.0/src/sandlock/_chroot.py +24 -0
  11. sandlock-0.1.0/src/sandlock/_context.py +577 -0
  12. sandlock-0.1.0/src/sandlock/_events.py +34 -0
  13. sandlock-0.1.0/src/sandlock/_image.py +122 -0
  14. sandlock-0.1.0/src/sandlock/_landlock.py +338 -0
  15. sandlock-0.1.0/src/sandlock/_mprotect.py +60 -0
  16. sandlock-0.1.0/src/sandlock/_notif.py +908 -0
  17. sandlock-0.1.0/src/sandlock/_notif_policy.py +205 -0
  18. sandlock-0.1.0/src/sandlock/_procfs.py +111 -0
  19. sandlock-0.1.0/src/sandlock/_profile.py +176 -0
  20. sandlock-0.1.0/src/sandlock/_ptrace.py +311 -0
  21. sandlock-0.1.0/src/sandlock/_runner.py +326 -0
  22. sandlock-0.1.0/src/sandlock/_seccomp.py +465 -0
  23. sandlock-0.1.0/src/sandlock/_syscall_table.py +164 -0
  24. sandlock-0.1.0/src/sandlock/_userns.py +110 -0
  25. sandlock-0.1.0/src/sandlock/_version.py +2 -0
  26. sandlock-0.1.0/src/sandlock/cli.py +266 -0
  27. sandlock-0.1.0/src/sandlock/exceptions.py +81 -0
  28. sandlock-0.1.0/src/sandlock/policy.py +235 -0
  29. sandlock-0.1.0/src/sandlock/sandbox.py +584 -0
  30. sandlock-0.1.0/src/sandlock.egg-info/PKG-INFO +318 -0
  31. sandlock-0.1.0/src/sandlock.egg-info/SOURCES.txt +43 -0
  32. sandlock-0.1.0/src/sandlock.egg-info/dependency_links.txt +1 -0
  33. sandlock-0.1.0/src/sandlock.egg-info/entry_points.txt +2 -0
  34. sandlock-0.1.0/src/sandlock.egg-info/requires.txt +3 -0
  35. sandlock-0.1.0/src/sandlock.egg-info/top_level.txt +1 -0
  36. sandlock-0.1.0/tests/test_branchfs.py +315 -0
  37. sandlock-0.1.0/tests/test_checkpoint.py +505 -0
  38. sandlock-0.1.0/tests/test_integration.py +810 -0
  39. sandlock-0.1.0/tests/test_landlock.py +62 -0
  40. sandlock-0.1.0/tests/test_notif.py +480 -0
  41. sandlock-0.1.0/tests/test_policy.py +175 -0
  42. sandlock-0.1.0/tests/test_profile.py +162 -0
  43. sandlock-0.1.0/tests/test_runner.py +58 -0
  44. sandlock-0.1.0/tests/test_sandbox.py +128 -0
  45. sandlock-0.1.0/tests/test_seccomp.py +135 -0
sandlock-0.1.0/LICENSE ADDED
@@ -0,0 +1,191 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2026 Multikernel Technologies, Inc.
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ http://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
@@ -0,0 +1,318 @@
1
+ Metadata-Version: 2.4
2
+ Name: sandlock
3
+ Version: 0.1.0
4
+ Summary: Lightweight process sandbox using Landlock, seccomp, and seccomp user notification
5
+ Author-email: Cong Wang <cwang@multikernel.io>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/multikernel/sandlock
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Operating System :: POSIX :: Linux
10
+ Requires-Python: >=3.10
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: tomli>=1.0; python_version < "3.11"
14
+ Dynamic: license-file
15
+
16
+ # Sandlock
17
+
18
+ Lightweight process sandbox for Linux. Confines untrusted code using
19
+ **Landlock** (filesystem + network + IPC), **seccomp-bpf** (syscall filtering),
20
+ and **seccomp user notification** (resource limits, IP enforcement, /proc
21
+ virtualization). No root, no cgroups, no containers.
22
+
23
+ ```
24
+ sandlock run -w /tmp -r /usr -m 512M -- python3 untrusted.py
25
+ ```
26
+
27
+ ## Why Sandlock?
28
+
29
+ Containers and VMs are powerful but heavy. Sandlock targets the gap: strict
30
+ confinement without image builds, overlay filesystems, or root privileges.
31
+
32
+ | Feature | Sandlock | Container | MicroVM (Firecracker) | gVisor |
33
+ |---|---|---|---|---|
34
+ | Root required | No | Yes* | Yes (KVM) | Yes |
35
+ | Image build | No | Yes | Yes | Yes |
36
+ | Startup time | ~1 ms (fork) | ~200 ms | ~100 ms | ~100 ms |
37
+ | Kernel | Shared | Shared | Separate guest | Shared (sentry) |
38
+ | Filesystem isolation | Landlock | Overlay | Block-level (QCOW2) | ptrace/KVM |
39
+ | Network isolation | Landlock + seccomp notif | Network namespace | TAP device | Sentry kernel |
40
+ | Syscall filtering | seccomp-bpf | seccomp | N/A (full kernel) | Sentry kernel |
41
+ | Resource limits | seccomp notif + rlimit | cgroup v2 | VM config | cgroup v2 |
42
+ | Memory sharing | COW (fork), zero-copy | Bind-mount + re-init | Shared mem (explicit) | N/A |
43
+ | Nesting | Native (fork) | Complex (DinD/DooD) | Not supported | Supported |
44
+ | Checkpoint/restore | ptrace + BranchFS | CRIU | VM snapshot | N/A |
45
+
46
+ \* Rootless containers exist but require user namespace support, `/etc/subuid` configuration, and `fuse-overlayfs`.
47
+
48
+ ## Requirements
49
+
50
+ - **Linux 5.13+** (Landlock ABI v1), **Python 3.10+**
51
+ - No root, no cgroups, no C compiler (all kernel interfaces via ctypes)
52
+
53
+ | Feature | Minimum kernel |
54
+ |---|---|
55
+ | seccomp user notification | 5.6 |
56
+ | Landlock filesystem rules | 5.13 |
57
+ | Landlock TCP port rules | 6.7 (ABI v4) |
58
+ | Landlock IPC scoping | 6.12 (ABI v6) |
59
+
60
+ ## Quick Start
61
+
62
+ ### CLI
63
+
64
+ ```bash
65
+ # Basic confinement
66
+ sandlock run -r /usr -r /lib -w /tmp -- ls /tmp
67
+
68
+ # Interactive shell
69
+ sandlock run -i -r /usr -r /lib -r /lib64 -r /bin -r /etc -w /tmp -- /bin/sh
70
+
71
+ # Resource limits + timeout
72
+ sandlock run -m 512M -P 20 -t 30 -- ./compute.sh
73
+
74
+ # Domain-based network isolation
75
+ sandlock run --net-allow-host api.openai.com -r /usr -r /lib -r /etc -- python3 agent.py
76
+
77
+ # TCP port restrictions (Landlock)
78
+ sandlock run --net-bind 8080 --net-connect 443 -r /usr -r /lib -r /etc -- python3 server.py
79
+
80
+ # IPC scoping + clean environment
81
+ sandlock run --isolate-ipc --isolate-signals --clean-env --env CC=gcc \
82
+ -r /usr -r /lib -w /tmp -- make
83
+
84
+ # Use a saved profile (CLI flags override profile values)
85
+ sandlock run -p build -- make -j4
86
+ sandlock run -p build --max-memory 1G -- make
87
+
88
+ # Profile management
89
+ sandlock profile list
90
+ sandlock profile show build
91
+ ```
92
+
93
+ ### Profiles
94
+
95
+ Save reusable policies as TOML files in `~/.config/sandlock/profiles/`:
96
+
97
+ ```toml
98
+ # ~/.config/sandlock/profiles/build.toml
99
+ fs_writable = ["/tmp/work"]
100
+ fs_readable = ["/usr", "/lib", "/lib64", "/bin", "/etc"]
101
+ clean_env = true
102
+ isolate_ipc = true
103
+ max_memory = "512M"
104
+ max_processes = 50
105
+
106
+ [env]
107
+ CC = "gcc"
108
+ LANG = "C.UTF-8"
109
+ ```
110
+
111
+ Field names match `Policy` exactly. Unknown fields are rejected. CLI flags override profile values.
112
+
113
+ ### Python API
114
+
115
+ ```python
116
+ from sandlock import Sandbox, Policy
117
+
118
+ policy = Policy(
119
+ fs_writable=["/tmp/sandbox"],
120
+ fs_readable=["/usr", "/lib", "/etc"],
121
+ max_memory="256M",
122
+ max_processes=10,
123
+ isolate_ipc=True,
124
+ clean_env=True,
125
+ env={"LANG": "C.UTF-8"},
126
+ )
127
+
128
+ # Run a command
129
+ result = Sandbox(policy).run(["python3", "-c", "print('hello')"])
130
+
131
+ # Run a Python function
132
+ result = Sandbox(policy).call(lambda: sum(range(1_000_000)))
133
+ print(result.value) # 499999500000
134
+
135
+ # Use a saved TOML profile
136
+ result = Sandbox("build").run(["make", "-j4"])
137
+ ```
138
+
139
+ Long-lived and nested sandboxes:
140
+
141
+ ```python
142
+ with Sandbox(policy) as sb:
143
+ sb.exec(["python3", "server.py"])
144
+ sb.pause() # Freeze all processes atomically
145
+ sb.resume()
146
+ sb.update(max_memory="128M") # Adjust limits live
147
+ exit_code = sb.wait(timeout=30)
148
+
149
+ # Nested: child inherits parent's constraints
150
+ with Sandbox(parent_policy) as parent:
151
+ result = parent.sandbox(child_policy).call(untrusted_fn)
152
+ ```
153
+
154
+ ## Architecture
155
+
156
+ Sandlock applies confinement in sequence after `fork()`:
157
+
158
+ ```
159
+ Parent Child
160
+ │ fork() │
161
+ │──────────────────────────────────>│
162
+ │ ├─ 1. setpgid(0,0)
163
+ │ ├─ 2. unshare(NEWUSER) (if privileged)
164
+ │ ├─ 3. RLIMIT_CPU (if max_cpu)
165
+ │ ├─ 4. chroot (optional)
166
+ │ ├─ 5. Landlock (fs + net + IPC, irreversible)
167
+ │ ├─ 6. Combined seccomp filter (irreversible)
168
+ │ │ └─ send notify fd ──────> Parent
169
+ │ receive notify fd ├─ 7. Close fds 3+
170
+ │ start supervisor thread ├─ 8. Environment (clean_env + env)
171
+ │ pidfd_open() + poll() └─ 9. exec(cmd) or target()
172
+ ```
173
+
174
+ Each layer is **defense-in-depth**: bypassing one doesn't defeat the others.
175
+
176
+ ### Landlock: Filesystem + Network + IPC
177
+
178
+ [Landlock](https://landlock.io) restricts filesystem, network, and IPC
179
+ access without root. Rules are applied via `landlock_restrict_self()` and
180
+ are **irreversible**.
181
+
182
+ ```python
183
+ Policy(
184
+ fs_writable=["/tmp/work"],
185
+ fs_readable=["/usr", "/lib"], # ABI v1+: filesystem
186
+ net_bind=[8080], # ABI v4+: TCP ports
187
+ net_connect=[443, "5432"],
188
+ isolate_ipc=True, # ABI v6+: block abstract UNIX sockets to host
189
+ isolate_signals=True, # ABI v6+: block signals to host processes
190
+ )
191
+ ```
192
+
193
+ ### seccomp-bpf: Syscall Filtering
194
+
195
+ A cBPF filter blocks dangerous syscalls at the kernel entry point:
196
+ `ptrace`, `mount`, `unshare`, `setns`, `kexec_load`, `bpf`, `perf_event_open`,
197
+ `ioperm`/`iopl`, and more. Argument-level filtering blocks namespace flags
198
+ in `clone`/`clone3` and `TIOCSTI` in `ioctl`. Supports **x86_64** and **aarch64**.
199
+
200
+ ### Resource Limits
201
+
202
+ Enforced via **seccomp user notification** and **rlimit** — no cgroups or root required.
203
+
204
+ | Resource | Mechanism | Enforcement |
205
+ |---|---|---|
206
+ | Memory | seccomp notif on `mmap`/`munmap`/`brk`/`mremap` | `ENOMEM` when over budget |
207
+ | Processes | seccomp notif on `clone`/`fork`/`vfork` | `EAGAIN` when at limit |
208
+ | CPU | `RLIMIT_CPU` per-process | `SIGXCPU` → `SIGKILL` |
209
+
210
+ ### Network: Domain-Based Access Control
211
+
212
+ Specify allowed hostnames — everything else is blocked:
213
+
214
+ ```python
215
+ Policy(net_allow_hosts=["api.openai.com", "github.com"])
216
+ ```
217
+
218
+ Two cooperating layers: (1) `/etc/hosts` virtualization via seccomp notif
219
+ (programs see clean "host not found" for unlisted domains), and (2) `connect()`/`sendto()`
220
+ IP enforcement (blocks hardcoded IPs, raw DNS over UDP, etc.).
221
+
222
+ ### /proc and /sys Virtualization
223
+
224
+ Seccomp user notification intercepts `open()`/`openat()` for path-based rules.
225
+ Three actions: **ALLOW** (continue), **DENY** (return `-EACCES`), **VIRTUALIZE**
226
+ (inject `memfd` with fake content). Auto-enabled when `-r /proc` is passed.
227
+
228
+ Denies: `/proc/kcore`, `/proc/kallsyms`, `/proc/keys`, `/sys/kernel/`, `/sys/firmware/`.
229
+ Virtualizes: `/proc/self/mounts`, `/proc/self/mountinfo`.
230
+ PID isolation: blocks access to `/proc/<foreign_pid>/`.
231
+
232
+ ### BranchFS: Copy-on-Write Filesystem
233
+
234
+ With `fs_isolation=BRANCHFS`, sandbox writes go to an isolated COW branch
235
+ on a [BranchFS](https://github.com/user/branchfs) FUSE mount. Writes are
236
+ committed on success, discarded on failure. Nested sandboxes create child branches.
237
+
238
+ ```python
239
+ Policy(
240
+ fs_isolation=FsIsolation.BRANCHFS,
241
+ fs_mount="/mnt/workspace",
242
+ on_exit=BranchAction.COMMIT,
243
+ on_error=BranchAction.ABORT,
244
+ )
245
+ ```
246
+
247
+ ### Checkpoint/Restore
248
+
249
+ Two-layer checkpoint without CRIU or root:
250
+
251
+ 1. **OS-level**: ptrace + `/proc` dumps registers, memory, and file descriptors (transparent to child)
252
+ 2. **App-level**: optional `save_fn` callback for application state
253
+
254
+ Combined with BranchFS O(1) snapshots for full checkpoint/restore.
255
+
256
+ ```python
257
+ with Sandbox(policy) as sb:
258
+ sb.exec(["python3", "server.py"], save_fn=lambda: serialize_state())
259
+ cp = sb.checkpoint()
260
+ cp.save("my-env") # persist to disk
261
+
262
+ # Later: restore
263
+ cp = Checkpoint.load("my-env")
264
+ Sandbox.from_checkpoint(cp, restore_fn=lambda state: load_state(state))
265
+ ```
266
+
267
+ ### Privileged Mode
268
+
269
+ `privileged=True` maps UID 0 inside a user namespace. The child appears as
270
+ root but Landlock + seccomp still apply — "root" cannot escape confinement.
271
+
272
+ ## Policy Reference
273
+
274
+ ```python
275
+ @dataclass(frozen=True)
276
+ class Policy:
277
+ # Filesystem (Landlock)
278
+ fs_writable: Sequence[str] = [] # Read/write access
279
+ fs_readable: Sequence[str] = [] # Read-only access
280
+ fs_denied: Sequence[str] = [] # Explicitly denied
281
+
282
+ # Syscall filtering (seccomp)
283
+ deny_syscalls: Sequence[str] | None = None # None = default blocklist
284
+ allow_syscalls: Sequence[str] | None = None # Allowlist mode (stricter)
285
+
286
+ # Network
287
+ net_allow_hosts: Sequence[str] = [] # Domain allowlist (seccomp notif)
288
+ net_bind: Sequence[int | str] = [] # TCP bind ports (Landlock ABI v4+)
289
+ net_connect: Sequence[int | str] = [] # TCP connect ports (Landlock ABI v4+)
290
+
291
+ # IPC scoping (Landlock ABI v6+)
292
+ isolate_ipc: bool = False # Block abstract UNIX sockets to host
293
+ isolate_signals: bool = False # Block signals to host processes
294
+
295
+ # Resources (seccomp notif + rlimit)
296
+ max_memory: str | int | None = None # '512M'
297
+ max_processes: int | None = None # per-sandbox fork count
298
+ max_cpu: str | None = None # '50%' per-process
299
+
300
+ # Environment
301
+ clean_env: bool = False # Minimal env (PATH, HOME, TERM, LANG, USER, SHELL)
302
+ env: Mapping[str, str] = {} # Set/override env vars
303
+
304
+ # BranchFS COW isolation
305
+ fs_isolation: FsIsolation = NONE # NONE | BRANCHFS
306
+ fs_mount: str | None = None # BranchFS mount point
307
+ on_exit: BranchAction = COMMIT # COMMIT | ABORT | KEEP
308
+ on_error: BranchAction = ABORT
309
+
310
+ # Misc
311
+ chroot: str | None = None
312
+ close_fds: bool = True
313
+ strict: bool = True # Abort on confinement failure
314
+ privileged: bool = False # UID 0 inside user namespace
315
+ notif_policy: NotifPolicy | None = None
316
+ ```
317
+
318
+ `strict=False` degrades gracefully when Landlock or seccomp is unavailable.