]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lslocks: fix buffer overflow
authorKarel Zak <kzak@redhat.com>
Thu, 29 Feb 2024 19:43:35 +0000 (20:43 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 29 Feb 2024 19:43:35 +0000 (20:43 +0100)
* don't use memset() to init variables
* use xreaddir() to reduce code
* use ssize_t for readlinkat() return value to avoid buffer overflow

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/lslocks.c

index 2a88440b4468ed710334f90745531eab350e3b3a..3d70b047f3e79a639e356a370c1a8b7c47a31ec6 100644 (file)
@@ -48,6 +48,7 @@
 #include "optutils.h"
 #include "procfs.h"
 #include "column-list-table.h"
+#include "fileutils.h"
 
 /* column IDs */
 enum {
@@ -220,13 +221,12 @@ static char *get_filename_sz(ino_t inode, pid_t lock_pid, size_t *size)
        struct stat sb;
        struct dirent *dp;
        DIR *dirp;
-       size_t len;
+       size_t sz;
        int fd;
-       char path[PATH_MAX], sym[PATH_MAX], *ret = NULL;
+       char path[PATH_MAX] = { 0 },
+            sym[PATH_MAX] = { 0 }, *ret = NULL;
 
        *size = 0;
-       memset(path, 0, sizeof(path));
-       memset(sym, 0, sizeof(sym));
 
        if (lock_pid < 0)
                /* pid could be -1 for OFD locks */
@@ -241,16 +241,14 @@ static char *get_filename_sz(ino_t inode, pid_t lock_pid, size_t *size)
        if (!(dirp = opendir(path)))
                return NULL;
 
-       if ((len = strlen(path)) >= (sizeof(path) - 2))
+       if ((sz = strlen(path)) >= (sizeof(path) - 2))
                goto out;
 
        if ((fd = dirfd(dirp)) < 0 )
                goto out;
 
-       while ((dp = readdir(dirp))) {
-               if (!strcmp(dp->d_name, ".") ||
-                   !strcmp(dp->d_name, ".."))
-                       continue;
+       while ((dp = xreaddir(dirp))) {
+               ssize_t len;
 
                errno = 0;