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 inode *inode, int mask, enum ima_hooks func)
78 security_task_getsecid(current, &secid);
79 return ima_match_policy(inode, current_cred(), secid, func, mask,
80 IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL);
83 static int ima_fix_xattr(struct dentry *dentry,
84 struct integrity_iint_cache *iint)
87 u8 algo = iint->ima_hash->algo;
89 if (algo <= HASH_ALGO_SHA1) {
91 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
94 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
95 iint->ima_hash->xattr.ng.algo = algo;
97 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
98 &iint->ima_hash->xattr.data[offset],
99 (sizeof(iint->ima_hash->xattr) - offset) +
100 iint->ima_hash->length, 0);
104 /* Return specific func appraised cached result */
105 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
110 return iint->ima_mmap_status;
112 return iint->ima_bprm_status;
114 return iint->ima_creds_status;
117 return iint->ima_file_status;
118 case MODULE_CHECK ... MAX_CHECK - 1:
120 return iint->ima_read_status;
124 static void ima_set_cache_status(struct integrity_iint_cache *iint,
126 enum integrity_status status)
130 iint->ima_mmap_status = status;
133 iint->ima_bprm_status = status;
136 iint->ima_creds_status = status;
140 iint->ima_file_status = status;
142 case MODULE_CHECK ... MAX_CHECK - 1:
144 iint->ima_read_status = status;
149 static void ima_cache_flags(struct integrity_iint_cache *iint,
154 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
157 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
160 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
164 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
166 case MODULE_CHECK ... MAX_CHECK - 1:
168 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
173 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
176 struct signature_v2_hdr *sig;
179 if (!xattr_value || xattr_len < 2)
180 /* return default hash algo */
181 return ima_hash_algo;
183 switch (xattr_value->type) {
184 case EVM_IMA_XATTR_DIGSIG:
185 sig = (typeof(sig))xattr_value;
186 if (sig->version != 2 || xattr_len <= sizeof(*sig))
187 return ima_hash_algo;
188 return sig->hash_algo;
190 case IMA_XATTR_DIGEST_NG:
191 /* first byte contains algorithm id */
192 ret = xattr_value->data[0];
193 if (ret < HASH_ALGO__LAST)
196 case IMA_XATTR_DIGEST:
197 /* this is for backward compatibility */
198 if (xattr_len == 21) {
199 unsigned int zero = 0;
200 if (!memcmp(&xattr_value->data[16], &zero, 4))
201 return HASH_ALGO_MD5;
203 return HASH_ALGO_SHA1;
204 } else if (xattr_len == 17)
205 return HASH_ALGO_MD5;
209 /* return default hash algo */
210 return ima_hash_algo;
213 int ima_read_xattr(struct dentry *dentry,
214 struct evm_ima_xattr_data **xattr_value)
218 ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
220 if (ret == -EOPNOTSUPP)
226 * xattr_verify - verify xattr digest or signature
228 * Verify whether the hash or signature matches the file contents.
230 * Return 0 on success, error code otherwise.
232 static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
233 struct evm_ima_xattr_data *xattr_value, int xattr_len,
234 enum integrity_status *status, const char **cause)
236 int rc = -EINVAL, hash_start = 0;
238 switch (xattr_value->type) {
239 case IMA_XATTR_DIGEST_NG:
240 /* first byte contains algorithm id */
243 case IMA_XATTR_DIGEST:
244 if (iint->flags & IMA_DIGSIG_REQUIRED) {
245 *cause = "IMA-signature-required";
246 *status = INTEGRITY_FAIL;
249 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
250 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
251 iint->ima_hash->length)
253 * xattr length may be longer. md5 hash in previous
254 * version occupied 20 bytes in xattr, instead of 16
256 rc = memcmp(&xattr_value->data[hash_start],
257 iint->ima_hash->digest,
258 iint->ima_hash->length);
262 *cause = "invalid-hash";
263 *status = INTEGRITY_FAIL;
266 *status = INTEGRITY_PASS;
268 case EVM_IMA_XATTR_DIGSIG:
269 set_bit(IMA_DIGSIG, &iint->atomic_flags);
270 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
271 (const char *)xattr_value,
273 iint->ima_hash->digest,
274 iint->ima_hash->length);
275 if (rc == -EOPNOTSUPP) {
276 *status = INTEGRITY_UNKNOWN;
279 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
280 func == KEXEC_KERNEL_CHECK)
281 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
282 (const char *)xattr_value,
284 iint->ima_hash->digest,
285 iint->ima_hash->length);
287 *cause = "invalid-signature";
288 *status = INTEGRITY_FAIL;
290 *status = INTEGRITY_PASS;
294 *status = INTEGRITY_UNKNOWN;
295 *cause = "unknown-ima-data";
303 * modsig_verify - verify modsig signature
305 * Verify whether the signature matches the file contents.
307 * Return 0 on success, error code otherwise.
309 static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
310 enum integrity_status *status, const char **cause)
314 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
315 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
316 func == KEXEC_KERNEL_CHECK)
317 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
320 *cause = "invalid-signature";
321 *status = INTEGRITY_FAIL;
323 *status = INTEGRITY_PASS;
330 * ima_check_blacklist - determine if the binary is blacklisted.
332 * Add the hash of the blacklisted binary to the measurement list, based
335 * Returns -EPERM if the hash is blacklisted.
337 int ima_check_blacklist(struct integrity_iint_cache *iint,
338 const struct modsig *modsig, int pcr)
340 enum hash_algo hash_algo;
341 const u8 *digest = NULL;
345 if (!(iint->flags & IMA_CHECK_BLACKLIST))
348 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
349 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
351 rc = is_binary_blacklisted(digest, digestsize);
352 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
353 process_buffer_measurement(NULL, digest, digestsize,
354 "blacklisted-hash", NONE,
362 * ima_appraise_measurement - appraise file measurement
364 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
365 * Assuming success, compare the xattr hash with the collected measurement.
367 * Return 0 on success, error code otherwise
369 int ima_appraise_measurement(enum ima_hooks func,
370 struct integrity_iint_cache *iint,
371 struct file *file, const unsigned char *filename,
372 struct evm_ima_xattr_data *xattr_value,
373 int xattr_len, const struct modsig *modsig)
375 static const char op[] = "appraise_data";
376 const char *cause = "unknown";
377 struct dentry *dentry = file_dentry(file);
378 struct inode *inode = d_backing_inode(dentry);
379 enum integrity_status status = INTEGRITY_UNKNOWN;
381 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
383 /* If not appraising a modsig, we need an xattr. */
384 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
385 return INTEGRITY_UNKNOWN;
387 /* If reading the xattr failed and there's no modsig, error out. */
388 if (rc <= 0 && !try_modsig) {
389 if (rc && rc != -ENODATA)
392 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
393 "IMA-signature-required" : "missing-hash";
394 status = INTEGRITY_NOLABEL;
395 if (file->f_mode & FMODE_CREATED)
396 iint->flags |= IMA_NEW_FILE;
397 if ((iint->flags & IMA_NEW_FILE) &&
398 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
399 (inode->i_size == 0)))
400 status = INTEGRITY_PASS;
404 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
407 case INTEGRITY_PASS_IMMUTABLE:
408 case INTEGRITY_UNKNOWN:
410 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
411 /* It's fine not to have xattrs when using a modsig. */
415 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
416 cause = "missing-HMAC";
418 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
419 cause = "invalid-HMAC";
422 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
426 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
430 * If we have a modsig and either no imasig or the imasig's key isn't
431 * known, then try verifying the modsig.
434 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
436 rc = modsig_verify(func, modsig, &status, &cause);
440 * File signatures on some filesystems can not be properly verified.
441 * When such filesystems are mounted by an untrusted mounter or on a
442 * system not willing to accept such a risk, fail the file signature
445 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
446 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
447 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
448 status = INTEGRITY_FAIL;
449 cause = "unverifiable-signature";
450 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
452 } else if (status != INTEGRITY_PASS) {
453 /* Fix mode, but don't replace file signatures. */
454 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
456 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
457 if (!ima_fix_xattr(dentry, iint))
458 status = INTEGRITY_PASS;
461 /* Permit new files with file signatures, but without data. */
462 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
463 xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
464 status = INTEGRITY_PASS;
467 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
470 ima_cache_flags(iint, func);
473 ima_set_cache_status(iint, func, status);
478 * ima_update_xattr - update 'security.ima' hash value
480 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
482 struct dentry *dentry = file_dentry(file);
485 /* do not collect and update hash for digital signatures */
486 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
489 if ((iint->ima_file_status != INTEGRITY_PASS) &&
490 !(iint->flags & IMA_HASH))
493 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
497 inode_lock(file_inode(file));
498 ima_fix_xattr(dentry, iint);
499 inode_unlock(file_inode(file));
503 * ima_inode_post_setattr - reflect file metadata changes
504 * @dentry: pointer to the affected dentry
506 * Changes to a dentry's metadata might result in needing to appraise.
508 * This function is called from notify_change(), which expects the caller
509 * to lock the inode's i_mutex.
511 void ima_inode_post_setattr(struct dentry *dentry)
513 struct inode *inode = d_backing_inode(dentry);
514 struct integrity_iint_cache *iint;
517 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
518 || !(inode->i_opflags & IOP_XATTR))
521 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
523 __vfs_removexattr(dentry, XATTR_NAME_IMA);
524 iint = integrity_iint_find(inode);
526 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
528 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
533 * ima_protect_xattr - protect 'security.ima'
535 * Ensure that not just anyone can modify or remove 'security.ima'.
537 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
538 const void *xattr_value, size_t xattr_value_len)
540 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
541 if (!capable(CAP_SYS_ADMIN))
548 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
550 struct integrity_iint_cache *iint;
552 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
555 iint = integrity_iint_find(inode);
558 iint->measured_pcrs = 0;
559 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
561 set_bit(IMA_DIGSIG, &iint->atomic_flags);
563 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
566 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
567 const void *xattr_value, size_t xattr_value_len)
569 const struct evm_ima_xattr_data *xvalue = xattr_value;
572 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
575 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
577 ima_reset_appraise_flags(d_backing_inode(dentry),
578 xvalue->type == EVM_IMA_XATTR_DIGSIG);
584 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
588 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
590 ima_reset_appraise_flags(d_backing_inode(dentry), 0);