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>
18 static int __init default_appraise_setup(char *str)
20 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
21 if (strncmp(str, "off", 3) == 0)
23 else if (strncmp(str, "log", 3) == 0)
24 ima_appraise = IMA_APPRAISE_LOG;
25 else if (strncmp(str, "fix", 3) == 0)
26 ima_appraise = IMA_APPRAISE_FIX;
31 __setup("ima_appraise=", default_appraise_setup);
34 * is_ima_appraise_enabled - return appraise status
36 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
38 bool is_ima_appraise_enabled(void)
40 return ima_appraise & IMA_APPRAISE_ENFORCE;
44 * ima_must_appraise - set appraise flag
46 * Return 1 to appraise or hash
48 int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
55 security_task_getsecid(current, &secid);
56 return ima_match_policy(inode, current_cred(), secid, func, mask,
57 IMA_APPRAISE | IMA_HASH, NULL, NULL);
60 static int ima_fix_xattr(struct dentry *dentry,
61 struct integrity_iint_cache *iint)
64 u8 algo = iint->ima_hash->algo;
66 if (algo <= HASH_ALGO_SHA1) {
68 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
71 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
72 iint->ima_hash->xattr.ng.algo = algo;
74 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
75 &iint->ima_hash->xattr.data[offset],
76 (sizeof(iint->ima_hash->xattr) - offset) +
77 iint->ima_hash->length, 0);
81 /* Return specific func appraised cached result */
82 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
87 return iint->ima_mmap_status;
89 return iint->ima_bprm_status;
91 return iint->ima_creds_status;
94 return iint->ima_file_status;
95 case MODULE_CHECK ... MAX_CHECK - 1:
97 return iint->ima_read_status;
101 static void ima_set_cache_status(struct integrity_iint_cache *iint,
103 enum integrity_status status)
107 iint->ima_mmap_status = status;
110 iint->ima_bprm_status = status;
113 iint->ima_creds_status = status;
117 iint->ima_file_status = status;
119 case MODULE_CHECK ... MAX_CHECK - 1:
121 iint->ima_read_status = status;
126 static void ima_cache_flags(struct integrity_iint_cache *iint,
131 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
134 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
137 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
141 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
143 case MODULE_CHECK ... MAX_CHECK - 1:
145 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
150 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
153 struct signature_v2_hdr *sig;
156 if (!xattr_value || xattr_len < 2)
157 /* return default hash algo */
158 return ima_hash_algo;
160 switch (xattr_value->type) {
161 case EVM_IMA_XATTR_DIGSIG:
162 sig = (typeof(sig))xattr_value;
163 if (sig->version != 2 || xattr_len <= sizeof(*sig))
164 return ima_hash_algo;
165 return sig->hash_algo;
167 case IMA_XATTR_DIGEST_NG:
168 /* first byte contains algorithm id */
169 ret = xattr_value->data[0];
170 if (ret < HASH_ALGO__LAST)
173 case IMA_XATTR_DIGEST:
174 /* this is for backward compatibility */
175 if (xattr_len == 21) {
176 unsigned int zero = 0;
177 if (!memcmp(&xattr_value->data[16], &zero, 4))
178 return HASH_ALGO_MD5;
180 return HASH_ALGO_SHA1;
181 } else if (xattr_len == 17)
182 return HASH_ALGO_MD5;
186 /* return default hash algo */
187 return ima_hash_algo;
190 int ima_read_xattr(struct dentry *dentry,
191 struct evm_ima_xattr_data **xattr_value)
195 ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
197 if (ret == -EOPNOTSUPP)
203 * ima_appraise_measurement - appraise file measurement
205 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
206 * Assuming success, compare the xattr hash with the collected measurement.
208 * Return 0 on success, error code otherwise
210 int ima_appraise_measurement(enum ima_hooks func,
211 struct integrity_iint_cache *iint,
212 struct file *file, const unsigned char *filename,
213 struct evm_ima_xattr_data *xattr_value,
216 static const char op[] = "appraise_data";
217 const char *cause = "unknown";
218 struct dentry *dentry = file_dentry(file);
219 struct inode *inode = d_backing_inode(dentry);
220 enum integrity_status status = INTEGRITY_UNKNOWN;
221 int rc = xattr_len, hash_start = 0;
223 if (!(inode->i_opflags & IOP_XATTR))
224 return INTEGRITY_UNKNOWN;
227 if (rc && rc != -ENODATA)
230 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
231 "IMA-signature-required" : "missing-hash";
232 status = INTEGRITY_NOLABEL;
233 if (file->f_mode & FMODE_CREATED)
234 iint->flags |= IMA_NEW_FILE;
235 if ((iint->flags & IMA_NEW_FILE) &&
236 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
237 (inode->i_size == 0)))
238 status = INTEGRITY_PASS;
242 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
245 case INTEGRITY_PASS_IMMUTABLE:
246 case INTEGRITY_UNKNOWN:
248 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
249 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
250 cause = "missing-HMAC";
252 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
253 cause = "invalid-HMAC";
256 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
259 switch (xattr_value->type) {
260 case IMA_XATTR_DIGEST_NG:
261 /* first byte contains algorithm id */
264 case IMA_XATTR_DIGEST:
265 if (iint->flags & IMA_DIGSIG_REQUIRED) {
266 cause = "IMA-signature-required";
267 status = INTEGRITY_FAIL;
270 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
271 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
272 iint->ima_hash->length)
273 /* xattr length may be longer. md5 hash in previous
274 version occupied 20 bytes in xattr, instead of 16
276 rc = memcmp(&xattr_value->data[hash_start],
277 iint->ima_hash->digest,
278 iint->ima_hash->length);
282 cause = "invalid-hash";
283 status = INTEGRITY_FAIL;
286 status = INTEGRITY_PASS;
288 case EVM_IMA_XATTR_DIGSIG:
289 set_bit(IMA_DIGSIG, &iint->atomic_flags);
290 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
291 (const char *)xattr_value,
293 iint->ima_hash->digest,
294 iint->ima_hash->length);
295 if (rc == -EOPNOTSUPP) {
296 status = INTEGRITY_UNKNOWN;
299 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
300 func == KEXEC_KERNEL_CHECK)
301 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
302 (const char *)xattr_value,
304 iint->ima_hash->digest,
305 iint->ima_hash->length);
307 cause = "invalid-signature";
308 status = INTEGRITY_FAIL;
310 status = INTEGRITY_PASS;
314 status = INTEGRITY_UNKNOWN;
315 cause = "unknown-ima-data";
321 * File signatures on some filesystems can not be properly verified.
322 * When such filesystems are mounted by an untrusted mounter or on a
323 * system not willing to accept such a risk, fail the file signature
326 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
327 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
328 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
329 status = INTEGRITY_FAIL;
330 cause = "unverifiable-signature";
331 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
333 } else if (status != INTEGRITY_PASS) {
334 /* Fix mode, but don't replace file signatures. */
335 if ((ima_appraise & IMA_APPRAISE_FIX) &&
337 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
338 if (!ima_fix_xattr(dentry, iint))
339 status = INTEGRITY_PASS;
342 /* Permit new files with file signatures, but without data. */
343 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
344 xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
345 status = INTEGRITY_PASS;
348 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
351 ima_cache_flags(iint, func);
354 ima_set_cache_status(iint, func, status);
359 * ima_update_xattr - update 'security.ima' hash value
361 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
363 struct dentry *dentry = file_dentry(file);
366 /* do not collect and update hash for digital signatures */
367 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
370 if ((iint->ima_file_status != INTEGRITY_PASS) &&
371 !(iint->flags & IMA_HASH))
374 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
378 inode_lock(file_inode(file));
379 ima_fix_xattr(dentry, iint);
380 inode_unlock(file_inode(file));
384 * ima_inode_post_setattr - reflect file metadata changes
385 * @dentry: pointer to the affected dentry
387 * Changes to a dentry's metadata might result in needing to appraise.
389 * This function is called from notify_change(), which expects the caller
390 * to lock the inode's i_mutex.
392 void ima_inode_post_setattr(struct dentry *dentry)
394 struct inode *inode = d_backing_inode(dentry);
395 struct integrity_iint_cache *iint;
398 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
399 || !(inode->i_opflags & IOP_XATTR))
402 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
404 __vfs_removexattr(dentry, XATTR_NAME_IMA);
405 iint = integrity_iint_find(inode);
407 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
409 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
414 * ima_protect_xattr - protect 'security.ima'
416 * Ensure that not just anyone can modify or remove 'security.ima'.
418 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
419 const void *xattr_value, size_t xattr_value_len)
421 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
422 if (!capable(CAP_SYS_ADMIN))
429 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
431 struct integrity_iint_cache *iint;
433 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
436 iint = integrity_iint_find(inode);
439 iint->measured_pcrs = 0;
440 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
442 set_bit(IMA_DIGSIG, &iint->atomic_flags);
444 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
447 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
448 const void *xattr_value, size_t xattr_value_len)
450 const struct evm_ima_xattr_data *xvalue = xattr_value;
453 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
456 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
458 ima_reset_appraise_flags(d_backing_inode(dentry),
459 xvalue->type == EVM_IMA_XATTR_DIGSIG);
465 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
469 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
471 ima_reset_appraise_flags(d_backing_inode(dentry), 0);