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, int mask, enum ima_hooks func)
42 return ima_match_policy(inode, func, mask, IMA_APPRAISE);
45 static void ima_fix_xattr(struct dentry *dentry,
46 struct integrity_iint_cache *iint)
48 iint->ima_xattr.type = IMA_XATTR_DIGEST;
49 __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA, (u8 *)&iint->ima_xattr,
50 sizeof iint->ima_xattr, 0);
54 * ima_appraise_measurement - appraise file measurement
56 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
57 * Assuming success, compare the xattr hash with the collected measurement.
59 * Return 0 on success, error code otherwise
61 int ima_appraise_measurement(struct integrity_iint_cache *iint,
62 struct file *file, const unsigned char *filename)
64 struct dentry *dentry = file->f_dentry;
65 struct inode *inode = dentry->d_inode;
66 struct evm_ima_xattr_data *xattr_value = NULL;
67 enum integrity_status status = INTEGRITY_UNKNOWN;
68 const char *op = "appraise_data";
69 char *cause = "unknown";
74 if (!inode->i_op->getxattr)
75 return INTEGRITY_UNKNOWN;
77 if (iint->flags & IMA_APPRAISED)
78 return iint->ima_status;
80 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)&xattr_value,
83 if (rc && rc != -ENODATA)
86 cause = "missing-hash";
88 (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
92 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
93 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
94 if ((status == INTEGRITY_NOLABEL)
95 || (status == INTEGRITY_NOXATTRS))
96 cause = "missing-HMAC";
97 else if (status == INTEGRITY_FAIL)
98 cause = "invalid-HMAC";
102 switch (xattr_value->type) {
103 case IMA_XATTR_DIGEST:
104 rc = memcmp(xattr_value->digest, iint->ima_xattr.digest,
107 cause = "invalid-hash";
108 status = INTEGRITY_FAIL;
109 print_hex_dump_bytes("security.ima: ", DUMP_PREFIX_NONE,
110 xattr_value, sizeof(*xattr_value));
111 print_hex_dump_bytes("collected: ", DUMP_PREFIX_NONE,
112 (u8 *)&iint->ima_xattr,
113 sizeof iint->ima_xattr);
116 status = INTEGRITY_PASS;
118 case EVM_IMA_XATTR_DIGSIG:
119 iint->flags |= IMA_DIGSIG;
120 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
121 xattr_value->digest, rc - 1,
122 iint->ima_xattr.digest,
124 if (rc == -EOPNOTSUPP) {
125 status = INTEGRITY_UNKNOWN;
127 cause = "invalid-signature";
128 status = INTEGRITY_FAIL;
130 status = INTEGRITY_PASS;
134 status = INTEGRITY_UNKNOWN;
135 cause = "unknown-ima-data";
140 if (status != INTEGRITY_PASS) {
141 if ((ima_appraise & IMA_APPRAISE_FIX) &&
143 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
144 ima_fix_xattr(dentry, iint);
145 status = INTEGRITY_PASS;
147 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
150 iint->flags |= IMA_APPRAISED;
152 iint->ima_status = status;
158 * ima_update_xattr - update 'security.ima' hash value
160 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
162 struct dentry *dentry = file->f_dentry;
165 /* do not collect and update hash for digital signatures */
166 if (iint->flags & IMA_DIGSIG)
169 rc = ima_collect_measurement(iint, file);
173 ima_fix_xattr(dentry, iint);
177 * ima_inode_post_setattr - reflect file metadata changes
178 * @dentry: pointer to the affected dentry
180 * Changes to a dentry's metadata might result in needing to appraise.
182 * This function is called from notify_change(), which expects the caller
183 * to lock the inode's i_mutex.
185 void ima_inode_post_setattr(struct dentry *dentry)
187 struct inode *inode = dentry->d_inode;
188 struct integrity_iint_cache *iint;
189 int must_appraise, rc;
191 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
192 || !inode->i_op->removexattr)
195 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
196 iint = integrity_iint_find(inode);
199 iint->flags |= IMA_APPRAISE;
201 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
204 rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
209 * ima_protect_xattr - protect 'security.ima'
211 * Ensure that not just anyone can modify or remove 'security.ima'.
213 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
214 const void *xattr_value, size_t xattr_value_len)
216 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
217 if (!capable(CAP_SYS_ADMIN))
224 static void ima_reset_appraise_flags(struct inode *inode)
226 struct integrity_iint_cache *iint;
228 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
231 iint = integrity_iint_find(inode);
235 iint->flags &= ~IMA_DONE_MASK;
239 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
240 const void *xattr_value, size_t xattr_value_len)
244 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
247 ima_reset_appraise_flags(dentry->d_inode);
253 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
257 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
259 ima_reset_appraise_flags(dentry->d_inode);