landlock-lsm / go-landlock

A Go library for the Linux Landlock sandboxing feature
MIT License
105 stars 7 forks source link

Make it possible to create composable libraries of Landlock rules #25

Open gnoack opened 1 year ago

gnoack commented 1 year ago

Users should be able to group their own libraries of commonly used rules that are used together

idea:

package llopts

import ...

var SharedLibraries = landlock.GroupRules(
    landlock.RODirs("/usr/lib", "/lib"),
    landlock.RWDirs(os.Getenv("TMPDIR")),
)

// Looking a bit into the future here with Network rules...
var DNSClient = landlock.GroupRules(
    landlock.ROFiles("/etc/hosts"),
    landlock.DialTCP(53),
    landlock.DialUDP(53),
)

(This is just an example -- the details of these rules are not really fleshed out)

gnoack commented 1 year ago

Such a GroupRules meta-rule requires a bit of refactoring... I have been considering the following options:

Option A - fully compatible

Make the PathOpt struct be able to compose itself, e.g.

type PathOpt struct {
  // all the existing fields
  more []PathOpt
}

and extend all its methods accordingly.

Option B

Inheritance hierarchy

=> also no composition across FS and net rules...

Option C - turn types to runtime errors

Rule is the interface for options

type PathOpt = Rule  // for backwards compatibility
func (c *Config) RestrictPaths([]Rule) error
func RWDirs(...) FSRule

Note: RWDirs and friends do not return the actual Rule type, but their returned type is implementing that interface.

Slight API breakage, but probably OK to do for most use cases. The two use cases on Github do something like

var opts []landlock.PathOpt
// repeatedly
opts = append(opts, landlock.RWDirs(...))
landlock.V2.RestrictPaths(opts...)

That would continue to work because people tend to not spell out the return type of RWDirs and friends, and it is compatible with the landlock.Rule (a.k.a. PathOpt) interface.

Known API breakages:

Considerations:

gnoack commented 1 year ago

I am strongly leaning towards C, but will let it sink a little bit before committing.

gnoack commented 1 year ago

Work is happening on the options branch https://github.com/landlock-lsm/go-landlock/commits/options