* [PATCH] gen_init_cpio: Avoid race between call to stat() and call to open()
@ 2010-12-24 20:28 Jesper Juhl
2010-12-24 20:31 ` Jesper Juhl
2010-12-24 22:33 ` Jeff Garzik
0 siblings, 2 replies; 4+ messages in thread
From: Jesper Juhl @ 2010-12-24 20:28 UTC (permalink / raw)
To: linux-kernel; +Cc: jgarzik
Hi,
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>
---
gen_init_cpio.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c
index b2b3c2d..7cd4538 100644
--- a/usr/gen_init_cpio.c
+++ b/usr/gen_init_cpio.c
@@ -303,18 +303,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");
--
Jesper Juhl <jj@chaosbits.net> http://www.chaosbits.net/
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please.
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] gen_init_cpio: Avoid race between call to stat() and call to open()
2010-12-24 20:28 [PATCH] gen_init_cpio: Avoid race between call to stat() and call to open() Jesper Juhl
@ 2010-12-24 20:31 ` Jesper Juhl
2010-12-24 22:33 ` Jeff Garzik
1 sibling, 0 replies; 4+ messages in thread
From: Jesper Juhl @ 2010-12-24 20:31 UTC (permalink / raw)
To: linux-kernel; +Cc: jgarzik
Bad subject.
Better subject: Avoid risk of file changing between calls to stat() and open().
--
Jesper Juhl <jj@chaosbits.net> http://www.chaosbits.net/
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] gen_init_cpio: Avoid race between call to stat() and call to open()
2010-12-24 20:28 [PATCH] gen_init_cpio: Avoid race between call to stat() and call to open() Jesper Juhl
2010-12-24 20:31 ` Jesper Juhl
@ 2010-12-24 22:33 ` Jeff Garzik
2010-12-29 13:25 ` Michal Marek
1 sibling, 1 reply; 4+ messages in thread
From: Jeff Garzik @ 2010-12-24 22:33 UTC (permalink / raw)
To: Jesper Juhl; +Cc: linux-kernel, Andrew Morton
On 12/24/2010 03:28 PM, Jesper Juhl wrote:
> Hi,
>
> 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>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] gen_init_cpio: Avoid race between call to stat() and call to open()
2010-12-24 22:33 ` Jeff Garzik
@ 2010-12-29 13:25 ` Michal Marek
0 siblings, 0 replies; 4+ messages in thread
From: Michal Marek @ 2010-12-29 13:25 UTC (permalink / raw)
To: Jeff Garzik; +Cc: Jesper Juhl, linux-kernel, Andrew Morton
On Fri, Dec 24, 2010 at 05:33:48PM -0500, Jeff Garzik wrote:
> On 12/24/2010 03:28 PM, Jesper Juhl wrote:
> >Hi,
> >
> >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>
Looks like an academic issue to me, but the kernel should promote good
programming practices :). Applied to kbuild-2.6.git#kbuild, thanks.
Michal
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2010-12-29 13:25 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-24 20:28 [PATCH] gen_init_cpio: Avoid race between call to stat() and call to open() Jesper Juhl
2010-12-24 20:31 ` Jesper Juhl
2010-12-24 22:33 ` Jeff Garzik
2010-12-29 13:25 ` Michal Marek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox