1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 IBM Corporation
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/file.h>
12 #include <linux/xattr.h>
13 #include <linux/magic.h>
14 #include <linux/ima.h>
15 #include <linux/evm.h>
16 #include <linux/fsverity.h>
17 #include <keys/system_keyring.h>
18 #include <uapi/linux/fsverity.h>
22 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
23 static char *ima_appraise_cmdline_default __initdata;
24 core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0);
26 void __init ima_appraise_parse_cmdline(void)
28 const char *str = ima_appraise_cmdline_default;
29 bool sb_state = arch_ima_get_secureboot();
30 int appraisal_state = ima_appraise;
35 if (strncmp(str, "off", 3) == 0)
37 else if (strncmp(str, "log", 3) == 0)
38 appraisal_state = IMA_APPRAISE_LOG;
39 else if (strncmp(str, "fix", 3) == 0)
40 appraisal_state = IMA_APPRAISE_FIX;
41 else if (strncmp(str, "enforce", 7) == 0)
42 appraisal_state = IMA_APPRAISE_ENFORCE;
44 pr_err("invalid \"%s\" appraise option", str);
46 /* If appraisal state was changed, but secure boot is enabled,
49 if (!(appraisal_state & IMA_APPRAISE_ENFORCE))
50 pr_info("Secure boot enabled: ignoring ima_appraise=%s option",
53 ima_appraise = appraisal_state;
59 * is_ima_appraise_enabled - return appraise status
61 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
63 bool is_ima_appraise_enabled(void)
65 return ima_appraise & IMA_APPRAISE_ENFORCE;
69 * ima_must_appraise - set appraise flag
71 * Return 1 to appraise or hash
73 int ima_must_appraise(struct mnt_idmap *idmap, struct inode *inode,
74 int mask, enum ima_hooks func)
81 security_current_getsecid_subj(&secid);
82 return ima_match_policy(idmap, inode, current_cred(), secid,
83 func, mask, IMA_APPRAISE | IMA_HASH, NULL,
87 static int ima_fix_xattr(struct dentry *dentry,
88 struct integrity_iint_cache *iint)
91 u8 algo = iint->ima_hash->algo;
93 if (algo <= HASH_ALGO_SHA1) {
95 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
98 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
99 iint->ima_hash->xattr.ng.algo = algo;
101 rc = __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_IMA,
102 &iint->ima_hash->xattr.data[offset],
103 (sizeof(iint->ima_hash->xattr) - offset) +
104 iint->ima_hash->length, 0);
108 /* Return specific func appraised cached result */
109 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
114 case MMAP_CHECK_REQPROT:
115 return iint->ima_mmap_status;
117 return iint->ima_bprm_status;
119 return iint->ima_creds_status;
122 return iint->ima_file_status;
123 case MODULE_CHECK ... MAX_CHECK - 1:
125 return iint->ima_read_status;
129 static void ima_set_cache_status(struct integrity_iint_cache *iint,
131 enum integrity_status status)
135 case MMAP_CHECK_REQPROT:
136 iint->ima_mmap_status = status;
139 iint->ima_bprm_status = status;
142 iint->ima_creds_status = status;
146 iint->ima_file_status = status;
148 case MODULE_CHECK ... MAX_CHECK - 1:
150 iint->ima_read_status = status;
155 static void ima_cache_flags(struct integrity_iint_cache *iint,
160 case MMAP_CHECK_REQPROT:
161 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
164 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
167 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
171 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
173 case MODULE_CHECK ... MAX_CHECK - 1:
175 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
180 enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
183 struct signature_v2_hdr *sig;
186 if (!xattr_value || xattr_len < 2)
187 /* return default hash algo */
188 return ima_hash_algo;
190 switch (xattr_value->type) {
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;
197 case EVM_IMA_XATTR_DIGSIG:
198 sig = (typeof(sig))xattr_value;
199 if (sig->version != 2 || xattr_len <= sizeof(*sig)
200 || sig->hash_algo >= HASH_ALGO__LAST)
201 return ima_hash_algo;
202 return sig->hash_algo;
203 case IMA_XATTR_DIGEST_NG:
204 /* first byte contains algorithm id */
205 ret = xattr_value->data[0];
206 if (ret < HASH_ALGO__LAST)
209 case IMA_XATTR_DIGEST:
210 /* this is for backward compatibility */
211 if (xattr_len == 21) {
212 unsigned int zero = 0;
213 if (!memcmp(&xattr_value->data[16], &zero, 4))
214 return HASH_ALGO_MD5;
216 return HASH_ALGO_SHA1;
217 } else if (xattr_len == 17)
218 return HASH_ALGO_MD5;
222 /* return default hash algo */
223 return ima_hash_algo;
226 int ima_read_xattr(struct dentry *dentry,
227 struct evm_ima_xattr_data **xattr_value, int xattr_len)
231 ret = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, XATTR_NAME_IMA,
232 (char **)xattr_value, xattr_len, GFP_NOFS);
233 if (ret == -EOPNOTSUPP)
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
245 * IMA signature version 3 disambiguates the data that is signed by
246 * indirectly signing the hash of the ima_file_id structure data.
248 * Signing the ima_file_id struct is currently only supported for
249 * IMA_VERITY_DIGSIG type xattrs.
251 * Return 0 on success, error code otherwise.
253 static 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)
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];
261 if (type != IMA_VERITY_DIGSIG)
264 memcpy(file_id.hash, digest, hash_digest_size[algo]);
267 hash->length = hash_digest_size[algo];
269 return ima_calc_buffer_hash(&file_id, sizeof(file_id) - unused, hash);
273 * xattr_verify - verify xattr digest or signature
275 * Verify whether the hash or signature matches the file contents.
277 * Return 0 on success, error code otherwise.
279 static 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)
283 struct ima_max_digest_data hash;
284 struct signature_v2_hdr *sig;
285 int rc = -EINVAL, hash_start = 0;
288 switch (xattr_value->type) {
289 case IMA_XATTR_DIGEST_NG:
290 /* first byte contains algorithm id */
293 case IMA_XATTR_DIGEST:
294 if (*status != INTEGRITY_PASS_IMMUTABLE) {
295 if (iint->flags & IMA_DIGSIG_REQUIRED) {
296 if (iint->flags & IMA_VERITY_REQUIRED)
297 *cause = "verity-signature-required";
299 *cause = "IMA-signature-required";
300 *status = INTEGRITY_FAIL;
303 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
305 set_bit(IMA_DIGSIG, &iint->atomic_flags);
307 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
308 iint->ima_hash->length)
310 * xattr length may be longer. md5 hash in previous
311 * version occupied 20 bytes in xattr, instead of 16
313 rc = memcmp(&xattr_value->data[hash_start],
314 iint->ima_hash->digest,
315 iint->ima_hash->length);
319 *cause = "invalid-hash";
320 *status = INTEGRITY_FAIL;
323 *status = INTEGRITY_PASS;
325 case EVM_IMA_XATTR_DIGSIG:
326 set_bit(IMA_DIGSIG, &iint->atomic_flags);
328 mask = IMA_DIGSIG_REQUIRED | IMA_VERITY_REQUIRED;
329 if ((iint->flags & mask) == mask) {
330 *cause = "verity-signature-required";
331 *status = INTEGRITY_FAIL;
335 sig = (typeof(sig))xattr_value;
336 if (sig->version >= 3) {
337 *cause = "invalid-signature-version";
338 *status = INTEGRITY_FAIL;
341 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
342 (const char *)xattr_value,
344 iint->ima_hash->digest,
345 iint->ima_hash->length);
346 if (rc == -EOPNOTSUPP) {
347 *status = INTEGRITY_UNKNOWN;
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,
355 iint->ima_hash->digest,
356 iint->ima_hash->length);
358 *cause = "invalid-signature";
359 *status = INTEGRITY_FAIL;
361 *status = INTEGRITY_PASS;
364 case IMA_VERITY_DIGSIG:
365 set_bit(IMA_DIGSIG, &iint->atomic_flags);
367 if (iint->flags & IMA_DIGSIG_REQUIRED) {
368 if (!(iint->flags & IMA_VERITY_REQUIRED)) {
369 *cause = "IMA-signature-required";
370 *status = INTEGRITY_FAIL;
375 sig = (typeof(sig))xattr_value;
376 if (sig->version != 3) {
377 *cause = "invalid-signature-version";
378 *status = INTEGRITY_FAIL;
382 rc = calc_file_id_hash(IMA_VERITY_DIGSIG, iint->ima_hash->algo,
383 iint->ima_hash->digest, &hash.hdr);
385 *cause = "sigv3-hashing-error";
386 *status = INTEGRITY_FAIL;
390 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
391 (const char *)xattr_value,
392 xattr_len, hash.digest,
395 *cause = "invalid-verity-signature";
396 *status = INTEGRITY_FAIL;
398 *status = INTEGRITY_PASS;
403 *status = INTEGRITY_UNKNOWN;
404 *cause = "unknown-ima-data";
412 * modsig_verify - verify modsig signature
414 * Verify whether the signature matches the file contents.
416 * Return 0 on success, error code otherwise.
418 static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
419 enum integrity_status *status, const char **cause)
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,
429 *cause = "invalid-signature";
430 *status = INTEGRITY_FAIL;
432 *status = INTEGRITY_PASS;
439 * ima_check_blacklist - determine if the binary is blacklisted.
441 * Add the hash of the blacklisted binary to the measurement list, based
444 * Returns -EPERM if the hash is blacklisted.
446 int ima_check_blacklist(struct integrity_iint_cache *iint,
447 const struct modsig *modsig, int pcr)
449 enum hash_algo hash_algo;
450 const u8 *digest = NULL;
454 if (!(iint->flags & IMA_CHECK_BLACKLIST))
457 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
458 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
460 rc = is_binary_blacklisted(digest, digestsize);
461 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
462 process_buffer_measurement(&nop_mnt_idmap, NULL, digest, digestsize,
463 "blacklisted-hash", NONE,
464 pcr, NULL, false, NULL, 0);
471 * ima_appraise_measurement - appraise file measurement
473 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
474 * Assuming success, compare the xattr hash with the collected measurement.
476 * Return 0 on success, error code otherwise
478 int ima_appraise_measurement(enum ima_hooks func,
479 struct integrity_iint_cache *iint,
480 struct file *file, const unsigned char *filename,
481 struct evm_ima_xattr_data *xattr_value,
482 int xattr_len, const struct modsig *modsig)
484 static const char op[] = "appraise_data";
485 const char *cause = "unknown";
486 struct dentry *dentry = file_dentry(file);
487 struct inode *inode = d_backing_inode(dentry);
488 enum integrity_status status = INTEGRITY_UNKNOWN;
490 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
492 /* If not appraising a modsig, we need an xattr. */
493 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
494 return INTEGRITY_UNKNOWN;
496 /* If reading the xattr failed and there's no modsig, error out. */
497 if (rc <= 0 && !try_modsig) {
498 if (rc && rc != -ENODATA)
501 if (iint->flags & IMA_DIGSIG_REQUIRED) {
502 if (iint->flags & IMA_VERITY_REQUIRED)
503 cause = "verity-signature-required";
505 cause = "IMA-signature-required";
507 cause = "missing-hash";
510 status = INTEGRITY_NOLABEL;
511 if (file->f_mode & FMODE_CREATED)
512 iint->flags |= IMA_NEW_FILE;
513 if ((iint->flags & IMA_NEW_FILE) &&
514 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
515 (inode->i_size == 0)))
516 status = INTEGRITY_PASS;
520 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value,
521 rc < 0 ? 0 : rc, iint);
524 case INTEGRITY_PASS_IMMUTABLE:
525 case INTEGRITY_UNKNOWN:
527 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
528 /* It's fine not to have xattrs when using a modsig. */
532 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
533 cause = "missing-HMAC";
535 case INTEGRITY_FAIL_IMMUTABLE:
536 set_bit(IMA_DIGSIG, &iint->atomic_flags);
537 cause = "invalid-fail-immutable";
539 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
540 cause = "invalid-HMAC";
543 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
547 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
551 * If we have a modsig and either no imasig or the imasig's key isn't
552 * known, then try verifying the modsig.
555 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
557 rc = modsig_verify(func, modsig, &status, &cause);
561 * File signatures on some filesystems can not be properly verified.
562 * When such filesystems are mounted by an untrusted mounter or on a
563 * system not willing to accept such a risk, fail the file signature
566 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
567 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
568 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
569 status = INTEGRITY_FAIL;
570 cause = "unverifiable-signature";
571 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
573 } else if (status != INTEGRITY_PASS) {
574 /* Fix mode, but don't replace file signatures. */
575 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
577 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
578 if (!ima_fix_xattr(dentry, iint))
579 status = INTEGRITY_PASS;
583 * Permit new files with file/EVM portable signatures, but
586 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
587 test_bit(IMA_DIGSIG, &iint->atomic_flags)) {
588 status = INTEGRITY_PASS;
591 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
594 ima_cache_flags(iint, func);
597 ima_set_cache_status(iint, func, status);
602 * ima_update_xattr - update 'security.ima' hash value
604 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
606 struct dentry *dentry = file_dentry(file);
609 /* do not collect and update hash for digital signatures */
610 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
613 if ((iint->ima_file_status != INTEGRITY_PASS) &&
614 !(iint->flags & IMA_HASH))
617 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
621 inode_lock(file_inode(file));
622 ima_fix_xattr(dentry, iint);
623 inode_unlock(file_inode(file));
627 * ima_inode_post_setattr - reflect file metadata changes
628 * @idmap: idmap of the mount the inode was found from
629 * @dentry: pointer to the affected dentry
631 * Changes to a dentry's metadata might result in needing to appraise.
633 * This function is called from notify_change(), which expects the caller
634 * to lock the inode's i_mutex.
636 void ima_inode_post_setattr(struct mnt_idmap *idmap,
637 struct dentry *dentry)
639 struct inode *inode = d_backing_inode(dentry);
640 struct integrity_iint_cache *iint;
643 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
644 || !(inode->i_opflags & IOP_XATTR))
647 action = ima_must_appraise(idmap, inode, MAY_ACCESS, POST_SETATTR);
648 iint = integrity_iint_find(inode);
650 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
652 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
657 * ima_protect_xattr - protect 'security.ima'
659 * Ensure that not just anyone can modify or remove 'security.ima'.
661 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
662 const void *xattr_value, size_t xattr_value_len)
664 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
665 if (!capable(CAP_SYS_ADMIN))
672 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
674 struct integrity_iint_cache *iint;
676 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
679 iint = integrity_iint_find(inode);
682 iint->measured_pcrs = 0;
683 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
685 set_bit(IMA_DIGSIG, &iint->atomic_flags);
687 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
691 * validate_hash_algo() - Block setxattr with unsupported hash algorithms
692 * @dentry: object of the setxattr()
693 * @xattr_value: userland supplied xattr value
694 * @xattr_value_len: length of xattr_value
696 * The xattr value is mapped to its hash algorithm, and this algorithm
697 * must be built in the kernel for the setxattr to be allowed.
699 * Emit an audit message when the algorithm is invalid.
701 * Return: 0 on success, else an error.
703 static int validate_hash_algo(struct dentry *dentry,
704 const struct evm_ima_xattr_data *xattr_value,
705 size_t xattr_value_len)
707 char *path = NULL, *pathbuf = NULL;
708 enum hash_algo xattr_hash_algo;
709 const char *errmsg = "unavailable-hash-algorithm";
710 unsigned int allowed_hashes;
712 xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len);
714 allowed_hashes = atomic_read(&ima_setxattr_allowed_hash_algorithms);
716 if (allowed_hashes) {
717 /* success if the algorithm is allowed in the ima policy */
718 if (allowed_hashes & (1U << xattr_hash_algo))
722 * We use a different audit message when the hash algorithm
723 * is denied by a policy rule, instead of not being built
724 * in the kernel image
726 errmsg = "denied-hash-algorithm";
728 if (likely(xattr_hash_algo == ima_hash_algo))
731 /* allow any xattr using an algorithm built in the kernel */
732 if (crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0))
736 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
740 path = dentry_path(dentry, pathbuf, PATH_MAX);
742 integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path,
743 "set_data", errmsg, -EACCES, 0);
750 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
751 const void *xattr_value, size_t xattr_value_len)
753 const struct evm_ima_xattr_data *xvalue = xattr_value;
758 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
761 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
764 err = validate_hash_algo(dentry, xvalue, xattr_value_len);
768 digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG);
769 } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) {
770 digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG);
772 if (result == 1 || evm_revalidate_status(xattr_name)) {
773 ima_reset_appraise_flags(d_backing_inode(dentry), digsig);
780 int ima_inode_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
781 const char *acl_name, struct posix_acl *kacl)
783 if (evm_revalidate_status(acl_name))
784 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
789 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
793 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
794 if (result == 1 || evm_revalidate_status(xattr_name)) {
795 ima_reset_appraise_flags(d_backing_inode(dentry), 0);