Commit c725ee54 authored by Michal Nazarewicz's avatar Michal Nazarewicz Committed by Linus Torvalds

gen_init_cpio: avoid NULL pointer dereference and rework env expanding

getenv() may return NULL if given environment variable does not exist
which leads to NULL dereference when calling strncat.

Besides that, the environment variable name was copied to a temporary
env_var buffer, but this copying can be avoided by simply using the input
string.

Lastly, the whole loop can be greatly simplified by using the snprintf
function instead of the playing with strncat.

 By the way, the current implementation allows a recursive variable
 expansion, as in:

   $ echo 'out ${A} out ' | A='a ${B} a' B=b /tmp/a
   out a b a out

 I'm assuming this is just a side effect and not a conscious decision
 (especially as this may lead to infinite loop), but I didn't want to
 change this behaviour without consulting.

 If the current behaviour is deamed incorrect, I'll be happy to send
 a patch without recursive processing.
Signed-off-by: default avatarMichal Nazarewicz <mina86@mina86.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: Jesper Juhl <jj@codesealer.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 0ca43435
...@@ -382,24 +382,15 @@ static int cpio_mkfile(const char *name, const char *location, ...@@ -382,24 +382,15 @@ static int cpio_mkfile(const char *name, const char *location,
static char *cpio_replace_env(char *new_location) static char *cpio_replace_env(char *new_location)
{ {
char expanded[PATH_MAX + 1]; char expanded[PATH_MAX + 1];
char env_var[PATH_MAX + 1]; char *start, *end, *var;
char *start;
char *end; while ((start = strstr(new_location, "${")) &&
(end = strchr(start + 2, '}'))) {
for (start = NULL; (start = strstr(new_location, "${")); ) { *start = *end = 0;
end = strchr(start, '}'); var = getenv(start + 2);
if (start < end) { snprintf(expanded, sizeof expanded, "%s%s%s",
*env_var = *expanded = '\0'; new_location, var ? var : "", end + 1);
strncat(env_var, start + 2, end - start - 2); strcpy(new_location, expanded);
strncat(expanded, new_location, start - new_location);
strncat(expanded, getenv(env_var),
PATH_MAX - strlen(expanded));
strncat(expanded, end + 1,
PATH_MAX - strlen(expanded));
strncpy(new_location, expanded, PATH_MAX);
new_location[PATH_MAX] = 0;
} else
break;
} }
return new_location; return new_location;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment