From: Karel Zak Date: Thu, 29 Feb 2024 19:43:35 +0000 (+0100) Subject: lslocks: fix buffer overflow X-Git-Tag: v2.42-start~499 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fcb83efb61c7898fa2ef20e010bf760278ec6746;p=thirdparty%2Futil-linux.git lslocks: fix buffer overflow * 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 (cherry picked from commit f030775ffeaa8627c88434f7d0cba1a454aa0ffa) --- diff --git a/misc-utils/lslocks.c b/misc-utils/lslocks.c index 2a88440b4..3d70b047f 100644 --- a/misc-utils/lslocks.c +++ b/misc-utils/lslocks.c @@ -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;