blob: d9a627de393030ba07bf24d24d8ef12a56988e8a [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -05002/*
3 * Copyright (C) 2011 IBM Corporation
4 *
5 * Author:
6 * Mimi Zohar <zohar@us.ibm.com>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -05007 */
Ard Biesheuvelb000d5c2020-10-13 10:18:04 +02008#include <linux/module.h>
Paul Gortmaker876979c2018-12-09 15:36:29 -05009#include <linux/init.h>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050010#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 Jain273df862019-10-30 23:31:32 -040016#include <keys/system_keyring.h>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050017
18#include "ima.h"
19
Mimi Zohare1f5e012017-04-24 22:06:49 -040020#ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
Ard Biesheuvelb000d5c2020-10-13 10:18:04 +020021static char *ima_appraise_cmdline_default __initdata;
22core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0);
23
24void __init ima_appraise_parse_cmdline(void)
25{
26 const char *str = ima_appraise_cmdline_default;
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030027 bool sb_state = arch_ima_get_secureboot();
28 int appraisal_state = ima_appraise;
Bruno Meneguele311aa6a2020-07-13 13:48:30 -030029
Ard Biesheuvelb000d5c2020-10-13 10:18:04 +020030 if (!str)
31 return;
32
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050033 if (strncmp(str, "off", 3) == 0)
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030034 appraisal_state = 0;
Dmitry Kasatkin2faa6ef2014-05-08 13:11:29 +030035 else if (strncmp(str, "log", 3) == 0)
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030036 appraisal_state = IMA_APPRAISE_LOG;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050037 else if (strncmp(str, "fix", 3) == 0)
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030038 appraisal_state = IMA_APPRAISE_FIX;
Bruno Meneguele4afb28a2020-09-04 16:40:57 -030039 else if (strncmp(str, "enforce", 7) == 0)
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030040 appraisal_state = IMA_APPRAISE_ENFORCE;
Bruno Meneguele7fe2bb72020-09-04 16:40:58 -030041 else
42 pr_err("invalid \"%s\" appraise option", str);
Bruno Meneguelee4d7e2d2020-09-04 22:20:20 -030043
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 Zohar2fe5d6d2012-02-13 10:15:05 -050053}
Ard Biesheuvelb000d5c2020-10-13 10:18:04 +020054#endif
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050055
56/*
Mimi Zohar6f6723e2017-04-24 22:43:52 -040057 * is_ima_appraise_enabled - return appraise status
58 *
59 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
60 */
61bool is_ima_appraise_enabled(void)
62{
Thiago Jung Bauermanne5729f82017-10-17 22:53:14 -020063 return ima_appraise & IMA_APPRAISE_ENFORCE;
Mimi Zohar6f6723e2017-04-24 22:43:52 -040064}
65
66/*
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050067 * ima_must_appraise - set appraise flag
68 *
Mimi Zoharda1b0022016-09-29 10:04:52 -040069 * Return 1 to appraise or hash
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050070 */
Christian Braunera2d23292021-01-21 14:19:45 +010071int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode,
72 int mask, enum ima_hooks func)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050073{
Matthew Garrettd906c102018-01-08 13:36:20 -080074 u32 secid;
75
Mimi Zohar07f6a792011-03-09 22:25:48 -050076 if (!ima_appraise)
77 return 0;
78
Paul Moore4ebd7652021-02-19 14:26:21 -050079 security_task_getsecid_subj(current, &secid);
Christian Braunera2d23292021-01-21 14:19:45 +010080 return ima_match_policy(mnt_userns, inode, current_cred(), secid, func,
81 mask, IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050082}
83
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +030084static int ima_fix_xattr(struct dentry *dentry,
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030085 struct integrity_iint_cache *iint)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050086{
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +030087 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 Andersenc7c7a1a12021-01-21 14:19:28 +010098 rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA,
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +030099 &iint->ima_hash->xattr.data[offset],
100 (sizeof(iint->ima_hash->xattr) - offset) +
101 iint->ima_hash->length, 0);
102 return rc;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500103}
104
Mimi Zohard79d72e2012-12-03 17:08:11 -0500105/* Return specific func appraised cached result */
106enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500107 enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500108{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900109 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500110 case MMAP_CHECK:
111 return iint->ima_mmap_status;
112 case BPRM_CHECK:
113 return iint->ima_bprm_status;
Matthew Garrettd906c102018-01-08 13:36:20 -0800114 case CREDS_CHECK:
115 return iint->ima_creds_status;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500116 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500117 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500118 return iint->ima_file_status;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500119 case MODULE_CHECK ... MAX_CHECK - 1:
120 default:
121 return iint->ima_read_status;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500122 }
123}
124
125static void ima_set_cache_status(struct integrity_iint_cache *iint,
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500126 enum ima_hooks func,
127 enum integrity_status status)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500128{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900129 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500130 case MMAP_CHECK:
131 iint->ima_mmap_status = status;
132 break;
133 case BPRM_CHECK:
134 iint->ima_bprm_status = status;
135 break;
Matthew Garrettd906c102018-01-08 13:36:20 -0800136 case CREDS_CHECK:
137 iint->ima_creds_status = status;
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -0600138 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500139 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500140 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500141 iint->ima_file_status = status;
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500142 break;
143 case MODULE_CHECK ... MAX_CHECK - 1:
144 default:
145 iint->ima_read_status = status;
146 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500147 }
148}
149
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500150static void ima_cache_flags(struct integrity_iint_cache *iint,
151 enum ima_hooks func)
Mimi Zohard79d72e2012-12-03 17:08:11 -0500152{
Dmitry Kasatkin089bc8e2013-10-10 15:56:13 +0900153 switch (func) {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500154 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 Garrettd906c102018-01-08 13:36:20 -0800160 case CREDS_CHECK:
161 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
162 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500163 case FILE_CHECK:
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500164 case POST_SETATTR:
Mimi Zohard79d72e2012-12-03 17:08:11 -0500165 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
Mimi Zoharc6af8ef2015-11-19 12:39:22 -0500166 break;
167 case MODULE_CHECK ... MAX_CHECK - 1:
168 default:
169 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
170 break;
Mimi Zohard79d72e2012-12-03 17:08:11 -0500171 }
172}
173
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200174enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
175 int xattr_len)
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300176{
177 struct signature_v2_hdr *sig;
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500178 enum hash_algo ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300179
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300180 if (!xattr_value || xattr_len < 2)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200181 /* return default hash algo */
182 return ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300183
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300184 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 Kasatkin1525b062014-10-30 12:39:39 +0200188 return ima_hash_algo;
189 return sig->hash_algo;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300190 break;
191 case IMA_XATTR_DIGEST_NG:
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300192 /* first byte contains algorithm id */
193 ret = xattr_value->data[0];
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500194 if (ret < HASH_ALGO__LAST)
195 return ret;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300196 break;
197 case IMA_XATTR_DIGEST:
198 /* this is for backward compatibility */
199 if (xattr_len == 21) {
200 unsigned int zero = 0;
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300201 if (!memcmp(&xattr_value->data[16], &zero, 4))
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200202 return HASH_ALGO_MD5;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300203 else
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200204 return HASH_ALGO_SHA1;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300205 } else if (xattr_len == 17)
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200206 return HASH_ALGO_MD5;
Dmitry Kasatkin3ea7a562013-08-12 11:22:51 +0300207 break;
208 }
Dmitry Kasatkin1525b062014-10-30 12:39:39 +0200209
210 /* return default hash algo */
211 return ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300212}
213
214int ima_read_xattr(struct dentry *dentry,
215 struct evm_ima_xattr_data **xattr_value)
216{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200217 ssize_t ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300218
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100219 ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA,
220 (char **)xattr_value, 0, GFP_NOFS);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200221 if (ret == -EOPNOTSUPP)
222 ret = 0;
223 return ret;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300224}
225
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500226/*
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300227 * 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 */
233static 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. Silvadf561f662020-08-23 17:36:59 -0500243 fallthrough;
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300244 case IMA_XATTR_DIGEST:
Roberto Sassu7aa57832021-05-14 17:27:51 +0200245 if (*status != INTEGRITY_PASS_IMMUTABLE) {
246 if (iint->flags & IMA_DIGSIG_REQUIRED) {
247 *cause = "IMA-signature-required";
248 *status = INTEGRITY_FAIL;
249 break;
250 }
251 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
252 } else {
253 set_bit(IMA_DIGSIG, &iint->atomic_flags);
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300254 }
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300255 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
256 iint->ima_hash->length)
257 /*
258 * xattr length may be longer. md5 hash in previous
259 * version occupied 20 bytes in xattr, instead of 16
260 */
261 rc = memcmp(&xattr_value->data[hash_start],
262 iint->ima_hash->digest,
263 iint->ima_hash->length);
264 else
265 rc = -EINVAL;
266 if (rc) {
267 *cause = "invalid-hash";
268 *status = INTEGRITY_FAIL;
269 break;
270 }
271 *status = INTEGRITY_PASS;
272 break;
273 case EVM_IMA_XATTR_DIGSIG:
274 set_bit(IMA_DIGSIG, &iint->atomic_flags);
275 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
276 (const char *)xattr_value,
277 xattr_len,
278 iint->ima_hash->digest,
279 iint->ima_hash->length);
280 if (rc == -EOPNOTSUPP) {
281 *status = INTEGRITY_UNKNOWN;
282 break;
283 }
284 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
285 func == KEXEC_KERNEL_CHECK)
286 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
287 (const char *)xattr_value,
288 xattr_len,
289 iint->ima_hash->digest,
290 iint->ima_hash->length);
291 if (rc) {
292 *cause = "invalid-signature";
293 *status = INTEGRITY_FAIL;
294 } else {
295 *status = INTEGRITY_PASS;
296 }
297 break;
298 default:
299 *status = INTEGRITY_UNKNOWN;
300 *cause = "unknown-ima-data";
301 break;
302 }
303
304 return rc;
305}
306
307/*
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300308 * modsig_verify - verify modsig signature
309 *
310 * Verify whether the signature matches the file contents.
311 *
312 * Return 0 on success, error code otherwise.
313 */
314static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
315 enum integrity_status *status, const char **cause)
316{
317 int rc;
318
319 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
320 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
321 func == KEXEC_KERNEL_CHECK)
322 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
323 modsig);
324 if (rc) {
325 *cause = "invalid-signature";
326 *status = INTEGRITY_FAIL;
327 } else {
328 *status = INTEGRITY_PASS;
329 }
330
331 return rc;
332}
333
334/*
Nayna Jain273df862019-10-30 23:31:32 -0400335 * ima_check_blacklist - determine if the binary is blacklisted.
336 *
337 * Add the hash of the blacklisted binary to the measurement list, based
338 * on policy.
339 *
340 * Returns -EPERM if the hash is blacklisted.
341 */
342int ima_check_blacklist(struct integrity_iint_cache *iint,
343 const struct modsig *modsig, int pcr)
344{
345 enum hash_algo hash_algo;
346 const u8 *digest = NULL;
347 u32 digestsize = 0;
348 int rc = 0;
349
350 if (!(iint->flags & IMA_CHECK_BLACKLIST))
351 return 0;
352
353 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
354 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
355
356 rc = is_binary_blacklisted(digest, digestsize);
357 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
Christian Braunera2d23292021-01-21 14:19:45 +0100358 process_buffer_measurement(&init_user_ns, NULL, digest, digestsize,
Nayna Jain273df862019-10-30 23:31:32 -0400359 "blacklisted-hash", NONE,
Tushar Sugandhi291af652021-01-07 20:07:02 -0800360 pcr, NULL, false);
Nayna Jain273df862019-10-30 23:31:32 -0400361 }
362
363 return rc;
364}
365
366/*
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500367 * ima_appraise_measurement - appraise file measurement
368 *
369 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
370 * Assuming success, compare the xattr hash with the collected measurement.
371 *
372 * Return 0 on success, error code otherwise
373 */
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500374int ima_appraise_measurement(enum ima_hooks func,
375 struct integrity_iint_cache *iint,
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300376 struct file *file, const unsigned char *filename,
377 struct evm_ima_xattr_data *xattr_value,
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300378 int xattr_len, const struct modsig *modsig)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500379{
Mimi Zohar52a13282013-12-11 14:44:04 -0500380 static const char op[] = "appraise_data";
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300381 const char *cause = "unknown";
Miklos Szeredie71b9df2016-09-16 12:44:20 +0200382 struct dentry *dentry = file_dentry(file);
David Howellsc6f493d2015-03-17 22:26:22 +0000383 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500384 enum integrity_status status = INTEGRITY_UNKNOWN;
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300385 int rc = xattr_len;
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300386 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500387
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300388 /* If not appraising a modsig, we need an xattr. */
389 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500390 return INTEGRITY_UNKNOWN;
391
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300392 /* If reading the xattr failed and there's no modsig, error out. */
393 if (rc <= 0 && !try_modsig) {
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500394 if (rc && rc != -ENODATA)
395 goto out;
396
Thiago Jung Bauermann915d9d22017-06-07 22:49:12 -0300397 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
398 "IMA-signature-required" : "missing-hash";
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300399 status = INTEGRITY_NOLABEL;
Al Viro6035a272018-06-08 13:40:10 -0400400 if (file->f_mode & FMODE_CREATED)
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300401 iint->flags |= IMA_NEW_FILE;
Daniel Glöckner1ac202e2017-02-24 15:05:14 +0100402 if ((iint->flags & IMA_NEW_FILE) &&
Mimi Zoharb7e27bc2017-11-08 07:38:28 -0500403 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
404 (inode->i_size == 0)))
Dmitry Kasatkinb151d6b2014-06-27 18:04:27 +0300405 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500406 goto out;
407 }
408
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300409 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300410 switch (status) {
411 case INTEGRITY_PASS:
412 case INTEGRITY_PASS_IMMUTABLE:
413 case INTEGRITY_UNKNOWN:
414 break;
415 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300416 /* It's fine not to have xattrs when using a modsig. */
417 if (try_modsig)
418 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500419 fallthrough;
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300420 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
421 cause = "missing-HMAC";
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500422 goto out;
Roberto Sassucdef6852021-05-14 17:27:47 +0200423 case INTEGRITY_FAIL_IMMUTABLE:
Roberto Sassu7aa57832021-05-14 17:27:51 +0200424 set_bit(IMA_DIGSIG, &iint->atomic_flags);
Roberto Sassucdef6852021-05-14 17:27:47 +0200425 fallthrough;
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300426 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
427 cause = "invalid-HMAC";
428 goto out;
429 default:
430 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500431 }
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300432
Thiago Jung Bauermanna5fbeb62019-06-27 23:19:29 -0300433 if (xattr_value)
434 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
435 &cause);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300436
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300437 /*
438 * If we have a modsig and either no imasig or the imasig's key isn't
439 * known, then try verifying the modsig.
440 */
441 if (try_modsig &&
442 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
443 rc == -ENOKEY))
444 rc = modsig_verify(func, modsig, &status, &cause);
445
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500446out:
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500447 /*
448 * File signatures on some filesystems can not be properly verified.
Mimi Zohar9e670282018-02-21 11:36:32 -0500449 * When such filesystems are mounted by an untrusted mounter or on a
450 * system not willing to accept such a risk, fail the file signature
451 * verification.
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500452 */
Mimi Zohar9e670282018-02-21 11:36:32 -0500453 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
454 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
455 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500456 status = INTEGRITY_FAIL;
457 cause = "unverifiable-signature";
458 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
459 op, cause, rc, 0);
460 } else if (status != INTEGRITY_PASS) {
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300461 /* Fix mode, but don't replace file signatures. */
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -0300462 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300463 (!xattr_value ||
464 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +0300465 if (!ima_fix_xattr(dentry, iint))
466 status = INTEGRITY_PASS;
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300467 }
468
Roberto Sassu7aa57832021-05-14 17:27:51 +0200469 /*
470 * Permit new files with file/EVM portable signatures, but
471 * without data.
472 */
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300473 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
Roberto Sassu7aa57832021-05-14 17:27:51 +0200474 test_bit(IMA_DIGSIG, &iint->atomic_flags)) {
Mimi Zohar05d1a712016-02-29 19:52:05 -0500475 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500476 }
Thiago Jung Bauermannf5e51fa2018-03-15 17:33:42 -0300477
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500478 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
479 op, cause, rc, 0);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300480 } else {
Mimi Zohard79d72e2012-12-03 17:08:11 -0500481 ima_cache_flags(iint, func);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500482 }
Mimi Zohar57b56ac2018-02-21 11:33:37 -0500483
Mimi Zohard79d72e2012-12-03 17:08:11 -0500484 ima_set_cache_status(iint, func, status);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500485 return status;
486}
487
488/*
489 * ima_update_xattr - update 'security.ima' hash value
490 */
491void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
492{
Miklos Szeredie71b9df2016-09-16 12:44:20 +0200493 struct dentry *dentry = file_dentry(file);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500494 int rc = 0;
495
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300496 /* do not collect and update hash for digital signatures */
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200497 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300498 return;
499
Mimi Zoharda1b0022016-09-29 10:04:52 -0400500 if ((iint->ima_file_status != INTEGRITY_PASS) &&
501 !(iint->flags & IMA_HASH))
Roberto Sassu020aae32017-11-07 11:37:07 +0100502 return;
503
Thiago Jung Bauermann15588222019-06-27 23:19:31 -0300504 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500505 if (rc < 0)
506 return;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300507
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200508 inode_lock(file_inode(file));
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500509 ima_fix_xattr(dentry, iint);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200510 inode_unlock(file_inode(file));
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500511}
512
513/**
514 * ima_inode_post_setattr - reflect file metadata changes
Christian Braunera2d23292021-01-21 14:19:45 +0100515 * @mnt_userns: user namespace of the mount the inode was found from
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500516 * @dentry: pointer to the affected dentry
517 *
518 * Changes to a dentry's metadata might result in needing to appraise.
519 *
520 * This function is called from notify_change(), which expects the caller
521 * to lock the inode's i_mutex.
522 */
Christian Braunera2d23292021-01-21 14:19:45 +0100523void ima_inode_post_setattr(struct user_namespace *mnt_userns,
524 struct dentry *dentry)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500525{
David Howellsc6f493d2015-03-17 22:26:22 +0000526 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500527 struct integrity_iint_cache *iint;
Mimi Zoharda1b0022016-09-29 10:04:52 -0400528 int action;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500529
Roberto Sassua7560242014-09-12 19:35:54 +0200530 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200531 || !(inode->i_opflags & IOP_XATTR))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500532 return;
533
Christian Braunera2d23292021-01-21 14:19:45 +0100534 action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR);
Mimi Zoharda1b0022016-09-29 10:04:52 -0400535 if (!action)
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100536 __vfs_removexattr(&init_user_ns, dentry, XATTR_NAME_IMA);
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200537 iint = integrity_iint_find(inode);
538 if (iint) {
539 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
Mimi Zoharda1b0022016-09-29 10:04:52 -0400540 if (!action)
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200541 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
542 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500543}
Mimi Zohar42c63332011-03-10 18:54:15 -0500544
545/*
546 * ima_protect_xattr - protect 'security.ima'
547 *
548 * Ensure that not just anyone can modify or remove 'security.ima'.
549 */
550static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
551 const void *xattr_value, size_t xattr_value_len)
552{
553 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
554 if (!capable(CAP_SYS_ADMIN))
555 return -EPERM;
556 return 1;
557 }
558 return 0;
559}
560
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400561static void ima_reset_appraise_flags(struct inode *inode, int digsig)
Mimi Zohar42c63332011-03-10 18:54:15 -0500562{
563 struct integrity_iint_cache *iint;
564
Roberto Sassua7560242014-09-12 19:35:54 +0200565 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
Mimi Zohar42c63332011-03-10 18:54:15 -0500566 return;
567
568 iint = integrity_iint_find(inode);
569 if (!iint)
570 return;
Eric Richtera4226382016-06-01 13:14:06 -0500571 iint->measured_pcrs = 0;
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200572 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400573 if (digsig)
Dmitry Kasatkin0d73a552017-12-05 21:06:34 +0200574 set_bit(IMA_DIGSIG, &iint->atomic_flags);
575 else
576 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
Mimi Zohar42c63332011-03-10 18:54:15 -0500577}
578
579int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
580 const void *xattr_value, size_t xattr_value_len)
581{
Mimi Zohar060bdebfb2014-03-17 23:24:18 -0400582 const struct evm_ima_xattr_data *xvalue = xattr_value;
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200583 int digsig = 0;
Mimi Zohar42c63332011-03-10 18:54:15 -0500584 int result;
585
586 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
587 xattr_value_len);
588 if (result == 1) {
Dmitry Kasatkina48fda92014-10-28 13:31:22 +0200589 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
590 return -EINVAL;
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200591 digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG);
Roberto Sassu7aa57832021-05-14 17:27:51 +0200592 } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) {
593 digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG);
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200594 }
595 if (result == 1 || evm_revalidate_status(xattr_name)) {
596 ima_reset_appraise_flags(d_backing_inode(dentry), digsig);
597 if (result == 1)
598 result = 0;
Mimi Zohar42c63332011-03-10 18:54:15 -0500599 }
600 return result;
601}
602
603int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
604{
605 int result;
606
607 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200608 if (result == 1 || evm_revalidate_status(xattr_name)) {
David Howellsc6f493d2015-03-17 22:26:22 +0000609 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
Roberto Sassue3ccfe12021-05-14 17:27:45 +0200610 if (result == 1)
611 result = 0;
Mimi Zohar42c63332011-03-10 18:54:15 -0500612 }
613 return result;
614}