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