Skip to content

Commit 0dcec78

Browse files
committed
sign: ensure we are getting RW/RO access for the files
Signed-off-by: Morten Linderud <[email protected]>
1 parent 34544d7 commit 0dcec78

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,25 @@ func LandlockFromFileDatabase(state *config.State) error {
6969
return err
7070
}
7171
for _, entry := range files {
72-
llrules = append(llrules,
73-
landlock.PathAccess(accessFile, entry.File),
74-
)
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+
}
7578
if entry.File != entry.OutputFile {
76-
// We do an RWDirs on the directory and a RWFiles on the file itself. it
77-
// should be noted that the output file might not exist at this time
78-
llrules = append(llrules, landlock.RWDirs(
79-
filepath.Dir(entry.File),
80-
),
81-
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+
}
8291
}
8392
}
8493
lsm.RestrictAdditionalPaths(llrules...)

0 commit comments

Comments
 (0)