From 7d293e3efadfc4f6a09fa4c2a36bf99e1bbf6816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Thu, 17 Sep 2020 19:46:44 -0500 Subject: [PATCH] volume: Change owner of symlinks too This commit uses Lchown instead of Chown to change the owner of symlinks too. It doesn't change any behaviour. However, it could avoid some confusions as the symlinks are updated to the correct owner too. --- pkg/volume/volume_linux.go | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/pkg/volume/volume_linux.go b/pkg/volume/volume_linux.go index 17a1d3ef738..5c46e67cd26 100644 --- a/pkg/volume/volume_linux.go +++ b/pkg/volume/volume_linux.go @@ -89,32 +89,22 @@ func legacyOwnershipChange(mounter Mounter, fsGroup *int64) error { } func changeFilePermission(filename string, fsGroup *int64, readonly bool, info os.FileInfo) error { - // chown and chmod pass through to the underlying file for symlinks. + err := os.Lchown(filename, -1, int(*fsGroup)) + if err != nil { + klog.Errorf("Lchown failed on %v: %v", filename, err) + } + + // chmod passes through to the underlying file for symlinks. // Symlinks have a mode of 777 but this really doesn't mean anything. // The permissions of the underlying file are what matter. // However, if one reads the mode of a symlink then chmods the symlink // with that mode, it changes the mode of the underlying file, overridden // the defaultMode and permissions initialized by the volume plugin, which - // is not what we want; thus, we skip chown/chmod for symlinks. + // is not what we want; thus, we skip chmod for symlinks. if info.Mode()&os.ModeSymlink != 0 { return nil } - stat, ok := info.Sys().(*syscall.Stat_t) - if !ok { - return nil - } - - if stat == nil { - klog.Errorf("Got nil stat_t for path %v while setting ownership of volume", filename) - return nil - } - - err := os.Chown(filename, int(stat.Uid), int(*fsGroup)) - if err != nil { - klog.Errorf("Chown failed on %v: %v", filename, err) - } - mask := rwMask if readonly { mask = roMask