Skip to content

Commit f3be2df

Browse files
committed
OCPBUGS-39304: UPSTREAM: 848: fix: mount options for fsGroup delegation must ensure RWX for the group
If user sets `fsGroup: <gid>` in Pod's spec.securityContext, kubelet delegate fsGroup to CSI Driver, and NodeStageVolume() adds `gid=<gid>` to mount options. This might be not enough to make volume writable for the user: ``` $ kubectl exec fedora -- ls -ld /mnt/claim drwxr-xr-x. 2 root 1002 0 Sep 13 12:04 /mnt/claim $ kubectl exec fedora -- touch /mnt/claim/FILE touch: cannot touch '/mnt/claim/FILE': Permission denied ``` See kubernetes-csi#835
1 parent a12ac11 commit f3be2df

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

pkg/smb/nodeserver.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
200200
mountOptions = mountFlags
201201
if !gidPresent && volumeMountGroup != "" {
202202
mountOptions = append(mountOptions, fmt.Sprintf("gid=%s", volumeMountGroup))
203+
if !raiseGroupRWXInMountFlags(mountOptions, "file_mode") {
204+
mountOptions = append(mountOptions, "file_mode=0774")
205+
}
206+
if !raiseGroupRWXInMountFlags(mountOptions, "dir_mode") {
207+
mountOptions = append(mountOptions, "dir_mode=0775")
208+
}
203209
}
204210
if domain != "" {
205211
mountOptions = append(mountOptions, fmt.Sprintf("%s=%s", domainField, domain))
@@ -608,3 +614,25 @@ func deleteKerberosCache(krb5CacheDirectory, volumeID string) error {
608614

609615
return nil
610616
}
617+
618+
// Raises RWX bits for group access in the mode arg. If mode is invalid, keep it unchanged.
619+
func enableGroupRWX(mode string) string {
620+
v, e := strconv.ParseInt(mode, 0, 0)
621+
if e != nil || v < 0 {
622+
return mode
623+
}
624+
return fmt.Sprintf("0%o", v|070)
625+
}
626+
627+
// Apply enableGroupRWX() to the option "flag=xyz"
628+
func raiseGroupRWXInMountFlags(mountFlags []string, flag string) bool {
629+
for i, mountFlag := range mountFlags {
630+
mountFlagSplit := strings.Split(mountFlag, "=")
631+
if len(mountFlagSplit) != 2 || mountFlagSplit[0] != flag {
632+
continue
633+
}
634+
mountFlags[i] = fmt.Sprintf("%s=%s", flag, enableGroupRWX(mountFlagSplit[1]))
635+
return true
636+
}
637+
return false
638+
}

0 commit comments

Comments
 (0)