Skip to content

Commit

Permalink
If chcon fails, check if label is already correct
Browse files Browse the repository at this point in the history
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 <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Jun 30, 2022
1 parent b730778 commit a5f9878
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
16 changes: 16 additions & 0 deletions go-selinux/rchcon.go
@@ -1,3 +1,4 @@
//go:build linux && go1.16
// +build linux,go1.16

package selinux
Expand All @@ -11,7 +12,22 @@ import (
)

func rchcon(fpath, label string) error {
slowMode := false
// If the current label matches the new label, assume
// other labels are correct.
if currentLabel, err := lFileLabel(fpath); err == nil {
if label == currentLabel {
slowMode = true
}
}
return pwalkdir.Walk(fpath, func(p string, _ fs.DirEntry, _ error) error {
if slowMode {
if currentLabel, err := lFileLabel(p); err == nil {
if label == currentLabel {
return nil
}
}
}
e := lSetFileLabel(p, label)
// Walk a file tree can race with removal, so ignore ENOENT.
if errors.Is(e, os.ErrNotExist) {
Expand Down
13 changes: 12 additions & 1 deletion go-selinux/selinux_linux.go
Expand Up @@ -1102,7 +1102,18 @@ func chcon(fpath string, label string, recurse bool) error {
}

if !recurse {
return setFileLabel(fpath, label)
err := lSetFileLabel(fpath, label)
if err == nil {
return nil
}
if errors.Is(err, os.ErrNotExist) {
return err
}
flabel, _ := lFileLabel(fpath)
if flabel == label {
return nil
}
return err
}

return rchcon(fpath, label)
Expand Down

0 comments on commit a5f9878

Please sign in to comment.