erofs-utils: fsck: keep S{U,G}ID bits properly on extraction
authorGao Xiang <hsiangkao@linux.alibaba.com>
Fri, 24 Jan 2025 09:06:27 +0000 (17:06 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Thu, 6 Feb 2025 05:01:10 +0000 (13:01 +0800)
As chown(2) explained, "
When the owner or group of an executable file are changed by an
unprivileged user the S_ISUID and S_ISGID mode bits are cleared.
POSIX does not specify whether this also should happen when root does
the chown(); the Linux behavior depends on the kernel version."

Fix it by chown() first.

Fixes: 412c8f908132 ("erofs-utils: fsck: add --extract=X support to extract to path X")
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20250124090628.2865088-1-hsiangkao@linux.alibaba.com
fsck/main.c

index d375835738c1d32da9df01c72749893796eac637..b1d6214497576748fb85bbfe27f6b3c024a4d87d 100644 (file)
@@ -300,6 +300,12 @@ static void erofsfsck_set_attributes(struct erofs_inode *inode, char *path)
 #endif
                erofs_warn("failed to set times: %s", path);
 
+       if (fsckcfg.preserve_owner) {
+               ret = lchown(path, inode->i_uid, inode->i_gid);
+               if (ret < 0)
+                       erofs_warn("failed to change ownership: %s", path);
+       }
+
        if (!S_ISLNK(inode->i_mode)) {
                if (fsckcfg.preserve_perms)
                        ret = chmod(path, inode->i_mode);
@@ -308,12 +314,6 @@ static void erofsfsck_set_attributes(struct erofs_inode *inode, char *path)
                if (ret < 0)
                        erofs_warn("failed to set permissions: %s", path);
        }
-
-       if (fsckcfg.preserve_owner) {
-               ret = lchown(path, inode->i_uid, inode->i_gid);
-               if (ret < 0)
-                       erofs_warn("failed to change ownership: %s", path);
-       }
 }
 
 static int erofs_check_sb_chksum(void)