Skip to contentSkip to conversation

EVM: turn evm_config_xattrnames into a list

PATCH In review Matthew Garrett 3 participants 9 messages May 9, 2018 – May 11, 2018

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 EVM: turn evm_config_xattrnames into a list

Use a list of xattrs rather than an array - this makes it easier to extend the list at runtime.

---
 security/integrity/evm/evm.h        |  7 ++-
 security/integrity/evm/evm_crypto.c | 10 ++--
 security/integrity/evm/evm_main.c   | 72 +++++++++++++++++++----------
 3 files changed, 59 insertions(+), 30 deletions(-)
security/integrity/evm/evm.h +6 -1
index 45c4a89c02ff..1257c3c24723 100644
@@ -30,6 +30,11 @@
 #define EVM_INIT_MASK (EVM_INIT_HMAC | EVM_INIT_X509 | EVM_SETUP_COMPLETE | \
 		       EVM_ALLOW_METADATA_WRITES)
 
+struct xattr_list {
+	struct list_head list;
+	char *name;
+};
+
 extern int evm_initialized;
 
 #define EVM_ATTR_FSUUID		0x0001
@@ -40,7 +45,7 @@ extern struct crypto_shash *hmac_tfm;
 extern struct crypto_shash *hash_tfm;
 
 /* List of EVM protected security xattrs */
