2 * Copyright (C) 2011 IBM Corporation
5 * Mimi Zohar <zohar@us.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
11 #include <linux/module.h>
12 #include <linux/file.h>
14 #include <linux/xattr.h>
15 #include <linux/magic.h>
16 #include <linux/ima.h>
17 #include <linux/evm.h>
21 static int __init default_appraise_setup(char *str)
23 if (strncmp(str, "off", 3) == 0)
25 else if (strncmp(str, "fix", 3) == 0)
26 ima_appraise = IMA_APPRAISE_FIX;
30 __setup("ima_appraise=", default_appraise_setup);
33 * ima_must_appraise - set appraise flag
35 * Return 1 to appraise
37 int ima_must_appraise(struct inode *inode, enum ima_hooks func, int mask)
42 static void ima_fix_xattr(struct dentry *dentry,
43 struct integrity_iint_cache *iint)
45 iint->digest[0] = IMA_XATTR_DIGEST;
46 __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
47 iint->digest, IMA_DIGEST_SIZE + 1, 0);
51 * ima_appraise_measurement - appraise file measurement
53 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
54 * Assuming success, compare the xattr hash with the collected measurement.
56 * Return 0 on success, error code otherwise
58 int ima_appraise_measurement(struct integrity_iint_cache *iint,
59 struct file *file, const unsigned char *filename)
61 struct dentry *dentry = file->f_dentry;
62 struct inode *inode = dentry->d_inode;
63 u8 xattr_value[IMA_DIGEST_SIZE];
64 enum integrity_status status = INTEGRITY_UNKNOWN;
65 const char *op = "appraise_data";
66 char *cause = "unknown";
71 if (!inode->i_op->getxattr)
72 return INTEGRITY_UNKNOWN;
74 if (iint->flags & IMA_APPRAISED)
75 return iint->ima_status;
77 rc = inode->i_op->getxattr(dentry, XATTR_NAME_IMA, xattr_value,
80 if (rc && rc != -ENODATA)
83 cause = "missing-hash";
85 (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
89 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
90 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
91 if ((status == INTEGRITY_NOLABEL)
92 || (status == INTEGRITY_NOXATTRS))
93 cause = "missing-HMAC";
94 else if (status == INTEGRITY_FAIL)
95 cause = "invalid-HMAC";
99 rc = memcmp(xattr_value, iint->digest, IMA_DIGEST_SIZE);
101 status = INTEGRITY_FAIL;
102 cause = "invalid-hash";
103 print_hex_dump_bytes("security.ima: ", DUMP_PREFIX_NONE,
104 xattr_value, IMA_DIGEST_SIZE);
105 print_hex_dump_bytes("collected: ", DUMP_PREFIX_NONE,
106 iint->digest, IMA_DIGEST_SIZE);
109 status = INTEGRITY_PASS;
110 iint->flags |= IMA_APPRAISED;
112 if (status != INTEGRITY_PASS) {
113 if (ima_appraise & IMA_APPRAISE_FIX) {
114 ima_fix_xattr(dentry, iint);
115 status = INTEGRITY_PASS;
117 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
120 iint->ima_status = status;
125 * ima_update_xattr - update 'security.ima' hash value
127 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
129 struct dentry *dentry = file->f_dentry;
132 rc = ima_collect_measurement(iint, file);
135 ima_fix_xattr(dentry, iint);
139 * ima_inode_post_setattr - reflect file metadata changes
140 * @dentry: pointer to the affected dentry
142 * Changes to a dentry's metadata might result in needing to appraise.
144 * This function is called from notify_change(), which expects the caller
145 * to lock the inode's i_mutex.
147 void ima_inode_post_setattr(struct dentry *dentry)
149 struct inode *inode = dentry->d_inode;
150 struct integrity_iint_cache *iint;
151 int must_appraise, rc;
153 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
154 || !inode->i_op->removexattr)
157 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
158 iint = integrity_iint_find(inode);
161 iint->flags |= IMA_APPRAISE;
163 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
166 rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);