From 43f901fbc8ba94bfa8d58155ba9378d7a13af636 Mon Sep 17 00:00:00 2001 From: Thomas Chou Date: Wed, 6 Oct 2010 15:13:53 +0800 Subject: gen_init_cpio: remove leading `/' from file names When we extracted the generated cpio archive using "cpio -id" command, it complained, cpio: Removing leading `/' from member names var/run cpio: Removing leading `/' from member names var/lib cpio: Removing leading `/' from member names var/lib/misc It is worse with the latest "cpio" or "pax", which tries to overwrite the host file system with the leading '/'. So the leading '/' of file names should be removed. This is consistent with the initramfs come with major distributions such as Fedora or Debian, etc. Signed-off-by: Thomas Chou Acked-by: Mike Frysinger Signed-off-by: Michal Marek --- usr/gen_init_cpio.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'usr') diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c index b2b3c2d..59df70d 100644 --- a/usr/gen_init_cpio.c +++ b/usr/gen_init_cpio.c @@ -104,6 +104,8 @@ static int cpio_mkslink(const char *name, const char *target, char s[256]; time_t mtime = time(NULL); + if (name[0] == '/') + name++; sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", "070701", /* magic */ @@ -152,6 +154,8 @@ static int cpio_mkgeneric(const char *name, unsigned int mode, char s[256]; time_t mtime = time(NULL); + if (name[0] == '/') + name++; sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", "070701", /* magic */ @@ -245,6 +249,8 @@ static int cpio_mknod(const char *name, unsigned int mode, else mode |= S_IFCHR; + if (name[0] == '/') + name++; sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", "070701", /* magic */ @@ -332,6 +338,8 @@ static int cpio_mkfile(const char *name, const char *location, /* data goes on last link */ if (i == nlinks) size = buf.st_size; + if (name[0] == '/') + name++; namesize = strlen(name) + 1; sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" "%08lX%08X%08X%08X%08X%08X%08X", -- cgit v1.1 From 96aebafa63418f447ddc823e40da341cc40553dd Mon Sep 17 00:00:00 2001 From: Jesper Juhl Date: Fri, 24 Dec 2010 21:28:56 +0100 Subject: 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 --- usr/gen_init_cpio.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'usr') diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c index 59df70d..f463caf 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"); -- cgit v1.1 From a3c888fcda911fcb6e3c071aecf49ccb6effe79d Mon Sep 17 00:00:00 2001 From: Andrew Morton Date: Wed, 5 Jan 2011 23:49:53 +0100 Subject: gen_init_cpio: checkpatch fixes Cc: Jesper Juhl Cc: Michal Marek Signed-off-by: Andrew Morton Signed-off-by: Michal Marek --- usr/gen_init_cpio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'usr') diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c index f463caf..7f06884 100644 --- a/usr/gen_init_cpio.c +++ b/usr/gen_init_cpio.c @@ -315,9 +315,9 @@ static int cpio_mkfile(const char *name, const char *location, goto error; } - retval = fstat (file, &buf); + retval = fstat(file, &buf); if (retval) { - fprintf (stderr, "File %s could not be stat()'ed\n", location); + fprintf(stderr, "File %s could not be stat()'ed\n", location); goto error; } -- cgit v1.1