From: Mimi Zohar <zohar@linux.vnet.ibm.com>
To: initramfs <initramfs@vger.kernel.org>
Cc: Mimi Zohar <zohar@linux.vnet.ibm.com>,
Al Viro <viro@ZenIV.linux.org.uk>,
linux-ima-devel@lists.sourceforge.net,
linux-security-module <linux-security-module@vger.kernel.org>,
linux-kernel <linux-kernel@vger.kernel.org>
Subject: [RFC][PATCH 5/9] gen_init_cpio: include the file extended attributes
Date: Wed, 7 Jan 2015 15:52:56 -0500 [thread overview]
Message-ID: <1420663980-20842-6-git-send-email-zohar@linux.vnet.ibm.com> (raw)
In-Reply-To: <1420663980-20842-1-git-send-email-zohar@linux.vnet.ibm.com>
This patch reads the xattr(s), creating a buffer containing the
number of xattrs, the xattr(s) name, data size, and data. This
buffer size is included in the CPIO header. The buffer is written
out to the cpio file after the file name.
This patch also defines the '-x' option to enable the inclusion
of the xattrs.
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
---
usr/gen_init_cpio.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 100 insertions(+), 7 deletions(-)
diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c
index 0d9c6e8..08994d6 100644
--- a/usr/gen_init_cpio.c
+++ b/usr/gen_init_cpio.c
@@ -9,6 +9,7 @@
#include <errno.h>
#include <ctype.h>
#include <limits.h>
+#include <attr/xattr.h>
/*
* Original work by Jeff Garzik
@@ -36,6 +37,71 @@ struct file_handler {
int (*handler)(const char *line);
};
+#define MAX_XATTRNAMES_SIZE 500
+static char xattr_names[MAX_XATTRNAMES_SIZE];
+static char xattr_header[8]; /* number xattrs */
+static ssize_t xattr_nameslen;
+static unsigned int xattrs_buflen;
+
+static char xattr_buf[1000];
+static unsigned int get_xattrs(const char *name)
+{
+ char xattr_num[9];
+ char *xname, *buf, *bufend;
+ int xattrsize = 0, num_xattrs = 0;
+
+ xattr_nameslen = listxattr(name, NULL, 0);
+ if (xattr_nameslen <= 0 || xattr_nameslen > MAX_XATTRNAMES_SIZE)
+ return 0;
+
+ xattr_names[xattr_nameslen] = 0;
+ xattr_nameslen = listxattr(name, xattr_names, xattr_nameslen);
+ if (xattr_nameslen <= 0)
+ return 0;
+
+ /* xattr format: name value-len value */
+ buf = xattr_buf + sizeof xattr_header;
+ bufend = xattr_buf + sizeof xattr_buf;
+
+ for (xname = xattr_names; xname < (xattr_names + xattr_nameslen);
+ xname += strlen(xname) + 1) {
+ char sizebuf[9];
+ int offset;
+
+ /* skip security.evm as it is file system specific */
+ if (strcmp(xname, "security.evm") == 0)
+ continue;
+
+ offset = strlen(xname) + 1 + 8;
+ xattrsize = getxattr(name, xname, NULL, 0);
+ if (buf + offset + xattrsize > bufend) {
+ fprintf(stderr, "%s: xattrs too large \n", name);
+ return 0;
+ }
+
+ xattrsize = getxattr(name, xname, buf + offset,
+ bufend - (buf + offset));
+ if (xattrsize <= 0)
+ continue;
+
+ num_xattrs++;
+ fprintf(stderr, "%s: %s %x (%d)\n", name, xname, xattrsize,
+ num_xattrs);
+ strcpy(buf, xname);
+ buf += strlen(xname) + 1;
+ sprintf(sizebuf, "%08X", (int)xattrsize);
+ memcpy(buf, sizebuf, 8);
+ buf += (8 + xattrsize);
+ }
+
+ *buf = 0;
+ buf++;
+ sprintf(xattr_num, "%08X", num_xattrs);
+ memcpy(xattr_buf, xattr_num, 8);
+
+ return buf - xattr_buf;
+}
+
static void push_string(const char *name)
{
unsigned int name_len = strlen(name) + 1;
@@ -106,11 +172,24 @@ static void cpio_trailer(void)
}
}
+static void include_xattrs(void)
+{
+ if (!xattrs_buflen)
+ return;
+
+ if (fwrite(xattr_buf, xattrs_buflen, 1, stdout) != 1)
+ fprintf(stderr, "writing xattrs failed\n");
+ offset += xattrs_buflen;
+
+ push_pad();
+}
+
static int cpio_mkslink(const char *name, const char *target,
unsigned int mode, uid_t uid, gid_t gid)
{
char s[256];
+ xattrs_buflen = newcx ? get_xattrs(name) : 0;
if (name[0] == '/')
name++;
sprintf(s, newcx ? newcxfmt : newcfmt,
@@ -127,13 +206,15 @@ static int cpio_mkslink(const char *name, const char *target,
0, /* rmajor */
0, /* rminor */
(unsigned)strlen(name) + 1,/* namesize */
- 0, /* xattrs-size */
+ xattrs_buflen, /* xattrs-size */
0); /* chksum */
push_hdr(s);
push_string(name);
push_pad();
push_string(target);
push_pad();
+ if (newcx)
+ include_xattrs();
return 0;
}
@@ -160,6 +241,7 @@ static int cpio_mkgeneric(const char *name, unsigned int mode,
{
char s[256];
+ xattrs_buflen = newcx ? get_xattrs(name) : 0;
if (name[0] == '/')
name++;
sprintf(s, newcx ? newcxfmt : newcfmt,
@@ -176,10 +258,12 @@ static int cpio_mkgeneric(const char *name, unsigned int mode,
0, /* rmajor */
0, /* rminor */
(unsigned)strlen(name) + 1,/* namesize */
- 0, /* xattrs-size */
+ xattrs_buflen, /* xattrs-size */
0); /* chksum */
push_hdr(s);
push_rest(name);
+ if (newcx)
+ include_xattrs();
return 0;
}
@@ -339,9 +423,14 @@ static int cpio_mkfile(const char *name, const char *location,
}
size = 0;
+ xattrs_buflen = 0;
for (i = 1; i <= nlinks; i++) {
/* data goes on last link */
- if (i == nlinks) size = buf.st_size;
+ if (i == nlinks) {
+ size = buf.st_size;
+ if (newcx)
+ xattrs_buflen = get_xattrs(location);
+ }
if (name[0] == '/')
name++;
@@ -360,12 +449,13 @@ static int cpio_mkfile(const char *name, const char *location,
0, /* rmajor */
0, /* rminor */
namesize, /* namesize */
- 0, /* xattrs-size */
+ xattrs_buflen, /* xattrs-size */
0); /* chksum */
push_hdr(s);
push_string(name);
push_pad();
-
+ if (newcx)
+ include_xattrs();
if (size) {
if (fwrite(filebuf, size, 1, stdout) != 1) {
fprintf(stderr, "writing filebuf failed\n");
@@ -458,7 +548,7 @@ static int cpio_mkfile_line(const char *line)
static void usage(const char *prog)
{
fprintf(stderr, "Usage:\n"
- "\t%s [-t <timestamp>] <cpio_list>\n"
+ "\t%s [-t <timestamp>] [-x] <cpio_list>\n"
"\n"
"<cpio_list> is a file containing newline separated entries that\n"
"describe the files to be included in the initramfs archive:\n"
@@ -535,7 +625,7 @@ int main (int argc, char *argv[])
default_mtime = time(NULL);
while (1) {
- int opt = getopt(argc, argv, "t:h");
+ int opt = getopt(argc, argv, "t:h:x");
char *invalid;
if (opt == -1)
@@ -550,6 +640,9 @@ int main (int argc, char *argv[])
exit(1);
}
break;
+ case 'x':
+ newcx = 1;
+ break;
case 'h':
case '?':
usage(argv[0]);
--
1.8.1.4
next prev parent reply other threads:[~2015-01-07 20:56 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-07 20:52 [RFC][PATCH 0/9] extend initramfs archive format to support xattrs Mimi Zohar
2015-01-07 20:52 ` [RFC][PATCH 1/9] initramfs: separate reading cpio method from header Mimi Zohar
2015-01-07 20:52 ` [RFC][PATCH 2/9] initramfs: add extended attribute support Mimi Zohar
2015-01-07 20:52 ` [RFC][PATCH 3/9] gen_init_cpio: replace inline format string with common variable Mimi Zohar
2015-01-07 20:52 ` [RFC][PATCH 4/9] gen_init_cpio: define new CPIO format to support xattrs Mimi Zohar
2015-01-07 20:52 ` Mimi Zohar [this message]
2015-01-07 20:52 ` [RFC][PATCH 6/9] gen_initramfs_list.sh: include xattrs Mimi Zohar
2015-01-08 14:01 ` Josh Boyer
2015-01-08 15:13 ` Mimi Zohar
2015-01-08 18:19 ` Rob Landley
2015-01-08 22:08 ` Mimi Zohar
2015-01-13 18:48 ` Rob Landley
2015-01-13 20:20 ` Mimi Zohar
2015-01-13 21:42 ` Rob Landley
2015-01-14 3:23 ` Mimi Zohar
2015-01-14 4:34 ` Rob Landley
2015-01-14 13:23 ` Mimi Zohar
2015-01-14 19:36 ` Paul Moore
2015-01-07 20:52 ` [RFC][PATCH 7/9] evm: make rootfs a special case Mimi Zohar
2015-01-07 20:52 ` [RFC][PATCH 8/9] ima: include tmpfs in ima_appraise_tcb policy Mimi Zohar
2015-01-08 13:53 ` Josh Boyer
2015-01-08 15:13 ` Mimi Zohar
2015-01-07 20:53 ` [RFC][PATCH 9/9] init: remove "root=" command line option test for tmpfs decision Mimi Zohar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1420663980-20842-6-git-send-email-zohar@linux.vnet.ibm.com \
--to=zohar@linux.vnet.ibm.com \
--cc=initramfs@vger.kernel.org \
--cc=linux-ima-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=viro@ZenIV.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).