]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-dhcp-server: reopen fd only when it is valid
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 13 Jun 2025 15:27:06 +0000 (00:27 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 13 Jun 2025 17:53:31 +0000 (02:53 +0900)
Fixes a bug in 11b88419ae0004547a0724aa459ddcb5d243f25c.

src/libsystemd-network/sd-dhcp-server.c

index 099362d1ae3e8865f86caaab6345f7659ced8f35..335f01f1645aa149fa678b54bfebc988f5ebce4e 100644 (file)
@@ -1605,9 +1605,13 @@ int sd_dhcp_server_set_lease_file(sd_dhcp_server *server, int dir_fd, const char
         if (!path_is_safe(path))
                 return -EINVAL;
 
-        _cleanup_close_ int fd = fd_reopen(dir_fd, O_CLOEXEC | O_DIRECTORY | O_PATH);
-        if (fd < 0)
-                return fd;
+        _cleanup_close_ int fd = AT_FDCWD; /* Unlike our usual coding style, AT_FDCWD needs to be set,
+                                            * to pass a 'valid' fd. */
+        if (dir_fd >= 0) {
+                fd = fd_reopen(dir_fd, O_CLOEXEC | O_DIRECTORY | O_PATH);
+                if (fd < 0)
+                        return fd;
+        }
 
         r = free_and_strdup(&server->lease_file, path);
         if (r < 0)