Skip to content

Commit 7142492

Browse files
imuni4funmugdha-adhav
authored andcommitted
Implement SELinux context handling in mount options
Added SELinux context option to mount options if not already set. This hard codes just to test. Will make configurable if works.
1 parent 6d78654 commit 7142492

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

pkg/backend/containerd/containerd.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func NewMounter(socketPath string) backend.Mounter {
3636
})
3737
}
3838

39+
var SELinuxNoRelabel = true //TODO: set this in config
40+
3941
// mountInHostNamespace mounts directly in the host mount namespace using nsenter
4042
func mountInHostNamespace(ctx context.Context, mounts []mount.Mount, target string) error {
4143
// For each mount, execute it in the host namespace
@@ -47,8 +49,25 @@ func mountInHostNamespace(ctx context.Context, mounts []mount.Mount, target stri
4749
args = append(args, "-t", m.Type)
4850
}
4951

50-
if len(m.Options) > 0 {
51-
args = append(args, "-o", strings.Join(m.Options, ","))
52+
// Add SELinux context option if config enabled
53+
mountOptions := m.Options
54+
if SELinuxNoRelabel {
55+
// Only add context option if not already there
56+
contextOpt := `context="system_u:object_r:container_file_t:s0"`
57+
alreadySet := false
58+
for _, opt := range mountOptions {
59+
if strings.HasPrefix(opt, "context=") {
60+
alreadySet = true
61+
break
62+
}
63+
}
64+
if !alreadySet {
65+
mountOptions = append(mountOptions, contextOpt)
66+
}
67+
}
68+
69+
if len(mountOptions) > 0 {
70+
args = append(args, "-o", strings.Join(mountOptions, ","))
5271
}
5372

5473
args = append(args, m.Source, target)
@@ -65,7 +84,7 @@ func mountInHostNamespace(ctx context.Context, mounts []mount.Mount, target stri
6584
return fmt.Errorf("mount failed: %w, output: %s", err, string(output))
6685
}
6786
klog.V(4).Infof("mounted %s to %s with type %s and options %v using nsenter (mount %d/%d)",
68-
m.Source, target, m.Type, m.Options, i+1, len(mounts))
87+
m.Source, target, m.Type, mountOptions, i+1, len(mounts))
6988
}
7089
return nil
7190
}

0 commit comments

Comments
 (0)