ima: Move IMA-Appraisal to LSM infrastructure
[linux-2.6-block.git] / security / integrity / ima / ima_appraise.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
2fe5d6de
MZ
2/*
3 * Copyright (C) 2011 IBM Corporation
4 *
5 * Author:
6 * Mimi Zohar <zohar@us.ibm.com>
2fe5d6de 7 */
b000d5cb 8#include <linux/module.h>
876979c9 9#include <linux/init.h>
2fe5d6de
MZ
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>
398c42e2 16#include <linux/fsverity.h>
273df864 17#include <keys/system_keyring.h>
398c42e2 18#include <uapi/linux/fsverity.h>
2fe5d6de
MZ
19
20#include "ima.h"
21
e1f5e01f 22#ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
b000d5cb
AB
23static char *ima_appraise_cmdline_default __initdata;
24core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0);
25
26void __init ima_appraise_parse_cmdline(void)
27{
28 const char *str = ima_appraise_cmdline_default;
e4d7e2df
BM
29 bool sb_state = arch_ima_get_secureboot();
30 int appraisal_state = ima_appraise;
311aa6aa 31
b000d5cb
AB
32 if (!str)
33 return;
34
2fe5d6de 35 if (strncmp(str, "off", 3) == 0)
e4d7e2df 36 appraisal_state = 0;
2faa6ef3 37 else if (strncmp(str, "log", 3) == 0)
e4d7e2df 38 appraisal_state = IMA_APPRAISE_LOG;
2fe5d6de 39 else if (strncmp(str, "fix", 3) == 0)
e4d7e2df 40 appraisal_state = IMA_APPRAISE_FIX;
4afb28ab 41 else if (strncmp(str, "enforce", 7) == 0)
e4d7e2df 42 appraisal_state = IMA_APPRAISE_ENFORCE;
7fe2bb7e
BM
43 else
44 pr_err("invalid \"%s\" appraise option", str);
e4d7e2df
BM
45
46 /* If appraisal state was changed, but secure boot is enabled,
47 * keep its default */
48 if (sb_state) {
49 if (!(appraisal_state & IMA_APPRAISE_ENFORCE))
50 pr_info("Secure boot enabled: ignoring ima_appraise=%s option",
51 str);
52 } else {
53 ima_appraise = appraisal_state;
54 }
2fe5d6de 55}
b000d5cb 56#endif
2fe5d6de 57
6f6723e2
MZ
58/*
59 * is_ima_appraise_enabled - return appraise status
60 *
61 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
62 */
63bool is_ima_appraise_enabled(void)
64{
e5729f86 65 return ima_appraise & IMA_APPRAISE_ENFORCE;
6f6723e2
MZ
66}
67
2fe5d6de
MZ
68/*
69 * ima_must_appraise - set appraise flag
70 *
da1b0029 71 * Return 1 to appraise or hash
2fe5d6de 72 */
39f60c1c 73int ima_must_appraise(struct mnt_idmap *idmap, struct inode *inode,
a2d2329e 74 int mask, enum ima_hooks func)
2fe5d6de 75{
d906c10d
MG
76 u32 secid;
77
07f6a794
MZ
78 if (!ima_appraise)
79 return 0;
80
6326948f 81 security_current_getsecid_subj(&secid);
39f60c1c 82 return ima_match_policy(idmap, inode, current_cred(), secid,
1624dc00
TS
83 func, mask, IMA_APPRAISE | IMA_HASH, NULL,
84 NULL, NULL, NULL);
2fe5d6de
MZ
85}
86
def3e8b9 87static int ima_fix_xattr(struct dentry *dentry,
c7c8bb23 88 struct integrity_iint_cache *iint)
2fe5d6de 89{
3ea7a560
DK
90 int rc, offset;
91 u8 algo = iint->ima_hash->algo;
92
93 if (algo <= HASH_ALGO_SHA1) {
94 offset = 1;
95 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
96 } else {
97 offset = 0;
98 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
99 iint->ima_hash->xattr.ng.algo = algo;
100 }
39f60c1c 101 rc = __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_IMA,
3ea7a560
DK
102 &iint->ima_hash->xattr.data[offset],
103 (sizeof(iint->ima_hash->xattr) - offset) +
104 iint->ima_hash->length, 0);
105 return rc;
2fe5d6de
MZ
106}
107
d79d72e0
MZ
108/* Return specific func appraised cached result */
109enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
4ad87a3d 110 enum ima_hooks func)
d79d72e0 111{
089bc8e9 112 switch (func) {
d79d72e0 113 case MMAP_CHECK:
4958db32 114 case MMAP_CHECK_REQPROT:
d79d72e0
MZ
115 return iint->ima_mmap_status;
116 case BPRM_CHECK:
117 return iint->ima_bprm_status;
d906c10d
MG
118 case CREDS_CHECK:
119 return iint->ima_creds_status;
d79d72e0 120 case FILE_CHECK:
c6af8efe 121 case POST_SETATTR:
d79d72e0 122 return iint->ima_file_status;
c6af8efe
MZ
123 case MODULE_CHECK ... MAX_CHECK - 1:
124 default:
125 return iint->ima_read_status;
d79d72e0
MZ
126 }
127}
128
129static void ima_set_cache_status(struct integrity_iint_cache *iint,
4ad87a3d
MZ
130 enum ima_hooks func,
131 enum integrity_status status)
d79d72e0 132{
089bc8e9 133 switch (func) {
d79d72e0 134 case MMAP_CHECK:
4958db32 135 case MMAP_CHECK_REQPROT:
d79d72e0
MZ
136 iint->ima_mmap_status = status;
137 break;
138 case BPRM_CHECK:
139 iint->ima_bprm_status = status;
140 break;
d906c10d
MG
141 case CREDS_CHECK:
142 iint->ima_creds_status = status;
09186e50 143 break;
d79d72e0 144 case FILE_CHECK:
c6af8efe 145 case POST_SETATTR:
d79d72e0 146 iint->ima_file_status = status;
c6af8efe
MZ
147 break;
148 case MODULE_CHECK ... MAX_CHECK - 1:
149 default:
150 iint->ima_read_status = status;
151 break;
d79d72e0
MZ
152 }
153}
154
4ad87a3d
MZ
155static void ima_cache_flags(struct integrity_iint_cache *iint,
156 enum ima_hooks func)
d79d72e0 157{
089bc8e9 158 switch (func) {
d79d72e0 159 case MMAP_CHECK:
4958db32 160 case MMAP_CHECK_REQPROT:
d79d72e0
MZ
161 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
162 break;
163 case BPRM_CHECK:
164 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
165 break;
d906c10d
MG
166 case CREDS_CHECK:
167 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
168 break;
d79d72e0 169 case FILE_CHECK:
c6af8efe 170 case POST_SETATTR:
d79d72e0 171 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
c6af8efe
MZ
172 break;
173 case MODULE_CHECK ... MAX_CHECK - 1:
174 default:
175 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
176 break;
d79d72e0
MZ
177 }
178}
179
50f742dd 180enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
1525b06d 181 int xattr_len)
d3634d0f
DK
182{
183 struct signature_v2_hdr *sig;
b4bfec7f 184 enum hash_algo ret;
d3634d0f 185
3ea7a560 186 if (!xattr_value || xattr_len < 2)
1525b06d
DK
187 /* return default hash algo */
188 return ima_hash_algo;
d3634d0f 189
3ea7a560 190 switch (xattr_value->type) {
398c42e2
MZ
191 case IMA_VERITY_DIGSIG:
192 sig = (typeof(sig))xattr_value;
193 if (sig->version != 3 || xattr_len <= sizeof(*sig) ||
194 sig->hash_algo >= HASH_ALGO__LAST)
195 return ima_hash_algo;
196 return sig->hash_algo;
3ea7a560
DK
197 case EVM_IMA_XATTR_DIGSIG:
198 sig = (typeof(sig))xattr_value;
cb181da1
TS
199 if (sig->version != 2 || xattr_len <= sizeof(*sig)
200 || sig->hash_algo >= HASH_ALGO__LAST)
1525b06d
DK
201 return ima_hash_algo;
202 return sig->hash_algo;
3ea7a560 203 case IMA_XATTR_DIGEST_NG:
650b29db
TJB
204 /* first byte contains algorithm id */
205 ret = xattr_value->data[0];
b4bfec7f
SF
206 if (ret < HASH_ALGO__LAST)
207 return ret;
3ea7a560
DK
208 break;
209 case IMA_XATTR_DIGEST:
210 /* this is for backward compatibility */
211 if (xattr_len == 21) {
212 unsigned int zero = 0;
650b29db 213 if (!memcmp(&xattr_value->data[16], &zero, 4))
1525b06d 214 return HASH_ALGO_MD5;
3ea7a560 215 else
1525b06d 216 return HASH_ALGO_SHA1;
3ea7a560 217 } else if (xattr_len == 17)
1525b06d 218 return HASH_ALGO_MD5;
3ea7a560
DK
219 break;
220 }
1525b06d
DK
221
222 /* return default hash algo */
223 return ima_hash_algo;
d3634d0f
DK
224}
225
226int ima_read_xattr(struct dentry *dentry,
f6fbd8cb 227 struct evm_ima_xattr_data **xattr_value, int xattr_len)
d3634d0f 228{
f6fbd8cb 229 int ret;
d3634d0f 230
4609e1f1 231 ret = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, XATTR_NAME_IMA,
f6fbd8cb 232 (char **)xattr_value, xattr_len, GFP_NOFS);
5d6c3191
AG
233 if (ret == -EOPNOTSUPP)
234 ret = 0;
235 return ret;
d3634d0f
DK
236}
237
398c42e2
MZ
238/*
239 * calc_file_id_hash - calculate the hash of the ima_file_id struct data
240 * @type: xattr type [enum evm_ima_xattr_type]
241 * @algo: hash algorithm [enum hash_algo]
242 * @digest: pointer to the digest to be hashed
243 * @hash: (out) pointer to the hash
244 *
245 * IMA signature version 3 disambiguates the data that is signed by
246 * indirectly signing the hash of the ima_file_id structure data.
247 *
248 * Signing the ima_file_id struct is currently only supported for
249 * IMA_VERITY_DIGSIG type xattrs.
250 *
251 * Return 0 on success, error code otherwise.
252 */
253static int calc_file_id_hash(enum evm_ima_xattr_type type,
254 enum hash_algo algo, const u8 *digest,
255 struct ima_digest_data *hash)
256{
257 struct ima_file_id file_id = {
258 .hash_type = IMA_VERITY_DIGSIG, .hash_algorithm = algo};
259 unsigned int unused = HASH_MAX_DIGESTSIZE - hash_digest_size[algo];
260
261 if (type != IMA_VERITY_DIGSIG)
262 return -EINVAL;
263
264 memcpy(file_id.hash, digest, hash_digest_size[algo]);
265
266 hash->algo = algo;
267 hash->length = hash_digest_size[algo];
268
269 return ima_calc_buffer_hash(&file_id, sizeof(file_id) - unused, hash);
270}
271
a5fbeb61
TJB
272/*
273 * xattr_verify - verify xattr digest or signature
274 *
275 * Verify whether the hash or signature matches the file contents.
276 *
277 * Return 0 on success, error code otherwise.
278 */
279static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
280 struct evm_ima_xattr_data *xattr_value, int xattr_len,
281 enum integrity_status *status, const char **cause)
282{
398c42e2
MZ
283 struct ima_max_digest_data hash;
284 struct signature_v2_hdr *sig;
a5fbeb61 285 int rc = -EINVAL, hash_start = 0;
398c42e2 286 int mask;
a5fbeb61
TJB
287
288 switch (xattr_value->type) {
289 case IMA_XATTR_DIGEST_NG:
290 /* first byte contains algorithm id */
291 hash_start = 1;
df561f66 292 fallthrough;
a5fbeb61 293 case IMA_XATTR_DIGEST:
7aa5783d
RS
294 if (*status != INTEGRITY_PASS_IMMUTABLE) {
295 if (iint->flags & IMA_DIGSIG_REQUIRED) {
398c42e2
MZ
296 if (iint->flags & IMA_VERITY_REQUIRED)
297 *cause = "verity-signature-required";
298 else
299 *cause = "IMA-signature-required";
7aa5783d
RS
300 *status = INTEGRITY_FAIL;
301 break;
302 }
303 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
304 } else {
305 set_bit(IMA_DIGSIG, &iint->atomic_flags);
a5fbeb61 306 }
a5fbeb61
TJB
307 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
308 iint->ima_hash->length)
309 /*
310 * xattr length may be longer. md5 hash in previous
311 * version occupied 20 bytes in xattr, instead of 16
312 */
313 rc = memcmp(&xattr_value->data[hash_start],
314 iint->ima_hash->digest,
315 iint->ima_hash->length);
316 else
317 rc = -EINVAL;
318 if (rc) {
319 *cause = "invalid-hash";
320 *status = INTEGRITY_FAIL;
321 break;
322 }
323 *status = INTEGRITY_PASS;
324 break;
325 case EVM_IMA_XATTR_DIGSIG:
326 set_bit(IMA_DIGSIG, &iint->atomic_flags);
398c42e2
MZ
327
328 mask = IMA_DIGSIG_REQUIRED | IMA_VERITY_REQUIRED;
329 if ((iint->flags & mask) == mask) {
330 *cause = "verity-signature-required";
331 *status = INTEGRITY_FAIL;
332 break;
333 }
334
335 sig = (typeof(sig))xattr_value;
336 if (sig->version >= 3) {
337 *cause = "invalid-signature-version";
338 *status = INTEGRITY_FAIL;
339 break;
340 }
a5fbeb61
TJB
341 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
342 (const char *)xattr_value,
343 xattr_len,
344 iint->ima_hash->digest,
345 iint->ima_hash->length);
346 if (rc == -EOPNOTSUPP) {
347 *status = INTEGRITY_UNKNOWN;
348 break;
349 }
350 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
351 func == KEXEC_KERNEL_CHECK)
352 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
353 (const char *)xattr_value,
354 xattr_len,
355 iint->ima_hash->digest,
356 iint->ima_hash->length);
357 if (rc) {
358 *cause = "invalid-signature";
359 *status = INTEGRITY_FAIL;
360 } else {
361 *status = INTEGRITY_PASS;
362 }
398c42e2
MZ
363 break;
364 case IMA_VERITY_DIGSIG:
365 set_bit(IMA_DIGSIG, &iint->atomic_flags);
366
367 if (iint->flags & IMA_DIGSIG_REQUIRED) {
368 if (!(iint->flags & IMA_VERITY_REQUIRED)) {
369 *cause = "IMA-signature-required";
370 *status = INTEGRITY_FAIL;
371 break;
372 }
373 }
374
375 sig = (typeof(sig))xattr_value;
376 if (sig->version != 3) {
377 *cause = "invalid-signature-version";
378 *status = INTEGRITY_FAIL;
379 break;
380 }
381
382 rc = calc_file_id_hash(IMA_VERITY_DIGSIG, iint->ima_hash->algo,
383 iint->ima_hash->digest, &hash.hdr);
384 if (rc) {
385 *cause = "sigv3-hashing-error";
386 *status = INTEGRITY_FAIL;
387 break;
388 }
389
390 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
391 (const char *)xattr_value,
392 xattr_len, hash.digest,
393 hash.hdr.length);
394 if (rc) {
395 *cause = "invalid-verity-signature";
396 *status = INTEGRITY_FAIL;
397 } else {
398 *status = INTEGRITY_PASS;
399 }
400
a5fbeb61
TJB
401 break;
402 default:
403 *status = INTEGRITY_UNKNOWN;
404 *cause = "unknown-ima-data";
405 break;
406 }
407
408 return rc;
409}
410
39b07096
TJB
411/*
412 * modsig_verify - verify modsig signature
413 *
414 * Verify whether the signature matches the file contents.
415 *
416 * Return 0 on success, error code otherwise.
417 */
418static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
419 enum integrity_status *status, const char **cause)
420{
421 int rc;
422
423 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
424 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
425 func == KEXEC_KERNEL_CHECK)
426 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
427 modsig);
428 if (rc) {
429 *cause = "invalid-signature";
430 *status = INTEGRITY_FAIL;
431 } else {
432 *status = INTEGRITY_PASS;
433 }
434
435 return rc;
436}
437
273df864
NJ
438/*
439 * ima_check_blacklist - determine if the binary is blacklisted.
440 *
441 * Add the hash of the blacklisted binary to the measurement list, based
442 * on policy.
443 *
444 * Returns -EPERM if the hash is blacklisted.
445 */
446int ima_check_blacklist(struct integrity_iint_cache *iint,
447 const struct modsig *modsig, int pcr)
448{
449 enum hash_algo hash_algo;
450 const u8 *digest = NULL;
451 u32 digestsize = 0;
452 int rc = 0;
453
454 if (!(iint->flags & IMA_CHECK_BLACKLIST))
455 return 0;
456
457 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
458 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
459
460 rc = is_binary_blacklisted(digest, digestsize);
f20765fd
ES
461 } else if (iint->flags & IMA_DIGSIG_REQUIRED && iint->ima_hash)
462 rc = is_binary_blacklisted(iint->ima_hash->digest, iint->ima_hash->length);
463
464 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
465 process_buffer_measurement(&nop_mnt_idmap, NULL, digest, digestsize,
466 "blacklisted-hash", NONE,
467 pcr, NULL, false, NULL, 0);
273df864
NJ
468
469 return rc;
470}
471
2fe5d6de
MZ
472/*
473 * ima_appraise_measurement - appraise file measurement
474 *
475 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
476 * Assuming success, compare the xattr hash with the collected measurement.
477 *
478 * Return 0 on success, error code otherwise
479 */
4ad87a3d
MZ
480int ima_appraise_measurement(enum ima_hooks func,
481 struct integrity_iint_cache *iint,
d3634d0f
DK
482 struct file *file, const unsigned char *filename,
483 struct evm_ima_xattr_data *xattr_value,
39b07096 484 int xattr_len, const struct modsig *modsig)
2fe5d6de 485{
52a13284 486 static const char op[] = "appraise_data";
f5e51fa3 487 const char *cause = "unknown";
e71b9dff 488 struct dentry *dentry = file_dentry(file);
c6f493d6 489 struct inode *inode = d_backing_inode(dentry);
2fe5d6de 490 enum integrity_status status = INTEGRITY_UNKNOWN;
a5fbeb61 491 int rc = xattr_len;
39b07096 492 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
2fe5d6de 493
39b07096
TJB
494 /* If not appraising a modsig, we need an xattr. */
495 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
2fe5d6de
MZ
496 return INTEGRITY_UNKNOWN;
497
39b07096
TJB
498 /* If reading the xattr failed and there's no modsig, error out. */
499 if (rc <= 0 && !try_modsig) {
2fe5d6de
MZ
500 if (rc && rc != -ENODATA)
501 goto out;
502
398c42e2
MZ
503 if (iint->flags & IMA_DIGSIG_REQUIRED) {
504 if (iint->flags & IMA_VERITY_REQUIRED)
505 cause = "verity-signature-required";
506 else
507 cause = "IMA-signature-required";
508 } else {
509 cause = "missing-hash";
510 }
511
b151d6b0 512 status = INTEGRITY_NOLABEL;
6035a27b 513 if (file->f_mode & FMODE_CREATED)
b151d6b0 514 iint->flags |= IMA_NEW_FILE;
1ac202e9 515 if ((iint->flags & IMA_NEW_FILE) &&
b7e27bc1
MZ
516 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
517 (inode->i_size == 0)))
b151d6b0 518 status = INTEGRITY_PASS;
2fe5d6de
MZ
519 goto out;
520 }
521
d2ee2cfc
HL
522 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value,
523 rc < 0 ? 0 : rc, iint);
f5e51fa3
TJB
524 switch (status) {
525 case INTEGRITY_PASS:
526 case INTEGRITY_PASS_IMMUTABLE:
527 case INTEGRITY_UNKNOWN:
528 break;
529 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
39b07096
TJB
530 /* It's fine not to have xattrs when using a modsig. */
531 if (try_modsig)
532 break;
df561f66 533 fallthrough;
f5e51fa3
TJB
534 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
535 cause = "missing-HMAC";
536 goto out;
cdef685b 537 case INTEGRITY_FAIL_IMMUTABLE:
7aa5783d 538 set_bit(IMA_DIGSIG, &iint->atomic_flags);
55748ac6
MZ
539 cause = "invalid-fail-immutable";
540 goto out;
f5e51fa3
TJB
541 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
542 cause = "invalid-HMAC";
2fe5d6de 543 goto out;
f5e51fa3
TJB
544 default:
545 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
2fe5d6de 546 }
f5e51fa3 547
a5fbeb61
TJB
548 if (xattr_value)
549 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
550 &cause);
8606404f 551
39b07096
TJB
552 /*
553 * If we have a modsig and either no imasig or the imasig's key isn't
554 * known, then try verifying the modsig.
555 */
556 if (try_modsig &&
557 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
558 rc == -ENOKEY))
559 rc = modsig_verify(func, modsig, &status, &cause);
560
2fe5d6de 561out:
57b56ac6
MZ
562 /*
563 * File signatures on some filesystems can not be properly verified.
9e67028e
MZ
564 * When such filesystems are mounted by an untrusted mounter or on a
565 * system not willing to accept such a risk, fail the file signature
566 * verification.
57b56ac6 567 */
9e67028e
MZ
568 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
569 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
570 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
57b56ac6
MZ
571 status = INTEGRITY_FAIL;
572 cause = "unverifiable-signature";
573 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
574 op, cause, rc, 0);
575 } else if (status != INTEGRITY_PASS) {
f5e51fa3 576 /* Fix mode, but don't replace file signatures. */
39b07096 577 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
8606404f
DK
578 (!xattr_value ||
579 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
def3e8b9
DK
580 if (!ima_fix_xattr(dentry, iint))
581 status = INTEGRITY_PASS;
f5e51fa3
TJB
582 }
583
7aa5783d
RS
584 /*
585 * Permit new files with file/EVM portable signatures, but
586 * without data.
587 */
f5e51fa3 588 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
7aa5783d 589 test_bit(IMA_DIGSIG, &iint->atomic_flags)) {
05d1a717 590 status = INTEGRITY_PASS;
2fe5d6de 591 }
f5e51fa3 592
2fe5d6de
MZ
593 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
594 op, cause, rc, 0);
8606404f 595 } else {
d79d72e0 596 ima_cache_flags(iint, func);
2fe5d6de 597 }
57b56ac6 598
d79d72e0 599 ima_set_cache_status(iint, func, status);
2fe5d6de
MZ
600 return status;
601}
602
603/*
604 * ima_update_xattr - update 'security.ima' hash value
605 */
606void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
607{
e71b9dff 608 struct dentry *dentry = file_dentry(file);
2fe5d6de
MZ
609 int rc = 0;
610
8606404f 611 /* do not collect and update hash for digital signatures */
0d73a552 612 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
8606404f
DK
613 return;
614
da1b0029
MZ
615 if ((iint->ima_file_status != INTEGRITY_PASS) &&
616 !(iint->flags & IMA_HASH))
020aae3e
RS
617 return;
618
15588227 619 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
2fe5d6de
MZ
620 if (rc < 0)
621 return;
8606404f 622
0d73a552 623 inode_lock(file_inode(file));
2fe5d6de 624 ima_fix_xattr(dentry, iint);
0d73a552 625 inode_unlock(file_inode(file));
2fe5d6de
MZ
626}
627
628/**
629 * ima_inode_post_setattr - reflect file metadata changes
39f60c1c 630 * @idmap: idmap of the mount the inode was found from
2fe5d6de 631 * @dentry: pointer to the affected dentry
bad5247a 632 * @ia_valid: for the UID and GID status
2fe5d6de
MZ
633 *
634 * Changes to a dentry's metadata might result in needing to appraise.
635 *
636 * This function is called from notify_change(), which expects the caller
637 * to lock the inode's i_mutex.
638 */
84594c9e
RS
639static void ima_inode_post_setattr(struct mnt_idmap *idmap,
640 struct dentry *dentry, int ia_valid)
2fe5d6de 641{
c6f493d6 642 struct inode *inode = d_backing_inode(dentry);
2fe5d6de 643 struct integrity_iint_cache *iint;
da1b0029 644 int action;
2fe5d6de 645
a756024e 646 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
5d6c3191 647 || !(inode->i_opflags & IOP_XATTR))
2fe5d6de
MZ
648 return;
649
39f60c1c 650 action = ima_must_appraise(idmap, inode, MAY_ACCESS, POST_SETATTR);
2fe5d6de
MZ
651 iint = integrity_iint_find(inode);
652 if (iint) {
0d73a552 653 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
da1b0029 654 if (!action)
0d73a552 655 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
2fe5d6de 656 }
2fe5d6de 657}
42c63330
MZ
658
659/*
660 * ima_protect_xattr - protect 'security.ima'
661 *
662 * Ensure that not just anyone can modify or remove 'security.ima'.
663 */
664static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
665 const void *xattr_value, size_t xattr_value_len)
666{
667 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
668 if (!capable(CAP_SYS_ADMIN))
669 return -EPERM;
670 return 1;
671 }
672 return 0;
673}
674
060bdebf 675static void ima_reset_appraise_flags(struct inode *inode, int digsig)
42c63330
MZ
676{
677 struct integrity_iint_cache *iint;
678
a756024e 679 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
42c63330
MZ
680 return;
681
682 iint = integrity_iint_find(inode);
683 if (!iint)
684 return;
a422638d 685 iint->measured_pcrs = 0;
0d73a552 686 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
060bdebf 687 if (digsig)
0d73a552
DK
688 set_bit(IMA_DIGSIG, &iint->atomic_flags);
689 else
690 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
42c63330
MZ
691}
692
50f742dd
TS
693/**
694 * validate_hash_algo() - Block setxattr with unsupported hash algorithms
695 * @dentry: object of the setxattr()
696 * @xattr_value: userland supplied xattr value
697 * @xattr_value_len: length of xattr_value
698 *
699 * The xattr value is mapped to its hash algorithm, and this algorithm
700 * must be built in the kernel for the setxattr to be allowed.
701 *
702 * Emit an audit message when the algorithm is invalid.
703 *
704 * Return: 0 on success, else an error.
705 */
706static int validate_hash_algo(struct dentry *dentry,
707 const struct evm_ima_xattr_data *xattr_value,
708 size_t xattr_value_len)
709{
710 char *path = NULL, *pathbuf = NULL;
711 enum hash_algo xattr_hash_algo;
4f2946aa
TS
712 const char *errmsg = "unavailable-hash-algorithm";
713 unsigned int allowed_hashes;
50f742dd
TS
714
715 xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len);
716
4f2946aa
TS
717 allowed_hashes = atomic_read(&ima_setxattr_allowed_hash_algorithms);
718
719 if (allowed_hashes) {
720 /* success if the algorithm is allowed in the ima policy */
721 if (allowed_hashes & (1U << xattr_hash_algo))
722 return 0;
723
724 /*
725 * We use a different audit message when the hash algorithm
726 * is denied by a policy rule, instead of not being built
727 * in the kernel image
728 */
729 errmsg = "denied-hash-algorithm";
730 } else {
731 if (likely(xattr_hash_algo == ima_hash_algo))
732 return 0;
733
734 /* allow any xattr using an algorithm built in the kernel */
735 if (crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0))
736 return 0;
737 }
50f742dd
TS
738
739 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
740 if (!pathbuf)
741 return -EACCES;
742
743 path = dentry_path(dentry, pathbuf, PATH_MAX);
744
745 integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path,
4f2946aa 746 "set_data", errmsg, -EACCES, 0);
50f742dd
TS
747
748 kfree(pathbuf);
749
750 return -EACCES;
751}
752
84594c9e
RS
753static int ima_inode_setxattr(struct mnt_idmap *idmap, struct dentry *dentry,
754 const char *xattr_name, const void *xattr_value,
755 size_t xattr_value_len, int flags)
42c63330 756{
060bdebf 757 const struct evm_ima_xattr_data *xvalue = xattr_value;
e3ccfe1a 758 int digsig = 0;
42c63330 759 int result;
5926586f 760 int err;
42c63330
MZ
761
762 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
763 xattr_value_len);
764 if (result == 1) {
a48fda9d
DK
765 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
766 return -EINVAL;
5926586f
MZ
767
768 err = validate_hash_algo(dentry, xvalue, xattr_value_len);
769 if (err)
770 return err;
771
e3ccfe1a 772 digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG);
7aa5783d
RS
773 } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) {
774 digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG);
e3ccfe1a
RS
775 }
776 if (result == 1 || evm_revalidate_status(xattr_name)) {
777 ima_reset_appraise_flags(d_backing_inode(dentry), digsig);
5926586f
MZ
778 if (result == 1)
779 result = 0;
42c63330
MZ
780 }
781 return result;
782}
783
84594c9e
RS
784static int ima_inode_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
785 const char *acl_name, struct posix_acl *kacl)
e61b135f
CB
786{
787 if (evm_revalidate_status(acl_name))
788 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
789
790 return 0;
791}
792
84594c9e
RS
793static int ima_inode_removexattr(struct mnt_idmap *idmap, struct dentry *dentry,
794 const char *xattr_name)
42c63330
MZ
795{
796 int result;
797
798 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
e3ccfe1a 799 if (result == 1 || evm_revalidate_status(xattr_name)) {
c6f493d6 800 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
e3ccfe1a
RS
801 if (result == 1)
802 result = 0;
42c63330
MZ
803 }
804 return result;
805}
84594c9e
RS
806
807static int ima_inode_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry,
808 const char *acl_name)
809{
810 return ima_inode_set_acl(idmap, dentry, acl_name, NULL);
811}
812
813static struct security_hook_list ima_appraise_hooks[] __ro_after_init = {
814 LSM_HOOK_INIT(inode_post_setattr, ima_inode_post_setattr),
815 LSM_HOOK_INIT(inode_setxattr, ima_inode_setxattr),
816 LSM_HOOK_INIT(inode_set_acl, ima_inode_set_acl),
817 LSM_HOOK_INIT(inode_removexattr, ima_inode_removexattr),
818 LSM_HOOK_INIT(inode_remove_acl, ima_inode_remove_acl),
819};
820
821void __init init_ima_appraise_lsm(const struct lsm_id *lsmid)
822{
823 security_add_hooks(ima_appraise_hooks, ARRAY_SIZE(ima_appraise_hooks),
824 lsmid);
825}