From: Craig Small Date: Sun, 20 May 2018 11:41:20 +0000 (+1000) Subject: fuser: path fixes and command name length X-Git-Tag: v23.2~10 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d9ac11f92fdc4e96c818ce9d56e28ef40a508336;p=psmisc fuser: path fixes and command name length Change some of the paths to use dynamically allocated memory, so no limit, or use the PATH_MAX macro. Command Name increases to 64 and try to use the COMM_LEN or the size of the holding buffer consistently. --- diff --git a/ChangeLog b/ChangeLog index 1627765..f7ffbf6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ Changes in 23.2 =============== + * misc: Command names increased from 16 to 64 characters * buildsys: signals.c depends on signames.h !16 * killall: look at all namespaces by default * killall: Fix -INT option parsing #11 diff --git a/src/fuser.c b/src/fuser.c index 31ef30c..c44cee8 100644 --- a/src/fuser.c +++ b/src/fuser.c @@ -60,6 +60,7 @@ #include "signals.h" #include "i18n.h" #include "timeout.h" +#include "comm.h" //#define DEBUG 1 @@ -396,13 +397,13 @@ add_matched_proc(struct names *name_list, const pid_t pid, const uid_t uid, if ((asprintf(&pathname, "/proc/%d/stat", pid) > 0) && ((fp = fopen(pathname, "r")) != NULL) && (fscanf(fp, "%*d (%100[^)]", cmdname) == 1)) - if ((pptr->command = (char *)malloc(MAX_CMDNAME + 1)) != NULL) { + if ((pptr->command = (char *)malloc(COMM_LEN + 1)) != NULL) { cmdlen = 0; - for (cptr = cmdname; cmdlen < MAX_CMDNAME && *cptr; + for (cptr = cmdname; cmdlen < COMM_LEN && *cptr; cptr++) { if (isprint(*cptr)) pptr->command[cmdlen++] = *cptr; - else if (cmdlen < (MAX_CMDNAME - 4)) + else if (cmdlen < (COMM_LEN - 4)) cmdlen += sprintf(&(pptr->command[cmdlen]), "\\%03o", *cptr); @@ -1551,17 +1552,22 @@ check_dir(const pid_t pid, const char *dirname, struct device_list *dev_head, struct device_list *dev_tmp; struct unixsocket_list *sock_tmp; struct stat st, lst; - char dirpath[MAX_PATHNAME]; - char filepath[MAX_PATHNAME]; + char *dirpath; + char filepath[PATH_MAX]; - snprintf(dirpath, MAX_PATHNAME, "/proc/%d/%s", pid, dirname); - if ((dirp = opendir(dirpath)) == NULL) + if (asprintf(&dirpath, "/proc/%d/%s", pid, dirname) < 0) + return; + if ((dirp = opendir(dirpath)) == NULL) { + free(dirpath); return; + } + free(dirpath); + while ((direntry = readdir(dirp)) != NULL) { if (direntry->d_name[0] < '0' || direntry->d_name[0] > '9') continue; - snprintf(filepath, MAX_PATHNAME, "/proc/%d/%s/%s", + snprintf(filepath, sizeof filepath - 1, "/proc/%d/%s/%s", pid, dirname, direntry->d_name); if (timeout(thestat, filepath, &st, 5) != 0) { @@ -1634,7 +1640,7 @@ check_map(const pid_t pid, const char *filename, struct device_list *dev_head, struct inode_list *ino_head, const uid_t uid, const char access) { - char pathname[MAX_PATHNAME]; + char *pathname; char line[BUFSIZ]; struct inode_list *ino_tmp; struct device_list *dev_tmp; @@ -1643,9 +1649,13 @@ check_map(const pid_t pid, const char *filename, unsigned int tmp_maj, tmp_min; dev_t tmp_device; - snprintf(pathname, MAX_PATHNAME, "/proc/%d/%s", pid, filename); - if ((fp = fopen(pathname, "r")) == NULL) + if (asprintf(&pathname, "/proc/%d/%s", pid, filename) < 0) + return; + if ((fp = fopen(pathname, "r")) == NULL) { + free(pathname); return; + } + free(pathname); while (fgets(line, BUFSIZ, fp)) { if (sscanf(line, "%*s %*s %*s %x:%x %lld", &tmp_maj, &tmp_min, &tmp_inode) == 3) { @@ -1668,13 +1678,16 @@ check_map(const pid_t pid, const char *filename, static uid_t getpiduid(const pid_t pid) { - char pathname[MAX_PATHNAME]; + char *pathname; struct stat st; - if (snprintf(pathname, MAX_PATHNAME, "/proc/%d", pid) < 0) + if (asprintf(&pathname, "/proc/%d", pid) < 0) return 0; - if (timeout(thestat, pathname, &st, 5) != 0) + if (timeout(thestat, pathname, &st, 5) != 0) { + free(pathname); return 0; + } + free(pathname); return st.st_uid; } @@ -2007,7 +2020,7 @@ static void clear_mntinfo(void) static void init_mntinfo(void) { - char mpoint[PATH_MAX*4 + 1]; // octal escaping takes 4 chars per 1 char + char mpoint[PATH_MAX *4 + 1]; // octal escaping takes 4 chars per 1 char int mid, parid, max = 0; uint maj, min; list_t sort; diff --git a/src/fuser.h b/src/fuser.h index a4df711..93020d5 100644 --- a/src/fuser.h +++ b/src/fuser.h @@ -104,8 +104,9 @@ typedef struct mntinfo_s { #define NAMESPACE_TCP 1 #define NAMESPACE_UDP 2 -#define MAX_PATHNAME 200 -#define MAX_CMDNAME 16 +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif /* PATH_MAX */ #define KNFSD_EXPORTS "/proc/fs/nfs/exports" #define PROC_MOUNTS "/proc/mounts"