1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 IBM Corporation
6 * Mimi Zohar <zohar@us.ibm.com>
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 <keys/system_keyring.h>
20 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
21 static char *ima_appraise_cmdline_default __initdata;
22 core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0);
24 void __init ima_appraise_parse_cmdline(void)
26 const char *str = ima_appraise_cmdline_default;
27 bool sb_state = arch_ima_get_secureboot();
28 int appraisal_state = ima_appraise;
33 if (strncmp(str, "off", 3) == 0)
35 else if (strncmp(str, "log", 3) == 0)
36 appraisal_state = IMA_APPRAISE_LOG;
37 else if (strncmp(str, "fix", 3) == 0)
38 appraisal_state = IMA_APPRAISE_FIX;
39 else if (strncmp(str, "enforce", 7) == 0)
40 appraisal_state = IMA_APPRAISE_ENFORCE;
42 pr_err("invalid \"%s\" appraise option", str);
44 /* If appraisal state was changed, but secure boot is enabled,
47 if (!(appraisal_state & IMA_APPRAISE_ENFORCE))
48 pr_info("Secure boot enabled: ignoring ima_appraise=%s option",
51 ima_appraise = appraisal_state;
57 * is_ima_appraise_enabled - return appraise status
59 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
61 bool is_ima_appraise_enabled(void)
63 return ima_appraise & IMA_APPRAISE_ENFORCE;
67 * ima_must_appraise - set appraise flag
69 * Return 1 to appraise or hash
71 int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode,
72 int mask, enum ima_hooks func)
79 security_task_getsecid_subj(current, &secid);
80 return ima_match_policy(mnt_userns, inode, current_cred(), secid,
81 func, mask, IMA_APPRAISE | IMA_HASH, NULL,
85 static int ima_fix_xattr(struct dentry *dentry,
86 struct integrity_iint_cache *iint)
89 u8 algo = iint->ima_hash->algo;
91 if (algo <= HASH_ALGO_SHA1) {
93 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
96 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
97 iint->ima_hash->xattr.ng.algo = algo;
99 rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA,
100 &iint->ima_hash->xattr.data[offset],
101 (sizeof(iint->ima_hash->xattr) - offset) +
102 iint->ima_hash->length, 0);
106 /* Return specific func appraised cached result */
107 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
112 return iint->ima_mmap_status;
114 return iint->ima_bprm_status;
116 return iint->ima_creds_status;
119 return iint->ima_file_status;
120 case MODULE_CHECK ... MAX_CHECK - 1:
122 return iint->ima_read_status;
126 static void ima_set_cache_status(struct integrity_iint_cache *iint,
128 enum integrity_status status)
132 iint->ima_mmap_status = status;
135 iint->ima_bprm_status = status;
138 iint->ima_creds_status = status;
142 iint->ima_file_status = status;
144 case MODULE_CHECK ... MAX_CHECK - 1:
146 iint->ima_read_status = status;
151 static void ima_cache_flags(struct integrity_iint_cache *iint,
156 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
159 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
162 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
166 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
168 case MODULE_CHECK ... MAX_CHECK - 1:
170 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
175 enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
178 struct signature_v2_hdr *sig;
181 if (!xattr_value || xattr_len < 2)
182 /* return default hash algo */
183 return ima_hash_algo;
185 switch (xattr_value->type) {
186 case EVM_IMA_XATTR_DIGSIG:
187 sig = (typeof(sig))xattr_value;
188 if (sig->version != 2 || xattr_len <= sizeof(*sig)
189 || sig->hash_algo >= HASH_ALGO__LAST)
190 return ima_hash_algo;
191 return sig->hash_algo;
193 case IMA_XATTR_DIGEST_NG:
194 /* first byte contains algorithm id */
195 ret = xattr_value->data[0];
196 if (ret < HASH_ALGO__LAST)
199 case IMA_XATTR_DIGEST:
200 /* this is for backward compatibility */
201 if (xattr_len == 21) {
202 unsigned int zero = 0;
203 if (!memcmp(&xattr_value->data[16], &zero, 4))
204 return HASH_ALGO_MD5;
206 return HASH_ALGO_SHA1;
207 } else if (xattr_len == 17)
208 return HASH_ALGO_MD5;
212 /* return default hash algo */
213 return ima_hash_algo;
216 int ima_read_xattr(struct dentry *dentry,
217 struct evm_ima_xattr_data **xattr_value)
221 ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA,
222 (char **)xattr_value, 0, GFP_NOFS);
223 if (ret == -EOPNOTSUPP)
229 * xattr_verify - verify xattr digest or signature
231 * Verify whether the hash or signature matches the file contents.
233 * Return 0 on success, error code otherwise.
235 static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
236 struct evm_ima_xattr_data *xattr_value, int xattr_len,
237 enum integrity_status *status, const char **cause)
239 int rc = -EINVAL, hash_start = 0;
241 switch (xattr_value->type) {
242 case IMA_XATTR_DIGEST_NG:
243 /* first byte contains algorithm id */
246 case IMA_XATTR_DIGEST:
247 if (*status != INTEGRITY_PASS_IMMUTABLE) {
248 if (iint->flags & IMA_DIGSIG_REQUIRED) {
249 *cause = "IMA-signature-required";
250 *status = INTEGRITY_FAIL;
253 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
255 set_bit(IMA_DIGSIG, &iint->atomic_flags);
257 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
258 iint->ima_hash->length)
260 * xattr length may be longer. md5 hash in previous
261 * version occupied 20 bytes in xattr, instead of 16
263 rc = memcmp(&xattr_value->data[hash_start],
264 iint->ima_hash->digest,
265 iint->ima_hash->length);
269 *cause = "invalid-hash";
270 *status = INTEGRITY_FAIL;
273 *status = INTEGRITY_PASS;
275 case EVM_IMA_XATTR_DIGSIG:
276 set_bit(IMA_DIGSIG, &iint->atomic_flags);
277 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
278 (const char *)xattr_value,
280 iint->ima_hash->digest,
281 iint->ima_hash->length);
282 if (rc == -EOPNOTSUPP) {
283 *status = INTEGRITY_UNKNOWN;
286 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
287 func == KEXEC_KERNEL_CHECK)
288 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
289 (const char *)xattr_value,
291 iint->ima_hash->digest,
292 iint->ima_hash->length);
294 *cause = "invalid-signature";
295 *status = INTEGRITY_FAIL;
297 *status = INTEGRITY_PASS;
301 *status = INTEGRITY_UNKNOWN;
302 *cause = "unknown-ima-data";
310 * modsig_verify - verify modsig signature
312 * Verify whether the signature matches the file contents.
314 * Return 0 on success, error code otherwise.
316 static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
317 enum integrity_status *status, const char **cause)
321 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
322 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
323 func == KEXEC_KERNEL_CHECK)
324 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
327 *cause = "invalid-signature";
328 *status = INTEGRITY_FAIL;
330 *status = INTEGRITY_PASS;
337 * ima_check_blacklist - determine if the binary is blacklisted.
339 * Add the hash of the blacklisted binary to the measurement list, based
342 * Returns -EPERM if the hash is blacklisted.
344 int ima_check_blacklist(struct integrity_iint_cache *iint,
345 const struct modsig *modsig, int pcr)
347 enum hash_algo hash_algo;
348 const u8 *digest = NULL;
352 if (!(iint->flags & IMA_CHECK_BLACKLIST))
355 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
356 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
358 rc = is_binary_blacklisted(digest, digestsize);
359 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
360 process_buffer_measurement(&init_user_ns, NULL, digest, digestsize,
361 "blacklisted-hash", NONE,
362 pcr, NULL, false, NULL, 0);
369 * ima_appraise_measurement - appraise file measurement
371 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
372 * Assuming success, compare the xattr hash with the collected measurement.
374 * Return 0 on success, error code otherwise
376 int ima_appraise_measurement(enum ima_hooks func,
377 struct integrity_iint_cache *iint,
378 struct file *file, const unsigned char *filename,
379 struct evm_ima_xattr_data *xattr_value,
380 int xattr_len, const struct modsig *modsig)
382 static const char op[] = "appraise_data";
383 const char *cause = "unknown";
384 struct dentry *dentry = file_dentry(file);
385 struct inode *inode = d_backing_inode(dentry);
386 enum integrity_status status = INTEGRITY_UNKNOWN;
388 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
390 /* If not appraising a modsig, we need an xattr. */
391 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
392 return INTEGRITY_UNKNOWN;
394 /* If reading the xattr failed and there's no modsig, error out. */
395 if (rc <= 0 && !try_modsig) {
396 if (rc && rc != -ENODATA)
399 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
400 "IMA-signature-required" : "missing-hash";
401 status = INTEGRITY_NOLABEL;
402 if (file->f_mode & FMODE_CREATED)
403 iint->flags |= IMA_NEW_FILE;
404 if ((iint->flags & IMA_NEW_FILE) &&
405 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
406 (inode->i_size == 0)))
407 status = INTEGRITY_PASS;
411 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value,
412 rc < 0 ? 0 : rc, iint);
415 case INTEGRITY_PASS_IMMUTABLE:
416 case INTEGRITY_UNKNOWN:
418 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
419 /* It's fine not to have xattrs when using a modsig. */
423 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
424 cause = "missing-HMAC";
426 case INTEGRITY_FAIL_IMMUTABLE:
427 set_bit(IMA_DIGSIG, &iint->atomic_flags);
428 cause = "invalid-fail-immutable";
430 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
431 cause = "invalid-HMAC";
434 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
438 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
442 * If we have a modsig and either no imasig or the imasig's key isn't
443 * known, then try verifying the modsig.
446 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
448 rc = modsig_verify(func, modsig, &status, &cause);
452 * File signatures on some filesystems can not be properly verified.
453 * When such filesystems are mounted by an untrusted mounter or on a
454 * system not willing to accept such a risk, fail the file signature
457 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
458 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
459 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
460 status = INTEGRITY_FAIL;
461 cause = "unverifiable-signature";
462 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
464 } else if (status != INTEGRITY_PASS) {
465 /* Fix mode, but don't replace file signatures. */
466 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
468 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
469 if (!ima_fix_xattr(dentry, iint))
470 status = INTEGRITY_PASS;
474 * Permit new files with file/EVM portable signatures, but
477 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
478 test_bit(IMA_DIGSIG, &iint->atomic_flags)) {
479 status = INTEGRITY_PASS;
482 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
485 ima_cache_flags(iint, func);
488 ima_set_cache_status(iint, func, status);
493 * ima_update_xattr - update 'security.ima' hash value
495 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
497 struct dentry *dentry = file_dentry(file);
500 /* do not collect and update hash for digital signatures */
501 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
504 if ((iint->ima_file_status != INTEGRITY_PASS) &&
505 !(iint->flags & IMA_HASH))
508 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
512 inode_lock(file_inode(file));
513 ima_fix_xattr(dentry, iint);
514 inode_unlock(file_inode(file));
518 * ima_inode_post_setattr - reflect file metadata changes
519 * @mnt_userns: user namespace of the mount the inode was found from
520 * @dentry: pointer to the affected dentry
522 * Changes to a dentry's metadata might result in needing to appraise.
524 * This function is called from notify_change(), which expects the caller
525 * to lock the inode's i_mutex.
527 void ima_inode_post_setattr(struct user_namespace *mnt_userns,
528 struct dentry *dentry)
530 struct inode *inode = d_backing_inode(dentry);
531 struct integrity_iint_cache *iint;
534 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
535 || !(inode->i_opflags & IOP_XATTR))
538 action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR);
539 iint = integrity_iint_find(inode);
541 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
543 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
548 * ima_protect_xattr - protect 'security.ima'
550 * Ensure that not just anyone can modify or remove 'security.ima'.
552 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
553 const void *xattr_value, size_t xattr_value_len)
555 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
556 if (!capable(CAP_SYS_ADMIN))
563 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
565 struct integrity_iint_cache *iint;
567 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
570 iint = integrity_iint_find(inode);
573 iint->measured_pcrs = 0;
574 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
576 set_bit(IMA_DIGSIG, &iint->atomic_flags);
578 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
582 * validate_hash_algo() - Block setxattr with unsupported hash algorithms
583 * @dentry: object of the setxattr()
584 * @xattr_value: userland supplied xattr value
585 * @xattr_value_len: length of xattr_value
587 * The xattr value is mapped to its hash algorithm, and this algorithm
588 * must be built in the kernel for the setxattr to be allowed.
590 * Emit an audit message when the algorithm is invalid.
592 * Return: 0 on success, else an error.
594 static int validate_hash_algo(struct dentry *dentry,
595 const struct evm_ima_xattr_data *xattr_value,
596 size_t xattr_value_len)
598 char *path = NULL, *pathbuf = NULL;
599 enum hash_algo xattr_hash_algo;
600 const char *errmsg = "unavailable-hash-algorithm";
601 unsigned int allowed_hashes;
603 xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len);
605 allowed_hashes = atomic_read(&ima_setxattr_allowed_hash_algorithms);
607 if (allowed_hashes) {
608 /* success if the algorithm is allowed in the ima policy */
609 if (allowed_hashes & (1U << xattr_hash_algo))
613 * We use a different audit message when the hash algorithm
614 * is denied by a policy rule, instead of not being built
615 * in the kernel image
617 errmsg = "denied-hash-algorithm";
619 if (likely(xattr_hash_algo == ima_hash_algo))
622 /* allow any xattr using an algorithm built in the kernel */
623 if (crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0))
627 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
631 path = dentry_path(dentry, pathbuf, PATH_MAX);
633 integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path,
634 "set_data", errmsg, -EACCES, 0);
641 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
642 const void *xattr_value, size_t xattr_value_len)
644 const struct evm_ima_xattr_data *xvalue = xattr_value;
649 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
652 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
655 err = validate_hash_algo(dentry, xvalue, xattr_value_len);
659 digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG);
660 } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) {
661 digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG);
663 if (result == 1 || evm_revalidate_status(xattr_name)) {
664 ima_reset_appraise_flags(d_backing_inode(dentry), digsig);
671 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
675 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
676 if (result == 1 || evm_revalidate_status(xattr_name)) {
677 ima_reset_appraise_flags(d_backing_inode(dentry), 0);