| 1 |
# Applying Landlock to the entire process |
| 2 |
|
| 3 |
Before Landlock ABI v8, the Landlock kernel API enabled Landlock only |
| 4 |
for the calling OS thread. This is a problem in Go, because (a) Go is |
| 5 |
inherently multithreaded, and (b) the mapping between (many) |
| 6 |
goroutines and (fewer) OS threads changes dynamically. |
| 7 |
|
| 8 |
Because the mapping between OS threads and Goroutines is not |
| 9 |
guaranteed anyway, and because there is no good security boundary |
| 10 |
between OS threads, the Go-Landlock API **always enables the given |
| 11 |
Landlock policy for the entire process**. |
| 12 |
|
| 13 |
Depending on the kernel ABI available on the running kernel, |
| 14 |
Go-Landlock uses one of two possible approaches for process-wide |
| 15 |
policy enforcement: |
| 16 |
|
| 17 |
## `LANDLOCK_RESTRICT_SELF_TSYNC` (ABI V8+) |
| 18 |
|
| 19 |
This flag for |
| 20 |
[*landlock_restrict_self*(2)](https://man.gnoack.org/2/landlock_restrict_self) |
| 21 |
was introduced in Landlock ABI V8 (Linux 7.0). |
| 22 |
|
| 23 |
## `libpsx` (ABI < V8) |
| 24 |
|
| 25 |
On Linux kernels prior to Landlock ABI V8 (Linux 7.0), Go-Landlock |
| 26 |
uses the `psx` library. `psx` exposes an API that does a system call |
| 27 |
with the given arguments on *every OS thread* in a running Go program. |
| 28 |
|
| 29 |
For pure Go programs, `psx` does the same as the |
| 30 |
[`syscall.AllThreadsSyscall` |
| 31 |
function](https://pkg.go.dev/syscall#AllThreadsSyscall) in the Go |
| 32 |
runtime (and that case is straightforward to understand). |
| 33 |
|
| 34 |
For programs linked with `cgo`, there can be more OS threads than just |
| 35 |
the ones that were started by the Go runtime. To cover these, `psx` |
| 36 |
intercepts calls to the pthread library to infer the list of all |
| 37 |
threads, and then uses a trick with Unix signals to execute the given |
| 38 |
system call on all of them. (This is unfortunately common practice for |
| 39 |
system calls that only apply to the current thread -- glibc uses the |
| 40 |
same approach for some system calls. To dig this up in the glibc |
| 41 |
source, see `sysdeps/nptl/setxid.h` and its users.) |
| 42 |
|
| 43 |
A deeper discussion of `psx` can be found at: |
| 44 |
https://sites.google.com/site/fullycapable/who-ordered-libpsx |
| 45 |
|
| 46 |
> [!TIP] |
| 47 |
> By using the `landlocktsync` build constraint, Go-Landlock will only |
| 48 |
> support Landlock ABI V8 and higher, effectively removing the |
| 49 |
> dependency on `libpsx`. |