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, func,
81 mask, IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL);
84 static int ima_fix_xattr(struct dentry *dentry,
85 struct integrity_iint_cache *iint)
88 u8 algo = iint->ima_hash->algo;
90 if (algo <= HASH_ALGO_SHA1) {
92 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
95 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
96 iint->ima_hash->xattr.ng.algo = algo;
98 rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA,
99 &iint->ima_hash->xattr.data[offset],
100 (sizeof(iint->ima_hash->xattr) - offset) +
101 iint->ima_hash->length, 0);
105 /* Return specific func appraised cached result */
106 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
111 return iint->ima_mmap_status;
113 return iint->ima_bprm_status;
115 return iint->ima_creds_status;
118 return iint->ima_file_status;
119 case MODULE_CHECK ... MAX_CHECK - 1:
121 return iint->ima_read_status;
125 static void ima_set_cache_status(struct integrity_iint_cache *iint,
127 enum integrity_status status)
131 iint->ima_mmap_status = status;
134 iint->ima_bprm_status = status;
137 iint->ima_creds_status = status;
141 iint->ima_file_status = status;
143 case MODULE_CHECK ... MAX_CHECK - 1:
145 iint->ima_read_status = status;
150 static void ima_cache_flags(struct integrity_iint_cache *iint,
155 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
158 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
161 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
165 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
167 case MODULE_CHECK ... MAX_CHECK - 1:
169 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
174 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
177 struct signature_v2_hdr *sig;
180 if (!xattr_value || xattr_len < 2)
181 /* return default hash algo */
182 return ima_hash_algo;
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))
188 return ima_hash_algo;
189 return sig->hash_algo;
191 case IMA_XATTR_DIGEST_NG:
192 /* first byte contains algorithm id */
193 ret = xattr_value->data[0];
194 if (ret < HASH_ALGO__LAST)
197 case IMA_XATTR_DIGEST:
198 /* this is for backward compatibility */
199 if (xattr_len == 21) {
200 unsigned int zero = 0;
201 if (!memcmp(&xattr_value->data[16], &zero, 4))
202 return HASH_ALGO_MD5;
204 return HASH_ALGO_SHA1;
205 } else if (xattr_len == 17)
206 return HASH_ALGO_MD5;
210 /* return default hash algo */
211 return ima_hash_algo;
214 int ima_read_xattr(struct dentry *dentry,
215 struct evm_ima_xattr_data **xattr_value)
219 ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA,
220 (char **)xattr_value, 0, GFP_NOFS);
221 if (ret == -EOPNOTSUPP)
227 * xattr_verify - verify xattr digest or signature
229 * Verify whether the hash or signature matches the file contents.
231 * Return 0 on success, error code otherwise.
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)
237 int rc = -EINVAL, hash_start = 0;
239 switch (xattr_value->type) {
240 case IMA_XATTR_DIGEST_NG:
241 /* first byte contains algorithm id */
244 case IMA_XATTR_DIGEST:
245 if (iint->flags & IMA_DIGSIG_REQUIRED) {
246 *cause = "IMA-signature-required";
247 *status = INTEGRITY_FAIL;
250 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
251 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
252 iint->ima_hash->length)
254 * xattr length may be longer. md5 hash in previous
255 * version occupied 20 bytes in xattr, instead of 16
257 rc = memcmp(&xattr_value->data[hash_start],
258 iint->ima_hash->digest,
259 iint->ima_hash->length);
263 *cause = "invalid-hash";
264 *status = INTEGRITY_FAIL;
267 *status = INTEGRITY_PASS;
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,
274 iint->ima_hash->digest,
275 iint->ima_hash->length);
276 if (rc == -EOPNOTSUPP) {
277 *status = INTEGRITY_UNKNOWN;
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,
285 iint->ima_hash->digest,
286 iint->ima_hash->length);
288 *cause = "invalid-signature";
289 *status = INTEGRITY_FAIL;
291 *status = INTEGRITY_PASS;
295 *status = INTEGRITY_UNKNOWN;
296 *cause = "unknown-ima-data";
304 * modsig_verify - verify modsig signature
306 * Verify whether the signature matches the file contents.
308 * Return 0 on success, error code otherwise.
310 static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
311 enum integrity_status *status, const char **cause)
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,
321 *cause = "invalid-signature";
322 *status = INTEGRITY_FAIL;
324 *status = INTEGRITY_PASS;
331 * ima_check_blacklist - determine if the binary is blacklisted.
333 * Add the hash of the blacklisted binary to the measurement list, based
336 * Returns -EPERM if the hash is blacklisted.
338 int ima_check_blacklist(struct integrity_iint_cache *iint,
339 const struct modsig *modsig, int pcr)
341 enum hash_algo hash_algo;
342 const u8 *digest = NULL;
346 if (!(iint->flags & IMA_CHECK_BLACKLIST))
349 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
350 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
352 rc = is_binary_blacklisted(digest, digestsize);
353 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
354 process_buffer_measurement(&init_user_ns, NULL, digest, digestsize,
355 "blacklisted-hash", NONE,
363 * ima_appraise_measurement - appraise file measurement
365 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
366 * Assuming success, compare the xattr hash with the collected measurement.
368 * Return 0 on success, error code otherwise
370 int ima_appraise_measurement(enum ima_hooks func,
371 struct integrity_iint_cache *iint,
372 struct file *file, const unsigned char *filename,
373 struct evm_ima_xattr_data *xattr_value,
374 int xattr_len, const struct modsig *modsig)
376 static const char op[] = "appraise_data";
377 const char *cause = "unknown";
378 struct dentry *dentry = file_dentry(file);
379 struct inode *inode = d_backing_inode(dentry);
380 enum integrity_status status = INTEGRITY_UNKNOWN;
382 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
384 /* If not appraising a modsig, we need an xattr. */
385 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
386 return INTEGRITY_UNKNOWN;
388 /* If reading the xattr failed and there's no modsig, error out. */
389 if (rc <= 0 && !try_modsig) {
390 if (rc && rc != -ENODATA)
393 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
394 "IMA-signature-required" : "missing-hash";
395 status = INTEGRITY_NOLABEL;
396 if (file->f_mode & FMODE_CREATED)
397 iint->flags |= IMA_NEW_FILE;
398 if ((iint->flags & IMA_NEW_FILE) &&
399 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
400 (inode->i_size == 0)))
401 status = INTEGRITY_PASS;
405 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
408 case INTEGRITY_PASS_IMMUTABLE:
409 case INTEGRITY_UNKNOWN:
411 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
412 /* It's fine not to have xattrs when using a modsig. */
416 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
417 cause = "missing-HMAC";
419 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
420 cause = "invalid-HMAC";
423 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
427 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
431 * If we have a modsig and either no imasig or the imasig's key isn't
432 * known, then try verifying the modsig.
435 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
437 rc = modsig_verify(func, modsig, &status, &cause);
441 * File signatures on some filesystems can not be properly verified.
442 * When such filesystems are mounted by an untrusted mounter or on a
443 * system not willing to accept such a risk, fail the file signature
446 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
447 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
448 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
449 status = INTEGRITY_FAIL;
450 cause = "unverifiable-signature";
451 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
453 } else if (status != INTEGRITY_PASS) {
454 /* Fix mode, but don't replace file signatures. */
455 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
457 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
458 if (!ima_fix_xattr(dentry, iint))
459 status = INTEGRITY_PASS;
462 /* Permit new files with file signatures, but without data. */
463 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
464 xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
465 status = INTEGRITY_PASS;
468 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
471 ima_cache_flags(iint, func);
474 ima_set_cache_status(iint, func, status);
479 * ima_update_xattr - update 'security.ima' hash value
481 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
483 struct dentry *dentry = file_dentry(file);
486 /* do not collect and update hash for digital signatures */
487 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
490 if ((iint->ima_file_status != INTEGRITY_PASS) &&
491 !(iint->flags & IMA_HASH))
494 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
498 inode_lock(file_inode(file));
499 ima_fix_xattr(dentry, iint);
500 inode_unlock(file_inode(file));
504 * ima_inode_post_setattr - reflect file metadata changes
505 * @mnt_userns: user namespace of the mount the inode was found from
506 * @dentry: pointer to the affected dentry
508 * Changes to a dentry's metadata might result in needing to appraise.
510 * This function is called from notify_change(), which expects the caller
511 * to lock the inode's i_mutex.
513 void ima_inode_post_setattr(struct user_namespace *mnt_userns,
514 struct dentry *dentry)
516 struct inode *inode = d_backing_inode(dentry);
517 struct integrity_iint_cache *iint;
520 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
521 || !(inode->i_opflags & IOP_XATTR))
524 action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR);
525 iint = integrity_iint_find(inode);
527 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
529 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
534 * ima_protect_xattr - protect 'security.ima'
536 * Ensure that not just anyone can modify or remove 'security.ima'.
538 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
539 const void *xattr_value, size_t xattr_value_len)
541 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
542 if (!capable(CAP_SYS_ADMIN))
549 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
551 struct integrity_iint_cache *iint;
553 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
556 iint = integrity_iint_find(inode);
559 iint->measured_pcrs = 0;
560 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
562 set_bit(IMA_DIGSIG, &iint->atomic_flags);
564 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
567 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
568 const void *xattr_value, size_t xattr_value_len)
570 const struct evm_ima_xattr_data *xvalue = xattr_value;
573 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
576 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
578 ima_reset_appraise_flags(d_backing_inode(dentry),
579 xvalue->type == EVM_IMA_XATTR_DIGSIG);
585 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
589 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
591 ima_reset_appraise_flags(d_backing_inode(dentry), 0);