From: Jesper Juhl Date: Fri, 24 Dec 2010 20:28:56 +0000 (+0100) Subject: gen_init_cpio: Avoid race between call to stat() and call to open() X-Git-Tag: firefly_0821_release~7613^2~3086^2~2 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=96aebafa63418f447ddc823e40da341cc40553dd;p=firefly-linux-kernel-4.4.55.git gen_init_cpio: Avoid race between call to stat() and call to open() In usr/gen_init_cpio.c::cpio_mkfile() a call to stat() is made based on pathname, subsequently the file is open()'ed and then the value of the initial stat() call is used to allocate a buffer. This is not safe since the file may change between the call to stat() and the call to open(). Safer to just open() the file and then do fstat() using the filedescriptor returned by open. Signed-off-by: Jesper Juhl Acked-by: Jeff Garzik Signed-off-by: Michal Marek --- diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c index 59df70d9d1dc..f463cafdccb2 100644 --- a/usr/gen_init_cpio.c +++ b/usr/gen_init_cpio.c @@ -309,18 +309,18 @@ static int cpio_mkfile(const char *name, const char *location, mode |= S_IFREG; - retval = stat (location, &buf); - if (retval) { - fprintf (stderr, "File %s could not be located\n", location); - goto error; - } - file = open (location, O_RDONLY); if (file < 0) { fprintf (stderr, "File %s could not be opened for reading\n", location); goto error; } + retval = fstat (file, &buf); + if (retval) { + fprintf (stderr, "File %s could not be stat()'ed\n", location); + goto error; + } + filebuf = malloc(buf.st_size); if (!filebuf) { fprintf (stderr, "out of memory\n");