mcexec/execve: fix shebangs handling

There were mainly two problems with shebangs:
 - Suffix arguments handling e.g. '#!/bin/sh -x'
 - Recursive handling e.g. script1 fetchs '#!/path/to/script2'
and script2 itself has a shebang
 - (did I say two?) running shebang would replace argv[optind] instead
of appending e.g. script with '#!/bin/sh' and running './script -c'
would run '/bin/sh -c' instead of '/bin/sh ./script -c'

There also are two places where this needs parsing:
 - starting a fresh program from mcexec
 - starting a new program from execve in mcexec

The first was easy to fix as we already had argv around, but the later
required a new way to transfer the 'new argv elements from the script'
to mckernel to append before its argv -- it used to be 'desc->shell_path'
but that was no longer used at some point and just one keyword is not
enough to handle this properly.

This commit does:
 - Refactors the lookup_path + load_elf_desc that was only done at most
twice in its own function that loops indefinitely and use that in both
situations described above
 - Transmits the argv addition in the transfer to mckernel after the
desc; mckernel allocates 4 pages (hardcoded) for the descs and we will
hopefully have room for the script arguments on top of that... (there is
no guard!!!)
 - Change flatten_strings to allow prepending a flattened string instead
of a single string.
Note that the flatten_string change also brought in a difference in the
format, to have the full length embedded within the string, the latest
slot that used to be zeroes now contains the position of the end of the
buffer (where the last+1 string would be if there had been one)
This required a trivial change in mckernel prepare args function that
used this property for no real reason.

Hopefully things work™, this probably warrants adding a couple of new
ostests...
 - create a couple of scripts with recursive invocation/arguments and
check their own argv.
 - execute "mcexec script args" and "mcexec sh -c 'script args'"

Change-Id: I2cf9cde5c07c9293f730de89c9731bd93dbfa789
Refs: #1115
This commit is contained in:
Dominique Martinet
2018-08-24 18:03:25 +09:00
parent 1226e692d9
commit b1681f4a3a
7 changed files with 263 additions and 246 deletions

View File

@@ -47,7 +47,6 @@ extern int sscanf(const char * buf, const char * fmt, ...);
extern int scnprintf(char * buf, size_t size, const char *fmt, ...);
unsigned long strtol(const char *cp, char **endp, unsigned int base);
int flatten_strings(int nr_strings, char *first, char **strings, char **flat);
int flatten_strings_from_user(int nr_strings, char *first, char **strings, char **flat);
int flatten_strings_from_user(char *pre_strings, char **strings, char **flat);
#endif

View File

