]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
setup_current_filesystem: fail if name_max is 0
authorLuke Mewburn <Luke@Mewburn.net>
Sat, 10 Jun 2023 10:44:52 +0000 (11:44 +0100)
committerMartin Matuška <martin@matuska.de>
Thu, 13 Jul 2023 22:12:05 +0000 (00:12 +0200)
Add error handling to the USE_READDIR_R code paths that set name_max
from struct statfs or statvfs; if the determined name_max == 0
then return an error.

Avoids a crash in tree_dir_next_posix() when the calculation of
dirent_size from name_max is too small for the memory allocated
for struct dirent.

This may fix Github issue #1149
This may fix NetBSD PR https://gnats.netbsd.org/56080

libarchive/archive_read_disk_posix.c

index 91f42b16838b8aedb3519e6a77da925a3e99d638..8de796fb830646aacf89f46be33101e75469d814 100644 (file)
@@ -1670,6 +1670,11 @@ setup_current_filesystem(struct archive_read_disk *a)
        else
                t->current_filesystem->name_max = nm;
 #endif
+       if (t->current_filesystem->name_max == 0) {
+               archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
+                   "Cannot determine name_max");
+               return (ARCHIVE_FAILED);
+       }
 #endif /* USE_READDIR_R */
        return (ARCHIVE_OK);
 }
@@ -1865,6 +1870,11 @@ setup_current_filesystem(struct archive_read_disk *a)
 #else
        t->current_filesystem->name_max = sfs.f_namelen;
 #endif
+       if (t->current_filesystem->name_max == 0) {
+               archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
+                   "Cannot determine name_max");
+               return (ARCHIVE_FAILED);
+       }
 #endif
        return (ARCHIVE_OK);
 }
@@ -1946,6 +1956,11 @@ setup_current_filesystem(struct archive_read_disk *a)
 #if defined(USE_READDIR_R)
        /* Set maximum filename length. */
        t->current_filesystem->name_max = svfs.f_namemax;
+       if (t->current_filesystem->name_max == 0) {
+               archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
+                   "Cannot determine name_max");
+               return (ARCHIVE_FAILED);
+       }
 #endif
        return (ARCHIVE_OK);
 }
@@ -2000,6 +2015,11 @@ setup_current_filesystem(struct archive_read_disk *a)
        else
                t->current_filesystem->name_max = nm;
 #  endif /* _PC_NAME_MAX */
+       if (t->current_filesystem->name_max == 0) {
+               archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
+                   "Cannot determine name_max");
+               return (ARCHIVE_FAILED);
+       }
 #endif /* USE_READDIR_R */
        return (ARCHIVE_OK);
 }