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 Jul 21, 2022
1 parent 85331a8 commit a445445
Show file tree
Hide file tree
Showing 319 changed files with 49,234 additions and 113,706 deletions.
13 changes: 13 additions & 0 deletions go-selinux/rchcon.go
Expand Up @@ -12,7 +12,20 @@ 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 &&
label == currentLabel {
slowMode = true
}
return pwalkdir.Walk(fpath, func(p string, _ fs.DirEntry, _ error) error {
if slowMode {
if currentLabel, err := lFileLabel(p); err == nil &&
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
4 changes: 2 additions & 2 deletions go.mod
@@ -1,5 +1,5 @@
module github.com/opencontainers/selinux

go 1.13
go 1.18

require golang.org/x/sys v0.0.0-20191115151921-52ab43148777
require golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
4 changes: 2 additions & 2 deletions go.sum
@@ -1,2 +1,2 @@
golang.org/x/sys v0.0.0-20191115151921-52ab43148777 h1:wejkGHRTr38uaKRqECZlsCsJ1/TGxIyFbH32x5zUdu4=
golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
30 changes: 30 additions & 0 deletions vendor/golang.org/x/sys/internal/unsafeheader/unsafeheader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions vendor/golang.org/x/sys/unix/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vendor/golang.org/x/sys/unix/aliases.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vendor/golang.org/x/sys/unix/asm_aix_ppc64.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 0 additions & 30 deletions vendor/golang.org/x/sys/unix/asm_darwin_arm.s

This file was deleted.

30 changes: 0 additions & 30 deletions vendor/golang.org/x/sys/unix/asm_darwin_arm64.s

This file was deleted.

29 changes: 0 additions & 29 deletions vendor/golang.org/x/sys/unix/asm_dragonfly_amd64.s

This file was deleted.

29 changes: 0 additions & 29 deletions vendor/golang.org/x/sys/unix/asm_freebsd_386.s

This file was deleted.

0 comments on commit a445445

Please sign in to comment.