]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Check the result of dirfd() before calling unlinkat()
authorOndřej Surý <ondrej@isc.org>
Thu, 15 Aug 2024 07:23:31 +0000 (09:23 +0200)
committerOndřej Surý <ondrej@isc.org>
Mon, 19 Aug 2024 09:57:28 +0000 (09:57 +0000)
Instead of directly using the result of dirfd() in the unlinkat() call,
check whether the returned file descriptor is actually valid.  That
doesn't really change the logic as the unlinkat() would fail with
invalid descriptor anyway, but this is cleaner and will report the right
error returned directly by dirfd() instead of EBADF from unlinkat().

lib/isc/log.c

index 40bb7fbb3292fc3249cb0c9faaeb95932cdaa14c..71fa5365c1e81d150400122ed4b374eb59a23306 100644 (file)
@@ -1037,8 +1037,10 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) {
                         * Remove any backup files that exceed versions.
                         */
                        if (*digit_end == '\0' && version >= versions) {
-                               int n = unlinkat(dirfd(dir.handle),
-                                                dir.entry.name, 0);
+                               int n = dirfd(dir.handle);
+                               if (n >= 0) {
+                                       n = unlinkat(n, dir.entry.name, 0);
+                               }
                                if (n < 0) {
                                        result = isc_errno_toresult(errno);
                                        if (result != ISC_R_SUCCESS &&
@@ -1184,8 +1186,10 @@ remove_old_tsversions(isc_logfile_t *file, int versions) {
                         * Remove any backup files that exceed versions.
                         */
                        if (*digit_end == '\0' && version < last) {
-                               int n = unlinkat(dirfd(dir.handle),
-                                                dir.entry.name, 0);
+                               int n = dirfd(dir.handle);
+                               if (n >= 0) {
+                                       n = unlinkat(n, dir.entry.name, 0);
+                               }
                                if (n < 0) {
                                        result = isc_errno_toresult(errno);
                                        if (result != ISC_R_SUCCESS &&