From 98b02dd7e73fc42676537dab885c70c45b0375fc Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 14 Jun 2025 00:27:06 +0900 Subject: [PATCH] sd-dhcp-server: reopen fd only when it is valid Fixes a bug in 11b88419ae0004547a0724aa459ddcb5d243f25c. --- src/libsystemd-network/sd-dhcp-server.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index 099362d1ae..335f01f164 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -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)