]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fs-util: make chmod_and_chown_at() work with empty path and AT_FDCWD
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 7 Dec 2022 00:51:30 +0000 (09:51 +0900)
committerLennart Poettering <lennart@poettering.net>
Thu, 8 Dec 2022 14:57:33 +0000 (15:57 +0100)
Follow-up for 7d000133c2fbf4b5986185ccfc0273a2428972a9.

Fixes CID#1500608.

src/basic/fs-util.c

index 33b4d1f07bfa51b86e52e62c641b77c11020e5d0..fbc7492b15264f0826b824403a67fe16a112e136 100644 (file)
@@ -204,9 +204,17 @@ int chmod_and_chown_at(int dir_fd, const char *path, mode_t mode, uid_t uid, gid
                 fd = openat(dir_fd, path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
                 if (fd < 0)
                         return -errno;
+                dir_fd = fd;
+
+        } else if (dir_fd == AT_FDCWD) {
+                /* Let's acquire an O_PATH fd of the current directory */
+                fd = openat(dir_fd, ".", O_PATH|O_CLOEXEC|O_NOFOLLOW|O_DIRECTORY);
+                if (fd < 0)
+                        return -errno;
+                dir_fd = fd;
         }
 
-        return fchmod_and_chown(path ? fd : dir_fd, mode, uid, gid);
+        return fchmod_and_chown(dir_fd, mode, uid, gid);
 }
 
 int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t uid, gid_t gid) {