Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <jj@chaosbits.net>
Acked-by: Jeff Garzik <jgarzik@redhat.com>
Signed-off-by: Michal Marek <mmarek@suse.cz>

authored by

Jesper Juhl and committed by
Michal Marek
96aebafa 731ece41

+6 -6
+6 -6
usr/gen_init_cpio.c
··· 309 309 310 310 mode |= S_IFREG; 311 311 312 - retval = stat (location, &buf); 313 - if (retval) { 314 - fprintf (stderr, "File %s could not be located\n", location); 315 - goto error; 316 - } 317 - 318 312 file = open (location, O_RDONLY); 319 313 if (file < 0) { 320 314 fprintf (stderr, "File %s could not be opened for reading\n", location); 315 + goto error; 316 + } 317 + 318 + retval = fstat (file, &buf); 319 + if (retval) { 320 + fprintf (stderr, "File %s could not be stat()'ed\n", location); 321 321 goto error; 322 322 } 323 323