From: Roberto Sassu Instead of changing the CPIO format, metadata are parsed from regular files with special name 'METADATA!!!'. This file immediately follows the file metadata are added to. This patch checks if the file being extracted has the special name and, if yes, creates a buffer with the content of that file and calls do_parse_metadata() to parse metadata from the buffer. Signed-off-by: Roberto Sassu Reported-by: kbuild test robot Signed-off-by: Tianxing Zhang Reviewed-by: Jason Yan Signed-off-by: Zheng Zengkai Signed-off-by: zhoushuiqing Signed-off-by: zhangguangzhi Signed-off-by: Danny Hu --- v5 changes: - clean up metadata buffer when returning from unpack_to_rootfs() init/initramfs.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/init/initramfs.c b/init/initramfs.c index 36254dba7822..a1744cc9523c 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -265,8 +265,8 @@ static void __init read_into(char *buf, unsigned size, enum state next) } } -static __initdata char *header_buf, *symlink_buf, *name_buf; -static __initdata char *metadata_buf; +static __initdata char *header_buf, *symlink_buf, *name_buf, *previous_name_buf; +static __initdata char *metadata_buf, *metadata_buf_ptr; static int __init do_start(void) { @@ -454,6 +454,7 @@ static int __init __maybe_unused do_parse_metadata(char *pathname) static __initdata struct file *wfile; static __initdata loff_t wfile_pos; +static __initdata int metadata; static int __init do_name(void) { @@ -471,6 +472,10 @@ static int __init do_name(void) if (strcmp(collected, "TRAILER!!!") == 0) { free_hash(); return 0; + } else if (strcmp(collected, METADATA_FILENAME) == 0) { + metadata = 1; + } else { + memcpy(previous_name_buf, collected, strlen(collected) + 1); } clean_path(collected, mode); if (S_ISREG(mode)) { @@ -508,11 +513,47 @@ static int __init do_name(void) return 0; } +static int __init do_process_metadata(char *buf, int len, bool last) +{ + int ret = 0; + + if (!metadata_buf) { + metadata_buf_ptr = metadata_buf = kmalloc(body_len, GFP_KERNEL); + if (!metadata_buf_ptr) { + ret = -ENOMEM; + goto out; + } + + metadata_len = body_len; + } + + if (metadata_buf_ptr + len > metadata_buf + metadata_len) { + ret = -EINVAL; + goto out; + } + + memcpy(metadata_buf_ptr, buf, len); + metadata_buf_ptr += len; + + if (last) + do_parse_metadata(previous_name_buf); +out: + if (ret < 0 || last) { + kfree(metadata_buf); + metadata_buf = NULL; + metadata = 0; + } + + return ret; +} + static int __init do_copy(void) { if (byte_count >= body_len) { if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len) error("write error"); + if (metadata) + do_process_metadata(victim, body_len, true); do_utime_path(&wfile->f_path, mtime); fput(wfile); @@ -524,6 +565,8 @@ static int __init do_copy(void) } else { if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count) error("write error"); + if (metadata) + do_process_metadata(victim, byte_count, false); body_len -= byte_count; eat(byte_count); return 1; @@ -539,6 +582,7 @@ static int __init do_symlink(void) return 1; } collected[N_ALIGN(name_len) + body_len] = '\0'; + memcpy(previous_name_buf, collected, strlen(collected) + 1); clean_path(collected, 0); init_symlink(collected + N_ALIGN(name_len), collected); init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW); @@ -614,6 +658,7 @@ char * __init unpack_to_rootfs(char *buf, unsigned long len) char header[CPIO_HDRLEN]; char symlink[PATH_MAX + N_ALIGN(PATH_MAX) + 1]; char name[N_ALIGN(PATH_MAX)]; + char prev_name[PATH_MAX + N_ALIGN(PATH_MAX) + 1]; } *bufs = kmalloc_obj(*bufs); if (!bufs) @@ -622,6 +667,7 @@ char * __init unpack_to_rootfs(char *buf, unsigned long len) header_buf = bufs->header; symlink_buf = bufs->symlink; name_buf = bufs->name; + previous_name_buf = bufs->prev_name; state = Start; this_header = 0; @@ -664,6 +710,9 @@ char * __init unpack_to_rootfs(char *buf, unsigned long len) dir_utime(); /* free any hardlink state collected without optional TRAILER!!! */ free_hash(); + kfree(metadata_buf); + metadata_buf = NULL; + metadata = 0; kfree(bufs); return message; } -- 2.51.0