From: Mimi Zohar <[email protected]>

This patch adds metadata to a file from a supplied buffer. The buffer might
contains multiple metadata records. The format of each record is:

<metadata len (ASCII, 8 chars)><version><type><metadata>

For now, only the TYPE_XATTR metadata type is supported. The specific
format of this metadata type is:

<xattr #N name>\0<xattr #N value>

[kamensky: fixed restoring of xattrs for symbolic links by using
           sys_lsetxattr() instead of sys_setxattr()]

[sassu: removed state management, kept only do_setxattrs(), added support
        for generic file metadata, replaced sys_lsetxattr() with
        vfs_setxattr(), added check for entry_size, added check for
        hdr->c_size, replaced strlen() with strnlen(); moved do_setxattrs()
        before do_name()]

[zhangguangzhi: fix checkpatch warning EMBEDDED_FILENAME in
                include/linux/initramfs.h]

Signed-off-by: Mimi Zohar <[email protected]>
Signed-off-by: Victor Kamensky <[email protected]>
Signed-off-by: Taras Kondratiuk <[email protected]>
Signed-off-by: Roberto Sassu <[email protected]>
Signed-off-by: Tianxing Zhang <[email protected]>
Reviewed-by: Jason Yan <[email protected]>
Signed-off-by: Zheng Zengkai <[email protected]>
Signed-off-by: zhoushuiqing <[email protected]>
Signed-off-by: zhangguangzhi <[email protected]>
Signed-off-by: Danny Hu <[email protected]>
---
Changes in v6:
 - Document lack of support for POSIX ACL restoration. The main use case is to
   restore security.ima xattr for IMA use. POSIX ACL support is out of scope
   for this patchset
 - Check xattr name and value lengths to ensure they are valid and do not
   exceed the limit
 - Compare metadata header and record sizes against the remaining buffer
   length to avoid pointer arithmetic overflow in bounds checks
 - Use the kernel u32 type instead of uint32_t

Changes in v5:
 - For debug print in do_setxattrs(), use print_hex_dump_debug() rather
   than printing the xattr value as a string
 - Use u32 metadata record lengths and reject records smaller than their
   header

 include/linux/initramfs.h |  19 +++++++
 init/initramfs.c          | 103 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 121 insertions(+), 1 deletion(-)
 create mode 100644 include/linux/initramfs.h

diff --git a/include/linux/initramfs.h b/include/linux/initramfs.h
new file mode 100644
index 000000000000..9b3839dc88a1
--- /dev/null
+++ b/include/linux/initramfs.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Include file for file metadata in the initial ram disk.
+ */
+#ifndef _LINUX_INITRAMFS_H
+#define _LINUX_INITRAMFS_H
+
+#define METADATA_FILENAME "METADATA!!!"
+
+enum metadata_types { TYPE_NONE, TYPE_XATTR, TYPE__LAST };
+
+struct metadata_hdr {
+       char c_size[8];     /* total size including c_size field */
+       char c_version;     /* header version */
+       char c_type;        /* metadata type */
+       char c_metadata[];  /* metadata */
+} __packed;
+
+#endif /*LINUX_INITRAMFS_H*/
diff --git a/init/initramfs.c b/init/initramfs.c
index ebddde9c8f0d..d206624d1160 100644
--- a/init/initramfs.c
+++ b/init/initramfs.c
@@ -10,7 +10,9 @@
 #include <linux/hex.h>
 #include <linux/init.h>
 #include <linux/init_syscalls.h>
+#include <linux/initramfs.h>
 #include <linux/kstrtox.h>
+#include <linux/limits.h>
 #include <linux/memblock.h>
 #include <linux/mm.h>
 #include <linux/namei.h>
@@ -22,6 +24,7 @@
 #include <linux/types.h>
 #include <linux/umh.h>
 #include <linux/utime.h>
+#include <linux/xattr.h>
 
 #include <asm/byteorder.h>
 
