Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 IBM Corporation |
| 4 | * |
| 5 | * Author: |
| 6 | * Mimi Zohar <zohar@us.ibm.com> |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 7 | */ |
Paul Gortmaker | 876979c | 2018-12-09 15:36:29 -0500 | [diff] [blame] | 8 | #include <linux/init.h> |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 9 | #include <linux/file.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/xattr.h> |
| 12 | #include <linux/magic.h> |
| 13 | #include <linux/ima.h> |
| 14 | #include <linux/evm.h> |
| 15 | |
| 16 | #include "ima.h" |
| 17 | |
| 18 | static int __init default_appraise_setup(char *str) |
| 19 | { |
Mimi Zohar | e1f5e01 | 2017-04-24 22:06:49 -0400 | [diff] [blame] | 20 | #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 21 | if (strncmp(str, "off", 3) == 0) |
| 22 | ima_appraise = 0; |
Dmitry Kasatkin | 2faa6ef | 2014-05-08 13:11:29 +0300 | [diff] [blame] | 23 | else if (strncmp(str, "log", 3) == 0) |
| 24 | ima_appraise = IMA_APPRAISE_LOG; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 25 | else if (strncmp(str, "fix", 3) == 0) |
| 26 | ima_appraise = IMA_APPRAISE_FIX; |
Mimi Zohar | e1f5e01 | 2017-04-24 22:06:49 -0400 | [diff] [blame] | 27 | #endif |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | __setup("ima_appraise=", default_appraise_setup); |
| 32 | |
| 33 | /* |
Mimi Zohar | 6f6723e | 2017-04-24 22:43:52 -0400 | [diff] [blame] | 34 | * is_ima_appraise_enabled - return appraise status |
| 35 | * |
| 36 | * Only return enabled, if not in ima_appraise="fix" or "log" modes. |
| 37 | */ |
| 38 | bool is_ima_appraise_enabled(void) |
| 39 | { |
Thiago Jung Bauermann | e5729f8 | 2017-10-17 22:53:14 -0200 | [diff] [blame] | 40 | return ima_appraise & IMA_APPRAISE_ENFORCE; |
Mimi Zohar | 6f6723e | 2017-04-24 22:43:52 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 44 | * ima_must_appraise - set appraise flag |
| 45 | * |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 46 | * Return 1 to appraise or hash |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 47 | */ |
Dmitry Kasatkin | d26e193 | 2012-09-27 18:26:53 +0300 | [diff] [blame] | 48 | 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] | 49 | { |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 50 | u32 secid; |
| 51 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 52 | if (!ima_appraise) |
| 53 | return 0; |
| 54 | |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 55 | security_task_getsecid(current, &secid); |
| 56 | return ima_match_policy(inode, current_cred(), secid, func, mask, |
| 57 | IMA_APPRAISE | IMA_HASH, NULL); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Dmitry Kasatkin | def3e8b | 2012-09-20 22:38:53 +0300 | [diff] [blame] | 60 | static int ima_fix_xattr(struct dentry *dentry, |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 61 | struct integrity_iint_cache *iint) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 62 | { |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 63 | int rc, offset; |
| 64 | u8 algo = iint->ima_hash->algo; |
| 65 | |
| 66 | if (algo <= HASH_ALGO_SHA1) { |
| 67 | offset = 1; |
| 68 | iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST; |
| 69 | } else { |
| 70 | offset = 0; |
| 71 | iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG; |
| 72 | iint->ima_hash->xattr.ng.algo = algo; |
| 73 | } |
| 74 | rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA, |
| 75 | &iint->ima_hash->xattr.data[offset], |
| 76 | (sizeof(iint->ima_hash->xattr) - offset) + |
| 77 | iint->ima_hash->length, 0); |
| 78 | return rc; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 81 | /* Return specific func appraised cached result */ |
| 82 | enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 83 | enum ima_hooks func) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 84 | { |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 85 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 86 | case MMAP_CHECK: |
| 87 | return iint->ima_mmap_status; |
| 88 | case BPRM_CHECK: |
| 89 | return iint->ima_bprm_status; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 90 | case CREDS_CHECK: |
| 91 | return iint->ima_creds_status; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 92 | case FILE_CHECK: |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 93 | case POST_SETATTR: |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 94 | return iint->ima_file_status; |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 95 | case MODULE_CHECK ... MAX_CHECK - 1: |
| 96 | default: |
| 97 | return iint->ima_read_status; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | static void ima_set_cache_status(struct integrity_iint_cache *iint, |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 102 | enum ima_hooks func, |
| 103 | enum integrity_status status) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 104 | { |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 105 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 106 | case MMAP_CHECK: |
| 107 | iint->ima_mmap_status = status; |
| 108 | break; |
| 109 | case BPRM_CHECK: |
| 110 | iint->ima_bprm_status = status; |
| 111 | break; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 112 | case CREDS_CHECK: |
| 113 | iint->ima_creds_status = status; |
Gustavo A. R. Silva | 09186e5 | 2019-02-08 14:54:53 -0600 | [diff] [blame] | 114 | break; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 115 | case FILE_CHECK: |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 116 | case POST_SETATTR: |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 117 | iint->ima_file_status = status; |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 118 | break; |
| 119 | case MODULE_CHECK ... MAX_CHECK - 1: |
| 120 | default: |
| 121 | iint->ima_read_status = status; |
| 122 | break; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 126 | static void ima_cache_flags(struct integrity_iint_cache *iint, |
| 127 | enum ima_hooks func) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 128 | { |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 129 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 130 | case MMAP_CHECK: |
| 131 | iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED); |
| 132 | break; |
| 133 | case BPRM_CHECK: |
| 134 | iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED); |
| 135 | break; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 136 | case CREDS_CHECK: |
| 137 | iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED); |
| 138 | break; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 139 | case FILE_CHECK: |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 140 | case POST_SETATTR: |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 141 | iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED); |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 142 | break; |
| 143 | case MODULE_CHECK ... MAX_CHECK - 1: |
| 144 | default: |
| 145 | iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED); |
| 146 | break; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 150 | enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, |
| 151 | int xattr_len) |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 152 | { |
| 153 | struct signature_v2_hdr *sig; |
Seth Forshee | b4bfec7 | 2016-08-01 08:19:10 -0500 | [diff] [blame] | 154 | enum hash_algo ret; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 155 | |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 156 | if (!xattr_value || xattr_len < 2) |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 157 | /* return default hash algo */ |
| 158 | return ima_hash_algo; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 159 | |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 160 | switch (xattr_value->type) { |
| 161 | case EVM_IMA_XATTR_DIGSIG: |
| 162 | sig = (typeof(sig))xattr_value; |
| 163 | if (sig->version != 2 || xattr_len <= sizeof(*sig)) |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 164 | return ima_hash_algo; |
| 165 | return sig->hash_algo; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 166 | break; |
| 167 | case IMA_XATTR_DIGEST_NG: |
Seth Forshee | b4bfec7 | 2016-08-01 08:19:10 -0500 | [diff] [blame] | 168 | ret = xattr_value->digest[0]; |
| 169 | if (ret < HASH_ALGO__LAST) |
| 170 | return ret; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 171 | break; |
| 172 | case IMA_XATTR_DIGEST: |
| 173 | /* this is for backward compatibility */ |
| 174 | if (xattr_len == 21) { |
| 175 | unsigned int zero = 0; |
| 176 | if (!memcmp(&xattr_value->digest[16], &zero, 4)) |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 177 | return HASH_ALGO_MD5; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 178 | else |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 179 | return HASH_ALGO_SHA1; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 180 | } else if (xattr_len == 17) |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 181 | return HASH_ALGO_MD5; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 182 | break; |
| 183 | } |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 184 | |
| 185 | /* return default hash algo */ |
| 186 | return ima_hash_algo; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | int ima_read_xattr(struct dentry *dentry, |
| 190 | struct evm_ima_xattr_data **xattr_value) |
| 191 | { |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 192 | ssize_t ret; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 193 | |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 194 | ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value, |
| 195 | 0, GFP_NOFS); |
| 196 | if (ret == -EOPNOTSUPP) |
| 197 | ret = 0; |
| 198 | return ret; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 199 | } |
| 200 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 201 | /* |
| 202 | * ima_appraise_measurement - appraise file measurement |
| 203 | * |
| 204 | * Call evm_verifyxattr() to verify the integrity of 'security.ima'. |
| 205 | * Assuming success, compare the xattr hash with the collected measurement. |
| 206 | * |
| 207 | * Return 0 on success, error code otherwise |
| 208 | */ |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 209 | int ima_appraise_measurement(enum ima_hooks func, |
| 210 | struct integrity_iint_cache *iint, |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 211 | struct file *file, const unsigned char *filename, |
| 212 | struct evm_ima_xattr_data *xattr_value, |
Al Viro | 6035a27 | 2018-06-08 13:40:10 -0400 | [diff] [blame] | 213 | int xattr_len) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 214 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 215 | static const char op[] = "appraise_data"; |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 216 | const char *cause = "unknown"; |
Miklos Szeredi | e71b9df | 2016-09-16 12:44:20 +0200 | [diff] [blame] | 217 | struct dentry *dentry = file_dentry(file); |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 218 | struct inode *inode = d_backing_inode(dentry); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 219 | enum integrity_status status = INTEGRITY_UNKNOWN; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 220 | int rc = xattr_len, hash_start = 0; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 221 | |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 222 | if (!(inode->i_opflags & IOP_XATTR)) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 223 | return INTEGRITY_UNKNOWN; |
| 224 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 225 | if (rc <= 0) { |
| 226 | if (rc && rc != -ENODATA) |
| 227 | goto out; |
| 228 | |
Thiago Jung Bauermann | 915d9d2 | 2017-06-07 22:49:12 -0300 | [diff] [blame] | 229 | cause = iint->flags & IMA_DIGSIG_REQUIRED ? |
| 230 | "IMA-signature-required" : "missing-hash"; |
Dmitry Kasatkin | b151d6b | 2014-06-27 18:04:27 +0300 | [diff] [blame] | 231 | status = INTEGRITY_NOLABEL; |
Al Viro | 6035a27 | 2018-06-08 13:40:10 -0400 | [diff] [blame] | 232 | if (file->f_mode & FMODE_CREATED) |
Dmitry Kasatkin | b151d6b | 2014-06-27 18:04:27 +0300 | [diff] [blame] | 233 | iint->flags |= IMA_NEW_FILE; |
Daniel Glöckner | 1ac202e | 2017-02-24 15:05:14 +0100 | [diff] [blame] | 234 | if ((iint->flags & IMA_NEW_FILE) && |
Mimi Zohar | b7e27bc | 2017-11-08 07:38:28 -0500 | [diff] [blame] | 235 | (!(iint->flags & IMA_DIGSIG_REQUIRED) || |
| 236 | (inode->i_size == 0))) |
Dmitry Kasatkin | b151d6b | 2014-06-27 18:04:27 +0300 | [diff] [blame] | 237 | status = INTEGRITY_PASS; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 238 | goto out; |
| 239 | } |
| 240 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 241 | status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint); |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 242 | switch (status) { |
| 243 | case INTEGRITY_PASS: |
| 244 | case INTEGRITY_PASS_IMMUTABLE: |
| 245 | case INTEGRITY_UNKNOWN: |
| 246 | break; |
| 247 | case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */ |
| 248 | case INTEGRITY_NOLABEL: /* No security.evm xattr. */ |
| 249 | cause = "missing-HMAC"; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 250 | goto out; |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 251 | case INTEGRITY_FAIL: /* Invalid HMAC/signature. */ |
| 252 | cause = "invalid-HMAC"; |
| 253 | goto out; |
| 254 | default: |
| 255 | WARN_ONCE(true, "Unexpected integrity status %d\n", status); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 256 | } |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 257 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 258 | switch (xattr_value->type) { |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 259 | case IMA_XATTR_DIGEST_NG: |
| 260 | /* first byte contains algorithm id */ |
| 261 | hash_start = 1; |
Thiago Jung Bauermann | bb543e3 | 2017-06-07 22:49:10 -0300 | [diff] [blame] | 262 | /* fall through */ |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 263 | case IMA_XATTR_DIGEST: |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 264 | if (iint->flags & IMA_DIGSIG_REQUIRED) { |
Richard Guy Briggs | 7e9001f | 2014-06-16 15:52:07 -0400 | [diff] [blame] | 265 | cause = "IMA-signature-required"; |
Dmitry Kasatkin | 0e5a247 | 2012-06-08 13:58:49 +0300 | [diff] [blame] | 266 | status = INTEGRITY_FAIL; |
| 267 | break; |
| 268 | } |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 269 | clear_bit(IMA_DIGSIG, &iint->atomic_flags); |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 270 | if (xattr_len - sizeof(xattr_value->type) - hash_start >= |
| 271 | iint->ima_hash->length) |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 272 | /* xattr length may be longer. md5 hash in previous |
| 273 | version occupied 20 bytes in xattr, instead of 16 |
| 274 | */ |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 275 | rc = memcmp(&xattr_value->digest[hash_start], |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 276 | iint->ima_hash->digest, |
| 277 | iint->ima_hash->length); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 278 | else |
| 279 | rc = -EINVAL; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 280 | if (rc) { |
| 281 | cause = "invalid-hash"; |
| 282 | status = INTEGRITY_FAIL; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 283 | break; |
| 284 | } |
| 285 | status = INTEGRITY_PASS; |
| 286 | break; |
| 287 | case EVM_IMA_XATTR_DIGSIG: |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 288 | set_bit(IMA_DIGSIG, &iint->atomic_flags); |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 289 | rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, |
Nayna Jain | d7cecb6 | 2018-12-09 01:57:05 +0530 | [diff] [blame] | 290 | (const char *)xattr_value, |
| 291 | xattr_len, |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 292 | iint->ima_hash->digest, |
| 293 | iint->ima_hash->length); |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 294 | if (rc == -EOPNOTSUPP) { |
| 295 | status = INTEGRITY_UNKNOWN; |
Nayna Jain | d7cecb6 | 2018-12-09 01:57:05 +0530 | [diff] [blame] | 296 | break; |
| 297 | } |
| 298 | if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && |
| 299 | func == KEXEC_KERNEL_CHECK) |
| 300 | rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM, |
| 301 | (const char *)xattr_value, |
| 302 | xattr_len, |
| 303 | iint->ima_hash->digest, |
| 304 | iint->ima_hash->length); |
| 305 | if (rc) { |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 306 | cause = "invalid-signature"; |
| 307 | status = INTEGRITY_FAIL; |
| 308 | } else { |
| 309 | status = INTEGRITY_PASS; |
| 310 | } |
| 311 | break; |
| 312 | default: |
| 313 | status = INTEGRITY_UNKNOWN; |
| 314 | cause = "unknown-ima-data"; |
| 315 | break; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 316 | } |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 317 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 318 | out: |
Mimi Zohar | 57b56ac | 2018-02-21 11:33:37 -0500 | [diff] [blame] | 319 | /* |
| 320 | * File signatures on some filesystems can not be properly verified. |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 321 | * When such filesystems are mounted by an untrusted mounter or on a |
| 322 | * system not willing to accept such a risk, fail the file signature |
| 323 | * verification. |
Mimi Zohar | 57b56ac | 2018-02-21 11:33:37 -0500 | [diff] [blame] | 324 | */ |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 325 | if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && |
| 326 | ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) || |
| 327 | (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) { |
Mimi Zohar | 57b56ac | 2018-02-21 11:33:37 -0500 | [diff] [blame] | 328 | status = INTEGRITY_FAIL; |
| 329 | cause = "unverifiable-signature"; |
| 330 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, |
| 331 | op, cause, rc, 0); |
| 332 | } else if (status != INTEGRITY_PASS) { |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 333 | /* Fix mode, but don't replace file signatures. */ |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 334 | if ((ima_appraise & IMA_APPRAISE_FIX) && |
| 335 | (!xattr_value || |
| 336 | xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { |
Dmitry Kasatkin | def3e8b | 2012-09-20 22:38:53 +0300 | [diff] [blame] | 337 | if (!ima_fix_xattr(dentry, iint)) |
| 338 | status = INTEGRITY_PASS; |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /* Permit new files with file signatures, but without data. */ |
| 342 | if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE && |
| 343 | xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) { |
Mimi Zohar | 05d1a71 | 2016-02-29 19:52:05 -0500 | [diff] [blame] | 344 | status = INTEGRITY_PASS; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 345 | } |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 346 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 347 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, |
| 348 | op, cause, rc, 0); |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 349 | } else { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 350 | ima_cache_flags(iint, func); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 351 | } |
Mimi Zohar | 57b56ac | 2018-02-21 11:33:37 -0500 | [diff] [blame] | 352 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 353 | ima_set_cache_status(iint, func, status); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 354 | return status; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * ima_update_xattr - update 'security.ima' hash value |
| 359 | */ |
| 360 | void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) |
| 361 | { |
Miklos Szeredi | e71b9df | 2016-09-16 12:44:20 +0200 | [diff] [blame] | 362 | struct dentry *dentry = file_dentry(file); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 363 | int rc = 0; |
| 364 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 365 | /* do not collect and update hash for digital signatures */ |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 366 | if (test_bit(IMA_DIGSIG, &iint->atomic_flags)) |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 367 | return; |
| 368 | |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 369 | if ((iint->ima_file_status != INTEGRITY_PASS) && |
| 370 | !(iint->flags & IMA_HASH)) |
Roberto Sassu | 020aae3 | 2017-11-07 11:37:07 +0100 | [diff] [blame] | 371 | return; |
| 372 | |
Mimi Zohar | cf22221 | 2016-01-14 17:57:47 -0500 | [diff] [blame] | 373 | rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 374 | if (rc < 0) |
| 375 | return; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 376 | |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 377 | inode_lock(file_inode(file)); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 378 | ima_fix_xattr(dentry, iint); |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 379 | inode_unlock(file_inode(file)); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /** |
| 383 | * ima_inode_post_setattr - reflect file metadata changes |
| 384 | * @dentry: pointer to the affected dentry |
| 385 | * |
| 386 | * Changes to a dentry's metadata might result in needing to appraise. |
| 387 | * |
| 388 | * This function is called from notify_change(), which expects the caller |
| 389 | * to lock the inode's i_mutex. |
| 390 | */ |
| 391 | void ima_inode_post_setattr(struct dentry *dentry) |
| 392 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 393 | struct inode *inode = d_backing_inode(dentry); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 394 | struct integrity_iint_cache *iint; |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 395 | int action; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 396 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 397 | if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 398 | || !(inode->i_opflags & IOP_XATTR)) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 399 | return; |
| 400 | |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 401 | action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR); |
| 402 | if (!action) |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 403 | __vfs_removexattr(dentry, XATTR_NAME_IMA); |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 404 | iint = integrity_iint_find(inode); |
| 405 | if (iint) { |
| 406 | set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags); |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 407 | if (!action) |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 408 | clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); |
| 409 | } |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 410 | } |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * ima_protect_xattr - protect 'security.ima' |
| 414 | * |
| 415 | * Ensure that not just anyone can modify or remove 'security.ima'. |
| 416 | */ |
| 417 | static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, |
| 418 | const void *xattr_value, size_t xattr_value_len) |
| 419 | { |
| 420 | if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { |
| 421 | if (!capable(CAP_SYS_ADMIN)) |
| 422 | return -EPERM; |
| 423 | return 1; |
| 424 | } |
| 425 | return 0; |
| 426 | } |
| 427 | |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 428 | static void ima_reset_appraise_flags(struct inode *inode, int digsig) |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 429 | { |
| 430 | struct integrity_iint_cache *iint; |
| 431 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 432 | if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)) |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 433 | return; |
| 434 | |
| 435 | iint = integrity_iint_find(inode); |
| 436 | if (!iint) |
| 437 | return; |
Eric Richter | a422638 | 2016-06-01 13:14:06 -0500 | [diff] [blame] | 438 | iint->measured_pcrs = 0; |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 439 | set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags); |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 440 | if (digsig) |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 441 | set_bit(IMA_DIGSIG, &iint->atomic_flags); |
| 442 | else |
| 443 | clear_bit(IMA_DIGSIG, &iint->atomic_flags); |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, |
| 447 | const void *xattr_value, size_t xattr_value_len) |
| 448 | { |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 449 | const struct evm_ima_xattr_data *xvalue = xattr_value; |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 450 | int result; |
| 451 | |
| 452 | result = ima_protect_xattr(dentry, xattr_name, xattr_value, |
| 453 | xattr_value_len); |
| 454 | if (result == 1) { |
Dmitry Kasatkin | a48fda9 | 2014-10-28 13:31:22 +0200 | [diff] [blame] | 455 | if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST)) |
| 456 | return -EINVAL; |
Mimi Zohar | f5acb3d | 2016-11-02 09:14:16 -0400 | [diff] [blame] | 457 | ima_reset_appraise_flags(d_backing_inode(dentry), |
Thiago Jung Bauermann | e5729f8 | 2017-10-17 22:53:14 -0200 | [diff] [blame] | 458 | xvalue->type == EVM_IMA_XATTR_DIGSIG); |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 459 | result = 0; |
| 460 | } |
| 461 | return result; |
| 462 | } |
| 463 | |
| 464 | int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) |
| 465 | { |
| 466 | int result; |
| 467 | |
| 468 | result = ima_protect_xattr(dentry, xattr_name, NULL, 0); |
| 469 | if (result == 1) { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 470 | ima_reset_appraise_flags(d_backing_inode(dentry), 0); |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 471 | result = 0; |
| 472 | } |
| 473 | return result; |
| 474 | } |