Skip to content

Commit d5bbe94

Browse files
committed
Merge remote-tracking branch 'origin/pull/344'
* origin/pull/344: bundles: disable landlock sign: ensure we are getting RW/RO access for the files lsm: Implement TruncFile
2 parents ff7dadd + e200257 commit d5bbe94

File tree

5 files changed

+46
-28
lines changed

5 files changed

+46
-28
lines changed

cmd/sbctl/list-bundles.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/foxboron/sbctl/config"
1010
"github.com/foxboron/sbctl/hierarchy"
1111
"github.com/foxboron/sbctl/logging"
12-
"github.com/foxboron/sbctl/lsm"
1312
"github.com/spf13/cobra"
1413
)
1514

@@ -29,11 +28,11 @@ var listBundlesCmd = &cobra.Command{
2928

3029
logging.Errorf("The bundle/uki support in sbctl is deprecated. Please move to dracut/mkinitcpio/ukify.")
3130

32-
if state.Config.Landlock {
33-
if err := lsm.Restrict(); err != nil {
34-
return err
35-
}
36-
}
31+
// if state.Config.Landlock {
32+
// if err := lsm.Restrict(); err != nil {
33+
// return err
34+
// }
35+
// }
3736

3837
bundles := []JsonBundle{}
3938
var isSigned bool

cmd/sbctl/sign-all.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ var signAllCmd = &cobra.Command{
2323
RunE: func(cmd *cobra.Command, args []string) error {
2424
var gerr error
2525
state := cmd.Context().Value(stateDataKey{}).(*config.State)
26-
if state.Config.Landlock {
26+
// Don't run landlock if we are making UKIs
27+
if state.Config.Landlock && !generate {
2728
if err := sbctl.LandlockFromFileDatabase(state); err != nil {
2829
return err
2930
}

cmd/sbctl/sign.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/foxboron/sbctl/logging"
1212
"github.com/foxboron/sbctl/lsm"
1313
"github.com/landlock-lsm/go-landlock/landlock"
14+
"github.com/spf13/afero"
1415
"github.com/spf13/cobra"
1516
)
1617

@@ -30,27 +31,32 @@ var signCmd = &cobra.Command{
3031
os.Exit(1)
3132
}
3233

34+
var rules []landlock.Rule
35+
3336
// Ensure we have absolute paths
3437
file, err := filepath.Abs(args[0])
3538
if err != nil {
3639
return err
3740
}
3841
if output == "" {
3942
output = file
43+
rules = append(rules, lsm.TruncFile(file).IgnoreIfMissing())
4044
} else {
4145
output, err = filepath.Abs(output)
4246
if err != nil {
4347
return err
4448
}
49+
// Set input file to RO and output dir/file to RW
50+
rules = append(rules, landlock.ROFiles(file).IgnoreIfMissing())
51+
if ok, _ := afero.Exists(state.Fs, output); ok {
52+
rules = append(rules, lsm.TruncFile(output))
53+
} else {
54+
rules = append(rules, landlock.RWDirs(filepath.Dir(output)))
55+
}
4556
}
4657

4758
if state.Config.Landlock {
48-
lsm.RestrictAdditionalPaths(
49-
// TODO: This doesn't work quite how I want it to
50-
// setting RWFiles to the path gets EACCES
51-
// but setting RWDirs on the dir is fine
52-
landlock.RWDirs(filepath.Dir(output)),
53-
)
59+
lsm.RestrictAdditionalPaths(rules...)
5460
if err := lsm.Restrict(); err != nil {
5561
return err
5662
}

database.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/foxboron/sbctl/lsm"
1111
"github.com/landlock-lsm/go-landlock/landlock"
1212

13-
ll "github.com/landlock-lsm/go-landlock/landlock/syscall"
1413
"github.com/spf13/afero"
1514
)
1615

@@ -63,28 +62,32 @@ func SigningEntryIter(state *config.State, fn func(s *SigningEntry) error) error
6362
return nil
6463
}
6564

66-
const (
67-
// We open the file with O_TRUNC
68-
accessFile landlock.AccessFSSet = ll.AccessFSExecute | ll.AccessFSWriteFile | ll.AccessFSReadFile | ll.AccessFSTruncate
69-
)
70-
7165
func LandlockFromFileDatabase(state *config.State) error {
7266
var llrules []landlock.Rule
7367
files, err := ReadFileDatabase(state.Fs, state.Config.FilesDb)
7468
if err != nil {
7569
return err
7670
}
7771
for _, entry := range files {
78-
llrules = append(llrules,
79-
landlock.PathAccess(accessFile, entry.File),
80-
)
72+
if entry.File == entry.OutputFile {
73+
// If file is the same as output, set RW+Trunc on file
74+
llrules = append(llrules,
75+
lsm.TruncFile(entry.File).IgnoreIfMissing(),
76+
)
77+
}
8178
if entry.File != entry.OutputFile {
82-
// We do an RWDirs on the directory and a RWFiles on the file itself. it
83-
// should be noted that the output file might not exist at this time
84-
llrules = append(llrules, landlock.RWDirs(
85-
filepath.Dir(entry.File),
86-
),
87-
landlock.RWFiles(entry.File).IgnoreIfMissing())
79+
// Set input file to RO, ignore if missing so we can bubble a useable
80+
// error to the user
81+
llrules = append(llrules, landlock.ROFiles(entry.File).IgnoreIfMissing())
82+
83+
// Check if output file exists
84+
// if it does we set RW on the file directly
85+
// if it doesnt, we set RW on the directory
86+
if ok, _ := afero.Exists(state.Fs, entry.OutputFile); ok {
87+
llrules = append(llrules, lsm.TruncFile(entry.OutputFile))
88+
} else {
89+
llrules = append(llrules, landlock.RWDirs(filepath.Dir(entry.OutputFile)))
90+
}
8891
}
8992
}
9093
lsm.RestrictAdditionalPaths(llrules...)

lsm/lsm.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@ import (
66

77
"github.com/foxboron/sbctl/config"
88
"github.com/landlock-lsm/go-landlock/landlock"
9+
10+
ll "github.com/landlock-lsm/go-landlock/landlock/syscall"
911
)
1012

1113
var (
1214
rules []landlock.Rule
15+
16+
// Include file truncation
17+
truncFile landlock.AccessFSSet = ll.AccessFSExecute | ll.AccessFSWriteFile | ll.AccessFSReadFile | ll.AccessFSTruncate
1318
)
1419

20+
func TruncFile(p string) landlock.FSRule {
21+
return landlock.PathAccess(truncFile, p)
22+
}
23+
1524
func LandlockRulesFromConfig(conf *config.Config) {
1625
rules = append(rules,
1726
landlock.RODirs(

0 commit comments

Comments
 (0)