Skip to content

Commit

Permalink
Use SELinux package to apply context
Browse files Browse the repository at this point in the history
Use `github.com/opencontainers/selinux/go-selinux` instead of
calling the `chcon` command to set SELinux context.

Signed-off-by: Vitaliy Emporopulo <[email protected]>
  • Loading branch information
empovit authored and elezar committed Aug 27, 2024
1 parent c181c46 commit df9a902
Show file tree
Hide file tree
Showing 11 changed files with 2,229 additions and 9 deletions.
14 changes: 5 additions & 9 deletions cmd/mps-control-daemon/mps/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os/exec"
"path/filepath"

"github.com/opencontainers/selinux/go-selinux"
"k8s.io/klog/v2"

"github.com/NVIDIA/k8s-device-plugin/internal/rm"
Expand All @@ -37,6 +38,8 @@ const (

computeModeExclusiveProcess = computeMode("EXCLUSIVE_PROCESS")
computeModeDefault = computeMode("DEFAULT")

unprivilegedContainerSELinuxLabel = "system_u:object_r:container_file_t:s0"
)

// Daemon represents an MPS daemon.
Expand Down Expand Up @@ -98,7 +101,7 @@ func (d *Daemon) Start() error {
return fmt.Errorf("error creating directory %v: %w", pipeDir, err)
}

if err := setSELinuxContext(pipeDir, "container_file_t"); err != nil {
if err := setSELinuxContext(pipeDir, unprivilegedContainerSELinuxLabel); err != nil {
return fmt.Errorf("error setting SELinux context: %w", err)
}

Expand Down Expand Up @@ -151,14 +154,7 @@ func setSELinuxContext(path string, context string) error {
}

klog.InfoS("SELinux enabled, setting context", "path", path, "context", context)
chconCmd := exec.Command("chcon", "-R", "-t", context, path)
output, err := chconCmd.CombinedOutput()
if err != nil {
klog.Errorf("\n%v", string(output))
return err
}

return nil
return selinux.Chcon(path, context, true)
}

// Stop ensures that the MPS daemon is quit.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0
github.com/google/renameio v1.0.1
github.com/google/uuid v1.6.0
github.com/opencontainers/selinux v1.11.0
github.com/prometheus/procfs v0.15.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
Expand Down
201 changes: 201 additions & 0 deletions vendor/github.com/opencontainers/selinux/LICENSE

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

13 changes: 13 additions & 0 deletions vendor/github.com/opencontainers/selinux/go-selinux/doc.go

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

Loading

0 comments on commit df9a902

Please sign in to comment.