Debian Codegraph @

landlock/landlock.go

raw
1 // Package landlock enforces sandboxing policies using Linux's Landlock LSM.
2 //
3 // # Restricting file access
4 //
5 // The following invocation will restrict all goroutines so that they
6 // can only read from /usr, /bin and /tmp, and only write to /tmp:
7 //
8 // err := landlock.V9.BestEffort().RestrictPaths(
9 // landlock.RODirs("/usr", "/bin"),
10 // landlock.RWDirs("/tmp"),
11 // )
12 //
13 // This will restrict file access using Landlock V9, if available. If
14 // unavailable, it will attempt using earlier Landlock versions than
15 // the one requested. If no Landlock version is available, it will
16 // still succeed, without restricting file accesses.
17 //
18 // # Restricting networking
19 //
20 // The following invocation will restrict all goroutines so that they
21 // can only bind to TCP port 8080 and only connect to TCP port 53:
22 //
23 // err := landlock.V9.BestEffort().RestrictNet(
24 // landlock.BindTCP(8080),
25 // landlock.ConnectTCP(53),
26 // )
27 //
28 // This functionality is available since Landlock V4.
29 //
30 // **IMPORTANT:** Landlock's TCP restrictions only apply to "classic"
31 // TCP sockets, not to Multipath TCP sockets, which can also serve
32 // non-multipath clients. Since Go 1.24, Multipath TCP is the default
33 // for [net.Listen], and therefore, [net.Listen] can not be restricted
34 // with Landlock.
35 //
36 // The bug needs to be fixed in the Linux kernel and is tracked here:
37 // https://github.com/landlock-lsm/linux/issues/54
38 //
39 // # Restricting IPC scopes
40 //
41 // The following invocation will restrict IPC to more privileged
42 // Landlock domains, if possible:
43 //
44 // err := landlock.V9.BestEffort().RestrictScoped()
45 //
46 // This functionality is available since Landlock V6.
47 //
48 // # Restricting all types of operations at once
49 //
50 // The following invocation restricts all types of operations at once.
51 // The effect is the same as calling [Config.RestrictPaths],
52 // [Config.RestrictNet] and [Config.RestrictScoped] one after another,
53 // but it happens in one step.
54 //
55 // err := landlock.V9.BestEffort().Restrict(
56 // landlock.RODirs("/usr", "/bin"),
57 // landlock.RWDirs("/tmp"),
58 // landlock.BindTCP(8080),
59 // landlock.ConnectTCP(53),
60 // )
61 //
62 // # More possible invocations
63 //
64 // landlock.V9.RestrictPaths(...) (without the call to
65 // [Config.BestEffort]) enforces the given rules using the
66 // capabilities of Landlock V9, but returns an error if that
67 // functionality is not available on the system that the program is
68 // running on.
69 //
70 // If Audit logging is enabled in the Linux kernel, kernels with
71 // Landlock ABI V7 and higher will log Landlock denials to the audit
72 // log. By default, this only happens for denials affecting the same
73 // process which enforced the policy, and it also happens for nested
74 // Landlock policies.
75 //
76 // The situations in which audit logging happens can be configured
77 // using [Config.DisableLoggingForOriginatingProcess],
78 // [Config.EnableLoggingForSubprocesses] and
79 // [Config.DisableLoggingForSubdomains].
80 //
81 // # Landlock ABI versioning
82 //
83 // The Landlock ABI is versioned, so that callers can probe for the
84 // availability of different Landlock features.
85 //
86 // When using the Go-Landlock package, callers need to identify at
87 // which ABI level they want to use Landlock and call one of the
88 // restriction methods (e.g., [Config.RestrictPaths]) on the
89 // corresponding ABI constant.
90 //
91 // When new Landlock versions become available in landlock, users will
92 // manually need to upgrade their usages to higher Landlock versions,
93 // as there is a risk that new Landlock versions will break operations
94 // that their programs rely on.
95 //
96 // The documentation for [landlock.V9] and the adjacent version
97 // numbers explains what you need to look for when upgrading between
98 // Landlock ABI versions.
99 //
100 // # Graceful degradation on older kernels
101 //
102 // Programs that get run on different kernel versions will want to use
103 // the [Config.BestEffort] method to gracefully degrade to using the
104 // best available Landlock version on the current kernel.
105 //
106 // In this case, the Go-Landlock library will enforce as much as
107 // possible, but it will ensure that all the requested access rights
108 // are permitted after Landlock enforcement.
109 //
110 // # Current limitations (Filesystem)
111 //
112 // Landlock can not currently restrict all filesystem operations.
113 // The operations that can and can not be restricted yet are listed in
114 // the [Kernel Documentation about Access Rights].
115 //
116 // Enabling Landlock implicitly turns off the following filesystem
117 // features:
118 //
119 // - File reparenting: renaming or linking a file to a different parent directory is denied,
120 // unless it is explicitly enabled on both directories with the "Refer" access modifier,
121 // and the new target directory does not grant the file additional rights through its
122 // Landlock access rules.
123 // - Filesystem topology modification: arbitrary mounts are always denied.
124 //
125 // These are Landlock limitations that will be resolved in future
126 // versions. See the [Kernel Documentation about Current Limitations]
127 // for more details.
128 //
129 // # Multithreading Limitations
130 //
131 // This warning only applies to programs using cgo and linking C
132 // libraries that start OS threads through means other than
133 // pthread_create() before landlock is called:
134 //
135 // Before Landlock ABI V8, when using cgo, the landlock package relies
136 // on libpsx in order to apply the rules across all OS threads,
137 // (rather than just the ones managed by the Go runtime). psx
138 // achieves this by wrapping the C-level pthread_create() API which is
139 // very commonly used on UNIX to start threads. However, C libraries
140 // calling clone(2) through other means before landlock is called
141 // might still create threads that won't have Landlock protections.
142 //
143 // [Kernel Documentation about Access Rights]: https://www.kernel.org/doc/html/latest/userspace-api/landlock.html#access-rights
144 // [Kernel Documentation about Current Limitations]: https://www.kernel.org/doc/html/latest/userspace-api/landlock.html#current-limitations
145 package landlock