Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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/integrity/evm/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.

Signed-off-by: Matthew Garrett <mjg59@google.com>
Reviewed-by: James Morris <james.morris@microsoft.com>
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>

authored by

Matthew Garrett and committed by
Mimi Zohar
fa516b66 21af7663

+202 -4
+13
Documentation/ABI/testing/evm
··· 57 57 dracut (via 97masterkey and 98integrity) and systemd (via 58 58 core/ima-setup) have support for loading keys at boot 59 59 time. 60 + 61 + What: security/integrity/evm/evm_xattrs 62 + Date: April 2018 63 + Contact: Matthew Garrett <mjg59@google.com> 64 + Description: 65 + Shows the set of extended attributes used to calculate or 66 + validate the EVM signature, and allows additional attributes 67 + to be added at runtime. Any signatures generated after 68 + additional attributes are added (and on files posessing those 69 + additional attributes) will only be valid if the same 70 + additional attributes are configured on system boot. Writing 71 + a single period (.) will lock the xattr list from any further 72 + modification.
+1
include/uapi/linux/audit.h
··· 147 147 #define AUDIT_INTEGRITY_HASH 1803 /* Integrity HASH type */ 148 148 #define AUDIT_INTEGRITY_PCR 1804 /* PCR invalidation msgs */ 149 149 #define AUDIT_INTEGRITY_RULE 1805 /* policy rule */ 150 + #define AUDIT_INTEGRITY_EVM_XATTR 1806 /* New EVM-covered xattr */ 150 151 151 152 #define AUDIT_KERNEL 2000 /* Asynchronous audit record. NOT A REQUEST. */ 152 153
+11
security/integrity/evm/Kconfig
··· 42 42 additional info to the calculation, requires existing EVM 43 43 labeled file systems to be relabeled. 44 44 45 + config EVM_ADD_XATTRS 46 + bool "Add additional EVM extended attributes at runtime" 47 + depends on EVM 48 + default n 49 + help 50 + Allow userland to provide additional xattrs for HMAC calculation. 51 + 52 + When this option is enabled, root can add additional xattrs to the 53 + list used by EVM by writing them into 54 + /sys/kernel/security/integrity/evm/evm_xattrs. 55 + 45 56 config EVM_LOAD_X509 46 57 bool "Load an X509 certificate onto the '.evm' trusted keyring" 47 58 depends on EVM && INTEGRITY_TRUSTED_KEYRING
+1 -1
security/integrity/evm/evm_crypto.c
··· 208 208 return PTR_ERR(desc); 209 209 210 210 error = -ENODATA; 211 - list_for_each_entry(xattr, &evm_config_xattrnames, list) { 211 + list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) { 212 212 bool is_ima = false; 213 213 214 214 if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
+3 -3
security/integrity/evm/evm_main.c
··· 35 35 }; 36 36 int evm_hmac_attrs; 37 37 38 - static struct xattr_list evm_config_default_xattrnames[] __ro_after_init = { 38 + static struct xattr_list evm_config_default_xattrnames[] = { 39 39 #ifdef CONFIG_SECURITY_SELINUX 40 40 {.name = XATTR_NAME_SELINUX}, 41 41 #endif ··· 101 101 if (!(inode->i_opflags & IOP_XATTR)) 102 102 return -EOPNOTSUPP; 103 103 104 - list_for_each_entry(xattr, &evm_config_xattrnames, list) { 104 + list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) { 105 105 error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0); 106 106 if (error < 0) { 107 107 if (error == -ENODATA) ··· 228 228 struct xattr_list *xattr; 229 229 230 230 namelen = strlen(req_xattr_name); 231 - list_for_each_entry(xattr, &evm_config_xattrnames, list) { 231 + list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) { 232 232 if ((strlen(xattr->name) == namelen) 233 233 && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) { 234 234 found = 1;
+173
security/integrity/evm/evm_secfs.c
··· 15 15 16 16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 17 17 18 + #include <linux/audit.h> 18 19 #include <linux/uaccess.h> 19 20 #include <linux/module.h> 21 + #include <linux/mutex.h> 20 22 #include "evm.h" 21 23 22 24 static struct dentry *evm_dir; 23 25 static struct dentry *evm_init_tpm; 24 26 static struct dentry *evm_symlink; 27 + 28 + #ifdef CONFIG_EVM_ADD_XATTRS 29 + static struct dentry *evm_xattrs; 30 + static DEFINE_MUTEX(xattr_list_mutex); 31 + static int evm_xattrs_locked; 32 + #endif 25 33 26 34 /** 27 35 * evm_read_key - read() for <securityfs>/evm ··· 117 109 .write = evm_write_key, 118 110 }; 119 111 112 + #ifdef CONFIG_EVM_ADD_XATTRS 113 + /** 114 + * evm_read_xattrs - read() for <securityfs>/evm_xattrs 115 + * 116 + * @filp: file pointer, not actually used 117 + * @buf: where to put the result 118 + * @count: maximum to send along 119 + * @ppos: where to start 120 + * 121 + * Returns number of bytes read or error code, as appropriate 122 + */ 123 + static ssize_t evm_read_xattrs(struct file *filp, char __user *buf, 124 + size_t count, loff_t *ppos) 125 + { 126 + char *temp; 127 + int offset = 0; 128 + ssize_t rc, size = 0; 129 + struct xattr_list *xattr; 130 + 131 + if (*ppos != 0) 132 + return 0; 133 + 134 + rc = mutex_lock_interruptible(&xattr_list_mutex); 135 + if (rc) 136 + return -ERESTARTSYS; 137 + 138 + list_for_each_entry(xattr, &evm_config_xattrnames, list) 139 + size += strlen(xattr->name) + 1; 140 + 141 + temp = kmalloc(size + 1, GFP_KERNEL); 142 + if (!temp) 143 + return -ENOMEM; 144 + 145 + list_for_each_entry(xattr, &evm_config_xattrnames, list) { 146 + sprintf(temp + offset, "%s\n", xattr->name); 147 + offset += strlen(xattr->name) + 1; 148 + } 149 + 150 + mutex_unlock(&xattr_list_mutex); 151 + rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); 152 + 153 + return rc; 154 + } 155 + 156 + /** 157 + * evm_write_xattrs - write() for <securityfs>/evm_xattrs 158 + * @file: file pointer, not actually used 159 + * @buf: where to get the data from 160 + * @count: bytes sent 161 + * @ppos: where to start 162 + * 163 + * Returns number of bytes written or error code, as appropriate 164 + */ 165 + static ssize_t evm_write_xattrs(struct file *file, const char __user *buf, 166 + size_t count, loff_t *ppos) 167 + { 168 + int len, err; 169 + struct xattr_list *xattr, *tmp; 170 + struct audit_buffer *ab; 171 + struct iattr newattrs; 172 + struct inode *inode; 173 + 174 + if (!capable(CAP_SYS_ADMIN) || evm_xattrs_locked) 175 + return -EPERM; 176 + 177 + if (*ppos != 0) 178 + return -EINVAL; 179 + 180 + if (count > XATTR_NAME_MAX) 181 + return -E2BIG; 182 + 183 + ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_EVM_XATTR); 184 + if (IS_ERR(ab)) 185 + return PTR_ERR(ab); 186 + 187 + xattr = kmalloc(sizeof(struct xattr_list), GFP_KERNEL); 188 + if (!xattr) { 189 + err = -ENOMEM; 190 + goto out; 191 + } 192 + 193 + xattr->name = memdup_user_nul(buf, count); 194 + if (IS_ERR(xattr->name)) { 195 + err = PTR_ERR(xattr->name); 196 + xattr->name = NULL; 197 + goto out; 198 + } 199 + 200 + /* Remove any trailing newline */ 201 + len = strlen(xattr->name); 202 + if (xattr->name[len-1] == '\n') 203 + xattr->name[len-1] = '\0'; 204 + 205 + if (strcmp(xattr->name, ".") == 0) { 206 + evm_xattrs_locked = 1; 207 + newattrs.ia_mode = S_IFREG | 0440; 208 + newattrs.ia_valid = ATTR_MODE; 209 + inode = evm_xattrs->d_inode; 210 + inode_lock(inode); 211 + err = simple_setattr(evm_xattrs, &newattrs); 212 + inode_unlock(inode); 213 + audit_log_format(ab, "locked"); 214 + if (!err) 215 + err = count; 216 + goto out; 217 + } 218 + 219 + audit_log_format(ab, "xattr="); 220 + audit_log_untrustedstring(ab, xattr->name); 221 + 222 + if (strncmp(xattr->name, XATTR_SECURITY_PREFIX, 223 + XATTR_SECURITY_PREFIX_LEN) != 0) { 224 + err = -EINVAL; 225 + goto out; 226 + } 227 + 228 + /* Guard against races in evm_read_xattrs */ 229 + mutex_lock(&xattr_list_mutex); 230 + list_for_each_entry(tmp, &evm_config_xattrnames, list) { 231 + if (strcmp(xattr->name, tmp->name) == 0) { 232 + err = -EEXIST; 233 + mutex_unlock(&xattr_list_mutex); 234 + goto out; 235 + } 236 + } 237 + list_add_tail_rcu(&xattr->list, &evm_config_xattrnames); 238 + mutex_unlock(&xattr_list_mutex); 239 + 240 + audit_log_format(ab, " res=0"); 241 + audit_log_end(ab); 242 + return count; 243 + out: 244 + audit_log_format(ab, " res=%d", err); 245 + audit_log_end(ab); 246 + kfree(xattr->name); 247 + kfree(xattr); 248 + return err; 249 + } 250 + 251 + static const struct file_operations evm_xattr_ops = { 252 + .read = evm_read_xattrs, 253 + .write = evm_write_xattrs, 254 + }; 255 + 256 + static int evm_init_xattrs(void) 257 + { 258 + evm_xattrs = securityfs_create_file("evm_xattrs", 0660, evm_dir, NULL, 259 + &evm_xattr_ops); 260 + if (!evm_xattrs || IS_ERR(evm_xattrs)) 261 + return -EFAULT; 262 + 263 + return 0; 264 + } 265 + #else 266 + static int evm_init_xattrs(void) 267 + { 268 + return 0; 269 + } 270 + #endif 271 + 120 272 int __init evm_init_secfs(void) 121 273 { 122 274 int error = 0; ··· 295 127 evm_symlink = securityfs_create_symlink("evm", NULL, 296 128 "integrity/evm/evm", NULL); 297 129 if (!evm_symlink || IS_ERR(evm_symlink)) { 130 + error = -EFAULT; 131 + goto out; 132 + } 133 + 134 + if (evm_init_xattrs() != 0) { 298 135 error = -EFAULT; 299 136 goto out; 300 137 }