Skip to content

Commit b85f8fd

Browse files
committed
If chcon fails, check if label is already correct
Currently if a user attempts to chcon a file or directory and fails for any reason check if the file already has the right label, and continue. Signed-off-by: Daniel J Walsh <[email protected]>
1 parent 00d547f commit b85f8fd

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

go-selinux/rchcon.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ import (
1212
)
1313

1414
func rchcon(fpath, label string) error {
15+
fastMode := false
16+
// If the current label matches the new label, assume
17+
// other labels are correct.
18+
if cLabel, err := lFileLabel(fpath); err == nil && cLabel == label {
19+
fastMode = true
20+
}
1521
return pwalkdir.Walk(fpath, func(p string, _ fs.DirEntry, _ error) error {
22+
if fastMode {
23+
if cLabel, err := lFileLabel(fpath); err == nil && cLabel == label {
24+
return nil
25+
}
26+
}
1627
e := lSetFileLabel(p, label)
1728
// Walk a file tree can race with removal, so ignore ENOENT.
1829
if errors.Is(e, os.ErrNotExist) {

go-selinux/selinux_linux.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,24 @@ func chcon(fpath string, label string, recurse bool) error {
10841084
}
10851085

10861086
if !recurse {
1087-
return setFileLabel(fpath, label)
1087+
err := lSetFileLabel(fpath, label)
1088+
if err != nil {
1089+
// Check if file doesn't exist, must have been removed
1090+
if errors.Is(err, os.ErrNotExist) {
1091+
return nil
1092+
}
1093+
// Check if current label is correct on disk
1094+
flabel, nerr := lFileLabel(fpath)
1095+
if nerr == nil && flabel == label {
1096+
return nil
1097+
}
1098+
// Check if file doesn't exist, must have been removed
1099+
if errors.Is(nerr, os.ErrNotExist) {
1100+
return nil
1101+
}
1102+
return err
1103+
}
1104+
return nil
10881105
}
10891106

10901107
return rchcon(fpath, label)

0 commit comments

Comments
 (0)