@@ -219,79 +219,30 @@ int memcmp(const void *s1, const void *s2, size_t n)
/*
* Flatten out a (char **) string array into the following format:
* [nr_strings][char *offset of string_0]...[char *offset of string_n-1][NULL][string0]...[stringn_1]
* if nr_strings == -1, we assume the last item is NULL
* [nr_strings][char *offset of string_0]...[char *offset of string_n-1][char *offset of end of string][string0]...[stringn_1]
*
* sizes all are longs.
*
* NOTE: copy this string somewhere, add the address of the string to each offset
* and we get back a valid argv or envp array.
*
* pre_strings is already flattened, so we just need to manage counts and copy
* the string parts appropriately.
*
* returns the total length of the flat string and updates flat to
* point to the beginning.
*/
int flatten_strings(int nr_strings, char *first, char **strings, char **flat)
int flatten_strings_from_user(char *pre_strings, char **strings, char **flat)
{
int full_len, string_i;
unsigned long flat_offset;
char *_flat;
/* How many strings do we have? */
if (nr_strings == -1) {
for (nr_strings = 0; strings[nr_strings]; ++nr_strings);
}
/* Count full length */
full_len = sizeof(long) + sizeof(char *); // Counter and terminating NULL
if (first) {
full_len += sizeof(char *) + strlen(first) + 1;
}
for (string_i = 0; string_i < nr_strings; ++string_i) {
// Pointer + actual value
full_len += sizeof(char *) + strlen(strings[string_i]) + 1;
}
full_len = (full_len + sizeof(long) - 1) & ~(sizeof(long) - 1);
_flat = (char *)kmalloc(full_len, IHK_MC_AP_NOWAIT);
if (!_flat) {
return 0;
}
memset(_flat, 0, full_len);
/* Number of strings */
*((long *)_flat) = nr_strings + (first ? 1 : 0);
// Actual offset
flat_offset = sizeof(long) + sizeof(char *) * (nr_strings + 1 +
(first ? 1 : 0));
if (first) {
*((char **)(_flat + sizeof(long))) = (void *)flat_offset;
memcpy(_flat + flat_offset, first, strlen(first) + 1);
flat_offset += strlen(first) + 1;
}
for (string_i = 0; string_i < nr_strings; ++string_i) {
/* Fabricate the string */
*((char **)(_flat + sizeof(long) + (string_i + (first ? 1 : 0))
* sizeof(char *))) = (void *)flat_offset;
memcpy(_flat + flat_offset, strings[string_i], strlen(strings[string_i]) + 1);
flat_offset += strlen(strings[string_i]) + 1;
}
*flat = _flat;
return full_len;
}
int flatten_strings_from_user(int nr_strings, char *first, char **strings, char **flat)
{
int full_len, string_i;
int full_len, i;
int nr_strings = 0;
int pre_strings_count = 0;
int pre_strings_len = 0;
long *_flat;
long *pre_strings_flat;
char *p;
long r;
int n, ret;
int ret;
/* When strings is NULL, make array one NULL */
if (!strings) {
@@ -306,35 +257,34 @@ int flatten_strings_from_user(int nr_strings, char *first, char **strings, char
}
/* How many strings do we have? */
if (nr_strings == -1) {
nr_strings = 0;
for (;;) {
ret = getlong_user(&r, (void *)(strings + nr_strings));
if (ret < 0)
return ret;
for (;;) {
ret = getlong_user(&r, (void *)(strings + nr_strings));
if (ret < 0)
return ret;
if (r == 0)
break;
if (r == 0)
break;
++nr_strings;
}
++nr_strings;
}
/* Count full length */
full_len = sizeof(long) + sizeof(char *); // Counter and terminating NULL
if (first) {
int len = strlen(first);
if (pre_strings) {
pre_strings_flat = (long *)pre_strings;
pre_strings_count = pre_strings_flat[0];
if(len < 0)
return len;
full_len += sizeof(char *) + len + 1;
pre_strings_len = pre_strings_flat[pre_strings_count + 1];
pre_strings_len -= sizeof(long) * (pre_strings_count + 2);
full_len += pre_strings_count * sizeof(long) + pre_strings_len;
}
for (string_i = 0; string_i < nr_strings; ++string_i) {
for (i = 0; i < nr_strings; ++i) {
char *userp;
int len;
ret = getlong_user((long *)&userp, (void *)(strings + string_i));
ret = getlong_user((long *)&userp, (void *)(strings + i));
if (ret < 0)
return ret;
@@ -354,31 +304,33 @@ int flatten_strings_from_user(int nr_strings, char *first, char **strings, char
}
/* Number of strings */
n = first? 1: 0;
_flat[0] = nr_strings + n;
// Actual offset
p = (char *)(_flat + nr_strings + 2 + n);
_flat[0] = nr_strings + pre_strings_count;
n = 1;
if (first) {
_flat[n++] = p - (char *)_flat;
strcpy(p, first);
p = strchr(p, '\0') + 1;
// Actual offset
p = (char *)(_flat + nr_strings + pre_strings_count + 2);
if (pre_strings) {
for (i = 0; i < pre_strings_count; i++) {
_flat[i + 1] = pre_strings_flat[i + 1] +
nr_strings * sizeof(long);
}
memcpy(p, pre_strings + pre_strings_flat[1],
pre_strings_len);
p += pre_strings_len;
}
for (string_i = 0; string_i < nr_strings; ++string_i) {
for (i = 0; i < nr_strings; ++i) {
char *userp;
_flat[n++] = p - (char *)_flat;
_flat[i + pre_strings_count + 1] = p - (char *)_flat;
ret = getlong_user((long *)&userp, (void *)(strings + string_i));
ret = getlong_user((long *)&userp, (void *)(strings + i));
if (ret < 0)
return ret;
strcpy_from_user(p, userp);
p = strchr(p, '\0') + 1;
}
_flat[n] = 0;
_flat[nr_strings + pre_strings_count + 1] = p - (char *)_flat;
*flat = (char *)_flat;
return full_len;