Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify SELinux context of MPS pipe directory #789

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/mps-control-daemon/mps/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 Down Expand Up @@ -97,6 +98,12 @@ func (d *Daemon) Start() error {
return fmt.Errorf("error creating directory %v: %w", pipeDir, err)
}

if selinux.EnforceMode() == selinux.Enforcing {
if err := selinux.Chcon(pipeDir, "container_file_t", true); err != nil {
return fmt.Errorf("error setting SELinux context: %w", err)
}
}
Comment on lines +101 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a one-time initialization step, isn't it? I'm wondering if it's possible that a user sets SELinux to permissive (e.g. for debugging), installs the driver, and then changes SELinux back to enforcing. Won't it be safer to rely on selinux.GetEnabled() or selinux.DefaultEnforceMode()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just tested chcon on a system with SELinux disabled, and it works. I guess it won't have any effect, but won't fail either.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to switch to GetEnabled() if we think that's cleaner.

It's a one-time initialization step, isn't it?

This step is only run as the MPS daemon container is started -- or if the configuration changes. What is the expected behaviour if a user changes the SELinux mode?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, EnforceMode contains the logic to extract the CURRENT mode.

// enforceMode returns the current SELinux mode Enforcing, Permissive, Disabled
func enforceMode() int {
	var enforce int

	enforceB, err := os.ReadFile(selinuxEnforcePath())
	if err != nil {
		return -1
	}
	enforce, err = strconv.Atoi(string(enforceB))
	if err != nil {
		return -1
	}
	return enforce
}

Whereas GetEnabled() checks whether SELinux is available at all -- regardless of the enforcing mode.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so in the enforcing mode the new type will do the tick, in the permissive mode everything is permitted anyway, so changing the type won't have any effect. Unless the mode is switched back to enforcing, in which case the type will already be set to the right one.

The only reason we may need GetEnable() is to avoid errors when trying to change the type on a system that doesn't run SELinux, IMO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it looks like enabling/disabling SELinux or changing the mode (enforcing/permissive) requires rebooting the node anyway. So the type will be applied whenever needed in any case.


logDir := d.LogDir()
if err := os.MkdirAll(logDir, 0755); err != nil {
return fmt.Errorf("error creating directory %v: %w", logDir, err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/mittwald/go-helm-client v0.12.9
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
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