new file mode 100644
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * include/linux/initramfs.h
+ *
+ * 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*/
@@ -10,6 +10,9 @@
#include <linux/syscalls.h>
#include <linux/utime.h>
#include <linux/file.h>
+#include <linux/namei.h>
+#include <linux/xattr.h>
+#include <linux/initramfs.h>
static ssize_t __init xwrite(int fd, const char *p, size_t count)
{
@@ -146,7 +149,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;
@@ -218,7 +221,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 *header_buf, *symlink_buf, *name_buf, *metadata_buf;
static int __init do_start(void)
{
@@ -315,6 +318,87 @@ 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;
+ size_t 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;
+ }
+
+ xattr_value = xattr_name + xattr_name_size + 1;
+ xattr_value_size = buf + size - xattr_value;
+
+ ret = kern_path(pathname, 0, &path);
+ if (!ret) {
+ ret = vfs_setxattr(path.dentry, xattr_name, xattr_value,
+ xattr_value_size, 0);
+
+ path_put(&path);
+ }
+
+ pr_debug("%s: %s size: %lu val: %s (ret: %d)\n", pathname,
+ xattr_name, xattr_value_size, xattr_value, ret);
+
+ 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];
+ size_t entry_size;
+
+ if (!metadata_len)
+ return 0;
+
+ str[sizeof(hdr->c_size)] = 0;
+
+ while (buf < bufend) {
+ int ret;
+
+ if (buf + sizeof(*hdr) > bufend) {
+ 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 = kstrtoul(str, 16, &entry_size);
+ if (ret || buf + entry_size > bufend || !entry_size) {
+ 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 int wfd;
static int __init do_name(void)