-extern char *evm_config_xattrnames[];
+extern struct list_head evm_config_xattrnames;
 
 int evm_init_key(void);
 int evm_update_evmxattr(struct dentry *dentry,
security/integrity/evm/evm_crypto.c +5 -5
index a46fba322340..caeea20670cc 100644
@@ -192,8 +192,8 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
 				char type, char *digest)
 {
 	struct inode *inode = d_backing_inode(dentry);
+	struct xattr_list *xattr;
 	struct shash_desc *desc;
-	char **xattrname;
 	size_t xattr_size = 0;
 	char *xattr_value = NULL;
 	int error;
@@ -208,14 +208,14 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
 		return PTR_ERR(desc);
 
 	error = -ENODATA;
-	for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
+	list_for_each_entry(xattr, &evm_config_xattrnames, list) {
 		bool is_ima = false;
 
-		if (strcmp(*xattrname, XATTR_NAME_IMA) == 0)
+		if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
 			is_ima = true;
 
 		if ((req_xattr_name && req_xattr_value)
-		    && !strcmp(*xattrname, req_xattr_name)) {
+		    && !strcmp(xattr->name, req_xattr_name)) {
 			error = 0;
 			crypto_shash_update(desc, (const u8 *)req_xattr_value,
 					     req_xattr_value_len);
@@ -223,7 +223,7 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
 				ima_present = true;
 			continue;
 		}
-		size = vfs_getxattr_alloc(dentry, *xattrname,
+		size = vfs_getxattr_alloc(dentry, xattr->name,
 					  &xattr_value, xattr_size, GFP_NOFS);
 		if (size == -ENOMEM) {
 			error = -ENOMEM;
security/integrity/evm/evm_main.c +48 -24

151 lines not on this page · raw message

Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
#1 lore raw

PATCH v4 2/2 EVM: Allow runtime modification of the set of verified xattrs

Sites may wish to provide additional metadata alongside files in order to make more fine-grained security decisions[1]. The security of this is enhanced if this metadata is protected, something that EVM makes possible. However, the kernel cannot know about the set of extended attributes that local admins may wish to protect, and hardcoding this policy in the kernel makes it difficult to change over time and less convenient for distributions to enable.

This patch adds a new /sys/kernel/security/evm_xattrs node, which can be read to obtain the current set of EVM-protected extended attributes or written to in order to add new entries. Extending this list will not change the validity of any existing signatures provided that the file in question does not have any of the additional extended attributes - missing xattrs are skipped when calculating the EVM hash.

[1] For instance, a package manager could install information about the package uploader in an additional extended attribute. Local LSM policy could then be associated with that extended attribute in order to restrict the privileges available to packages from less trusted uploaders.

---

Updated to add support for locking down the list, and also to forbid adding the same xattr twice.

 Documentation/ABI/testing/evm       |  13 +++
 security/integrity/evm/Kconfig      |  11 ++
 security/integrity/evm/evm_crypto.c |   2 +-
 security/integrity/evm/evm_main.c   |   6 +-
 security/integrity/evm/evm_secfs.c  | 153 ++++++++++++++++++++++++++--
 5 files changed, 175 insertions(+), 10 deletions(-)
Documentation/ABI/testing/evm +13 -0
index d12cb2eae9ee..fa31df7fd30b 100644
@@ -57,3 +57,16 @@ Description:
 		dracut (via 97masterkey and 98integrity) and systemd (via
 		core/ima-setup) have support for loading keys at boot
 		time.
+
+What:		security/evm_xattrs
+Date:		April 2018
+Contact:	Matthew Garrett <[email protected]>
+Description:
+		Shows the set of extended attributes used to calculate or
+		validate the EVM signature, and allows additional attributes
+		to be added at runtime. Any signatures generated after
+		additional attributes are added (and on files posessing those
+		additional attributes) will only be valid if the same
+		additional attributes are configured on system boot. Writing
+		a single period (.) will lock the xattr list from any further
+		modification.
security/integrity/evm/Kconfig +11 -0
index e825e0ae78e7..54adb3f9ad1d 100644
@@ -42,6 +42,17 @@ config EVM_EXTRA_SMACK_XATTRS
 	  additional info to the calculation, requires existing EVM
 	  labeled file systems to be relabeled.
 
+config EVM_ADD_XATTRS
+	bool "Add additional EVM extended attributes at runtime"
+	depends on EVM
+	default n
+	help
+	  Allow userland to provide additional xattrs for HMAC calculation.
+
+	  When this option is enabled, root can add additional xattrs to the
+	  list used by EVM by writing them into
+	  /sys/kernel/security/evm_xattrs.
+
 config EVM_LOAD_X509
 	bool "Load an X509 certificate onto the '.evm' trusted keyring"
 	depends on EVM && INTEGRITY_TRUSTED_KEYRING
security/integrity/evm/evm_crypto.c +1 -1
index caeea20670cc..494da5fcc092 100644
@@ -208,7 +208,7 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
 		return PTR_ERR(desc);
 
 	error = -ENODATA;
-	list_for_each_entry(xattr, &evm_config_xattrnames, list) {
+	list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
 		bool is_ima = false;
 
 		if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
security/integrity/evm/evm_main.c +3 -3
index dd2415c55982..f049af2cc037 100644
@@ -35,7 +35,7 @@ static const char * const integrity_status_msg[] = {
 };
 int evm_hmac_attrs;
 
-static struct xattr_list evm_config_default_xattrnames[] __ro_after_init = {
+static struct xattr_list evm_config_default_xattrnames[] = {
 #ifdef CONFIG_SECURITY_SELINUX
 	{.name = XATTR_NAME_SELINUX},
 #endif
@@ -98,7 +98,7 @@ static int evm_find_protected_xattrs(struct dentry *dentry)
 	if (!(inode->i_opflags & IOP_XATTR))
 		return -EOPNOTSUPP;
 
-	list_for_each_entry(xattr, &evm_config_xattrnames, list) {
+	list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
 		error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0);
 		if (error < 0) {
 			if (error == -ENODATA)
@@ -225,7 +225,7 @@ static int evm_protected_xattr(const char *req_xattr_name)
 	struct xattr_list *xattr;
 
 	namelen = strlen(req_xattr_name);
-	list_for_each_entry(xattr, &evm_config_xattrnames, list) {
+	list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
 		if ((strlen(xattr->name) == namelen)
 		    && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
 			found = 1;
security/integrity/evm/evm_secfs.c +147 -6

170 lines not on this page · raw message

Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
maintainer Reviewed-by on v4 2/2
#2 lore raw

Re: [PATCH V4 2/2] EVM: Allow runtime modification of the set of verified xattrs

Quoted 6 lines · Matthew Garrett

---

Updated to add support for locking down the list, and also to forbid adding the same xattr twice.

Quoted 6 lines
 Documentation/ABI/testing/evm       |  13 +++
 security/integrity/evm/Kconfig      |  11 ++
 security/integrity/evm/evm_crypto.c |   2 +-
 security/integrity/evm/evm_main.c   |   6 +-
 security/integrity/evm/evm_secfs.c  | 153 ++++++++++++++++++++++++++--
 5 files changed, 175 insertions(+), 10 deletions(-)
Signature · 2 lines
James Morris
<[email protected]>
#3 lore raw

Re: [PATCH V4 2/2] EVM: Allow runtime modification of the set of verified xattrs

In reply to Matthew Garrett
Quoted 19 lines · Matthew Garrett
security/integrity/evm/evm_secfs.c +19 -0
+/**
+ * evm_write_xattrs - write() for <securityfs>/evm_xattrs
+ * @file: file pointer, not actually used
+ * @buf: where to get the data from
+ * @count: bytes sent
+ * @ppos: where to start
+ *
+ * Returns number of bytes written or error code, as appropriate
+ */
+static ssize_t evm_write_xattrs(struct file *file, const char __user *buf,
+				size_t count, loff_t *ppos)
+{
+	int len, err;
+	struct xattr_list *xattr, *tmp;
+
+	if (!capable(CAP_SYS_ADMIN) || evm_xattrs_locked)
+		return -EPERM;
+
+	if (*ppos != 0)

security/integrity/evm/evm_secfs.c static const struct file_operations evm_key_ops = { · line 189

Quoted 10 lines
security/integrity/evm/evm_secfs.c +10 -0
+		return -EINVAL;
+
+	if (count > XATTR_NAME_MAX)
+		return -E2BIG;
+
+	xattr = kmalloc(sizeof(struct xattr_list), GFP_KERNEL);
+	if (!xattr)
+		return -ENOMEM;
+
+	xattr->name = memdup_user_nul(buf, count);

Up to now, the set of protected EVM xattrs was in the security domain. The current code permits any string in any domain. If that is the intention, there needs to be an explanation of the security implications of this change at least in the patch description.

Quoted 4 lines
security/integrity/evm/evm_secfs.c +4 -0
+	if (IS_ERR(xattr->name)) {
+		err = PTR_ERR(xattr->name);
+		kfree(xattr);
+		return err;

security/integrity/evm/evm_secfs.c static const struct file_operations evm_key_ops = { · line 203

Quoted 10 lines
security/integrity/evm/evm_secfs.c +10 -0
+	}
+
+	/* Remove any trailing newline */
+	len = strlen(xattr->name);
+	if (xattr->name[len-1] == '\n')
+		xattr->name[len-1] = '\0';
+
+	if (strcmp(xattr->name, ".") == 0) {
+		evm_xattrs_locked = 1;
+		err = count;

Please update the file mode bits of <securityfs>/evm_xattrs.

Quoted 21 lines
security/integrity/evm/evm_secfs.c +21 -0
+		goto out;
+	}
+
+	/* Guard against races in evm_read_xattrs */
+	mutex_lock(&xattr_list_mutex);
+	list_for_each_entry(tmp, &evm_config_xattrnames, list) {
+		if (strcmp(xattr->name, tmp->name) == 0) {
+			err = -EEXIST;
+			mutex_unlock(&xattr_list_mutex);
+			goto out;
+		}
+	}
+	list_add_tail_rcu(&xattr->list, &evm_config_xattrnames);
+	mutex_unlock(&xattr_list_mutex);
+
+	return count;
+out:
+	kfree(xattr->name);
+	kfree(xattr);
+	return err;
+}

security/integrity/evm/evm_secfs.c static const struct file_operations evm_key_ops = { · line 234

Quoted 10 lines
security/integrity/evm/evm_secfs.c +10 -0
+
+static const struct file_operations evm_xattr_ops = {
+	.read		= evm_read_xattrs,
+	.write		= evm_write_xattrs,
+};
+
+static int evm_init_xattrs(void)
+{
+	evm_xattrs = securityfs_create_file("evm_xattrs", 0440, NULL, NULL,
+					    &evm_xattr_ops);

The mode bits should reflect the status of the current status of evm_xattrs. Initially it would be writeable, but later it might change to read-only.

Should "evm_xattrs" be defined directly in the securityfs directory or in a subdirectory similar to ima? It will be difficult later on to move "evm_xattrs" to a subdirectory once applications start reading/writing to it. What would the subdirectory be called?

Mimi

#4 lore raw

Re: [PATCH V4 2/2] EVM: Allow runtime modification of the set of verified xattrs

Quoted 7 lines · Mimi Zohar
Quoted 1 line · Matthew Garrett

+ xattr->name = memdup_user_nul(buf, count);

Up to now, the set of protected EVM xattrs was in the security domain. The current code permits any string in any domain. If that is the intention, there needs to be an explanation of the security implications of this change at least in the patch description.

Restricting it to security makes sense for now - if it turns out that it seems useful to use other attributes then we can talk about changing that later.

Quoted 5 lines

security/integrity/evm/evm_secfs.c static const struct file_operations evm_key_ops = { · line 203

Quoted 3 lines
security/integrity/evm/evm_secfs.c +3 -0
+     if (strcmp(xattr->name, ".") == 0) {
+             evm_xattrs_locked = 1;
+             err = count;

Please update the file mode bits of <securityfs>/evm_xattrs.

Ok.

Quoted 4 lines

Should "evm_xattrs" be defined directly in the securityfs directory or in a subdirectory similar to ima? It will be difficult later on to move "evm_xattrs" to a subdirectory once applications start reading/writing to it. What would the subdirectory be called?

Yeah, that's tricky - the obvious directory would be evm, but that's already in use. integrity makes sense, but then ima should also be under it.

#5 lore raw

Re: [PATCH V4 2/2] EVM: Allow runtime modification of the set of verified xattrs

Quoted 7 lines · Matthew Garrett
Quoted 4 lines

Should "evm_xattrs" be defined directly in the securityfs directory or in a subdirectory similar to ima? It will be difficult later on to move "evm_xattrs" to a subdirectory once applications start reading/writing to it. What would the subdirectory be called?

Yeah, that's tricky - the obvious directory would be evm, but that's already in use. integrity makes sense, but then ima should also be under it.

I haven't tried, but maybe we could use softlinks (eg. <securityfs>/ima ==> <securityfs>/integrity/ima)?

And then there are namespaces ...

Mimi

#6 lore raw

Re: [PATCH V4 2/2] EVM: Allow runtime modification of the set of verified xattrs

In reply to Matthew Garrett
Quoted 54 lines · Matthew Garrett
security/integrity/evm/evm_secfs.c +54 -0
+/**
+ * evm_write_xattrs - write() for <securityfs>/evm_xattrs
+ * @file: file pointer, not actually used
+ * @buf: where to get the data from
+ * @count: bytes sent
+ * @ppos: where to start
+ *
+ * Returns number of bytes written or error code, as appropriate
+ */
+static ssize_t evm_write_xattrs(struct file *file, const char __user *buf,
+				size_t count, loff_t *ppos)
+{
+	int len, err;
+	struct xattr_list *xattr, *tmp;
+
+	if (!capable(CAP_SYS_ADMIN) || evm_xattrs_locked)
+		return -EPERM;
+
+	if (*ppos != 0)
+		return -EINVAL;
+
+	if (count > XATTR_NAME_MAX)
+		return -E2BIG;
+
+	xattr = kmalloc(sizeof(struct xattr_list), GFP_KERNEL);
+	if (!xattr)
+		return -ENOMEM;
+
+	xattr->name = memdup_user_nul(buf, count);
+	if (IS_ERR(xattr->name)) {
+		err = PTR_ERR(xattr->name);
+		kfree(xattr);
+		return err;
+	}
+
+	/* Remove any trailing newline */
+	len = strlen(xattr->name);
+	if (xattr->name[len-1] == '\n')
+		xattr->name[len-1] = '\0';
+
+	if (strcmp(xattr->name, ".") == 0) {
+		evm_xattrs_locked = 1;
+		err = count;
+		goto out;
+	}
+
+	/* Guard against races in evm_read_xattrs */
+	mutex_lock(&xattr_list_mutex);
+	list_for_each_entry(tmp, &evm_config_xattrnames, list) {
+		if (strcmp(xattr->name, tmp->name) == 0) {
+			err = -EEXIST;
+			mutex_unlock(&xattr_list_mutex);
+			goto out;
+		}

security/integrity/evm/evm_secfs.c static const struct file_operations evm_key_ops = { · line 224

Quoted 10 lines
security/integrity/evm/evm_secfs.c +10 -0
+	}
+	list_add_tail_rcu(&xattr->list, &evm_config_xattrnames);
+	mutex_unlock(&xattr_list_mutex);
+
+	return count;
+out:
+	kfree(xattr->name);
+	kfree(xattr);
+	return err;
+}

Shouldn't new xattrs be audited/logged?

Mimi

maintainer Reviewed-by on v4 1/2
#7 lore raw
In reply to Matthew Garrett
Quoted 9 lines · Matthew Garrett

Use a list of xattrs rather than an array - this makes it easier to extend the list at runtime.

---
 security/integrity/evm/evm.h        |  7 ++-
 security/integrity/evm/evm_crypto.c | 10 ++--
 security/integrity/evm/evm_main.c   | 72 +++++++++++++++++++----------
 3 files changed, 59 insertions(+), 30 deletions(-)
Signature · 2 lines
James Morris
<[email protected]>
#8 lore raw
In reply to Matthew Garrett
Quoted 5 lines · Matthew Garrett
security/integrity/evm/evm_main.c +0 -1
@@ -565,10 +588,11 @@ static int __init init_evm(void)
  */
 static int __init evm_display_config(void)
 {
-	char **xattrname;

security/integrity/evm/evm_main.c static int __init init_evm(void) · line 598

Quoted 10 lines
security/integrity/evm/evm_main.c +4 -2
+	struct xattr_list *xattr;
+
+	list_for_each_entry(xattr, &evm_config_xattrnames, list)
+		pr_info("%s\n", xattr->name);
 
-	for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
-		pr_info("%s\n", *xattrname);
 	return 0;
 }
 

The list is only created in init_evm(), which is initialized at late_initcall(), yet here we're displaying the list at pure_initcall(). None of the xattrs are being displayed.

Mimi