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> |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 18 | #include <crypto/hash_info.h> |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 19 | |
| 20 | #include "ima.h" |
| 21 | |
| 22 | static int __init default_appraise_setup(char *str) |
| 23 | { |
| 24 | if (strncmp(str, "off", 3) == 0) |
| 25 | ima_appraise = 0; |
Dmitry Kasatkin | 2faa6ef | 2014-05-08 13:11:29 +0300 | [diff] [blame^] | 26 | else if (strncmp(str, "log", 3) == 0) |
| 27 | ima_appraise = IMA_APPRAISE_LOG; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 28 | else if (strncmp(str, "fix", 3) == 0) |
| 29 | ima_appraise = IMA_APPRAISE_FIX; |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | __setup("ima_appraise=", default_appraise_setup); |
| 34 | |
| 35 | /* |
| 36 | * ima_must_appraise - set appraise flag |
| 37 | * |
| 38 | * Return 1 to appraise |
| 39 | */ |
Dmitry Kasatkin | d26e193 | 2012-09-27 18:26:53 +0300 | [diff] [blame] | 40 | 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] | 41 | { |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 42 | if (!ima_appraise) |
| 43 | return 0; |
| 44 | |
| 45 | return ima_match_policy(inode, func, mask, IMA_APPRAISE); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Dmitry Kasatkin | def3e8b | 2012-09-20 22:38:53 +0300 | [diff] [blame] | 48 | static int ima_fix_xattr(struct dentry *dentry, |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 49 | struct integrity_iint_cache *iint) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 50 | { |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 51 | int rc, offset; |
| 52 | u8 algo = iint->ima_hash->algo; |
| 53 | |
| 54 | if (algo <= HASH_ALGO_SHA1) { |
| 55 | offset = 1; |
| 56 | iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST; |
| 57 | } else { |
| 58 | offset = 0; |
| 59 | iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG; |
| 60 | iint->ima_hash->xattr.ng.algo = algo; |
| 61 | } |
| 62 | rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA, |
| 63 | &iint->ima_hash->xattr.data[offset], |
| 64 | (sizeof(iint->ima_hash->xattr) - offset) + |
| 65 | iint->ima_hash->length, 0); |
| 66 | return rc; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 69 | /* Return specific func appraised cached result */ |
| 70 | enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, |
| 71 | int func) |
| 72 | { |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 73 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 74 | case MMAP_CHECK: |
| 75 | return iint->ima_mmap_status; |
| 76 | case BPRM_CHECK: |
| 77 | return iint->ima_bprm_status; |
| 78 | case MODULE_CHECK: |
| 79 | return iint->ima_module_status; |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 80 | case FIRMWARE_CHECK: |
| 81 | return iint->ima_firmware_status; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 82 | case FILE_CHECK: |
| 83 | default: |
| 84 | return iint->ima_file_status; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static void ima_set_cache_status(struct integrity_iint_cache *iint, |
| 89 | int func, enum integrity_status status) |
| 90 | { |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 91 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 92 | case MMAP_CHECK: |
| 93 | iint->ima_mmap_status = status; |
| 94 | break; |
| 95 | case BPRM_CHECK: |
| 96 | iint->ima_bprm_status = status; |
| 97 | break; |
| 98 | case MODULE_CHECK: |
| 99 | iint->ima_module_status = status; |
| 100 | break; |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 101 | case FIRMWARE_CHECK: |
| 102 | iint->ima_firmware_status = status; |
| 103 | break; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 104 | case FILE_CHECK: |
| 105 | default: |
| 106 | iint->ima_file_status = status; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static void ima_cache_flags(struct integrity_iint_cache *iint, int func) |
| 112 | { |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 113 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 114 | case MMAP_CHECK: |
| 115 | iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED); |
| 116 | break; |
| 117 | case BPRM_CHECK: |
| 118 | iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED); |
| 119 | break; |
| 120 | case MODULE_CHECK: |
| 121 | iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED); |
| 122 | break; |
Mimi Zohar | 5a9196d | 2014-07-22 10:39:48 -0400 | [diff] [blame] | 123 | case FIRMWARE_CHECK: |
| 124 | iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED); |
| 125 | break; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 126 | case FILE_CHECK: |
| 127 | default: |
| 128 | iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 133 | void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len, |
| 134 | struct ima_digest_data *hash) |
| 135 | { |
| 136 | struct signature_v2_hdr *sig; |
| 137 | |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 138 | if (!xattr_value || xattr_len < 2) |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 139 | return; |
| 140 | |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 141 | switch (xattr_value->type) { |
| 142 | case EVM_IMA_XATTR_DIGSIG: |
| 143 | sig = (typeof(sig))xattr_value; |
| 144 | if (sig->version != 2 || xattr_len <= sizeof(*sig)) |
| 145 | return; |
| 146 | hash->algo = sig->hash_algo; |
| 147 | break; |
| 148 | case IMA_XATTR_DIGEST_NG: |
| 149 | hash->algo = xattr_value->digest[0]; |
| 150 | break; |
| 151 | case IMA_XATTR_DIGEST: |
| 152 | /* this is for backward compatibility */ |
| 153 | if (xattr_len == 21) { |
| 154 | unsigned int zero = 0; |
| 155 | if (!memcmp(&xattr_value->digest[16], &zero, 4)) |
| 156 | hash->algo = HASH_ALGO_MD5; |
| 157 | else |
| 158 | hash->algo = HASH_ALGO_SHA1; |
| 159 | } else if (xattr_len == 17) |
| 160 | hash->algo = HASH_ALGO_MD5; |
| 161 | break; |
| 162 | } |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | int ima_read_xattr(struct dentry *dentry, |
| 166 | struct evm_ima_xattr_data **xattr_value) |
| 167 | { |
| 168 | struct inode *inode = dentry->d_inode; |
| 169 | |
| 170 | if (!inode->i_op->getxattr) |
| 171 | return 0; |
| 172 | |
| 173 | return vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value, |
| 174 | 0, GFP_NOFS); |
| 175 | } |
| 176 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 177 | /* |
| 178 | * ima_appraise_measurement - appraise file measurement |
| 179 | * |
| 180 | * Call evm_verifyxattr() to verify the integrity of 'security.ima'. |
| 181 | * Assuming success, compare the xattr hash with the collected measurement. |
| 182 | * |
| 183 | * Return 0 on success, error code otherwise |
| 184 | */ |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 185 | int ima_appraise_measurement(int func, struct integrity_iint_cache *iint, |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 186 | struct file *file, const unsigned char *filename, |
| 187 | struct evm_ima_xattr_data *xattr_value, |
Dmitry Kasatkin | 3034a14 | 2014-06-27 18:15:44 +0300 | [diff] [blame] | 188 | int xattr_len, int opened) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 189 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 190 | static const char op[] = "appraise_data"; |
| 191 | char *cause = "unknown"; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 192 | struct dentry *dentry = file->f_dentry; |
| 193 | struct inode *inode = dentry->d_inode; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 194 | enum integrity_status status = INTEGRITY_UNKNOWN; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 195 | int rc = xattr_len, hash_start = 0; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 196 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 197 | if (!inode->i_op->getxattr) |
| 198 | return INTEGRITY_UNKNOWN; |
| 199 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 200 | if (rc <= 0) { |
| 201 | if (rc && rc != -ENODATA) |
| 202 | goto out; |
| 203 | |
| 204 | cause = "missing-hash"; |
Dmitry Kasatkin | b151d6b | 2014-06-27 18:04:27 +0300 | [diff] [blame] | 205 | status = INTEGRITY_NOLABEL; |
Dmitry Kasatkin | 3034a14 | 2014-06-27 18:15:44 +0300 | [diff] [blame] | 206 | if (opened & FILE_CREATED) { |
Dmitry Kasatkin | b151d6b | 2014-06-27 18:04:27 +0300 | [diff] [blame] | 207 | iint->flags |= IMA_NEW_FILE; |
| 208 | status = INTEGRITY_PASS; |
| 209 | } |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 210 | goto out; |
| 211 | } |
| 212 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 213 | status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 214 | if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) { |
| 215 | if ((status == INTEGRITY_NOLABEL) |
| 216 | || (status == INTEGRITY_NOXATTRS)) |
| 217 | cause = "missing-HMAC"; |
| 218 | else if (status == INTEGRITY_FAIL) |
| 219 | cause = "invalid-HMAC"; |
| 220 | goto out; |
| 221 | } |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 222 | switch (xattr_value->type) { |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 223 | case IMA_XATTR_DIGEST_NG: |
| 224 | /* first byte contains algorithm id */ |
| 225 | hash_start = 1; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 226 | case IMA_XATTR_DIGEST: |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 227 | if (iint->flags & IMA_DIGSIG_REQUIRED) { |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame] | 228 | cause = "IMA-signature-required"; |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 229 | status = INTEGRITY_FAIL; |
| 230 | break; |
| 231 | } |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 232 | if (xattr_len - sizeof(xattr_value->type) - hash_start >= |
| 233 | iint->ima_hash->length) |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 234 | /* xattr length may be longer. md5 hash in previous |
| 235 | version occupied 20 bytes in xattr, instead of 16 |
| 236 | */ |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 237 | rc = memcmp(&xattr_value->digest[hash_start], |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 238 | iint->ima_hash->digest, |
| 239 | iint->ima_hash->length); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 240 | else |
| 241 | rc = -EINVAL; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 242 | if (rc) { |
| 243 | cause = "invalid-hash"; |
| 244 | status = INTEGRITY_FAIL; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | status = INTEGRITY_PASS; |
| 248 | break; |
| 249 | case EVM_IMA_XATTR_DIGSIG: |
| 250 | iint->flags |= IMA_DIGSIG; |
| 251 | rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, |
Dmitry Kasatkin | b1aaab2 | 2013-10-10 16:12:03 +0900 | [diff] [blame] | 252 | (const char *)xattr_value, rc, |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 253 | iint->ima_hash->digest, |
| 254 | iint->ima_hash->length); |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 255 | if (rc == -EOPNOTSUPP) { |
| 256 | status = INTEGRITY_UNKNOWN; |
| 257 | } else if (rc) { |
| 258 | cause = "invalid-signature"; |
| 259 | status = INTEGRITY_FAIL; |
| 260 | } else { |
| 261 | status = INTEGRITY_PASS; |
| 262 | } |
| 263 | break; |
| 264 | default: |
| 265 | status = INTEGRITY_UNKNOWN; |
| 266 | cause = "unknown-ima-data"; |
| 267 | break; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 268 | } |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 269 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 270 | out: |
| 271 | if (status != INTEGRITY_PASS) { |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 272 | if ((ima_appraise & IMA_APPRAISE_FIX) && |
| 273 | (!xattr_value || |
| 274 | xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { |
Dmitry Kasatkin | def3e8b | 2012-09-20 22:38:53 +0300 | [diff] [blame] | 275 | if (!ima_fix_xattr(dentry, iint)) |
| 276 | status = INTEGRITY_PASS; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 277 | } |
| 278 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, |
| 279 | op, cause, rc, 0); |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 280 | } else { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 281 | ima_cache_flags(iint, func); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 282 | } |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 283 | ima_set_cache_status(iint, func, status); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 284 | return status; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * ima_update_xattr - update 'security.ima' hash value |
| 289 | */ |
| 290 | void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) |
| 291 | { |
| 292 | struct dentry *dentry = file->f_dentry; |
| 293 | int rc = 0; |
| 294 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 295 | /* do not collect and update hash for digital signatures */ |
| 296 | if (iint->flags & IMA_DIGSIG) |
| 297 | return; |
| 298 | |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 299 | rc = ima_collect_measurement(iint, file, NULL, NULL); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 300 | if (rc < 0) |
| 301 | return; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 302 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 303 | ima_fix_xattr(dentry, iint); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * ima_inode_post_setattr - reflect file metadata changes |
| 308 | * @dentry: pointer to the affected dentry |
| 309 | * |
| 310 | * Changes to a dentry's metadata might result in needing to appraise. |
| 311 | * |
| 312 | * This function is called from notify_change(), which expects the caller |
| 313 | * to lock the inode's i_mutex. |
| 314 | */ |
| 315 | void ima_inode_post_setattr(struct dentry *dentry) |
| 316 | { |
| 317 | struct inode *inode = dentry->d_inode; |
| 318 | struct integrity_iint_cache *iint; |
| 319 | int must_appraise, rc; |
| 320 | |
| 321 | if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode) |
| 322 | || !inode->i_op->removexattr) |
| 323 | return; |
| 324 | |
| 325 | must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR); |
| 326 | iint = integrity_iint_find(inode); |
| 327 | if (iint) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 328 | iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED | |
| 329 | IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK | |
| 330 | IMA_ACTION_FLAGS); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 331 | if (must_appraise) |
| 332 | iint->flags |= IMA_APPRAISE; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 333 | } |
| 334 | if (!must_appraise) |
| 335 | rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA); |
| 336 | return; |
| 337 | } |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * ima_protect_xattr - protect 'security.ima' |
| 341 | * |
| 342 | * Ensure that not just anyone can modify or remove 'security.ima'. |
| 343 | */ |
| 344 | static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, |
| 345 | const void *xattr_value, size_t xattr_value_len) |
| 346 | { |
| 347 | if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { |
| 348 | if (!capable(CAP_SYS_ADMIN)) |
| 349 | return -EPERM; |
| 350 | return 1; |
| 351 | } |
| 352 | return 0; |
| 353 | } |
| 354 | |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 355 | static void ima_reset_appraise_flags(struct inode *inode, int digsig) |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 356 | { |
| 357 | struct integrity_iint_cache *iint; |
| 358 | |
| 359 | if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)) |
| 360 | return; |
| 361 | |
| 362 | iint = integrity_iint_find(inode); |
| 363 | if (!iint) |
| 364 | return; |
| 365 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 366 | iint->flags &= ~IMA_DONE_MASK; |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 367 | if (digsig) |
| 368 | iint->flags |= IMA_DIGSIG; |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 369 | return; |
| 370 | } |
| 371 | |
| 372 | int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, |
| 373 | const void *xattr_value, size_t xattr_value_len) |
| 374 | { |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 375 | const struct evm_ima_xattr_data *xvalue = xattr_value; |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 376 | int result; |
| 377 | |
| 378 | result = ima_protect_xattr(dentry, xattr_name, xattr_value, |
| 379 | xattr_value_len); |
| 380 | if (result == 1) { |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 381 | ima_reset_appraise_flags(dentry->d_inode, |
| 382 | (xvalue->type == EVM_IMA_XATTR_DIGSIG) ? 1 : 0); |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 383 | result = 0; |
| 384 | } |
| 385 | return result; |
| 386 | } |
| 387 | |
| 388 | int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) |
| 389 | { |
| 390 | int result; |
| 391 | |
| 392 | result = ima_protect_xattr(dentry, xattr_name, NULL, 0); |
| 393 | if (result == 1) { |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 394 | ima_reset_appraise_flags(dentry->d_inode, 0); |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 395 | result = 0; |
| 396 | } |
| 397 | return result; |
| 398 | } |