Merge tag 'mm-nonmm-stable-2022-05-26' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / init / initramfs.c
index 2f3d96d..dc84cf7 100644 (file)
 #include <linux/init_syscalls.h>
 #include <linux/umh.h>
 
-static ssize_t __init xwrite(struct file *file, const char *p, size_t count,
-               loff_t *pos)
+static __initdata bool csum_present;
+static __initdata u32 io_csum;
+
+static ssize_t __init xwrite(struct file *file, const unsigned char *p,
+               size_t count, loff_t *pos)
 {
        ssize_t out = 0;
 
@@ -33,6 +36,13 @@ static ssize_t __init xwrite(struct file *file, const char *p, size_t count,
                } else if (rv == 0)
                        break;
 
+               if (csum_present) {
+                       ssize_t i;
+
+                       for (i = 0; i < rv; i++)
+                               io_csum += p[i];
+               }
+
                p += rv;
                out += rv;
                count -= rv;
@@ -116,31 +126,36 @@ static void __init free_hash(void)
        }
 }
 
-static long __init do_utime(char *filename, time64_t mtime)
+#ifdef CONFIG_INITRAMFS_PRESERVE_MTIME
+static void __init do_utime(char *filename, time64_t mtime)
 {
-       struct timespec64 t[2];
+       struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } };
+       init_utimes(filename, t);
+}
 
-       t[0].tv_sec = mtime;
-       t[0].tv_nsec = 0;
-       t[1].tv_sec = mtime;
-       t[1].tv_nsec = 0;
-       return init_utimes(filename, t);
+static void __init do_utime_path(const struct path *path, time64_t mtime)
+{
+       struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } };
+       vfs_utimes(path, t);
 }
 
 static __initdata LIST_HEAD(dir_list);
 struct dir_entry {
        struct list_head list;
-       char *name;
        time64_t mtime;
+       char name[];
 };
 
 static void __init dir_add(const char *name, time64_t mtime)
 {
-       struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
+       size_t nlen = strlen(name) + 1;
+       struct dir_entry *de;
+
+       de = kmalloc(sizeof(struct dir_entry) + nlen, GFP_KERNEL);
        if (!de)
                panic_show_mem("can't allocate dir_entry buffer");
        INIT_LIST_HEAD(&de->list);
-       de->name = kstrdup(name, GFP_KERNEL);
+       strscpy(de->name, name, nlen);
        de->mtime = mtime;
        list_add(&de->list, &dir_list);
 }
@@ -151,10 +166,15 @@ static void __init dir_utime(void)
        list_for_each_entry_safe(de, tmp, &dir_list, list) {
                list_del(&de->list);
                do_utime(de->name, de->mtime);
-               kfree(de->name);
                kfree(de);
        }
 }
+#else
+static void __init do_utime(char *filename, time64_t mtime) {}
+static void __init do_utime_path(const struct path *path, time64_t mtime) {}
+static void __init dir_add(const char *name, time64_t mtime) {}
+static void __init dir_utime(void) {}
+#endif
 
 static __initdata time64_t mtime;
 
@@ -166,15 +186,16 @@ static __initdata unsigned long body_len, name_len;
 static __initdata uid_t uid;
 static __initdata gid_t gid;
 static __initdata unsigned rdev;
+static __initdata u32 hdr_csum;
 
 static void __init parse_header(char *s)
 {
-       unsigned long parsed[12];
+       unsigned long parsed[13];
        char buf[9];
        int i;
 
        buf[8] = '\0';
-       for (i = 0, s += 6; i < 12; i++, s += 8) {
+       for (i = 0, s += 6; i < 13; i++, s += 8) {
                memcpy(buf, s, 8);
                parsed[i] = simple_strtoul(buf, NULL, 16);
        }
@@ -189,6 +210,7 @@ static void __init parse_header(char *s)
        minor = parsed[8];
        rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
        name_len = parsed[11];
+       hdr_csum = parsed[12];
 }
 
 /* FSM */
@@ -257,12 +279,15 @@ static int __init do_collect(void)
 
 static int __init do_header(void)
 {
-       if (memcmp(collected, "070707", 6)==0) {
-               error("incorrect cpio method used: use -H newc option");
-               return 1;
-       }
-       if (memcmp(collected, "070701", 6)) {
-               error("no cpio magic");
+       if (!memcmp(collected, "070701", 6)) {
+               csum_present = false;
+       } else if (!memcmp(collected, "070702", 6)) {
+               csum_present = true;
+       } else {
+               if (memcmp(collected, "070707", 6) == 0)
+                       error("incorrect cpio method used: use -H newc option");
+               else
+                       error("no cpio magic");
                return 1;
        }
        parse_header(collected);
@@ -353,6 +378,7 @@ static int __init do_name(void)
                        if (IS_ERR(wfile))
                                return 0;
                        wfile_pos = 0;
+                       io_csum = 0;
 
                        vfs_fchown(wfile, uid, gid);
                        vfs_fchmod(wfile, mode);
@@ -380,15 +406,13 @@ static int __init do_name(void)
 static int __init do_copy(void)
 {
        if (byte_count >= body_len) {
-               struct timespec64 t[2] = { };
                if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
                        error("write error");
 
-               t[0].tv_sec = mtime;
-               t[1].tv_sec = mtime;
-               vfs_utimes(&wfile->f_path, t);
-
+               do_utime_path(&wfile->f_path, mtime);
                fput(wfile);
+               if (csum_present && io_csum != hdr_csum)
+                       error("bad data checksum");
                eat(body_len);
                state = SkipIt;
                return 0;