Rework the argv and env string fetching for execve to be more robust.

Before truss would fetch 100 string pointers and happily walk off the end
of the array if it never found a NULL.  This also means for a short argv
list it could fail entirely if the 100 string pointers spanned into an
unmapped page.

Instead, fetch page-aligned blocks of string pointers in a loop fetching
each string until a NULL is found.

While here, make use of the open memstream file descriptor instead of
allocating a temporary array.  This allows us to fetch each string once
instead of twice.
This commit is contained in:
jhb 2015-08-20 14:33:30 +00:00
parent ca3ff2693c
commit edef59ae52

View File

@ -913,36 +913,54 @@ print_arg(struct syscall_args *sc, unsigned long *args, long retval,
break; break;
} }
case StringArray: { case StringArray: {
int num, size, i; uintptr_t addr;
char *tmp2; union {
char *strarray[0];
char buf[PAGE_SIZE];
} u;
char *string; char *string;
char *strarray[100]; /* XXX This is ugly. */ size_t len;
int first, i;
if (get_struct(pid, (void *)args[sc->offset], /*
(void *)&strarray, sizeof(strarray)) == -1) * Read a page of pointers at a time. Punt if the top-level
err(1, "get_struct %p", (void *)args[sc->offset]); * pointer is not aligned. Note that the first read is of
num = 0; * a partial page.
size = 0; */
addr = args[sc->offset];
/* Find out how large of a buffer we'll need. */ if (addr % sizeof(char *) != 0) {
while (strarray[num] != NULL) { fprintf(fp, "0x%lx", args[sc->offset]);
string = get_string(pid, (void*)strarray[num], 0); break;
size += strlen(string);
free(string);
num++;
} }
size += 4 + (num * 4);
tmp = (char *)malloc(size);
tmp2 = tmp;
tmp2 += sprintf(tmp2, " ["); len = PAGE_SIZE - (addr & PAGE_MASK);
for (i = 0; i < num; i++) { if (get_struct(pid, (void *)addr, u.buf, len) == -1) {
string = get_string(pid, (void*)strarray[i], 0); fprintf(fp, "0x%lx", args[sc->offset]);
tmp2 += sprintf(tmp2, " \"%s\"%c", string, break;
(i + 1 == num) ? ' ' : ',');
free(string);
} }
tmp2 += sprintf(tmp2, "]");
fputc('[', fp);
first = 1;
i = 0;
while (u.strarray[i] != NULL) {
string = get_string(pid, u.strarray[i], 0);
fprintf(fp, "%s \"%s\"", first ? "" : ",", string);
free(string);
first = 0;
i++;
if (i == len / sizeof(char *)) {
addr += len;
len = PAGE_SIZE;
if (get_struct(pid, (void *)addr, u.buf, len) ==
-1) {
fprintf(fp, ", <inval>");
break;
}
i = 0;
}
}
fputs(" ]", fp);
break; break;
} }
#ifdef __LP64__ #ifdef __LP64__