Skip to content

Commit

Permalink
Merge pull request #907 from empovit/mps-use-selinux-package
Browse files Browse the repository at this point in the history
Use SELinux package to apply context in MPS controller
  • Loading branch information
elezar authored Aug 28, 2024
2 parents 48bc2bc + df9a902 commit f21de56
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 f21de56

Please sign in to comment.