Debian Codegraph @

README.md

raw symbols SCIP
1 # Landrun <img src="https://avatars.githubusercontent.com/u/21111839?s=48&v=4" align="right"/>
2
3 A lightweight, secure sandbox for running Linux processes using Landlock. Think firejail, but with kernel-level security and minimal overhead.
4
5 Linux Landlock is a kernel-native security module that lets unprivileged processes sandbox themselves.
6
7 Landrun is designed to make it practical to sandbox any command with fine-grained filesystem and network access controls. No root. No containers. No SELinux/AppArmor configs.
8
9 It's lightweight, auditable, and wraps Landlock up to v9 features (file access, TCP restrictions, IPC scoping, and UNIX-socket controls).
10
11 ## Features
12
13 - 🔒 Kernel-level security using Landlock (up to ABI v9)
14 - 🚀 Lightweight and fast execution
15 - 🛡️ Fine-grained access control for directories and files
16 - 🔄 Support for read and write paths
17 - ⚡ Path-specific execution permissions
18 - 🌐 TCP network access control (binding and connecting)
19 - 📡 IPC scoping for abstract UNIX sockets and signals (ABI v6+)
20 - 🔌 Pathname UNIX domain socket connect/sendmsg control (ABI v9+)
21 - 📝 Audit logging configuration for Landlock denials (ABI v7+)
22
23 ## Demo
24
25 <p align="center">
26 <img src="demo.gif" alt="landrun demo" width="700"/>
27 </p>
28
29 ## Requirements
30
31 - Linux kernel 5.13 or later with Landlock enabled
32 - Linux kernel 6.7 or later for network restrictions (TCP bind/connect)
33 - Go 1.24 or later (for building from source)
34
35 > By default landrun targets the highest Landlock ABI (v9). On older kernels, pass `--best-effort` so it gracefully degrades to the best ABI the running kernel supports (see [Best-Effort Mode](#best-effort-mode)).
36
37 ## Installation
38
39 ### Quick Install
40
41 ```bash
42 go install github.com/zouuup/landrun/cmd/landrun@latest
43 ```
44
45 ### From Source
46
47 ```bash
48 git clone https://github.com/zouuup/landrun.git
49 cd landrun
50 go build -o landrun cmd/landrun/main.go
51 sudo cp landrun /usr/local/bin/
52 ```
53
54 ### Distros
55
56 #### Arch (AUR)
57
58 - [stable](https://aur.archlinux.org/packages/landrun) maintained by [Vcalv](https://github.com/vcalv)
59 - [latest commit](https://aur.archlinux.org/packages/landrun-git) maintained by [juxuanu](https://github.com/juxuanu/)
60
61 #### Slackware
62
63 maintained by [r1w1s1](https://github.com/r1w1s1)
64
65 [Slackbuild](https://slackbuilds.org/repository/15.0/network/landrun/?search=landrun)
66 ```bash
67 sudo sbopkg -i packagename
68 ```
69
70 #### Debian/Ubuntu
71
72 Available in Ubuntu since `questing` / `resolute` (26.04 LTS), available in Debian since `forky`.
73
74 ```bash
75 sudo apt install landrun
76 ```
77
78
79 ## Usage
80
81 Basic syntax:
82
83 ```bash
84 landrun [options] <command> [args...]
85 ```
86
87 ### Options
88
89 - `--ro <path>`: Allow read-only access to specified path (can be specified multiple times or as comma-separated values)
90 - `--rox <path>`: Allow read-only access with execution to specified path (can be specified multiple times or as comma-separated values)
91 - `--rw <path>`: Allow read-write access to specified path (can be specified multiple times or as comma-separated values)
92 - `--rwx <path>`: Allow read-write access with execution to specified path (can be specified multiple times or as comma-separated values)
93 - `--unix <path>`: Allow `connect(2)`/`sendmsg(2)` on the specified pathname UNIX domain socket (Landlock ABI v9+; can be specified multiple times or as comma-separated values)
94 - `--bind-tcp <port>`: Allow binding to specified TCP port (can be specified multiple times or as comma-separated values)
95 - `--connect-tcp <port>`: Allow connecting to specified TCP port (can be specified multiple times or as comma-separated values)
96 - `--env <var>`: Environment variable to pass to the sandboxed command (format: KEY=VALUE or just KEY to pass current value)
97 - `--best-effort`: Use best effort mode, falling back to less restrictive sandbox if necessary [default: disabled]
98 - `--log-level <level>`: Set logging level (error, info, debug) [default: "error"]
99 - `--unrestricted-network`: Allows unrestricted network access (disables all network restrictions)
100 - `--unrestricted-filesystem`: Allows unrestricted filesystem access (disables all filesystem restrictions)
101 - `--unrestricted-scoped`: Allows unrestricted IPC scoping, i.e. does not restrict abstract UNIX sockets and signals (Landlock ABI v6+) [default: disabled]
102 - `--ignore-missing`: Gracefully ignore paths that do not exist instead of failing [default: disabled]
103 - `--log-disable-originating`: Disable audit logging of denials from the originating process (Landlock ABI v7+)
104 - `--log-enable-subprocesses`: Enable audit logging of denials after `execve(2)` in subprocesses (Landlock ABI v7+)
105 - `--log-disable-subdomains`: Disable audit logging of denials from nested Landlock domains (Landlock ABI v7+)
106 - `--add-exec`: Automatically adds the executing binary to --rox
107 - `--ldd`: Automatically adds required libraries to --rox
108
109 ### Important Notes
110
111 - You must explicitly add the directory or files to the command you want to run with `--rox` flag
112 - For system commands, you typically need to include `/usr/bin`, `/usr/lib`, and other system directories
113 - Use `--rwx` for directories or files where you need both write access and the ability to execute files
114 - Network restrictions require Linux kernel 6.7 or later with Landlock ABI v4
115 - By default, no environment variables are passed to the sandboxed command. Use `--env` to explicitly pass environment variables
116 - The `--best-effort` flag allows graceful degradation on older kernels that don't support all requested restrictions. Because the default target is Landlock ABI v9, you will usually want `--best-effort` unless you are on a very recent kernel
117 - Paths can be specified either using multiple flags or as comma-separated values (e.g., `--ro /usr,/lib,/home`)
118 - If no paths or network rules are specified and neither unrestricted flag is set, landrun will apply maximum restrictions (denying all access)
119 - By default, IPC scoping is restricted: the sandboxed process cannot connect to abstract UNIX sockets or send signals to processes outside its Landlock domain (ABI v6+). Use `--unrestricted-scoped` if this breaks your workload (e.g. some X11 or D-Bus setups)
120 - On ABI v9+ kernels, connecting to pathname UNIX domain sockets created outside the sandbox (e.g. DNS/NSS via `nscd`, D-Bus, database sockets) is restricted. Grant access to specific sockets with `--unix <path>`
121
122 ### Environment Variables
123
124 - `LANDRUN_LOG_LEVEL`: Set logging level (error, info, debug)
125
126 ### Examples
127
128 1. Run a command that allows exec access to a specific file
129
130 ```bash
131 landrun --rox /usr/bin/ls --rox /usr/lib --ro /home ls /home
132 ```
133
134 2. Run a command with read-only access to a directory:
135
136 ```bash
137 landrun --rox /usr/ --ro /path/to/dir ls /path/to/dir
138 ```
139
140 3. Run a command with write access to a directory:
141
142 ```bash
143 landrun --rox /usr/bin --ro /lib --rw /path/to/dir touch /path/to/dir/newfile
144 ```
145
146 4. Run a command with write access to a file:
147
148 ```bash
149 landrun --rox /usr/bin --ro /lib --rw /path/to/dir/newfile touch /path/to/dir/newfile
150 ```
151
152 5. Run a command with execution permissions:
153
154 ```bash
155 landrun --rox /usr/ --ro /lib,/lib64 /usr/bin/bash
156 ```
157
158 6. Run with debug logging:
159
160 ```bash
161 landrun --log-level debug --rox /usr/ --ro /lib,/lib64,/path/to/dir ls /path/to/dir
162 ```
163
164 7. Run with network restrictions:
165
166 ```bash
167 landrun --rox /usr/ --ro /lib,/lib64 --bind-tcp 8080 --connect-tcp 80 /usr/bin/my-server
168 ```
169
170 This will allow the program to only bind to TCP port 8080 and connect to TCP port 80.
171
172 8. Run a DNS client with appropriate permissions:
173
174 ```bash
175 landrun --log-level debug --ro /etc,/usr --rox /usr/ --connect-tcp 443 nc kernel.org 443
176 ```
177
178 This allows connections to port 443, requires access to /etc/resolv.conf for resolving DNS.
179
180 9. Run a web server with selective network permissions:
181
182 ```bash
183 landrun --rox /usr/bin --ro /lib,/lib64,/var/www --rwx /var/log --bind-tcp 80,443 /usr/bin/nginx
184 ```
185
186 10. Running anything without providing parameters is... maximum security jail!
187
188 ```bash
189 landrun ls
190 ```
191
192 11. If you keep getting permission denied without knowing what exactly going on, best to use strace with it.
193
194 ```bash
195 landrun --rox /usr strace -f -e trace=all ls
196 ```
197
198 12. Run with specific environment variables:
199
200 ```bash
201 landrun --rox /usr --ro /etc --env HOME --env PATH --env CUSTOM_VAR=my_value -- env
202 ```
203
204 This example passes the current HOME and PATH variables, plus a custom variable named CUSTOM_VAR.
205
206 13. Run command with explicity access to files instead of directories:
207 ```bash
208 landrun --rox /usr/lib/libc.so.6 --rox /usr/lib64/ld-linux-x86-64.so.2 --rox /usr/bin/true /usr/bin/true
209 ```
210
211 14. Run a command with --add-exec which automatically adds target binary to --rox
212
213 ```bash
214 landrun --rox /usr/lib/ --add-exec /usr/bin/true
215 ```
216
217 15. Run a command with --ldd and --add-exec which automatically adds required libraries and target binary to --rox
218
219 ```bash
220 landrun --ldd --add-exec /usr/bin/true
221 ```
222
223 Note that shared libs always need exec permission due to how they are loaded, PROT_EXEC on mmap() etc.
224
225 16. Allow connecting to a pathname UNIX domain socket (ABI v9+), e.g. a database socket:
226
227 ```bash
228 landrun --best-effort --rox /usr --ro /etc --unix /run/postgresql/.s.PGSQL.5432 -- psql ...
229 ```
230
231 17. Gracefully ignore optional paths that may not exist:
232
233 ```bash
234 landrun --best-effort --ignore-missing --rox /usr --ro /etc,/opt/optional-config -- myapp
235 ```
236
237 18. Allow abstract UNIX sockets / signals to reach outside the sandbox (relax IPC scoping):
238
239 ```bash
240 landrun --best-effort --unrestricted-scoped --rox /usr --ro /etc -- some-gui-app
241 ```
242
243 ## Systemd Integration
244
245 landrun can be integrated with systemd to run services with enhanced security. Here's an example of running nginx with landrun:
246
247 1. Create a systemd service file (e.g., `/etc/systemd/system/nginx-landrun.service`):
248
249 ```ini
250 [Unit]
251 Description=nginx with landrun sandbox
252 After=network.target
253
254 [Service]
255 Type=simple
256 ExecStart=/usr/bin/landrun \
257 --best-effort \
258 --rox /usr/bin,/usr/lib \
259 --ro /etc/nginx,/etc/ssl,/etc/passwd,/etc/group,/etc/nsswitch.conf \
260 --rwx /var/log/nginx \
261 --rwx /var/cache/nginx \
262 --bind-tcp 80,443 \
263 /usr/bin/nginx -g 'daemon off;'
264 Restart=always
265 User=nginx
266 Group=nginx
267
268 [Install]
269 WantedBy=multi-user.target
270 ```
271
272 2. Enable and start the service:
273
274 ```bash
275 sudo systemctl daemon-reload
276 sudo systemctl enable nginx-landrun
277 sudo systemctl start nginx-landrun
278 ```
279
280 3. Check the service status:
281
282 ```bash
283 sudo systemctl status nginx-landrun
284 ```
285
286 This configuration:
287 - Runs nginx with minimal required permissions
288 - Allows binding to ports 80 and 443
289 - Provides read-only access to configuration files
290 - Allows write access only to log and cache directories
291 - Runs as the nginx user and group
292 - Automatically restarts on failure
293
294 You can adjust the permissions based on your specific needs. For example, if you need to serve static files from `/var/www`, add `--ro /var/www` to the ExecStart line.
295
296 ## Security
297
298 landrun uses Linux's Landlock to create a secure sandbox environment. It provides:
299
300 - File system access control
301 - Directory access restrictions
302 - Execution control
303 - TCP network restrictions
304 - Process isolation
305 - Default restrictive mode when no rules are specified
306
307 Landlock is an access-control system that enables processes to securely restrict themselves and their future children. As a stackable Linux Security Module (LSM), it creates additional security layers on top of existing system-wide access controls, helping to mitigate security impacts from bugs or malicious behavior in applications.
308
309 ### Landlock Access Control Rights
310
311 landrun leverages Landlock's fine-grained access control mechanisms, which include:
312
313 **File-specific rights:**
314
315 - Execute files (`LANDLOCK_ACCESS_FS_EXECUTE`)
316 - Write to files (`LANDLOCK_ACCESS_FS_WRITE_FILE`)
317 - Read files (`LANDLOCK_ACCESS_FS_READ_FILE`)
318 - Truncate files (`LANDLOCK_ACCESS_FS_TRUNCATE`) - Available since Landlock ABI v3
319 - IOCTL operations on devices (`LANDLOCK_ACCESS_FS_IOCTL_DEV`) - Available since Landlock ABI v5
320 - Connect/sendmsg on pathname UNIX sockets (`LANDLOCK_ACCESS_FS_RESOLVE_UNIX`) - Available since Landlock ABI v9 (granted per-path with `--unix`)
321
322 **Directory-specific rights:**
323
324 - Read directory contents (`LANDLOCK_ACCESS_FS_READ_DIR`)
325 - Remove directories (`LANDLOCK_ACCESS_FS_REMOVE_DIR`)
326 - Remove files (`LANDLOCK_ACCESS_FS_REMOVE_FILE`)
327 - Create various filesystem objects (char devices, directories, regular files, sockets, etc.)
328 - Refer/reparent files across directories (`LANDLOCK_ACCESS_FS_REFER`) - Available since Landlock ABI v2
329
330 **Network-specific rights** (requires Linux 6.7+ with Landlock ABI v4):
331
332 - Bind to specific TCP ports (`LANDLOCK_ACCESS_NET_BIND_TCP`)
333 - Connect to specific TCP ports (`LANDLOCK_ACCESS_NET_CONNECT_TCP`)
334
335 **IPC scoping** (requires Linux 6.12+ with Landlock ABI v6):
336
337 - Restrict connections to abstract UNIX sockets outside the domain (`LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET`)
338 - Restrict sending signals to processes outside the domain (`LANDLOCK_SCOPE_SIGNAL`)
339
340 These are restricted by default and can be relaxed with `--unrestricted-scoped`.
341
342 ### Limitations
343
344 - Landlock must be supported by your kernel
345 - Network restrictions require Linux kernel 6.7 or later with Landlock ABI v4
346 - TCP restrictions only apply to "classic" TCP sockets, not Multipath TCP. Since Go 1.24, `net.Listen` defaults to Multipath TCP and therefore cannot currently be restricted by Landlock (kernel bug [landlock-lsm/linux#54](https://github.com/landlock-lsm/linux/issues/54))
347 - Some operations may require additional permissions
348 - Files or directories opened before sandboxing are not subject to Landlock restrictions
349
350 ## Kernel Compatibility Table
351
352 | Feature | Minimum Kernel Version | Landlock ABI Version |
353 | --------------------------------------------- | ---------------------- | -------------------- |
354 | Basic filesystem sandboxing | 5.13 | 1 |
355 | File referring/reparenting control | 5.19 | 2 |
356 | File truncation control | 6.2 | 3 |
357 | Network TCP restrictions | 6.7 | 4 |
358 | IOCTL on special files | 6.10 | 5 |
359 | IPC scoping (abstract UNIX sockets, signals) | 6.12 | 6 |
360 | Audit logging of denials | 6.15 | 7 |
361 | Thread synchronization (TSYNC) | 7.0 | 8 |
362 | Pathname UNIX socket connect/sendmsg control | latest | 9 |
363
364 ## Troubleshooting
365
366 If you receive "permission denied" or similar errors:
367
368 1. Ensure you've added all necessary paths with `--ro` or `--rw`
369 2. Try running with `--log-level debug` to see detailed permission information
370 3. Check that Landlock is supported and enabled on your system:
371 ```bash
372 grep -E 'landlock|lsm=' /boot/config-$(uname -r)
373 # alternatively, if there are no /boot/config-* files
374 zgrep -iE 'landlock|lsm=' /proc/config.gz
375 # another alternate method
376 grep -iE 'landlock|lsm=' /lib/modules/$(uname -r)/config
377 ```
378 You should see `CONFIG_SECURITY_LANDLOCK=y` and `lsm=landlock,...` in the output
379 4. For network restrictions, verify your kernel version is 6.7+ with Landlock ABI v4:
380 ```bash
381 uname -r
382 ```
383
384 ## Technical Details
385
386 ### Implementation
387
388 This project uses the [landlock-lsm/go-landlock](https://github.com/landlock-lsm/go-landlock) package (v0.9.0) for sandboxing, which provides filesystem, network and IPC-scope restrictions. The current implementation targets Landlock ABI v9 and supports:
389
390 - Read/write/execute restrictions for files and directories
391 - TCP port binding restrictions
392 - TCP port connection restrictions
393 - IPC scoping (abstract UNIX sockets and signals)
394 - Pathname UNIX domain socket connect/sendmsg control (`--unix`)
395 - Audit logging configuration for Landlock denials (`--log-*`)
396 - Graceful handling of missing paths (`--ignore-missing`)
397 - Best-effort mode for graceful degradation on older kernels
398
399 ### Best-Effort Mode
400
401 By default, landrun targets the highest Landlock ABI (v9) in strict mode, so on any kernel that does not support v9 it will fail unless `--best-effort` is used.
402
403 When using `--best-effort` (disabled by default), landrun will gracefully degrade to using the best available Landlock version on the current kernel. This means:
404
405 - On Linux 7.0+: All of the below, plus thread synchronization (TSYNC) and, on ABI v9 kernels, pathname UNIX socket controls
406 - On Linux 6.15+: Adds audit logging configuration (ABI v7)
407 - On Linux 6.12+: Adds IPC scoping for abstract UNIX sockets and signals (ABI v6)
408 - On Linux 6.10+: Filesystem, network and IOCTL restrictions (ABI v5)
409 - On Linux 6.7+: Full filesystem and network restrictions (ABI v4)
410 - On Linux 6.2-6.6: Filesystem restrictions including truncation, but no network restrictions
411 - On Linux 5.19-6.1: Basic filesystem restrictions including file reparenting, but no truncation control or network restrictions
412 - On Linux 5.13-5.18: Basic filesystem restrictions without file reparenting, truncation control, or network restrictions
413 - On older Linux: No restrictions (sandbox disabled)
414
415 When no rules are specified and neither unrestricted flag is set, landrun will apply maximum restrictions available for the current kernel version.
416
417 ### Tests
418
419 The project includes a comprehensive test suite that verifies:
420
421 - Basic filesystem access controls (read-only, read-write, execute)
422 - Directory traversal and path handling
423 - Network restrictions (TCP bind/connect)
424 - Environment variable isolation
425 - System command execution
426 - Edge cases and regression tests
427
428 Run the tests with:
429
430 ```bash
431 ./test.sh
432 ```
433
434 Use `--keep-binary` to preserve the test binary after completion:
435
436 ```bash
437 ./test.sh --keep-binary
438 ```
439
440 Use `--use-system` to test against the system-installed landrun binary:
441
442 ```bash
443 ./test.sh --use-system
444 ```
445
446 ## Future Features
447
448 Based on the Linux Landlock API capabilities, we plan to add:
449
450 - 🔒 Enhanced filesystem controls with more fine-grained permissions
451 - 🌐 Support for UDP and other network protocol restrictions (when supported by Linux kernel)
452 - 🛡️ Additional security features as they become available in the Landlock API
453
454 ## Acknowledgements
455
456 This project wouldn't exist without:
457
458 - [Landlock](https://landlock.io), the kernel security module enabling unprivileged sandboxing - maintained by [@l0kod](https://github.com/l0kod)
459 - [go-landlock](https://github.com/landlock-lsm/go-landlock), the Go bindings powering this tool - developed by [@gnoack](https://github.com/gnoack)
460
461 ## Contributing
462
463 Contributions are welcome! Please feel free to submit a Pull Request.