Skip to content

Commit

Permalink
Validate Landlock config when calling RestrictPath().
Browse files Browse the repository at this point in the history
This checks that the provided set of handledAccessFS permissions is
within the set known to go-landlock. (see #12)
  • Loading branch information
gnoack committed Aug 28, 2021
1 parent 0a9e761 commit 71e1285
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions landlock/abi_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var abiInfos = []abiInfo{
},
}

var highestKnownABIVersion = abiInfos[len(abiInfos)-1]

// getSupportedABIVersion returns the kernel-supported ABI version.
//
// If the ABI version supported by the kernel is higher than the
Expand Down
12 changes: 12 additions & 0 deletions landlock/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package landlock

import (
"errors"
"fmt"

ll "github.com/landlock-lsm/go-landlock/landlock/syscall"
Expand Down Expand Up @@ -41,6 +42,17 @@ type Config struct {
bestEffort bool
}

// validate returns success when the given config is supported by
// go-landlock. (It may still be unsupported by your kernel though.)
func (c Config) validate() error {
safs := highestKnownABIVersion.supportedAccessFS
if !c.handledAccessFS.isSubset(safs) {
return errors.New("unsupported handledAccessFS value")
}
return nil
}

// String builds a human-readable representation of the Config.
func (c Config) String() string {
var abi abiInfo
for _, a := range abiInfos {
Expand Down
25 changes: 25 additions & 0 deletions landlock/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,28 @@ func TestConfigString(t *testing.T) {
}
}
}

func TestValidateSuccess(t *testing.T) {
for _, c := range []Config{
V1, V1.BestEffort(),
Config{handledAccessFS: ll.AccessFSWriteFile},
Config{handledAccessFS: 0},
} {
err := c.validate()
if err != nil {
t.Errorf("%v.validate(): expected success, got %v", c, err)
}
}
}

func TestValidateFailure(t *testing.T) {
for _, c := range []Config{
Config{handledAccessFS: 0xffffffffffffffff},
Config{handledAccessFS: highestKnownABIVersion.supportedAccessFS + 1},
} {
err := c.validate()
if err == nil {
t.Errorf("%v.validate(): expected error, got success", c)
}
}
}
4 changes: 4 additions & 0 deletions landlock/restrict.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (

// The actual restrictPaths implementation.
func restrictPaths(c Config, opts ...PathOpt) error {
err := c.validate()
if err != nil {
return fmt.Errorf("unsupported Landlock config %v (upgrade go-landlock?): %v", c, err)
}
handledAccessFS := c.handledAccessFS
abi := getSupportedABIVersion()
if c.bestEffort {
Expand Down

0 comments on commit 71e1285

Please sign in to comment.