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 | */ |
Ard Biesheuvel | b000d5c | 2020-10-13 10:18:04 +0200 | [diff] [blame] | 8 | #include <linux/module.h> |
Paul Gortmaker | 876979c | 2018-12-09 15:36:29 -0500 | [diff] [blame] | 9 | #include <linux/init.h> |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 10 | #include <linux/file.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/xattr.h> |
| 13 | #include <linux/magic.h> |
| 14 | #include <linux/ima.h> |
| 15 | #include <linux/evm.h> |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 16 | #include <keys/system_keyring.h> |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 17 | |
| 18 | #include "ima.h" |
| 19 | |
Mimi Zohar | e1f5e01 | 2017-04-24 22:06:49 -0400 | [diff] [blame] | 20 | #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM |
Ard Biesheuvel | b000d5c | 2020-10-13 10:18:04 +0200 | [diff] [blame] | 21 | static char *ima_appraise_cmdline_default __initdata; |
| 22 | core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0); |
| 23 | |
| 24 | void __init ima_appraise_parse_cmdline(void) |
| 25 | { |
| 26 | const char *str = ima_appraise_cmdline_default; |
Bruno Meneguele | e4d7e2d | 2020-09-04 22:20:20 -0300 | [diff] [blame] | 27 | bool sb_state = arch_ima_get_secureboot(); |
| 28 | int appraisal_state = ima_appraise; |
Bruno Meneguele | 311aa6a | 2020-07-13 13:48:30 -0300 | [diff] [blame] | 29 | |
Ard Biesheuvel | b000d5c | 2020-10-13 10:18:04 +0200 | [diff] [blame] | 30 | if (!str) |
| 31 | return; |
| 32 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 33 | if (strncmp(str, "off", 3) == 0) |
Bruno Meneguele | e4d7e2d | 2020-09-04 22:20:20 -0300 | [diff] [blame] | 34 | appraisal_state = 0; |
Dmitry Kasatkin | 2faa6ef | 2014-05-08 13:11:29 +0300 | [diff] [blame] | 35 | else if (strncmp(str, "log", 3) == 0) |
Bruno Meneguele | e4d7e2d | 2020-09-04 22:20:20 -0300 | [diff] [blame] | 36 | appraisal_state = IMA_APPRAISE_LOG; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 37 | else if (strncmp(str, "fix", 3) == 0) |
Bruno Meneguele | e4d7e2d | 2020-09-04 22:20:20 -0300 | [diff] [blame] | 38 | appraisal_state = IMA_APPRAISE_FIX; |
Bruno Meneguele | 4afb28a | 2020-09-04 16:40:57 -0300 | [diff] [blame] | 39 | else if (strncmp(str, "enforce", 7) == 0) |
Bruno Meneguele | e4d7e2d | 2020-09-04 22:20:20 -0300 | [diff] [blame] | 40 | appraisal_state = IMA_APPRAISE_ENFORCE; |
Bruno Meneguele | 7fe2bb7 | 2020-09-04 16:40:58 -0300 | [diff] [blame] | 41 | else |
| 42 | pr_err("invalid \"%s\" appraise option", str); |
Bruno Meneguele | e4d7e2d | 2020-09-04 22:20:20 -0300 | [diff] [blame] | 43 | |
| 44 | /* If appraisal state was changed, but secure boot is enabled, |
| 45 | * keep its default */ |
| 46 | if (sb_state) { |
| 47 | if (!(appraisal_state & IMA_APPRAISE_ENFORCE)) |
| 48 | pr_info("Secure boot enabled: ignoring ima_appraise=%s option", |
| 49 | str); |
| 50 | } else { |
| 51 | ima_appraise = appraisal_state; |
| 52 | } |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 53 | } |
Ard Biesheuvel | b000d5c | 2020-10-13 10:18:04 +0200 | [diff] [blame] | 54 | #endif |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 55 | |
| 56 | /* |
Mimi Zohar | 6f6723e | 2017-04-24 22:43:52 -0400 | [diff] [blame] | 57 | * is_ima_appraise_enabled - return appraise status |
| 58 | * |
| 59 | * Only return enabled, if not in ima_appraise="fix" or "log" modes. |
| 60 | */ |
| 61 | bool is_ima_appraise_enabled(void) |
| 62 | { |
Thiago Jung Bauermann | e5729f8 | 2017-10-17 22:53:14 -0200 | [diff] [blame] | 63 | return ima_appraise & IMA_APPRAISE_ENFORCE; |
Mimi Zohar | 6f6723e | 2017-04-24 22:43:52 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 67 | * ima_must_appraise - set appraise flag |
| 68 | * |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 69 | * Return 1 to appraise or hash |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 70 | */ |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 71 | int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode, |
| 72 | int mask, enum ima_hooks func) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 73 | { |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 74 | u32 secid; |
| 75 | |
Mimi Zohar | 07f6a79 | 2011-03-09 22:25:48 -0500 | [diff] [blame] | 76 | if (!ima_appraise) |
| 77 | return 0; |
| 78 | |
Paul Moore | 4ebd765 | 2021-02-19 14:26:21 -0500 | [diff] [blame] | 79 | security_task_getsecid_subj(current, &secid); |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 80 | return ima_match_policy(mnt_userns, inode, current_cred(), secid, func, |
| 81 | mask, IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Dmitry Kasatkin | def3e8b | 2012-09-20 22:38:53 +0300 | [diff] [blame] | 84 | static int ima_fix_xattr(struct dentry *dentry, |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 85 | struct integrity_iint_cache *iint) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 86 | { |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 87 | int rc, offset; |
| 88 | u8 algo = iint->ima_hash->algo; |
| 89 | |
| 90 | if (algo <= HASH_ALGO_SHA1) { |
| 91 | offset = 1; |
| 92 | iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST; |
| 93 | } else { |
| 94 | offset = 0; |
| 95 | iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG; |
| 96 | iint->ima_hash->xattr.ng.algo = algo; |
| 97 | } |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 98 | rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA, |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 99 | &iint->ima_hash->xattr.data[offset], |
| 100 | (sizeof(iint->ima_hash->xattr) - offset) + |
| 101 | iint->ima_hash->length, 0); |
| 102 | return rc; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 105 | /* Return specific func appraised cached result */ |
| 106 | enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 107 | enum ima_hooks func) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 108 | { |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 109 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 110 | case MMAP_CHECK: |
| 111 | return iint->ima_mmap_status; |
| 112 | case BPRM_CHECK: |
| 113 | return iint->ima_bprm_status; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 114 | case CREDS_CHECK: |
| 115 | return iint->ima_creds_status; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 116 | case FILE_CHECK: |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 117 | case POST_SETATTR: |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 118 | return iint->ima_file_status; |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 119 | case MODULE_CHECK ... MAX_CHECK - 1: |
| 120 | default: |
| 121 | return iint->ima_read_status; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | static void ima_set_cache_status(struct integrity_iint_cache *iint, |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 126 | enum ima_hooks func, |
| 127 | enum integrity_status status) |
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->ima_mmap_status = status; |
| 132 | break; |
| 133 | case BPRM_CHECK: |
| 134 | iint->ima_bprm_status = status; |
| 135 | break; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 136 | case CREDS_CHECK: |
| 137 | iint->ima_creds_status = status; |
Gustavo A. R. Silva | 09186e5 | 2019-02-08 14:54:53 -0600 | [diff] [blame] | 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->ima_file_status = status; |
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->ima_read_status = status; |
| 146 | break; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 150 | static void ima_cache_flags(struct integrity_iint_cache *iint, |
| 151 | enum ima_hooks func) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 152 | { |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 153 | switch (func) { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 154 | case MMAP_CHECK: |
| 155 | iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED); |
| 156 | break; |
| 157 | case BPRM_CHECK: |
| 158 | iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED); |
| 159 | break; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 160 | case CREDS_CHECK: |
| 161 | iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED); |
| 162 | break; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 163 | case FILE_CHECK: |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 164 | case POST_SETATTR: |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 165 | iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED); |
Mimi Zohar | c6af8ef | 2015-11-19 12:39:22 -0500 | [diff] [blame] | 166 | break; |
| 167 | case MODULE_CHECK ... MAX_CHECK - 1: |
| 168 | default: |
| 169 | iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED); |
| 170 | break; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 174 | enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, |
| 175 | int xattr_len) |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 176 | { |
| 177 | struct signature_v2_hdr *sig; |
Seth Forshee | b4bfec7 | 2016-08-01 08:19:10 -0500 | [diff] [blame] | 178 | enum hash_algo ret; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 179 | |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 180 | if (!xattr_value || xattr_len < 2) |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 181 | /* return default hash algo */ |
| 182 | return ima_hash_algo; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 183 | |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 184 | switch (xattr_value->type) { |
| 185 | case EVM_IMA_XATTR_DIGSIG: |
| 186 | sig = (typeof(sig))xattr_value; |
| 187 | if (sig->version != 2 || xattr_len <= sizeof(*sig)) |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 188 | return ima_hash_algo; |
| 189 | return sig->hash_algo; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 190 | break; |
| 191 | case IMA_XATTR_DIGEST_NG: |
Thiago Jung Bauermann | 650b29d | 2019-06-11 03:28:08 -0300 | [diff] [blame] | 192 | /* first byte contains algorithm id */ |
| 193 | ret = xattr_value->data[0]; |
Seth Forshee | b4bfec7 | 2016-08-01 08:19:10 -0500 | [diff] [blame] | 194 | if (ret < HASH_ALGO__LAST) |
| 195 | return ret; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 196 | break; |
| 197 | case IMA_XATTR_DIGEST: |
| 198 | /* this is for backward compatibility */ |
| 199 | if (xattr_len == 21) { |
| 200 | unsigned int zero = 0; |
Thiago Jung Bauermann | 650b29d | 2019-06-11 03:28:08 -0300 | [diff] [blame] | 201 | if (!memcmp(&xattr_value->data[16], &zero, 4)) |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 202 | return HASH_ALGO_MD5; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 203 | else |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 204 | return HASH_ALGO_SHA1; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 205 | } else if (xattr_len == 17) |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 206 | return HASH_ALGO_MD5; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 207 | break; |
| 208 | } |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 209 | |
| 210 | /* return default hash algo */ |
| 211 | return ima_hash_algo; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | int ima_read_xattr(struct dentry *dentry, |
| 215 | struct evm_ima_xattr_data **xattr_value) |
| 216 | { |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 217 | ssize_t ret; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 218 | |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 219 | ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA, |
| 220 | (char **)xattr_value, 0, GFP_NOFS); |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 221 | if (ret == -EOPNOTSUPP) |
| 222 | ret = 0; |
| 223 | return ret; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 224 | } |
| 225 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 226 | /* |
Thiago Jung Bauermann | a5fbeb6 | 2019-06-27 23:19:29 -0300 | [diff] [blame] | 227 | * xattr_verify - verify xattr digest or signature |
| 228 | * |
| 229 | * Verify whether the hash or signature matches the file contents. |
| 230 | * |
| 231 | * Return 0 on success, error code otherwise. |
| 232 | */ |
| 233 | static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint, |
| 234 | struct evm_ima_xattr_data *xattr_value, int xattr_len, |
| 235 | enum integrity_status *status, const char **cause) |
| 236 | { |
| 237 | int rc = -EINVAL, hash_start = 0; |
| 238 | |
| 239 | switch (xattr_value->type) { |
| 240 | case IMA_XATTR_DIGEST_NG: |
| 241 | /* first byte contains algorithm id */ |
| 242 | hash_start = 1; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 243 | fallthrough; |
Thiago Jung Bauermann | a5fbeb6 | 2019-06-27 23:19:29 -0300 | [diff] [blame] | 244 | case IMA_XATTR_DIGEST: |
| 245 | if (iint->flags & IMA_DIGSIG_REQUIRED) { |
| 246 | *cause = "IMA-signature-required"; |
| 247 | *status = INTEGRITY_FAIL; |
| 248 | break; |
| 249 | } |
| 250 | clear_bit(IMA_DIGSIG, &iint->atomic_flags); |
| 251 | if (xattr_len - sizeof(xattr_value->type) - hash_start >= |
| 252 | iint->ima_hash->length) |
| 253 | /* |
| 254 | * xattr length may be longer. md5 hash in previous |
| 255 | * version occupied 20 bytes in xattr, instead of 16 |
| 256 | */ |
| 257 | rc = memcmp(&xattr_value->data[hash_start], |
| 258 | iint->ima_hash->digest, |
| 259 | iint->ima_hash->length); |
| 260 | else |
| 261 | rc = -EINVAL; |
| 262 | if (rc) { |
| 263 | *cause = "invalid-hash"; |
| 264 | *status = INTEGRITY_FAIL; |
| 265 | break; |
| 266 | } |
| 267 | *status = INTEGRITY_PASS; |
| 268 | break; |
| 269 | case EVM_IMA_XATTR_DIGSIG: |
| 270 | set_bit(IMA_DIGSIG, &iint->atomic_flags); |
| 271 | rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, |
| 272 | (const char *)xattr_value, |
| 273 | xattr_len, |
| 274 | iint->ima_hash->digest, |
| 275 | iint->ima_hash->length); |
| 276 | if (rc == -EOPNOTSUPP) { |
| 277 | *status = INTEGRITY_UNKNOWN; |
| 278 | break; |
| 279 | } |
| 280 | if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && |
| 281 | func == KEXEC_KERNEL_CHECK) |
| 282 | rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM, |
| 283 | (const char *)xattr_value, |
| 284 | xattr_len, |
| 285 | iint->ima_hash->digest, |
| 286 | iint->ima_hash->length); |
| 287 | if (rc) { |
| 288 | *cause = "invalid-signature"; |
| 289 | *status = INTEGRITY_FAIL; |
| 290 | } else { |
| 291 | *status = INTEGRITY_PASS; |
| 292 | } |
| 293 | break; |
| 294 | default: |
| 295 | *status = INTEGRITY_UNKNOWN; |
| 296 | *cause = "unknown-ima-data"; |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | return rc; |
| 301 | } |
| 302 | |
| 303 | /* |
Thiago Jung Bauermann | 39b0709 | 2019-06-27 23:19:30 -0300 | [diff] [blame] | 304 | * modsig_verify - verify modsig signature |
| 305 | * |
| 306 | * Verify whether the signature matches the file contents. |
| 307 | * |
| 308 | * Return 0 on success, error code otherwise. |
| 309 | */ |
| 310 | static int modsig_verify(enum ima_hooks func, const struct modsig *modsig, |
| 311 | enum integrity_status *status, const char **cause) |
| 312 | { |
| 313 | int rc; |
| 314 | |
| 315 | rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig); |
| 316 | if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && |
| 317 | func == KEXEC_KERNEL_CHECK) |
| 318 | rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM, |
| 319 | modsig); |
| 320 | if (rc) { |
| 321 | *cause = "invalid-signature"; |
| 322 | *status = INTEGRITY_FAIL; |
| 323 | } else { |
| 324 | *status = INTEGRITY_PASS; |
| 325 | } |
| 326 | |
| 327 | return rc; |
| 328 | } |
| 329 | |
| 330 | /* |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 331 | * ima_check_blacklist - determine if the binary is blacklisted. |
| 332 | * |
| 333 | * Add the hash of the blacklisted binary to the measurement list, based |
| 334 | * on policy. |
| 335 | * |
| 336 | * Returns -EPERM if the hash is blacklisted. |
| 337 | */ |
| 338 | int ima_check_blacklist(struct integrity_iint_cache *iint, |
| 339 | const struct modsig *modsig, int pcr) |
| 340 | { |
| 341 | enum hash_algo hash_algo; |
| 342 | const u8 *digest = NULL; |
| 343 | u32 digestsize = 0; |
| 344 | int rc = 0; |
| 345 | |
| 346 | if (!(iint->flags & IMA_CHECK_BLACKLIST)) |
| 347 | return 0; |
| 348 | |
| 349 | if (iint->flags & IMA_MODSIG_ALLOWED && modsig) { |
| 350 | ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize); |
| 351 | |
| 352 | rc = is_binary_blacklisted(digest, digestsize); |
| 353 | if ((rc == -EPERM) && (iint->flags & IMA_MEASURE)) |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 354 | process_buffer_measurement(&init_user_ns, NULL, digest, digestsize, |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 355 | "blacklisted-hash", NONE, |
Tushar Sugandhi | 291af65 | 2021-01-07 20:07:02 -0800 | [diff] [blame] | 356 | pcr, NULL, false); |
Nayna Jain | 273df86 | 2019-10-30 23:31:32 -0400 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | return rc; |
| 360 | } |
| 361 | |
| 362 | /* |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 363 | * ima_appraise_measurement - appraise file measurement |
| 364 | * |
| 365 | * Call evm_verifyxattr() to verify the integrity of 'security.ima'. |
| 366 | * Assuming success, compare the xattr hash with the collected measurement. |
| 367 | * |
| 368 | * Return 0 on success, error code otherwise |
| 369 | */ |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 370 | int ima_appraise_measurement(enum ima_hooks func, |
| 371 | struct integrity_iint_cache *iint, |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 372 | struct file *file, const unsigned char *filename, |
| 373 | struct evm_ima_xattr_data *xattr_value, |
Thiago Jung Bauermann | 39b0709 | 2019-06-27 23:19:30 -0300 | [diff] [blame] | 374 | int xattr_len, const struct modsig *modsig) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 375 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 376 | static const char op[] = "appraise_data"; |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 377 | const char *cause = "unknown"; |
Miklos Szeredi | e71b9df | 2016-09-16 12:44:20 +0200 | [diff] [blame] | 378 | struct dentry *dentry = file_dentry(file); |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 379 | struct inode *inode = d_backing_inode(dentry); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 380 | enum integrity_status status = INTEGRITY_UNKNOWN; |
Thiago Jung Bauermann | a5fbeb6 | 2019-06-27 23:19:29 -0300 | [diff] [blame] | 381 | int rc = xattr_len; |
Thiago Jung Bauermann | 39b0709 | 2019-06-27 23:19:30 -0300 | [diff] [blame] | 382 | bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 383 | |
Thiago Jung Bauermann | 39b0709 | 2019-06-27 23:19:30 -0300 | [diff] [blame] | 384 | /* If not appraising a modsig, we need an xattr. */ |
| 385 | if (!(inode->i_opflags & IOP_XATTR) && !try_modsig) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 386 | return INTEGRITY_UNKNOWN; |
| 387 | |
Thiago Jung Bauermann | 39b0709 | 2019-06-27 23:19:30 -0300 | [diff] [blame] | 388 | /* If reading the xattr failed and there's no modsig, error out. */ |
| 389 | if (rc <= 0 && !try_modsig) { |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 390 | if (rc && rc != -ENODATA) |
| 391 | goto out; |
| 392 | |
Thiago Jung Bauermann | 915d9d2 | 2017-06-07 22:49:12 -0300 | [diff] [blame] | 393 | cause = iint->flags & IMA_DIGSIG_REQUIRED ? |
| 394 | "IMA-signature-required" : "missing-hash"; |
Dmitry Kasatkin | b151d6b | 2014-06-27 18:04:27 +0300 | [diff] [blame] | 395 | status = INTEGRITY_NOLABEL; |
Al Viro | 6035a27 | 2018-06-08 13:40:10 -0400 | [diff] [blame] | 396 | if (file->f_mode & FMODE_CREATED) |
Dmitry Kasatkin | b151d6b | 2014-06-27 18:04:27 +0300 | [diff] [blame] | 397 | iint->flags |= IMA_NEW_FILE; |
Daniel Glöckner | 1ac202e | 2017-02-24 15:05:14 +0100 | [diff] [blame] | 398 | if ((iint->flags & IMA_NEW_FILE) && |
Mimi Zohar | b7e27bc | 2017-11-08 07:38:28 -0500 | [diff] [blame] | 399 | (!(iint->flags & IMA_DIGSIG_REQUIRED) || |
| 400 | (inode->i_size == 0))) |
Dmitry Kasatkin | b151d6b | 2014-06-27 18:04:27 +0300 | [diff] [blame] | 401 | status = INTEGRITY_PASS; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 402 | goto out; |
| 403 | } |
| 404 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 405 | status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint); |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 406 | switch (status) { |
| 407 | case INTEGRITY_PASS: |
| 408 | case INTEGRITY_PASS_IMMUTABLE: |
| 409 | case INTEGRITY_UNKNOWN: |
| 410 | break; |
| 411 | case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */ |
Thiago Jung Bauermann | 39b0709 | 2019-06-27 23:19:30 -0300 | [diff] [blame] | 412 | /* It's fine not to have xattrs when using a modsig. */ |
| 413 | if (try_modsig) |
| 414 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 415 | fallthrough; |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 416 | case INTEGRITY_NOLABEL: /* No security.evm xattr. */ |
| 417 | cause = "missing-HMAC"; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 418 | goto out; |
Roberto Sassu | cdef685 | 2021-05-14 17:27:47 +0200 | [diff] [blame] | 419 | case INTEGRITY_FAIL_IMMUTABLE: |
| 420 | fallthrough; |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 421 | case INTEGRITY_FAIL: /* Invalid HMAC/signature. */ |
| 422 | cause = "invalid-HMAC"; |
| 423 | goto out; |
| 424 | default: |
| 425 | WARN_ONCE(true, "Unexpected integrity status %d\n", status); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 426 | } |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 427 | |
Thiago Jung Bauermann | a5fbeb6 | 2019-06-27 23:19:29 -0300 | [diff] [blame] | 428 | if (xattr_value) |
| 429 | rc = xattr_verify(func, iint, xattr_value, xattr_len, &status, |
| 430 | &cause); |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 431 | |
Thiago Jung Bauermann | 39b0709 | 2019-06-27 23:19:30 -0300 | [diff] [blame] | 432 | /* |
| 433 | * If we have a modsig and either no imasig or the imasig's key isn't |
| 434 | * known, then try verifying the modsig. |
| 435 | */ |
| 436 | if (try_modsig && |
| 437 | (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG || |
| 438 | rc == -ENOKEY)) |
| 439 | rc = modsig_verify(func, modsig, &status, &cause); |
| 440 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 441 | out: |
Mimi Zohar | 57b56ac | 2018-02-21 11:33:37 -0500 | [diff] [blame] | 442 | /* |
| 443 | * File signatures on some filesystems can not be properly verified. |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 444 | * When such filesystems are mounted by an untrusted mounter or on a |
| 445 | * system not willing to accept such a risk, fail the file signature |
| 446 | * verification. |
Mimi Zohar | 57b56ac | 2018-02-21 11:33:37 -0500 | [diff] [blame] | 447 | */ |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 448 | if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && |
| 449 | ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) || |
| 450 | (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) { |
Mimi Zohar | 57b56ac | 2018-02-21 11:33:37 -0500 | [diff] [blame] | 451 | status = INTEGRITY_FAIL; |
| 452 | cause = "unverifiable-signature"; |
| 453 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, |
| 454 | op, cause, rc, 0); |
| 455 | } else if (status != INTEGRITY_PASS) { |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 456 | /* Fix mode, but don't replace file signatures. */ |
Thiago Jung Bauermann | 39b0709 | 2019-06-27 23:19:30 -0300 | [diff] [blame] | 457 | if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig && |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 458 | (!xattr_value || |
| 459 | xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { |
Dmitry Kasatkin | def3e8b | 2012-09-20 22:38:53 +0300 | [diff] [blame] | 460 | if (!ima_fix_xattr(dentry, iint)) |
| 461 | status = INTEGRITY_PASS; |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* Permit new files with file signatures, but without data. */ |
| 465 | if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE && |
| 466 | xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) { |
Mimi Zohar | 05d1a71 | 2016-02-29 19:52:05 -0500 | [diff] [blame] | 467 | status = INTEGRITY_PASS; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 468 | } |
Thiago Jung Bauermann | f5e51fa | 2018-03-15 17:33:42 -0300 | [diff] [blame] | 469 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 470 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, |
| 471 | op, cause, rc, 0); |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 472 | } else { |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 473 | ima_cache_flags(iint, func); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 474 | } |
Mimi Zohar | 57b56ac | 2018-02-21 11:33:37 -0500 | [diff] [blame] | 475 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 476 | ima_set_cache_status(iint, func, status); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 477 | return status; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * ima_update_xattr - update 'security.ima' hash value |
| 482 | */ |
| 483 | void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) |
| 484 | { |
Miklos Szeredi | e71b9df | 2016-09-16 12:44:20 +0200 | [diff] [blame] | 485 | struct dentry *dentry = file_dentry(file); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 486 | int rc = 0; |
| 487 | |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 488 | /* do not collect and update hash for digital signatures */ |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 489 | if (test_bit(IMA_DIGSIG, &iint->atomic_flags)) |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 490 | return; |
| 491 | |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 492 | if ((iint->ima_file_status != INTEGRITY_PASS) && |
| 493 | !(iint->flags & IMA_HASH)) |
Roberto Sassu | 020aae3 | 2017-11-07 11:37:07 +0100 | [diff] [blame] | 494 | return; |
| 495 | |
Thiago Jung Bauermann | 1558822 | 2019-06-27 23:19:31 -0300 | [diff] [blame] | 496 | rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 497 | if (rc < 0) |
| 498 | return; |
Dmitry Kasatkin | 8606404 | 2011-08-31 14:07:06 +0300 | [diff] [blame] | 499 | |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 500 | inode_lock(file_inode(file)); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 501 | ima_fix_xattr(dentry, iint); |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 502 | inode_unlock(file_inode(file)); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /** |
| 506 | * ima_inode_post_setattr - reflect file metadata changes |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 507 | * @mnt_userns: user namespace of the mount the inode was found from |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 508 | * @dentry: pointer to the affected dentry |
| 509 | * |
| 510 | * Changes to a dentry's metadata might result in needing to appraise. |
| 511 | * |
| 512 | * This function is called from notify_change(), which expects the caller |
| 513 | * to lock the inode's i_mutex. |
| 514 | */ |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 515 | void ima_inode_post_setattr(struct user_namespace *mnt_userns, |
| 516 | struct dentry *dentry) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 517 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 518 | struct inode *inode = d_backing_inode(dentry); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 519 | struct integrity_iint_cache *iint; |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 520 | int action; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 521 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 522 | if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 523 | || !(inode->i_opflags & IOP_XATTR)) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 524 | return; |
| 525 | |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 526 | action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR); |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 527 | if (!action) |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 528 | __vfs_removexattr(&init_user_ns, dentry, XATTR_NAME_IMA); |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 529 | iint = integrity_iint_find(inode); |
| 530 | if (iint) { |
| 531 | set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags); |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 532 | if (!action) |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 533 | clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); |
| 534 | } |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 535 | } |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * ima_protect_xattr - protect 'security.ima' |
| 539 | * |
| 540 | * Ensure that not just anyone can modify or remove 'security.ima'. |
| 541 | */ |
| 542 | static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, |
| 543 | const void *xattr_value, size_t xattr_value_len) |
| 544 | { |
| 545 | if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { |
| 546 | if (!capable(CAP_SYS_ADMIN)) |
| 547 | return -EPERM; |
| 548 | return 1; |
| 549 | } |
| 550 | return 0; |
| 551 | } |
| 552 | |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 553 | static void ima_reset_appraise_flags(struct inode *inode, int digsig) |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 554 | { |
| 555 | struct integrity_iint_cache *iint; |
| 556 | |
Roberto Sassu | a756024 | 2014-09-12 19:35:54 +0200 | [diff] [blame] | 557 | if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)) |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 558 | return; |
| 559 | |
| 560 | iint = integrity_iint_find(inode); |
| 561 | if (!iint) |
| 562 | return; |
Eric Richter | a422638 | 2016-06-01 13:14:06 -0500 | [diff] [blame] | 563 | iint->measured_pcrs = 0; |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 564 | set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags); |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 565 | if (digsig) |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 566 | set_bit(IMA_DIGSIG, &iint->atomic_flags); |
| 567 | else |
| 568 | clear_bit(IMA_DIGSIG, &iint->atomic_flags); |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, |
| 572 | const void *xattr_value, size_t xattr_value_len) |
| 573 | { |
Mimi Zohar | 060bdebfb | 2014-03-17 23:24:18 -0400 | [diff] [blame] | 574 | const struct evm_ima_xattr_data *xvalue = xattr_value; |
Roberto Sassu | e3ccfe1 | 2021-05-14 17:27:45 +0200 | [diff] [blame] | 575 | int digsig = 0; |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 576 | int result; |
| 577 | |
| 578 | result = ima_protect_xattr(dentry, xattr_name, xattr_value, |
| 579 | xattr_value_len); |
| 580 | if (result == 1) { |
Dmitry Kasatkin | a48fda9 | 2014-10-28 13:31:22 +0200 | [diff] [blame] | 581 | if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST)) |
| 582 | return -EINVAL; |
Roberto Sassu | e3ccfe1 | 2021-05-14 17:27:45 +0200 | [diff] [blame] | 583 | digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG); |
| 584 | } |
| 585 | if (result == 1 || evm_revalidate_status(xattr_name)) { |
| 586 | ima_reset_appraise_flags(d_backing_inode(dentry), digsig); |
| 587 | if (result == 1) |
| 588 | result = 0; |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 589 | } |
| 590 | return result; |
| 591 | } |
| 592 | |
| 593 | int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) |
| 594 | { |
| 595 | int result; |
| 596 | |
| 597 | result = ima_protect_xattr(dentry, xattr_name, NULL, 0); |
Roberto Sassu | e3ccfe1 | 2021-05-14 17:27:45 +0200 | [diff] [blame] | 598 | if (result == 1 || evm_revalidate_status(xattr_name)) { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 599 | ima_reset_appraise_flags(d_backing_inode(dentry), 0); |
Roberto Sassu | e3ccfe1 | 2021-05-14 17:27:45 +0200 | [diff] [blame] | 600 | if (result == 1) |
| 601 | result = 0; |
Mimi Zohar | 42c6333 | 2011-03-10 18:54:15 -0500 | [diff] [blame] | 602 | } |
| 603 | return result; |
| 604 | } |