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