Skip to content

Commit

Permalink
Merge pull request #1055 from jsafrane/4.7-selinux-guess
Browse files Browse the repository at this point in the history
[4.7] Bug 2022720: UPSTREAM: 106261: Don't guess SELinux support on error
  • Loading branch information
openshift-merge-robot committed Dec 3, 2021
2 parents 63f841a + b25d4fb commit c343126
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
5 changes: 5 additions & 0 deletions pkg/kubelet/volumemanager/cache/actual_state_of_world.go
Expand Up @@ -506,6 +506,11 @@ func (asw *actualStateOfWorld) AddPodToVolume(markVolumeOpts operationexecutor.M
// If pod exists, reset remountRequired value
podObj.remountRequired = false
podObj.volumeMountStateForPod = markVolumeOpts.VolumeMountState
if mounter != nil {
// The mounter stored in the object may have old information,
// use the newest one.
podObj.mounter = mounter
}
asw.attachedVolumes[volumeName].mountedPods[podName] = podObj
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/volume/csi/csi_mounter.go
Expand Up @@ -263,7 +263,8 @@ func (c *csiMountMgr) SetUpAt(dir string, mounterArgs volume.MounterArgs) error

c.supportsSELinux, err = c.kubeVolHost.GetHostUtil().GetSELinuxSupport(dir)
if err != nil {
klog.V(2).Info(log("error checking for SELinux support: %s", err))
// The volume is mounted. Return UncertainProgressError, so kubelet will unmount it when user deletes the pod.
return volumetypes.NewUncertainProgressError(fmt.Sprintf("error checking for SELinux support: %s", err))
}

if c.supportsFSGroup(fsType, mounterArgs.FsGroup, c.fsGroupPolicy) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/volume/util/hostutil/BUILD
Expand Up @@ -15,11 +15,13 @@ go_library(
"//staging/src/k8s.io/mount-utils:go_default_library",
] + select({
"@io_bazel_rules_go//go/platform:android": [
"//pkg/util/selinux:go_default_library",
"//vendor/golang.org/x/sys/unix:go_default_library",
"//vendor/k8s.io/klog/v2:go_default_library",
"//vendor/k8s.io/utils/path:go_default_library",
],
"@io_bazel_rules_go//go/platform:linux": [
"//pkg/util/selinux:go_default_library",
"//vendor/golang.org/x/sys/unix:go_default_library",
"//vendor/k8s.io/klog/v2:go_default_library",
"//vendor/k8s.io/utils/path:go_default_library",
Expand Down
2 changes: 1 addition & 1 deletion pkg/volume/util/hostutil/fake_hostutil.go
Expand Up @@ -108,7 +108,7 @@ func (hu *FakeHostUtil) GetOwner(pathname string) (int64, int64, error) {
// GetSELinuxSupport tests if pathname is on a mount that supports SELinux.
// Not implemented for testing
func (hu *FakeHostUtil) GetSELinuxSupport(pathname string) (bool, error) {
return false, errors.New("GetSELinuxSupport not implemented")
return false, nil
}

// GetMode returns permissions of pathname.
Expand Down
13 changes: 11 additions & 2 deletions pkg/volume/util/hostutil/hostutil_linux.go
Expand Up @@ -28,6 +28,7 @@ import (

"golang.org/x/sys/unix"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/util/selinux"
"k8s.io/mount-utils"
utilpath "k8s.io/utils/path"
)
Expand Down Expand Up @@ -229,8 +230,16 @@ func DoMakeRShared(path string, mountInfoFilename string) error {
return nil
}

// selinux.SELinuxEnabled implementation for unit tests
type seLinuxEnabledFunc func() bool

// GetSELinux is common implementation of GetSELinuxSupport on Linux.
func GetSELinux(path string, mountInfoFilename string) (bool, error) {
func GetSELinux(path string, mountInfoFilename string, selinuxEnabled seLinuxEnabledFunc) (bool, error) {
// Skip /proc/mounts parsing if SELinux is disabled.
if !selinuxEnabled() {
return false, nil
}

info, err := findMountInfo(path, mountInfoFilename)
if err != nil {
return false, err
Expand All @@ -253,7 +262,7 @@ func GetSELinux(path string, mountInfoFilename string) (bool, error) {
// GetSELinuxSupport returns true if given path is on a mount that supports
// SELinux.
func (hu *HostUtil) GetSELinuxSupport(pathname string) (bool, error) {
return GetSELinux(pathname, procMountInfoPath)
return GetSELinux(pathname, procMountInfoPath, selinux.SELinuxEnabled)
}

// GetOwner returns the integer ID for the user and group of the given path
Expand Down
12 changes: 11 additions & 1 deletion pkg/volume/util/hostutil/hostutil_linux_test.go
Expand Up @@ -156,27 +156,37 @@ func TestGetSELinuxSupport(t *testing.T) {
tests := []struct {
name string
mountPoint string
selinuxEnabled bool
expectedResult bool
}{
{
"ext4 on / with disabled SELinux",
"/",
false,
false,
},
{
"ext4 on /",
"/",
true,
true,
},
{
"tmpfs on /var/lib/bar",
"/var/lib/bar",
true,
false,
},
{
"nfsv4",
"/media/nfs_vol",
true,
false,
},
}

for _, test := range tests {
out, err := GetSELinux(test.mountPoint, filename)
out, err := GetSELinux(test.mountPoint, filename, func() bool { return test.selinuxEnabled })
if err != nil {
t.Errorf("Test %s failed with error: %s", test.name, err)
}
Expand Down

0 comments on commit c343126

Please sign in to comment.