Skip to content

Commit 96aebaf

Browse files
jjuhlmichal42
authored andcommitted
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>
1 parent 731ece4 commit 96aebaf

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

‎usr/gen_init_cpio.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,18 @@ static int cpio_mkfile(const char *name, const char *location,
309309

310310
mode |= S_IFREG;
311311

312-
retval = stat (location, &buf);
313-
if (retval) {
314-
fprintf (stderr, "File %s could not be located\n", location);
315-
goto error;
316-
}
317-
318312
file = open (location, O_RDONLY);
319313
if (file < 0) {
320314
fprintf (stderr, "File %s could not be opened for reading\n", location);
321315
goto error;
322316
}
323317

318+
retval = fstat (file, &buf);
319+
if (retval) {
320+
fprintf (stderr, "File %s could not be stat()'ed\n", location);
321+
goto error;
322+
}
323+
324324
filebuf = malloc(buf.st_size);
325325
if (!filebuf) {
326326
fprintf (stderr, "out of memory\n");

0 commit comments

Comments
 (0)