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