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 if (arch_ima_get_secureboot()) {
23 pr_info("Secure boot enabled: ignoring ima_appraise=%s boot parameter option",
28 if (strncmp(str, "off", 3) == 0)
30 else if (strncmp(str, "log", 3) == 0)
31 ima_appraise = IMA_APPRAISE_LOG;
32 else if (strncmp(str, "fix", 3) == 0)
33 ima_appraise = IMA_APPRAISE_FIX;
38 __setup("ima_appraise=", default_appraise_setup);
41 * is_ima_appraise_enabled - return appraise status
43 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
45 bool is_ima_appraise_enabled(void)
47 return ima_appraise & IMA_APPRAISE_ENFORCE;
51 * ima_must_appraise - set appraise flag
53 * Return 1 to appraise or hash
55 int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
62 security_task_getsecid(current, &secid);
63 return ima_match_policy(inode, current_cred(), secid, func, mask,
64 IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL);
67 static int ima_fix_xattr(struct dentry *dentry,
68 struct integrity_iint_cache *iint)
71 u8 algo = iint->ima_hash->algo;
73 if (algo <= HASH_ALGO_SHA1) {
75 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
78 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
79 iint->ima_hash->xattr.ng.algo = algo;
81 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
82 &iint->ima_hash->xattr.data[offset],
83 (sizeof(iint->ima_hash->xattr) - offset) +
84 iint->ima_hash->length, 0);
88 /* Return specific func appraised cached result */
89 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
94 return iint->ima_mmap_status;
96 return iint->ima_bprm_status;
98 return iint->ima_creds_status;
101 return iint->ima_file_status;
102 case MODULE_CHECK ... MAX_CHECK - 1:
104 return iint->ima_read_status;
108 static void ima_set_cache_status(struct integrity_iint_cache *iint,
110 enum integrity_status status)
114 iint->ima_mmap_status = status;
117 iint->ima_bprm_status = status;
120 iint->ima_creds_status = status;
124 iint->ima_file_status = status;
126 case MODULE_CHECK ... MAX_CHECK - 1:
128 iint->ima_read_status = status;
133 static void ima_cache_flags(struct integrity_iint_cache *iint,
138 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
141 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
144 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
148 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
150 case MODULE_CHECK ... MAX_CHECK - 1:
152 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
157 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
160 struct signature_v2_hdr *sig;
163 if (!xattr_value || xattr_len < 2)
164 /* return default hash algo */
165 return ima_hash_algo;
167 switch (xattr_value->type) {
168 case EVM_IMA_XATTR_DIGSIG:
169 sig = (typeof(sig))xattr_value;
170 if (sig->version != 2 || xattr_len <= sizeof(*sig))
171 return ima_hash_algo;
172 return sig->hash_algo;
174 case IMA_XATTR_DIGEST_NG:
175 /* first byte contains algorithm id */
176 ret = xattr_value->data[0];
177 if (ret < HASH_ALGO__LAST)
180 case IMA_XATTR_DIGEST:
181 /* this is for backward compatibility */
182 if (xattr_len == 21) {
183 unsigned int zero = 0;
184 if (!memcmp(&xattr_value->data[16], &zero, 4))
185 return HASH_ALGO_MD5;
187 return HASH_ALGO_SHA1;
188 } else if (xattr_len == 17)
189 return HASH_ALGO_MD5;
193 /* return default hash algo */
194 return ima_hash_algo;
197 int ima_read_xattr(struct dentry *dentry,
198 struct evm_ima_xattr_data **xattr_value)
202 ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
204 if (ret == -EOPNOTSUPP)
210 * xattr_verify - verify xattr digest or signature
212 * Verify whether the hash or signature matches the file contents.
214 * Return 0 on success, error code otherwise.
216 static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
217 struct evm_ima_xattr_data *xattr_value, int xattr_len,
218 enum integrity_status *status, const char **cause)
220 int rc = -EINVAL, hash_start = 0;
222 switch (xattr_value->type) {
223 case IMA_XATTR_DIGEST_NG:
224 /* first byte contains algorithm id */
227 case IMA_XATTR_DIGEST:
228 if (iint->flags & IMA_DIGSIG_REQUIRED) {
229 *cause = "IMA-signature-required";
230 *status = INTEGRITY_FAIL;
233 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
234 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
235 iint->ima_hash->length)
237 * xattr length may be longer. md5 hash in previous
238 * version occupied 20 bytes in xattr, instead of 16
240 rc = memcmp(&xattr_value->data[hash_start],
241 iint->ima_hash->digest,
242 iint->ima_hash->length);
246 *cause = "invalid-hash";
247 *status = INTEGRITY_FAIL;
250 *status = INTEGRITY_PASS;
252 case EVM_IMA_XATTR_DIGSIG:
253 set_bit(IMA_DIGSIG, &iint->atomic_flags);
254 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
255 (const char *)xattr_value,
257 iint->ima_hash->digest,
258 iint->ima_hash->length);
259 if (rc == -EOPNOTSUPP) {
260 *status = INTEGRITY_UNKNOWN;
263 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
264 func == KEXEC_KERNEL_CHECK)
265 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
266 (const char *)xattr_value,
268 iint->ima_hash->digest,
269 iint->ima_hash->length);
271 *cause = "invalid-signature";
272 *status = INTEGRITY_FAIL;
274 *status = INTEGRITY_PASS;
278 *status = INTEGRITY_UNKNOWN;
279 *cause = "unknown-ima-data";
287 * modsig_verify - verify modsig signature
289 * Verify whether the signature matches the file contents.
291 * Return 0 on success, error code otherwise.
293 static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
294 enum integrity_status *status, const char **cause)
298 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
299 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
300 func == KEXEC_KERNEL_CHECK)
301 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
304 *cause = "invalid-signature";
305 *status = INTEGRITY_FAIL;
307 *status = INTEGRITY_PASS;
314 * ima_check_blacklist - determine if the binary is blacklisted.
316 * Add the hash of the blacklisted binary to the measurement list, based
319 * Returns -EPERM if the hash is blacklisted.
321 int ima_check_blacklist(struct integrity_iint_cache *iint,
322 const struct modsig *modsig, int pcr)
324 enum hash_algo hash_algo;
325 const u8 *digest = NULL;
329 if (!(iint->flags & IMA_CHECK_BLACKLIST))
332 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
333 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
335 rc = is_binary_blacklisted(digest, digestsize);
336 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
337 process_buffer_measurement(NULL, digest, digestsize,
338 "blacklisted-hash", NONE,
346 * ima_appraise_measurement - appraise file measurement
348 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
349 * Assuming success, compare the xattr hash with the collected measurement.
351 * Return 0 on success, error code otherwise
353 int ima_appraise_measurement(enum ima_hooks func,
354 struct integrity_iint_cache *iint,
355 struct file *file, const unsigned char *filename,
356 struct evm_ima_xattr_data *xattr_value,
357 int xattr_len, const struct modsig *modsig)
359 static const char op[] = "appraise_data";
360 const char *cause = "unknown";
361 struct dentry *dentry = file_dentry(file);
362 struct inode *inode = d_backing_inode(dentry);
363 enum integrity_status status = INTEGRITY_UNKNOWN;
365 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
367 /* If not appraising a modsig, we need an xattr. */
368 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
369 return INTEGRITY_UNKNOWN;
371 /* If reading the xattr failed and there's no modsig, error out. */
372 if (rc <= 0 && !try_modsig) {
373 if (rc && rc != -ENODATA)
376 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
377 "IMA-signature-required" : "missing-hash";
378 status = INTEGRITY_NOLABEL;
379 if (file->f_mode & FMODE_CREATED)
380 iint->flags |= IMA_NEW_FILE;
381 if ((iint->flags & IMA_NEW_FILE) &&
382 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
383 (inode->i_size == 0)))
384 status = INTEGRITY_PASS;
388 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
391 case INTEGRITY_PASS_IMMUTABLE:
392 case INTEGRITY_UNKNOWN:
394 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
395 /* It's fine not to have xattrs when using a modsig. */
399 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
400 cause = "missing-HMAC";
402 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
403 cause = "invalid-HMAC";
406 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
410 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
414 * If we have a modsig and either no imasig or the imasig's key isn't
415 * known, then try verifying the modsig.
418 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
420 rc = modsig_verify(func, modsig, &status, &cause);
424 * File signatures on some filesystems can not be properly verified.
425 * When such filesystems are mounted by an untrusted mounter or on a
426 * system not willing to accept such a risk, fail the file signature
429 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
430 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
431 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
432 status = INTEGRITY_FAIL;
433 cause = "unverifiable-signature";
434 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
436 } else if (status != INTEGRITY_PASS) {
437 /* Fix mode, but don't replace file signatures. */
438 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
440 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
441 if (!ima_fix_xattr(dentry, iint))
442 status = INTEGRITY_PASS;
445 /* Permit new files with file signatures, but without data. */
446 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
447 xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
448 status = INTEGRITY_PASS;
451 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
454 ima_cache_flags(iint, func);
457 ima_set_cache_status(iint, func, status);
462 * ima_update_xattr - update 'security.ima' hash value
464 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
466 struct dentry *dentry = file_dentry(file);
469 /* do not collect and update hash for digital signatures */
470 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
473 if ((iint->ima_file_status != INTEGRITY_PASS) &&
474 !(iint->flags & IMA_HASH))
477 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
481 inode_lock(file_inode(file));
482 ima_fix_xattr(dentry, iint);
483 inode_unlock(file_inode(file));
487 * ima_inode_post_setattr - reflect file metadata changes
488 * @dentry: pointer to the affected dentry
490 * Changes to a dentry's metadata might result in needing to appraise.
492 * This function is called from notify_change(), which expects the caller
493 * to lock the inode's i_mutex.
495 void ima_inode_post_setattr(struct dentry *dentry)
497 struct inode *inode = d_backing_inode(dentry);
498 struct integrity_iint_cache *iint;
501 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
502 || !(inode->i_opflags & IOP_XATTR))
505 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
507 __vfs_removexattr(dentry, XATTR_NAME_IMA);
508 iint = integrity_iint_find(inode);
510 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
512 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
517 * ima_protect_xattr - protect 'security.ima'
519 * Ensure that not just anyone can modify or remove 'security.ima'.
521 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
522 const void *xattr_value, size_t xattr_value_len)
524 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
525 if (!capable(CAP_SYS_ADMIN))
532 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
534 struct integrity_iint_cache *iint;
536 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
539 iint = integrity_iint_find(inode);
542 iint->measured_pcrs = 0;
543 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
545 set_bit(IMA_DIGSIG, &iint->atomic_flags);
547 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
550 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
551 const void *xattr_value, size_t xattr_value_len)
553 const struct evm_ima_xattr_data *xvalue = xattr_value;
556 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
559 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
561 ima_reset_appraise_flags(d_backing_inode(dentry),
562 xvalue->type == EVM_IMA_XATTR_DIGSIG);
568 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
572 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
574 ima_reset_appraise_flags(d_backing_inode(dentry), 0);