A few more OpenBSD fixes

Namely:

 o use malloc where an xCalloc slipped in

 o safeguard against an empty arg list - I don't think it's possible,
   but it would be potentially exploitable

 o we need to initialize the arg string to an empty string because we no
 longer use strlcpy(3)

 o annotate a tricky use of strlcpy(3)'s truncation
This commit is contained in:
Michael McConville 2016-03-05 23:38:12 -05:00
parent b08cb7352e
commit 4b780a3499
1 changed files with 7 additions and 2 deletions

View File

@ -161,7 +161,7 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
* (argv[0]) if we fail to construct the full command. * (argv[0]) if we fail to construct the full command.
*/ */
arg = kvm_getargv(kd, kproc, 500); arg = kvm_getargv(kd, kproc, 500);
if (arg == NULL) { if (arg == NULL || *arg == NULL) {
*basenameEnd = strlen(kproc->p_comm); *basenameEnd = strlen(kproc->p_comm);
return xStrdup(kproc->p_comm); return xStrdup(kproc->p_comm);
} }
@ -169,18 +169,23 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
len += strlen(arg[i]) + 1; /* room for arg and trailing space or NUL */ len += strlen(arg[i]) + 1; /* room for arg and trailing space or NUL */
} }
/* don't use xMalloc here - we want to handle huge argv's gracefully */ /* don't use xMalloc here - we want to handle huge argv's gracefully */
if ((s = xCalloc(len, 1)) == NULL) { if ((s = malloc(len)) == NULL) {
*basenameEnd = strlen(kproc->p_comm); *basenameEnd = strlen(kproc->p_comm);
return xStrdup(kproc->p_comm); return xStrdup(kproc->p_comm);
} }
*s = '\0';
for (i = 0; arg[i] != NULL; i++) { for (i = 0; arg[i] != NULL; i++) {
n = strlcat(s, arg[i], len); n = strlcat(s, arg[i], len);
if (i == 0) { if (i == 0) {
/* TODO: rename all basenameEnd to basenameLen, make size_t */ /* TODO: rename all basenameEnd to basenameLen, make size_t */
*basenameEnd = MINIMUM(n, len-1); *basenameEnd = MINIMUM(n, len-1);
} }
/* the trailing space should get truncated anyway */
strlcat(s, " ", len); strlcat(s, " ", len);
} }
return s; return s;
} }