gen_init_cpio: avoid NULL pointer dereference and rework env expanding 46/219946/4
authorMichal Nazarewicz <mina86@mina86.com>
Tue, 12 Nov 2013 23:08:41 +0000 (15:08 -0800)
committerDongkyun Son <dongkyun.s@samsung.com>
Fri, 13 Dec 2019 11:04:28 +0000 (20:04 +0900)
commit8f0c8f9da0ca57c8083d9fd3a63c12763eb1e895
tree01487e75b3831acc193d9847b6f256babcc53a31
parent38e652dfe97eb57857ec2d56401a8b28d7da53a2
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: Michal 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: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
[dongkyun.s: cherry-pick mainline commit c725ee54c30b for gcc 9 build]
Signed-off-by: Dongkyun Son <dongkyun.s@samsung.com>
Change-Id: I16e6c2691b3e67651de4bee374c1fab87606337c
usr/gen_init_cpio.c