1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 IBM Corporation
6 * Mimi Zohar <zohar@us.ibm.com>
8 #include <linux/init.h>
9 #include <linux/file.h>
11 #include <linux/xattr.h>
12 #include <linux/magic.h>
13 #include <linux/ima.h>
14 #include <linux/evm.h>
15 #include <keys/system_keyring.h>
19 static int __init default_appraise_setup(char *str)
21 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
22 bool sb_state = arch_ima_get_secureboot();
23 int appraisal_state = ima_appraise;
25 if (strncmp(str, "off", 3) == 0)
27 else if (strncmp(str, "log", 3) == 0)
28 appraisal_state = IMA_APPRAISE_LOG;
29 else if (strncmp(str, "fix", 3) == 0)
30 appraisal_state = IMA_APPRAISE_FIX;
31 else if (strncmp(str, "enforce", 7) == 0)
32 appraisal_state = IMA_APPRAISE_ENFORCE;
34 pr_err("invalid \"%s\" appraise option", str);
36 /* If appraisal state was changed, but secure boot is enabled,
39 if (!(appraisal_state & IMA_APPRAISE_ENFORCE))
40 pr_info("Secure boot enabled: ignoring ima_appraise=%s option",
43 ima_appraise = appraisal_state;
49 __setup("ima_appraise=", default_appraise_setup);
52 * is_ima_appraise_enabled - return appraise status
54 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
56 bool is_ima_appraise_enabled(void)
58 return ima_appraise & IMA_APPRAISE_ENFORCE;
62 * ima_must_appraise - set appraise flag
64 * Return 1 to appraise or hash
66 int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
73 security_task_getsecid(current, &secid);
74 return ima_match_policy(inode, current_cred(), secid, func, mask,
75 IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL);
78 static int ima_fix_xattr(struct dentry *dentry,
79 struct integrity_iint_cache *iint)
82 u8 algo = iint->ima_hash->algo;
84 if (algo <= HASH_ALGO_SHA1) {
86 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
89 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
90 iint->ima_hash->xattr.ng.algo = algo;
92 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
93 &iint->ima_hash->xattr.data[offset],
94 (sizeof(iint->ima_hash->xattr) - offset) +
95 iint->ima_hash->length, 0);
99 /* Return specific func appraised cached result */
100 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
105 return iint->ima_mmap_status;
107 return iint->ima_bprm_status;
109 return iint->ima_creds_status;
112 return iint->ima_file_status;
113 case MODULE_CHECK ... MAX_CHECK - 1:
115 return iint->ima_read_status;
119 static void ima_set_cache_status(struct integrity_iint_cache *iint,
121 enum integrity_status status)
125 iint->ima_mmap_status = status;
128 iint->ima_bprm_status = status;
131 iint->ima_creds_status = status;
135 iint->ima_file_status = status;
137 case MODULE_CHECK ... MAX_CHECK - 1:
139 iint->ima_read_status = status;
144 static void ima_cache_flags(struct integrity_iint_cache *iint,
149 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
152 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
155 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
159 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
161 case MODULE_CHECK ... MAX_CHECK - 1:
163 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
168 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
171 struct signature_v2_hdr *sig;
174 if (!xattr_value || xattr_len < 2)
175 /* return default hash algo */
176 return ima_hash_algo;
178 switch (xattr_value->type) {
179 case EVM_IMA_XATTR_DIGSIG:
180 sig = (typeof(sig))xattr_value;
181 if (sig->version != 2 || xattr_len <= sizeof(*sig))
182 return ima_hash_algo;
183 return sig->hash_algo;
185 case IMA_XATTR_DIGEST_NG:
186 /* first byte contains algorithm id */
187 ret = xattr_value->data[0];
188 if (ret < HASH_ALGO__LAST)
191 case IMA_XATTR_DIGEST:
192 /* this is for backward compatibility */
193 if (xattr_len == 21) {
194 unsigned int zero = 0;
195 if (!memcmp(&xattr_value->data[16], &zero, 4))
196 return HASH_ALGO_MD5;
198 return HASH_ALGO_SHA1;
199 } else if (xattr_len == 17)
200 return HASH_ALGO_MD5;
204 /* return default hash algo */
205 return ima_hash_algo;
208 int ima_read_xattr(struct dentry *dentry,
209 struct evm_ima_xattr_data **xattr_value)
213 ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
215 if (ret == -EOPNOTSUPP)
221 * xattr_verify - verify xattr digest or signature
223 * Verify whether the hash or signature matches the file contents.
225 * Return 0 on success, error code otherwise.
227 static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
228 struct evm_ima_xattr_data *xattr_value, int xattr_len,
229 enum integrity_status *status, const char **cause)
231 int rc = -EINVAL, hash_start = 0;
233 switch (xattr_value->type) {
234 case IMA_XATTR_DIGEST_NG:
235 /* first byte contains algorithm id */
238 case IMA_XATTR_DIGEST:
239 if (iint->flags & IMA_DIGSIG_REQUIRED) {
240 *cause = "IMA-signature-required";
241 *status = INTEGRITY_FAIL;
244 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
245 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
246 iint->ima_hash->length)
248 * xattr length may be longer. md5 hash in previous
249 * version occupied 20 bytes in xattr, instead of 16
251 rc = memcmp(&xattr_value->data[hash_start],
252 iint->ima_hash->digest,
253 iint->ima_hash->length);
257 *cause = "invalid-hash";
258 *status = INTEGRITY_FAIL;
261 *status = INTEGRITY_PASS;
263 case EVM_IMA_XATTR_DIGSIG:
264 set_bit(IMA_DIGSIG, &iint->atomic_flags);
265 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
266 (const char *)xattr_value,
268 iint->ima_hash->digest,
269 iint->ima_hash->length);
270 if (rc == -EOPNOTSUPP) {
271 *status = INTEGRITY_UNKNOWN;
274 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
275 func == KEXEC_KERNEL_CHECK)
276 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
277 (const char *)xattr_value,
279 iint->ima_hash->digest,
280 iint->ima_hash->length);
282 *cause = "invalid-signature";
283 *status = INTEGRITY_FAIL;
285 *status = INTEGRITY_PASS;
289 *status = INTEGRITY_UNKNOWN;
290 *cause = "unknown-ima-data";
298 * modsig_verify - verify modsig signature
300 * Verify whether the signature matches the file contents.
302 * Return 0 on success, error code otherwise.
304 static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
305 enum integrity_status *status, const char **cause)
309 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
310 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
311 func == KEXEC_KERNEL_CHECK)
312 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
315 *cause = "invalid-signature";
316 *status = INTEGRITY_FAIL;
318 *status = INTEGRITY_PASS;
325 * ima_check_blacklist - determine if the binary is blacklisted.
327 * Add the hash of the blacklisted binary to the measurement list, based
330 * Returns -EPERM if the hash is blacklisted.
332 int ima_check_blacklist(struct integrity_iint_cache *iint,
333 const struct modsig *modsig, int pcr)
335 enum hash_algo hash_algo;
336 const u8 *digest = NULL;
340 if (!(iint->flags & IMA_CHECK_BLACKLIST))
343 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
344 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
346 rc = is_binary_blacklisted(digest, digestsize);
347 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
348 process_buffer_measurement(NULL, digest, digestsize,
349 "blacklisted-hash", NONE,
357 * ima_appraise_measurement - appraise file measurement
359 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
360 * Assuming success, compare the xattr hash with the collected measurement.
362 * Return 0 on success, error code otherwise
364 int ima_appraise_measurement(enum ima_hooks func,
365 struct integrity_iint_cache *iint,
366 struct file *file, const unsigned char *filename,
367 struct evm_ima_xattr_data *xattr_value,
368 int xattr_len, const struct modsig *modsig)
370 static const char op[] = "appraise_data";
371 const char *cause = "unknown";
372 struct dentry *dentry = file_dentry(file);
373 struct inode *inode = d_backing_inode(dentry);
374 enum integrity_status status = INTEGRITY_UNKNOWN;
376 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
378 /* If not appraising a modsig, we need an xattr. */
379 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
380 return INTEGRITY_UNKNOWN;
382 /* If reading the xattr failed and there's no modsig, error out. */
383 if (rc <= 0 && !try_modsig) {
384 if (rc && rc != -ENODATA)
387 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
388 "IMA-signature-required" : "missing-hash";
389 status = INTEGRITY_NOLABEL;
390 if (file->f_mode & FMODE_CREATED)
391 iint->flags |= IMA_NEW_FILE;
392 if ((iint->flags & IMA_NEW_FILE) &&
393 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
394 (inode->i_size == 0)))
395 status = INTEGRITY_PASS;
399 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value,
400 rc < 0 ? 0 : rc, iint);
403 case INTEGRITY_PASS_IMMUTABLE:
404 case INTEGRITY_UNKNOWN:
406 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
407 /* It's fine not to have xattrs when using a modsig. */
411 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
412 cause = "missing-HMAC";
414 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
415 cause = "invalid-HMAC";
418 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
422 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
426 * If we have a modsig and either no imasig or the imasig's key isn't
427 * known, then try verifying the modsig.
430 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
432 rc = modsig_verify(func, modsig, &status, &cause);
436 * File signatures on some filesystems can not be properly verified.
437 * When such filesystems are mounted by an untrusted mounter or on a
438 * system not willing to accept such a risk, fail the file signature
441 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
442 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
443 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
444 status = INTEGRITY_FAIL;
445 cause = "unverifiable-signature";
446 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
448 } else if (status != INTEGRITY_PASS) {
449 /* Fix mode, but don't replace file signatures. */
450 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
452 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
453 if (!ima_fix_xattr(dentry, iint))
454 status = INTEGRITY_PASS;
457 /* Permit new files with file signatures, but without data. */
458 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
459 xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
460 status = INTEGRITY_PASS;
463 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
466 ima_cache_flags(iint, func);
469 ima_set_cache_status(iint, func, status);
474 * ima_update_xattr - update 'security.ima' hash value
476 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
478 struct dentry *dentry = file_dentry(file);
481 /* do not collect and update hash for digital signatures */
482 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
485 if ((iint->ima_file_status != INTEGRITY_PASS) &&
486 !(iint->flags & IMA_HASH))
489 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
493 inode_lock(file_inode(file));
494 ima_fix_xattr(dentry, iint);
495 inode_unlock(file_inode(file));
499 * ima_inode_post_setattr - reflect file metadata changes
500 * @dentry: pointer to the affected dentry
502 * Changes to a dentry's metadata might result in needing to appraise.
504 * This function is called from notify_change(), which expects the caller
505 * to lock the inode's i_mutex.
507 void ima_inode_post_setattr(struct dentry *dentry)
509 struct inode *inode = d_backing_inode(dentry);
510 struct integrity_iint_cache *iint;
513 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
514 || !(inode->i_opflags & IOP_XATTR))
517 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
519 __vfs_removexattr(dentry, XATTR_NAME_IMA);
520 iint = integrity_iint_find(inode);
522 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
524 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
529 * ima_protect_xattr - protect 'security.ima'
531 * Ensure that not just anyone can modify or remove 'security.ima'.
533 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
534 const void *xattr_value, size_t xattr_value_len)
536 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
537 if (!capable(CAP_SYS_ADMIN))
544 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
546 struct integrity_iint_cache *iint;
548 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
551 iint = integrity_iint_find(inode);
554 iint->measured_pcrs = 0;
555 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
557 set_bit(IMA_DIGSIG, &iint->atomic_flags);
559 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
562 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
563 const void *xattr_value, size_t xattr_value_len)
565 const struct evm_ima_xattr_data *xvalue = xattr_value;
568 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
571 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
573 ima_reset_appraise_flags(d_backing_inode(dentry),
574 xvalue->type == EVM_IMA_XATTR_DIGSIG);
580 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
584 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
586 ima_reset_appraise_flags(d_backing_inode(dentry), 0);