]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/exec-credential: use CLEANUP_TMPFILE_AT
authorMike Yuan <me@yhndnzj.com>
Wed, 17 Sep 2025 20:27:37 +0000 (22:27 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Sep 2025 23:53:43 +0000 (08:53 +0900)
src/core/exec-credential.c

index 5e2bc8edd975241d8db04631c4c1575cb1f106b1..f7f388c2c017ec7e3c727060b327aee218de1aef 100644 (file)
@@ -336,40 +336,35 @@ static int write_credential(
         if (fd < 0)
                 return -errno;
 
+        CLEANUP_TMPFILE_AT(dfd, tmp);
+
         r = loop_write(fd, data, size);
         if (r < 0)
-                goto fail;
+                return r;
 
         r = RET_NERRNO(fchmod(fd, 0400)); /* Take away "w" bit */
         if (r < 0)
-                goto fail;
+                return r;
 
         if (uid_is_valid(uid) && uid != getuid()) {
                 r = fd_add_uid_acl_permission(fd, uid, ACL_READ);
-                if (r < 0) {
-                        /* Ideally we use ACLs, since we can neatly express what we want to express:
-                         * the user gets read access and nothing else. But if the backing fs can't
-                         * support that (e.g. ramfs), then we can use file ownership instead. But that's
-                         * only safe if we can then re-mount the whole thing read-only, so that the user
-                         * can no longer chmod() the file to gain write access. */
-                        if (!ownership_ok || (!ERRNO_IS_NOT_SUPPORTED(r) && !ERRNO_IS_PRIVILEGE(r)))
-                                goto fail;
-
+                /* Ideally we use ACLs, since we can neatly express what we want to express:
+                 * the user gets read access and nothing else. But if the backing fs can't
+                 * support that (e.g. ramfs), then we can use file ownership instead. But that's
+                 * only safe if we can then re-mount the whole thing read-only, so that the user
+                 * can no longer chmod() the file to gain write access. */
+                if ((ERRNO_IS_NEG_NOT_SUPPORTED(r) || ERRNO_IS_NEG_PRIVILEGE(r)) && ownership_ok)
                         r = RET_NERRNO(fchown(fd, uid, gid));
-                        if (r < 0)
-                                goto fail;
-                }
+                if (r < 0)
+                        return r;
         }
 
         r = RET_NERRNO(renameat(dfd, tmp, dfd, id));
         if (r < 0)
-                goto fail;
+                return r;
 
+        tmp = mfree(tmp); /* disarm CLEANUP_TMPFILE_AT() */
         return 0;
-
-fail:
-        (void) unlinkat(dfd, tmp, /* flags = */ 0);
-        return r;
 }
 
 typedef enum CredentialSearchPath {