@@ -189,7 +192,7 @@ static __initdata time64_t mtime;
 
 static __initdata unsigned long ino, major, minor, nlink;
 static __initdata umode_t mode;
-static __initdata unsigned long body_len, name_len;
+static __initdata unsigned long body_len, name_len, metadata_len;
 static __initdata uid_t uid;
 static __initdata gid_t gid;
 static __initdata unsigned rdev;
@@ -264,6 +267,7 @@ 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 int __init do_start(void)
 {
@@ -365,6 +369,103 @@ static int __init maybe_link(void)
        return 0;
 }
 
+static int __init do_setxattrs(char *pathname, char *buf, size_t size)
+{
+       struct path path;
+       char *xattr_name, *xattr_value;
+       u32 xattr_name_size, xattr_value_size;
+       int ret;
+
+       xattr_name = buf;
+       xattr_name_size = strnlen(xattr_name, size);
+       if (xattr_name_size == size) {
+               error("malformed xattrs");
+               return -EINVAL;
+       }
+
+       if (!xattr_name_size || xattr_name_size > XATTR_NAME_MAX) {
+               error("invalid xattr name length");
+               return -EINVAL;
+       }
+
+       xattr_value = xattr_name + xattr_name_size + 1;
+       xattr_value_size = buf + size - xattr_value;
+       if (xattr_value_size > XATTR_SIZE_MAX) {
+               error("xattr value exceeds size limit");
+               return -EINVAL;
+       }
+
+       ret = kern_path(pathname, 0, &path);
+       if (!ret) {
+               /*
+                * POSIX ACL restoration is currently unsupported.
+                * vfs_setxattr() rejects POSIX ACL xattrs with -EOPNOTSUPP.
+                */
+               ret = vfs_setxattr(&nop_mnt_idmap, path.dentry, xattr_name, 
xattr_value,
+                                  xattr_value_size, 0);
+
+               path_put(&path);
+       }
+
+       pr_debug("%s: %s size: %u (ret: %d)\n", pathname, xattr_name,
+                xattr_value_size, ret);
+       print_hex_dump_debug("xattr value: ", DUMP_PREFIX_OFFSET,
+                            16, 1, xattr_value, xattr_value_size, false);
+
+       return ret;
+}
+
+static int __init __maybe_unused do_parse_metadata(char *pathname)
+{
+       char *buf = metadata_buf;
+       char *bufend = metadata_buf + metadata_len;
+       struct metadata_hdr *hdr;
+       char str[sizeof(hdr->c_size) + 1];
+       u32 entry_size;
+
+       if (!metadata_len)
+               return 0;
+
+       str[sizeof(hdr->c_size)] = 0;
+
+       while (buf < bufend) {
+               int ret;
+
+               if (bufend - buf < sizeof(*hdr)) {
+                       error("malformed metadata");
+                       break;
+               }
+
+               hdr = (struct metadata_hdr *)buf;
+               if (hdr->c_version != 1) {
+                       pr_debug("Unsupported header version\n");
+                       break;
+               }
+
+               memcpy(str, hdr->c_size, sizeof(hdr->c_size));
+               ret = kstrtou32(str, 16, &entry_size);
+               if (ret || entry_size < sizeof(*hdr) ||
+                   entry_size > bufend - buf) {
+                       error("malformed xattrs");
+                       break;
+               }
+
+               switch (hdr->c_type) {
+               case TYPE_XATTR:
+                       do_setxattrs(pathname, buf + sizeof(*hdr),
+                                    entry_size - sizeof(*hdr));
+                       break;
+               default:
+                       pr_debug("Unsupported metadata type\n");
+                       break;
+               }
+
+               buf += entry_size;
+       }
+
+       return 0;
+}
+
 static __initdata struct file *wfile;
 static __initdata loff_t wfile_pos;
 
-- 
2.51.0


Reply via email to