landlock-lsm / go-landlock

A Go library for the Linux Landlock sandboxing feature
MIT License
115 stars 8 forks source link

Add Landlock V2 support. #22

Closed gnoack closed 2 years ago

gnoack commented 2 years ago

This adds support for the upcoming Landlock ABI V2.

In addition to the existing file system access rights, it is now possible to move or link files across different directories with the new 'refer' access right. (For details, see Landlock documentation.)

Changes in the library:

The 'refer' access right for a path may only be specified handledAccessFS also contains it (i.e. by using the landlock.V2 config).

Upgrade path:

Callers using the landlock.V1.BestEffort().RestrictPaths(...) form can switch to use landlock.V2.BestEffort().RestrictPaths(...) with the same parameters instead. This change is compatible with before.

If you additionally desire to link or move files between directories, make sure that both directories have the "refer" access right, by calling .WithRefer() on their landlock.PathOpt objects. For example:

err := landlock.V2.RestrictPaths(
    landlock.RWDirs("/src", "/dest").WithRefer(),
)

NOTE: Requiring the "refer" access right is incompatible with kernels before 5.19. If you want to use Landlock with earlier kernels, do not ask for that permission.

In particular, this means that using "best effort mode" in combination with the refer right will downgrade to "doing nothing" on kernels below 5.19, as linking and moving files would otherwise not work:

// Downgrades to no Landlock enforcement on Linux kernels before 5.19.
err := landlock.V2.BestEffort().RestrictPaths(
    landlock.RWDirs("/src", "/dest").WithRefer(),
)

Resolves #20.

gnoack commented 2 years ago

Kernel v5.19 is out, and I'll merge this in now. If you have late review comments, please feel free to ping me.

l0kod commented 2 years ago

LGTM. Nice work!