Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 IBM Corporation |
| 3 | * |
| 4 | * Author: |
| 5 | * Mimi Zohar <zohar@us.ibm.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, version 2 of the License. |
| 10 | */ |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/file.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/xattr.h> |
| 15 | #include <linux/magic.h> |
| 16 | #include <linux/ima.h> |
| 17 | #include <linux/evm.h> |
| 18 | |
| 19 | #include "ima.h" |
| 20 | |
| 21 | static int __init default_appraise_setup(char *str) |
| 22 | { |
| 23 | if (strncmp(str, "off", 3) == 0) |
| 24 | ima_appraise = 0; |
| 25 | else if (strncmp(str, "fix", 3) == 0) |
| 26 | ima_appraise = IMA_APPRAISE_FIX; |
| 27 | return 1; |
| 28 | } |
| 29 | |
| 30 | __setup("ima_appraise=", default_appraise_setup); |
| 31 | |
| 32 | /* |
| 33 | * ima_must_appraise - set appraise flag |
| 34 | * |
| 35 | * Return 1 to appraise |
| 36 | */ |
Dmitry Kasatkin | d26e1936 | 2012-09-27 18:26:53 +0300 | [diff] [blame] | 37 | int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 38 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 39 | if (!ima_appraise) |
| 40 | return 0; |
| 41 | |
| 42 | return ima_match_policy(inode, func, mask, IMA_APPRAISE); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Dmitry Kasatkin | def3e8b | 2012-09-20 22:38:53 +0300 | [diff] [blame] | 45 | static int ima_fix_xattr(struct dentry *dentry, |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 46 | struct integrity_iint_cache *iint) |
| 47 | { |
Mimi Zohar | 5a44b41 | 2012-01-09 22:59:36 -0500 | [diff] [blame] | 48 | iint->ima_xattr.type = IMA_XATTR_DIGEST; |
Dmitry Kasatkin | def3e8b | 2012-09-20 22:38:53 +0300 | [diff] [blame] | 49 | return __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA, |
| 50 | (u8 *)&iint->ima_xattr, |
| 51 | sizeof(iint->ima_xattr), 0); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* |
| 55 | * ima_appraise_measurement - appraise file measurement |
| 56 | * |
| 57 | * Call evm_verifyxattr() to verify the integrity of 'security.ima'. |
| 58 | * Assuming success, compare the xattr hash with the collected measurement. |
| 59 | * |
| 60 | * Return 0 on success, error code otherwise |
| 61 | */ |
| 62 | int ima_appraise_measurement(struct integrity_iint_cache *iint, |
| 63 | struct file *file, const unsigned char *filename) |
| 64 | { |
| 65 | struct dentry *dentry = file->f_dentry; |
| 66 | struct inode *inode = dentry->d_inode; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 67 | struct evm_ima_xattr_data *xattr_value = NULL; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 68 | enum integrity_status status = INTEGRITY_UNKNOWN; |
| 69 | const char *op = "appraise_data"; |
| 70 | char *cause = "unknown"; |
| 71 | int rc; |
| 72 | |
| 73 | if (!ima_appraise) |
| 74 | return 0; |
| 75 | if (!inode->i_op->getxattr) |
| 76 | return INTEGRITY_UNKNOWN; |
| 77 | |
| 78 | if (iint->flags & IMA_APPRAISED) |
| 79 | return iint->ima_status; |
| 80 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 81 | rc = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)&xattr_value, |
| 82 | 0, GFP_NOFS); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 83 | if (rc <= 0) { |
| 84 | if (rc && rc != -ENODATA) |
| 85 | goto out; |
| 86 | |
| 87 | cause = "missing-hash"; |
| 88 | status = |
| 89 | (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL; |
| 90 | goto out; |
| 91 | } |
| 92 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 93 | status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 94 | if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) { |
| 95 | if ((status == INTEGRITY_NOLABEL) |
| 96 | || (status == INTEGRITY_NOXATTRS)) |
| 97 | cause = "missing-HMAC"; |
| 98 | else if (status == INTEGRITY_FAIL) |
| 99 | cause = "invalid-HMAC"; |
| 100 | goto out; |
| 101 | } |
| 102 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 103 | switch (xattr_value->type) { |
| 104 | case IMA_XATTR_DIGEST: |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame^] | 105 | if (iint->flags & IMA_DIGSIG_REQUIRED) { |
| 106 | cause = "IMA signature required"; |
| 107 | status = INTEGRITY_FAIL; |
| 108 | break; |
| 109 | } |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 110 | rc = memcmp(xattr_value->digest, iint->ima_xattr.digest, |
| 111 | IMA_DIGEST_SIZE); |
| 112 | if (rc) { |
| 113 | cause = "invalid-hash"; |
| 114 | status = INTEGRITY_FAIL; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | status = INTEGRITY_PASS; |
| 118 | break; |
| 119 | case EVM_IMA_XATTR_DIGSIG: |
| 120 | iint->flags |= IMA_DIGSIG; |
| 121 | rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, |
| 122 | xattr_value->digest, rc - 1, |
| 123 | iint->ima_xattr.digest, |
| 124 | IMA_DIGEST_SIZE); |
| 125 | if (rc == -EOPNOTSUPP) { |
| 126 | status = INTEGRITY_UNKNOWN; |
| 127 | } else if (rc) { |
| 128 | cause = "invalid-signature"; |
| 129 | status = INTEGRITY_FAIL; |
| 130 | } else { |
| 131 | status = INTEGRITY_PASS; |
| 132 | } |
| 133 | break; |
| 134 | default: |
| 135 | status = INTEGRITY_UNKNOWN; |
| 136 | cause = "unknown-ima-data"; |
| 137 | break; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 138 | } |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 139 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 140 | out: |
| 141 | if (status != INTEGRITY_PASS) { |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 142 | if ((ima_appraise & IMA_APPRAISE_FIX) && |
| 143 | (!xattr_value || |
| 144 | xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { |
Dmitry Kasatkin | def3e8b | 2012-09-20 22:38:53 +0300 | [diff] [blame] | 145 | if (!ima_fix_xattr(dentry, iint)) |
| 146 | status = INTEGRITY_PASS; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 147 | } |
| 148 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, |
| 149 | op, cause, rc, 0); |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 150 | } else { |
| 151 | iint->flags |= IMA_APPRAISED; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 152 | } |
| 153 | iint->ima_status = status; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 154 | kfree(xattr_value); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 155 | return status; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * ima_update_xattr - update 'security.ima' hash value |
| 160 | */ |
| 161 | void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) |
| 162 | { |
| 163 | struct dentry *dentry = file->f_dentry; |
| 164 | int rc = 0; |
| 165 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 166 | /* do not collect and update hash for digital signatures */ |
| 167 | if (iint->flags & IMA_DIGSIG) |
| 168 | return; |
| 169 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 170 | rc = ima_collect_measurement(iint, file); |
| 171 | if (rc < 0) |
| 172 | return; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 173 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 174 | ima_fix_xattr(dentry, iint); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * ima_inode_post_setattr - reflect file metadata changes |
| 179 | * @dentry: pointer to the affected dentry |
| 180 | * |
| 181 | * Changes to a dentry's metadata might result in needing to appraise. |
| 182 | * |
| 183 | * This function is called from notify_change(), which expects the caller |
| 184 | * to lock the inode's i_mutex. |
| 185 | */ |
| 186 | void ima_inode_post_setattr(struct dentry *dentry) |
| 187 | { |
| 188 | struct inode *inode = dentry->d_inode; |
| 189 | struct integrity_iint_cache *iint; |
| 190 | int must_appraise, rc; |
| 191 | |
| 192 | if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode) |
| 193 | || !inode->i_op->removexattr) |
| 194 | return; |
| 195 | |
| 196 | must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR); |
| 197 | iint = integrity_iint_find(inode); |
| 198 | if (iint) { |
| 199 | if (must_appraise) |
| 200 | iint->flags |= IMA_APPRAISE; |
| 201 | else |
| 202 | iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED); |
| 203 | } |
| 204 | if (!must_appraise) |
| 205 | rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA); |
| 206 | return; |
| 207 | } |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * ima_protect_xattr - protect 'security.ima' |
| 211 | * |
| 212 | * Ensure that not just anyone can modify or remove 'security.ima'. |
| 213 | */ |
| 214 | static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, |
| 215 | const void *xattr_value, size_t xattr_value_len) |
| 216 | { |
| 217 | if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { |
| 218 | if (!capable(CAP_SYS_ADMIN)) |
| 219 | return -EPERM; |
| 220 | return 1; |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static void ima_reset_appraise_flags(struct inode *inode) |
| 226 | { |
| 227 | struct integrity_iint_cache *iint; |
| 228 | |
| 229 | if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)) |
| 230 | return; |
| 231 | |
| 232 | iint = integrity_iint_find(inode); |
| 233 | if (!iint) |
| 234 | return; |
| 235 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 236 | iint->flags &= ~IMA_DONE_MASK; |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | |
| 240 | int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, |
| 241 | const void *xattr_value, size_t xattr_value_len) |
| 242 | { |
| 243 | int result; |
| 244 | |
| 245 | result = ima_protect_xattr(dentry, xattr_name, xattr_value, |
| 246 | xattr_value_len); |
| 247 | if (result == 1) { |
| 248 | ima_reset_appraise_flags(dentry->d_inode); |
| 249 | result = 0; |
| 250 | } |
| 251 | return result; |
| 252 | } |
| 253 | |
| 254 | int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) |
| 255 | { |
| 256 | int result; |
| 257 | |
| 258 | result = ima_protect_xattr(dentry, xattr_name, NULL, 0); |
| 259 | if (result == 1) { |
| 260 | ima_reset_appraise_flags(dentry->d_inode); |
| 261 | result = 0; |
| 262 | } |
| 263 | return result; |
| 264 | } |