| 1 |
package landlock |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
ll "github.com/landlock-lsm/go-landlock/landlock/syscall" |
| 8 |
) |
| 9 |
|
| 10 |
// Access permission sets for filesystem access. |
| 11 |
const ( |
| 12 |
// The set of access rights that only apply to files. |
| 13 |
accessFile AccessFSSet = ll.AccessFSExecute | ll.AccessFSWriteFile | ll.AccessFSTruncate | ll.AccessFSReadFile |
| 14 |
|
| 15 |
// The set of access rights associated with read access to files and directories. |
| 16 |
accessFSRead AccessFSSet = ll.AccessFSExecute | ll.AccessFSReadFile | ll.AccessFSReadDir |
| 17 |
|
| 18 |
// The set of access rights associated with write access to files and directories. |
| 19 |
accessFSWrite AccessFSSet = ll.AccessFSWriteFile | ll.AccessFSRemoveDir | ll.AccessFSRemoveFile | ll.AccessFSMakeChar | ll.AccessFSMakeDir | ll.AccessFSMakeReg | ll.AccessFSMakeSock | ll.AccessFSMakeFifo | ll.AccessFSMakeBlock | ll.AccessFSMakeSym | ll.AccessFSTruncate |
| 20 |
|
| 21 |
// The set of access rights associated with read and write access to files and directories. |
| 22 |
accessFSReadWrite AccessFSSet = accessFSRead | accessFSWrite |
| 23 |
) |
| 24 |
|
| 25 |
// These are Landlock configurations for the currently supported |
| 26 |
// Landlock ABI versions, configured to restrict the highest possible |
| 27 |
// set of operations possible for each version. |
| 28 |
// |
| 29 |
// The higher the ABI version, the more operations Landlock will be |
| 30 |
// able to restrict. |
| 31 |
// |
| 32 |
// # Upgrading to V2 |
| 33 |
// |
| 34 |
// Upgrading from V1 to V2 should not break existing programs. |
| 35 |
// Programs that need it can now move and link files between |
| 36 |
// directories with the "refer" access right. |
| 37 |
// |
| 38 |
// The [RWFiles] and [RWDirs] helpers do not grant the "refer" right |
| 39 |
// automatically, but you can ask for the access right explicitly |
| 40 |
// using [FSRule.WithRefer]. |
| 41 |
// |
| 42 |
// # Upgrading to V3 |
| 43 |
// |
| 44 |
// Upgrading from V2 to V3 should not break existing programs, |
| 45 |
// as long as they are using [RWPaths] and [RWDirs] to express |
| 46 |
// access rights. |
| 47 |
// |
| 48 |
// Programs that spell out individual access rights might need |
| 49 |
// to add the "truncate" access right to the required access |
| 50 |
// rights. Note that the truncation right is often required |
| 51 |
// for opening files for writing, because that often does an |
| 52 |
// implicit truncation for existing files. |
| 53 |
// |
| 54 |
// # Upgrading to V4 |
| 55 |
// |
| 56 |
// When upgrading from V3 to V4, the TCP connect() and bind() |
| 57 |
// operations (required for [net.Dial] and [net.Listen]) will be |
| 58 |
// restricted when using [Config.Restrict] or [Config.RestrictNet]. |
| 59 |
// |
| 60 |
// Note: This only affects "classic" TCP, not Multipath TCP. |
| 61 |
// Multipath TCP, which is the default for [net.Listen] since |
| 62 |
// Go 1.24, continues to work. |
| 63 |
// |
| 64 |
// For comprehensive network sandboxing at this ABI level, we |
| 65 |
// recommend using additional sandboxing mechanisms. |
| 66 |
// |
| 67 |
// # Upgrading to V5 |
| 68 |
// |
| 69 |
// When upgrading from V4 to V5, if you use [Config.Restrict] or |
| 70 |
// [Config.RestrictPaths], IOCTL operations on device files are now |
| 71 |
// restricted. A small list of common IOCTLs continues to be |
| 72 |
// permitted and is listed in the [Kernel Documentation about Access Rights]. |
| 73 |
// |
| 74 |
// The [RWFiles] and [RWDirs] helpers do not grant IOCTL rights |
| 75 |
// automatically, but you can ask for the access right explicitly |
| 76 |
// using [FSRule.WithIoctlDev]. |
| 77 |
// |
| 78 |
// # Upgrading to V6 |
| 79 |
// |
| 80 |
// When upgrading from V5 to V6, the following operations are newly |
| 81 |
// restricted if you are using [Config.Restrict] or |
| 82 |
// [Config.RestrictScoped]: |
| 83 |
// |
| 84 |
// - Abstract UNIX Domain Socket connections that are reaching out to |
| 85 |
// a server outside of the enforced Landlock domain. |
| 86 |
// - UNIX signals that are signaling a program which is running outside |
| 87 |
// of the enforced Landlock domain. |
| 88 |
// |
| 89 |
// # Upgrading to V7 |
| 90 |
// |
| 91 |
// Upgrading from V6 to V7 is safe. |
| 92 |
// |
| 93 |
// With ABI V7, the following methods can be newly used to influence |
| 94 |
// audit logging of Landlock denials: |
| 95 |
// |
| 96 |
// - [Config.DisableLoggingForOriginatingProcess] |
| 97 |
// - [Config.EnableLoggingForSubprocesses] |
| 98 |
// - [Config.DisableLoggingForSubdomains] |
| 99 |
// |
| 100 |
// It is safe to use these in combination with [Config.BestEffort], |
| 101 |
// also on Linux systems that only support older Landlock ABIs. |
| 102 |
// |
| 103 |
// When one of these logging flags is set but [Config.BestEffort] is |
| 104 |
// omitted, you are asserting that you are running on a kernel that |
| 105 |
// supports ABI V7+, and you will get an error at restriction time if |
| 106 |
// the kernel does not support that. |
| 107 |
// |
| 108 |
// # Upgrading to V8 |
| 109 |
// |
| 110 |
// Upgrading from V7 to V8 is safe. |
| 111 |
// |
| 112 |
// ABI V8 adds an under-the-hood improvement for multithreaded |
| 113 |
// Landlock enforcement, which is used by Go-Landlock whenever it is |
| 114 |
// available. The [landlock.V8] configuration preset restricts the |
| 115 |
// same operations as [landlock.V7]. |
| 116 |
// |
| 117 |
// # Upgrading to V9 |
| 118 |
// |
| 119 |
// When upgrading from V8 to V9, if you use [Config.Restrict] or |
| 120 |
// [Config.RestrictPaths], connect(2) and sendmsg(2) calls on pathname |
| 121 |
// UNIX domain sockets (see unix(7)) are now restricted. This only |
| 122 |
// affects connections to UNIX server sockets that were created |
| 123 |
// outside of the enforced Landlock domain. Newly created UNIX |
| 124 |
// servers within the same Landlock domain continue to be accessible. |
| 125 |
// |
| 126 |
// The [RWFiles] and [RWDirs] helpers do not grant the "resolve unix" |
| 127 |
// access right automatically, but you can ask for the access right |
| 128 |
// explicitly using [FSRule.WithResolveUnix]. |
| 129 |
// |
| 130 |
// [Kernel Documentation about Access Rights]: https://www.kernel.org/doc/html/latest/userspace-api/landlock.html#access-rights |
| 131 |
var ( |
| 132 |
// Landlock V1 support (basic file operations). |
| 133 |
V1 = abiInfos[1].asConfig() |
| 134 |
// Landlock V2 support (V1 + file reparenting between different directories) |
| 135 |
V2 = abiInfos[2].asConfig() |
| 136 |
// Landlock V3 support (V2 + file truncation) |
| 137 |
V3 = abiInfos[3].asConfig() |
| 138 |
// Landlock V4 support (V3 + networking) |
| 139 |
V4 = abiInfos[4].asConfig() |
| 140 |
// Landlock V5 support (V4 + ioctl on device files) |
| 141 |
V5 = abiInfos[5].asConfig() |
| 142 |
// Landlock V6 support (V5 + IPC scopes for signals and |
| 143 |
// Abstract UNIX Domain Sockets (see unix(7))) |
| 144 |
V6 = abiInfos[6].asConfig() |
| 145 |
// Landlock V7 support (V6 + logging support) |
| 146 |
V7 = abiInfos[7].asConfig() |
| 147 |
// Landlock V8 support (V7 + thread synchronization) |
| 148 |
V8 = abiInfos[8].asConfig() |
| 149 |
// Landlock V9 support (V8 + restricting connect(2) and sendmsg(2) |
| 150 |
// on pathname UNIX domain sockets) |
| 151 |
V9 = abiInfos[9].asConfig() |
| 152 |
) |
| 153 |
|
| 154 |
// v0 denotes "no Landlock support". Only used internally. |
| 155 |
var v0 = Config{} |
| 156 |
|
| 157 |
// The Landlock configuration describes the desired set of |
| 158 |
// landlockable operations to be restricted and the constraints on it |
| 159 |
// (e.g., best effort mode). |
| 160 |
// |
| 161 |
// It is recommended to use one of the preset configurations such as |
| 162 |
// [landlock.V9], which restrict the full set of access rights |
| 163 |
// available at this Landlock ABI version. |
| 164 |
type Config struct { |
| 165 |
handledAccessFS AccessFSSet |
| 166 |
handledAccessNet AccessNetSet |
| 167 |
scoped ScopedSet |
| 168 |
flags restrictFlagsSet |
| 169 |
bestEffort bool |
| 170 |
} |
| 171 |
|
| 172 |
// NewConfig creates a new Landlock configuration with the given parameters. |
| 173 |
// |
| 174 |
// Passing an AccessFSSet, AccessNetSet or ScopedSet will set these as |
| 175 |
// the set of filesystem/network/scoped operations to restrict when |
| 176 |
// enabling Landlock. The sets need to stay within the bounds of what |
| 177 |
// Go-Landlock supports. (If you are getting an error, you might need |
| 178 |
// to upgrade to a newer version of Go-Landlock.) |
| 179 |
// |
| 180 |
// Example: |
| 181 |
// |
| 182 |
// cfg, err := NewConfig(AccessFSSet(llsyscall.AccessFSExecute)) |
| 183 |
func NewConfig(args ...any) (*Config, error) { |
| 184 |
// Implementation note: This factory is written with future |
| 185 |
// extensibility in mind. Only specific types are supported as |
| 186 |
// input, but in the future more might be added. |
| 187 |
// |
| 188 |
// This constructor ensures that callers can't construct |
| 189 |
// invalid Config values. |
| 190 |
var c Config |
| 191 |
for _, arg := range args { |
| 192 |
switch arg := arg.(type) { |
| 193 |
case AccessFSSet: |
| 194 |
if !c.handledAccessFS.isEmpty() { |
| 195 |
return nil, errors.New("only one AccessFSSet may be provided") |
| 196 |
} |
| 197 |
if !arg.valid() { |
| 198 |
return nil, errors.New("unsupported AccessFSSet value; upgrade go-landlock?") |
| 199 |
} |
| 200 |
c.handledAccessFS = arg |
| 201 |
case AccessNetSet: |
| 202 |
if !c.handledAccessNet.isEmpty() { |
| 203 |
return nil, errors.New("only one AccessNetSet may be provided") |
| 204 |
} |
| 205 |
if !arg.valid() { |
| 206 |
return nil, errors.New("unsupported AccessNetSet value; upgrade go-landlock?") |
| 207 |
} |
| 208 |
c.handledAccessNet = arg |
| 209 |
case ScopedSet: |
| 210 |
if !c.scoped.isEmpty() { |
| 211 |
return nil, errors.New("only one ScopedSet may be provided") |
| 212 |
} |
| 213 |
if !arg.valid() { |
| 214 |
return nil, errors.New("unsupported ScopedSet value; upgrade go-landlock?") |
| 215 |
} |
| 216 |
c.scoped = arg |
| 217 |
default: |
| 218 |
return nil, fmt.Errorf("unknown argument %v; only AccessFSSet-type argument is supported", arg) |
| 219 |
} |
| 220 |
} |
| 221 |
return &c, nil |
| 222 |
} |
| 223 |
|
| 224 |
// MustConfig is like NewConfig but panics on error. |
| 225 |
func MustConfig(args ...any) Config { |
| 226 |
c, err := NewConfig(args...) |
| 227 |
if err != nil { |
| 228 |
panic(err) |
| 229 |
} |
| 230 |
return *c |
| 231 |
} |
| 232 |
|
| 233 |
// String builds a human-readable representation of the Config. |
| 234 |
func (c Config) String() string { |
| 235 |
abi := abiInfo{version: -1} // invalid |
| 236 |
for i := len(abiInfos) - 1; i >= 0; i-- { |
| 237 |
a := abiInfos[i] |
| 238 |
if c.compatibleWithABI(a) { |
| 239 |
abi = a |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
fsDesc := c.handledAccessFS.String() |
| 244 |
if abi.supportedAccessFS == c.handledAccessFS && c.handledAccessFS != 0 { |
| 245 |
fsDesc = "all" |
| 246 |
} |
| 247 |
|
| 248 |
netDesc := c.handledAccessNet.String() |
| 249 |
if abi.supportedAccessNet == c.handledAccessNet && c.handledAccessNet != 0 { |
| 250 |
netDesc = "all" |
| 251 |
} |
| 252 |
|
| 253 |
scopedDesc := c.scoped.String() |
| 254 |
if abi.supportedScoped == c.scoped && c.scoped != 0 { |
| 255 |
scopedDesc = "all" |
| 256 |
} |
| 257 |
|
| 258 |
extra := "" |
| 259 |
if c.flags != 0 { |
| 260 |
extra += fmt.Sprintf(" (flags: %s)", c.flags.String()) |
| 261 |
} |
| 262 |
if c.bestEffort { |
| 263 |
extra += " (best effort)" |
| 264 |
} |
| 265 |
|
| 266 |
var version string |
| 267 |
if abi.version < 0 { |
| 268 |
version = "V???" |
| 269 |
} else { |
| 270 |
version = fmt.Sprintf("V%v", abi.version) |
| 271 |
} |
| 272 |
|
| 273 |
return fmt.Sprintf("{Landlock %v; FS: %v; Net: %v; Scoped: %v%v}", version, fsDesc, netDesc, scopedDesc, extra) |
| 274 |
} |
| 275 |
|
| 276 |
// BestEffort returns a config that will opportunistically enforce |
| 277 |
// the strongest rules it can, up to the given ABI version, working |
| 278 |
// with the level of Landlock support available in the running kernel. |
| 279 |
// |
| 280 |
// Warning: A best-effort call to RestrictPaths() will succeed without |
| 281 |
// error even when Landlock is not available at all on the current kernel. |
| 282 |
func (c Config) BestEffort() Config { |
| 283 |
cfg := c |
| 284 |
cfg.bestEffort = true |
| 285 |
return cfg |
| 286 |
} |
| 287 |
|
| 288 |
// DisableLoggingForOriginatingProcess disables logging of denied |
| 289 |
// accesses originating from the thread creating the Landlock domain, |
| 290 |
// as well as its children, as long as they continue running the same |
| 291 |
// executable code (i.e., without an intervening execve(2) call). |
| 292 |
// |
| 293 |
// This is intended for programs that execute unknown code without |
| 294 |
// invoking execve(2), such as script interpreters. Programs that |
| 295 |
// only sandbox themselves should not set this flag, so users can be |
| 296 |
// notified of unauthorized access attempts via system logs. |
| 297 |
// |
| 298 |
// Requires a Linux kernel that supports Landlock ABI V7 or higher. |
| 299 |
// In combination with [Config.BestEffort], the logging option will be |
| 300 |
// omitted on older kernels and not result in an error. |
| 301 |
func (c Config) DisableLoggingForOriginatingProcess() Config { |
| 302 |
cfg := c |
| 303 |
cfg.flags |= ll.FlagRestrictSelfLogSameExecOff |
| 304 |
return cfg |
| 305 |
} |
| 306 |
|
| 307 |
// EnableLoggingForSubprocesses enables logging of denied accesses |
| 308 |
// after an execve(2) call, providing visibility into unauthorized |
| 309 |
// access attempts by newly executed programs within the created |
| 310 |
// Landlock domain. |
| 311 |
// |
| 312 |
// This flag is recommended only when all potential executables in the |
| 313 |
// domain are expected to comply with the access restrictions, as |
| 314 |
// excessive audit log entries could make it more difficult to |
| 315 |
// identify critical events. |
| 316 |
// |
| 317 |
// Requires a Linux kernel that supports Landlock ABI V7 or higher. |
| 318 |
// In combination with [Config.BestEffort], the logging option will be |
| 319 |
// omitted on older kernels and not result in an error. |
| 320 |
func (c Config) EnableLoggingForSubprocesses() Config { |
| 321 |
cfg := c |
| 322 |
cfg.flags |= ll.FlagRestrictSelfLogNewExecOn |
| 323 |
return cfg |
| 324 |
} |
| 325 |
|
| 326 |
// DisableLoggingForSubdomains disables logging of denied accesses |
| 327 |
// originating from nested Landlock domains created by the caller or |
| 328 |
// its descendants. This flag should be set according to runtime |
| 329 |
// configuration, not hardcoded, to avoid suppressing important |
| 330 |
// security events. |
| 331 |
// |
| 332 |
// It is useful for container runtimes or sandboxing tools that may |
| 333 |
// launch programs which themselves create Landlock domains and could |
| 334 |
// otherwise generate excessive logs. Unlike |
| 335 |
// [DisableLoggingForOriginatingProcess], this affects future nested |
| 336 |
// domains, not the one being created. |
| 337 |
// |
| 338 |
// Requires a Linux kernel that supports Landlock ABI V7 or higher. |
| 339 |
// In combination with [Config.BestEffort], the logging option will be |
| 340 |
// omitted on older kernels and not result in an error. |
| 341 |
func (c Config) DisableLoggingForSubdomains() Config { |
| 342 |
cfg := c |
| 343 |
cfg.flags |= ll.FlagRestrictSelfLogSubdomainsOff |
| 344 |
return cfg |
| 345 |
} |
| 346 |
|
| 347 |
// RestrictPaths restricts all goroutines to only "see" the files |
| 348 |
// provided as inputs. After this call successfully returns, the |
| 349 |
// goroutines will only be able to use files in the ways as they were |
| 350 |
// specified in advance in the call to RestrictPaths. |
| 351 |
// |
| 352 |
// Example: The following invocation will restrict all goroutines so |
| 353 |
// that they can only read from /usr, /bin and /tmp, and only write to |
| 354 |
// /tmp: |
| 355 |
// |
| 356 |
// err := landlock.V9.RestrictPaths( |
| 357 |
// landlock.RODirs("/usr", "/bin"), |
| 358 |
// landlock.RWDirs("/tmp"), |
| 359 |
// ) |
| 360 |
// if err != nil { |
| 361 |
// log.Fatalf("landlock.V9.RestrictPaths(): %v", err) |
| 362 |
// } |
| 363 |
// |
| 364 |
// RestrictPaths returns an error if any of the given paths does not |
| 365 |
// denote an actual directory or file, or if Landlock can't be enforced |
| 366 |
// using the desired ABI version constraints. |
| 367 |
// |
| 368 |
// RestrictPaths also sets the "no new privileges" flag for all OS |
| 369 |
// threads managed by the Go runtime. |
| 370 |
// |
| 371 |
// # Restrictable access rights |
| 372 |
// |
| 373 |
// The notions of what "reading" and "writing" mean are limited by what |
| 374 |
// the selected Landlock version supports. |
| 375 |
// |
| 376 |
// Calling RestrictPaths with a given Landlock ABI version will |
| 377 |
// inhibit all future calls to the access rights supported by this ABI |
| 378 |
// version, unless the accessed path is in a file hierarchy that is |
| 379 |
// specifically allow-listed for a specific set of access rights. |
| 380 |
// |
| 381 |
// The overall set of operations that RestrictPaths can restrict are: |
| 382 |
// |
| 383 |
// For reading: |
| 384 |
// |
| 385 |
// - Executing a file (V1+) |
| 386 |
// - Opening a file with read access (V1+) |
| 387 |
// - Opening a directory or listing its content (V1+) |
| 388 |
// |
| 389 |
// For writing: |
| 390 |
// |
| 391 |
// - Opening a file with write access (V1+) |
| 392 |
// - Truncating file contents (V3+) |
| 393 |
// |
| 394 |
// For directory manipulation: |
| 395 |
// |
| 396 |
// - Removing an empty directory or renaming one (V1+) |
| 397 |
// - Removing (or renaming) a file (V1+) |
| 398 |
// - Creating (or renaming or linking) a character device (V1+) |
| 399 |
// - Creating (or renaming) a directory (V1+) |
| 400 |
// - Creating (or renaming or linking) a regular file (V1+) |
| 401 |
// - Creating (or renaming or linking) a UNIX domain socket (V1+) |
| 402 |
// - Creating (or renaming or linking) a named pipe (V1+) |
| 403 |
// - Creating (or renaming or linking) a block device (V1+) |
| 404 |
// - Creating (or renaming or linking) a symbolic link (V1+) |
| 405 |
// - Renaming or linking a file between directories (V2+) |
| 406 |
// |
| 407 |
// Future versions of Landlock will be able to inhibit more operations. |
| 408 |
// Quoting the Landlock documentation: |
| 409 |
// |
| 410 |
// It is currently not possible to restrict some file-related |
| 411 |
// actions accessible through these syscall families: chdir(2), |
| 412 |
// stat(2), flock(2), chmod(2), chown(2), setxattr(2), utime(2), |
| 413 |
// ioctl(2), fcntl(2), access(2). Future Landlock evolutions will |
| 414 |
// enable to restrict them. |
| 415 |
// |
| 416 |
// The access rights are documented in more depth in the |
| 417 |
// [Kernel Documentation about Access Rights]. |
| 418 |
// |
| 419 |
// # Helper functions for selecting access rights |
| 420 |
// |
| 421 |
// These helper functions help selecting common subsets of access rights: |
| 422 |
// |
| 423 |
// - [RODirs] selects access rights in the group "for reading". |
| 424 |
// In V1, this means reading files, listing directories and executing files. |
| 425 |
// - [RWDirs] selects access rights in the group "for reading", "for writing" and |
| 426 |
// "for directory manipulation". This grants the full set of access rights which are |
| 427 |
// available within the configuration. |
| 428 |
// - [ROFiles] is like [RODirs], but does not select directory-specific access rights. |
| 429 |
// In V1, this means reading and executing files. |
| 430 |
// - [RWFiles] is like [RWDirs], but does not select directory-specific access rights. |
| 431 |
// In V1, this means reading, writing and executing files. |
| 432 |
// |
| 433 |
// The [PathAccess] rule lets callers define custom subsets of these |
| 434 |
// access rights. AccessFSSets permitted using [PathAccess] must be a |
| 435 |
// subset of the [AccessFSSet] that the Config restricts. |
| 436 |
// |
| 437 |
// To restrict multiple types of access rights at the same time, use |
| 438 |
// the more generic [Config.Restrict]. |
| 439 |
// |
| 440 |
// [Kernel Documentation about Access Rights]: https://www.kernel.org/doc/html/latest/userspace-api/landlock.html#access-rights |
| 441 |
func (c Config) RestrictPaths(rules ...Rule) error { |
| 442 |
// clear out everything but filesystem access |
| 443 |
c = Config{ |
| 444 |
handledAccessFS: c.handledAccessFS, |
| 445 |
flags: c.flags, |
| 446 |
bestEffort: c.bestEffort, |
| 447 |
} |
| 448 |
return restrict(c, rules...) |
| 449 |
} |
| 450 |
|
| 451 |
// RestrictNet restricts network access in all goroutines. |
| 452 |
// |
| 453 |
// Using Landlock V4, this function restricts the use of bind(2) and |
| 454 |
// connect(2) for TCP ports, unless those TCP ports are specifically |
| 455 |
// permitted using these rules: |
| 456 |
// |
| 457 |
// - [ConnectTCP] permits connect(2) operations to a given TCP port. |
| 458 |
// - [BindTCP] permits bind(2) operations on a given TCP port. |
| 459 |
// |
| 460 |
// These network access rights are documented in more depth in the |
| 461 |
// [Kernel Documentation about Network flags]. |
| 462 |
// |
| 463 |
// The restrictions do not currently work with Multipath TCP, which is |
| 464 |
// the default for [net.Listen] since Go 1.24. See the discussion in |
| 465 |
// the package-level documentation. |
| 466 |
// |
| 467 |
// Landlock's network sandboxing support is still incomplete as of |
| 468 |
// Landlock ABI v9 and we recommend using additional sandboxing |
| 469 |
// mechanisms to augment it. |
| 470 |
// |
| 471 |
// To restrict multiple types of access rights at the same time, use |
| 472 |
// the more generic [Config.Restrict]. |
| 473 |
// |
| 474 |
// [Kernel Documentation about Network flags]: https://www.kernel.org/doc/html/latest/userspace-api/landlock.html#network-flags |
| 475 |
func (c Config) RestrictNet(rules ...Rule) error { |
| 476 |
// clear out everything but network access |
| 477 |
c = Config{ |
| 478 |
handledAccessNet: c.handledAccessNet, |
| 479 |
flags: c.flags, |
| 480 |
bestEffort: c.bestEffort, |
| 481 |
} |
| 482 |
return restrict(c, rules...) |
| 483 |
} |
| 484 |
|
| 485 |
// RestrictScoped restricts scoped IPC access in all goroutines. |
| 486 |
// |
| 487 |
// Starting with Landlock V6, this restricts the use of IPC mechanisms |
| 488 |
// like signals and abstract UNIX domain sockets, when talking to |
| 489 |
// processes in more privileged Landlock domains. |
| 490 |
// |
| 491 |
// To restrict multiple types of access rights at the same time, use |
| 492 |
// the more generic [Config.Restrict]. |
| 493 |
func (c Config) RestrictScoped() error { |
| 494 |
// clear out everything but scoped operations |
| 495 |
c = Config{ |
| 496 |
scoped: c.scoped, |
| 497 |
flags: c.flags, |
| 498 |
bestEffort: c.bestEffort, |
| 499 |
} |
| 500 |
return restrict(c) |
| 501 |
} |
| 502 |
|
| 503 |
// Restrict restricts all types of access rights which are |
| 504 |
// restrictable with the Config. |
| 505 |
// |
| 506 |
// Using Landlock V9, this is equivalent to calling all of |
| 507 |
// [Config.RestrictPaths], [Config.RestrictNet] and |
| 508 |
// [Config.RestrictScoped] with the respective subset of rule |
| 509 |
// arguments that apply to them. |
| 510 |
// |
| 511 |
// In future Landlock versions, this function might restrict |
| 512 |
// additional types of access rights which are specified in the [Config]. |
| 513 |
func (c Config) Restrict(rules ...Rule) error { |
| 514 |
return restrict(c, rules...) |
| 515 |
} |
| 516 |
|
| 517 |
// PathOpt is a deprecated alias for [Rule]. |
| 518 |
// |
| 519 |
// Deprecated: This alias is only kept around for backwards |
| 520 |
// compatibility and will disappear with the next major release. |
| 521 |
type PathOpt = Rule |
| 522 |
|
| 523 |
// compatibleWith is true if c is compatible to work at the given Landlock ABI level. |
| 524 |
func (c Config) compatibleWithABI(abi abiInfo) bool { |
| 525 |
return (c.handledAccessFS.isSubset(abi.supportedAccessFS) && |
| 526 |
c.handledAccessNet.isSubset(abi.supportedAccessNet) && |
| 527 |
c.scoped.isSubset(abi.supportedScoped)) && |
| 528 |
c.flags.isSubset(abi.supportedRestrictFlags) |
| 529 |
} |
| 530 |
|
| 531 |
// restrictTo returns a config that is a subset of c and which is compatible with the given ABI. |
| 532 |
func (c Config) restrictTo(abi abiInfo) Config { |
| 533 |
return Config{ |
| 534 |
handledAccessFS: c.handledAccessFS.intersect(abi.supportedAccessFS), |
| 535 |
handledAccessNet: c.handledAccessNet.intersect(abi.supportedAccessNet), |
| 536 |
scoped: c.scoped.intersect(abi.supportedScoped), |
| 537 |
flags: c.flags.intersect(abi.supportedRestrictFlags), |
| 538 |
bestEffort: true, |
| 539 |
} |
| 540 |
} |