Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005-2010 IBM Corporation |
| 3 | * |
| 4 | * Author: |
| 5 | * Mimi Zohar <zohar@us.ibm.com> |
| 6 | * Kylene Hall <kjhall@us.ibm.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation, version 2 of the License. |
| 11 | * |
| 12 | * File: evm_main.c |
| 13 | * implements evm_inode_setxattr, evm_inode_post_setxattr, |
| 14 | * evm_inode_removexattr, and evm_verifyxattr |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/crypto.h> |
| 19 | #include <linux/xattr.h> |
| 20 | #include <linux/integrity.h> |
Mimi Zohar | 3e1be52 | 2011-03-09 14:38:26 -0500 | [diff] [blame] | 21 | #include <linux/evm.h> |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame^] | 22 | #include <crypto/hash.h> |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 23 | #include "evm.h" |
| 24 | |
| 25 | int evm_initialized; |
| 26 | |
| 27 | char *evm_hmac = "hmac(sha1)"; |
| 28 | |
| 29 | char *evm_config_xattrnames[] = { |
| 30 | #ifdef CONFIG_SECURITY_SELINUX |
| 31 | XATTR_NAME_SELINUX, |
| 32 | #endif |
| 33 | #ifdef CONFIG_SECURITY_SMACK |
| 34 | XATTR_NAME_SMACK, |
| 35 | #endif |
| 36 | XATTR_NAME_CAPS, |
| 37 | NULL |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr |
| 42 | * |
| 43 | * Compute the HMAC on the dentry's protected set of extended attributes |
| 44 | * and compare it against the stored security.evm xattr. (For performance, |
| 45 | * use the previoulsy retrieved xattr value and length to calculate the |
| 46 | * HMAC.) |
| 47 | * |
| 48 | * Returns integrity status |
| 49 | */ |
| 50 | static enum integrity_status evm_verify_hmac(struct dentry *dentry, |
| 51 | const char *xattr_name, |
| 52 | char *xattr_value, |
| 53 | size_t xattr_value_len, |
| 54 | struct integrity_iint_cache *iint) |
| 55 | { |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 56 | struct evm_ima_xattr_data xattr_data; |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 57 | int rc; |
| 58 | |
| 59 | if (iint->hmac_status != INTEGRITY_UNKNOWN) |
| 60 | return iint->hmac_status; |
| 61 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 62 | rc = evm_calc_hmac(dentry, xattr_name, xattr_value, |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 63 | xattr_value_len, xattr_data.digest); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 64 | if (rc < 0) |
| 65 | return INTEGRITY_UNKNOWN; |
| 66 | |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 67 | xattr_data.type = EVM_XATTR_HMAC; |
| 68 | rc = vfs_xattr_cmp(dentry, XATTR_NAME_EVM, (u8 *)&xattr_data, |
| 69 | sizeof xattr_data, GFP_NOFS); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 70 | if (rc < 0) |
| 71 | goto err_out; |
| 72 | iint->hmac_status = INTEGRITY_PASS; |
| 73 | return iint->hmac_status; |
| 74 | |
| 75 | err_out: |
| 76 | switch (rc) { |
| 77 | case -ENODATA: /* file not labelled */ |
| 78 | iint->hmac_status = INTEGRITY_NOLABEL; |
| 79 | break; |
| 80 | case -EINVAL: |
| 81 | iint->hmac_status = INTEGRITY_FAIL; |
| 82 | break; |
| 83 | default: |
| 84 | iint->hmac_status = INTEGRITY_UNKNOWN; |
| 85 | } |
| 86 | return iint->hmac_status; |
| 87 | } |
| 88 | |
| 89 | static int evm_protected_xattr(const char *req_xattr_name) |
| 90 | { |
| 91 | char **xattrname; |
| 92 | int namelen; |
| 93 | int found = 0; |
| 94 | |
| 95 | namelen = strlen(req_xattr_name); |
| 96 | for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) { |
| 97 | if ((strlen(*xattrname) == namelen) |
| 98 | && (strncmp(req_xattr_name, *xattrname, namelen) == 0)) { |
| 99 | found = 1; |
| 100 | break; |
| 101 | } |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 102 | if (strncmp(req_xattr_name, |
| 103 | *xattrname + XATTR_SECURITY_PREFIX_LEN, |
| 104 | strlen(req_xattr_name)) == 0) { |
| 105 | found = 1; |
| 106 | break; |
| 107 | } |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 108 | } |
| 109 | return found; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * evm_verifyxattr - verify the integrity of the requested xattr |
| 114 | * @dentry: object of the verify xattr |
| 115 | * @xattr_name: requested xattr |
| 116 | * @xattr_value: requested xattr value |
| 117 | * @xattr_value_len: requested xattr value length |
| 118 | * |
| 119 | * Calculate the HMAC for the given dentry and verify it against the stored |
| 120 | * security.evm xattr. For performance, use the xattr value and length |
| 121 | * previously retrieved to calculate the HMAC. |
| 122 | * |
| 123 | * Returns the xattr integrity status. |
| 124 | * |
| 125 | * This function requires the caller to lock the inode's i_mutex before it |
| 126 | * is executed. |
| 127 | */ |
| 128 | enum integrity_status evm_verifyxattr(struct dentry *dentry, |
| 129 | const char *xattr_name, |
| 130 | void *xattr_value, size_t xattr_value_len) |
| 131 | { |
| 132 | struct inode *inode = dentry->d_inode; |
| 133 | struct integrity_iint_cache *iint; |
| 134 | enum integrity_status status; |
| 135 | |
| 136 | if (!evm_initialized || !evm_protected_xattr(xattr_name)) |
| 137 | return INTEGRITY_UNKNOWN; |
| 138 | |
| 139 | iint = integrity_iint_find(inode); |
| 140 | if (!iint) |
| 141 | return INTEGRITY_UNKNOWN; |
| 142 | status = evm_verify_hmac(dentry, xattr_name, xattr_value, |
| 143 | xattr_value_len, iint); |
| 144 | return status; |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(evm_verifyxattr); |
| 147 | |
| 148 | /* |
| 149 | * evm_protect_xattr - protect the EVM extended attribute |
| 150 | * |
| 151 | * Prevent security.evm from being modified or removed. |
| 152 | */ |
| 153 | static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name, |
| 154 | const void *xattr_value, size_t xattr_value_len) |
| 155 | { |
| 156 | if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) { |
| 157 | if (!capable(CAP_SYS_ADMIN)) |
| 158 | return -EPERM; |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * evm_inode_setxattr - protect the EVM extended attribute |
| 165 | * @dentry: pointer to the affected dentry |
| 166 | * @xattr_name: pointer to the affected extended attribute name |
| 167 | * @xattr_value: pointer to the new extended attribute value |
| 168 | * @xattr_value_len: pointer to the new extended attribute value length |
| 169 | * |
| 170 | * Prevent 'security.evm' from being modified |
| 171 | */ |
| 172 | int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name, |
| 173 | const void *xattr_value, size_t xattr_value_len) |
| 174 | { |
| 175 | return evm_protect_xattr(dentry, xattr_name, xattr_value, |
| 176 | xattr_value_len); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * evm_inode_removexattr - protect the EVM extended attribute |
| 181 | * @dentry: pointer to the affected dentry |
| 182 | * @xattr_name: pointer to the affected extended attribute name |
| 183 | * |
| 184 | * Prevent 'security.evm' from being removed. |
| 185 | */ |
| 186 | int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name) |
| 187 | { |
| 188 | return evm_protect_xattr(dentry, xattr_name, NULL, 0); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * evm_inode_post_setxattr - update 'security.evm' to reflect the changes |
| 193 | * @dentry: pointer to the affected dentry |
| 194 | * @xattr_name: pointer to the affected extended attribute name |
| 195 | * @xattr_value: pointer to the new extended attribute value |
| 196 | * @xattr_value_len: pointer to the new extended attribute value length |
| 197 | * |
| 198 | * Update the HMAC stored in 'security.evm' to reflect the change. |
| 199 | * |
| 200 | * No need to take the i_mutex lock here, as this function is called from |
| 201 | * __vfs_setxattr_noperm(). The caller of which has taken the inode's |
| 202 | * i_mutex lock. |
| 203 | */ |
| 204 | void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name, |
| 205 | const void *xattr_value, size_t xattr_value_len) |
| 206 | { |
| 207 | if (!evm_initialized || !evm_protected_xattr(xattr_name)) |
| 208 | return; |
| 209 | |
| 210 | evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * evm_inode_post_removexattr - update 'security.evm' after removing the xattr |
| 216 | * @dentry: pointer to the affected dentry |
| 217 | * @xattr_name: pointer to the affected extended attribute name |
| 218 | * |
| 219 | * Update the HMAC stored in 'security.evm' to reflect removal of the xattr. |
| 220 | */ |
| 221 | void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name) |
| 222 | { |
| 223 | struct inode *inode = dentry->d_inode; |
| 224 | |
| 225 | if (!evm_initialized || !evm_protected_xattr(xattr_name)) |
| 226 | return; |
| 227 | |
| 228 | mutex_lock(&inode->i_mutex); |
| 229 | evm_update_evmxattr(dentry, xattr_name, NULL, 0); |
| 230 | mutex_unlock(&inode->i_mutex); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * evm_inode_post_setattr - update 'security.evm' after modifying metadata |
| 236 | * @dentry: pointer to the affected dentry |
| 237 | * @ia_valid: for the UID and GID status |
| 238 | * |
| 239 | * For now, update the HMAC stored in 'security.evm' to reflect UID/GID |
| 240 | * changes. |
| 241 | * |
| 242 | * This function is called from notify_change(), which expects the caller |
| 243 | * to lock the inode's i_mutex. |
| 244 | */ |
| 245 | void evm_inode_post_setattr(struct dentry *dentry, int ia_valid) |
| 246 | { |
| 247 | if (!evm_initialized) |
| 248 | return; |
| 249 | |
| 250 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) |
| 251 | evm_update_evmxattr(dentry, NULL, NULL, 0); |
| 252 | return; |
| 253 | } |
| 254 | |
Mimi Zohar | cb723180 | 2011-03-09 14:40:44 -0500 | [diff] [blame] | 255 | /* |
| 256 | * evm_inode_init_security - initializes security.evm |
| 257 | */ |
| 258 | int evm_inode_init_security(struct inode *inode, |
| 259 | const struct xattr *lsm_xattr, |
| 260 | struct xattr *evm_xattr) |
| 261 | { |
| 262 | struct evm_ima_xattr_data *xattr_data; |
| 263 | int rc; |
| 264 | |
| 265 | if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name)) |
| 266 | return -EOPNOTSUPP; |
| 267 | |
| 268 | xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS); |
| 269 | if (!xattr_data) |
| 270 | return -ENOMEM; |
| 271 | |
| 272 | xattr_data->type = EVM_XATTR_HMAC; |
| 273 | rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest); |
| 274 | if (rc < 0) |
| 275 | goto out; |
| 276 | |
| 277 | evm_xattr->value = xattr_data; |
| 278 | evm_xattr->value_len = sizeof(*xattr_data); |
| 279 | evm_xattr->name = kstrdup(XATTR_EVM_SUFFIX, GFP_NOFS); |
| 280 | return 0; |
| 281 | out: |
| 282 | kfree(xattr_data); |
| 283 | return rc; |
| 284 | } |
| 285 | EXPORT_SYMBOL_GPL(evm_inode_init_security); |
| 286 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 287 | static int __init init_evm(void) |
| 288 | { |
| 289 | int error; |
| 290 | |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 291 | error = evm_init_secfs(); |
| 292 | if (error < 0) { |
| 293 | printk(KERN_INFO "EVM: Error registering secfs\n"); |
| 294 | goto err; |
| 295 | } |
| 296 | err: |
| 297 | return error; |
| 298 | } |
| 299 | |
| 300 | static void __exit cleanup_evm(void) |
| 301 | { |
| 302 | evm_cleanup_secfs(); |
Dmitry Kasatkin | d46eb36 | 2011-03-09 15:07:36 -0500 | [diff] [blame^] | 303 | if (hmac_tfm) |
| 304 | crypto_free_shash(hmac_tfm); |
Mimi Zohar | 66dbc325 | 2011-03-15 16:12:09 -0400 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* |
| 308 | * evm_display_config - list the EVM protected security extended attributes |
| 309 | */ |
| 310 | static int __init evm_display_config(void) |
| 311 | { |
| 312 | char **xattrname; |
| 313 | |
| 314 | for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) |
| 315 | printk(KERN_INFO "EVM: %s\n", *xattrname); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | pure_initcall(evm_display_config); |
| 320 | late_initcall(init_evm); |
| 321 | |
| 322 | MODULE_DESCRIPTION("Extended Verification Module"); |
| 323 | MODULE_LICENSE("GPL"); |