Skip to content

Commit

Permalink
gen_init_cpio: Avoid race between call to stat() and call to open()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Acked-by: Jeff Garzik <[email protected]>
Signed-off-by: Michal Marek <[email protected]>
  • Loading branch information
jjuhl authored and michal42 committed Dec 29, 2010
1 parent 731ece4 commit 96aebaf
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions usr/gen_init_cpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 96aebaf

Please sign in to comment.