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 int ima_fix_xattr(struct dentry *dentry,
46 struct integrity_iint_cache *iint)
48 iint->ima_xattr.type = IMA_XATTR_DIGEST;
49 return __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
50 (u8 *)&iint->ima_xattr,
51 sizeof(iint->ima_xattr), 0);
55 * ima_appraise_measurement - appraise file measurement
57 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
58 * Assuming success, compare the xattr hash with the collected measurement.
60 * Return 0 on success, error code otherwise
62 int ima_appraise_measurement(struct integrity_iint_cache *iint,
63 struct file *file, const unsigned char *filename)
65 struct dentry *dentry = file->f_dentry;
66 struct inode *inode = dentry->d_inode;
67 struct evm_ima_xattr_data *xattr_value = NULL;
68 enum integrity_status status = INTEGRITY_UNKNOWN;
69 const char *op = "appraise_data";
70 char *cause = "unknown";
75 if (!inode->i_op->getxattr)
76 return INTEGRITY_UNKNOWN;
78 if (iint->flags & IMA_APPRAISED)
79 return iint->ima_status;
81 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)&xattr_value,
84 if (rc && rc != -ENODATA)
87 cause = "missing-hash";
89 (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
93 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
94 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
95 if ((status == INTEGRITY_NOLABEL)
96 || (status == INTEGRITY_NOXATTRS))
97 cause = "missing-HMAC";
98 else if (status == INTEGRITY_FAIL)
99 cause = "invalid-HMAC";
103 switch (xattr_value->type) {
104 case IMA_XATTR_DIGEST:
105 rc = memcmp(xattr_value->digest, iint->ima_xattr.digest,
108 cause = "invalid-hash";
109 status = INTEGRITY_FAIL;
112 status = INTEGRITY_PASS;
114 case EVM_IMA_XATTR_DIGSIG:
115 iint->flags |= IMA_DIGSIG;
116 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
117 xattr_value->digest, rc - 1,
118 iint->ima_xattr.digest,
120 if (rc == -EOPNOTSUPP) {
121 status = INTEGRITY_UNKNOWN;
123 cause = "invalid-signature";
124 status = INTEGRITY_FAIL;
126 status = INTEGRITY_PASS;
130 status = INTEGRITY_UNKNOWN;
131 cause = "unknown-ima-data";
136 if (status != INTEGRITY_PASS) {
137 if ((ima_appraise & IMA_APPRAISE_FIX) &&
139 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
140 if (!ima_fix_xattr(dentry, iint))
141 status = INTEGRITY_PASS;
143 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
146 iint->flags |= IMA_APPRAISED;
148 iint->ima_status = status;
154 * ima_update_xattr - update 'security.ima' hash value
156 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
158 struct dentry *dentry = file->f_dentry;
161 /* do not collect and update hash for digital signatures */
162 if (iint->flags & IMA_DIGSIG)
165 rc = ima_collect_measurement(iint, file);
169 ima_fix_xattr(dentry, iint);
173 * ima_inode_post_setattr - reflect file metadata changes
174 * @dentry: pointer to the affected dentry
176 * Changes to a dentry's metadata might result in needing to appraise.
178 * This function is called from notify_change(), which expects the caller
179 * to lock the inode's i_mutex.
181 void ima_inode_post_setattr(struct dentry *dentry)
183 struct inode *inode = dentry->d_inode;
184 struct integrity_iint_cache *iint;
185 int must_appraise, rc;
187 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
188 || !inode->i_op->removexattr)
191 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
192 iint = integrity_iint_find(inode);
195 iint->flags |= IMA_APPRAISE;
197 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
200 rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
205 * ima_protect_xattr - protect 'security.ima'
207 * Ensure that not just anyone can modify or remove 'security.ima'.
209 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
210 const void *xattr_value, size_t xattr_value_len)
212 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
213 if (!capable(CAP_SYS_ADMIN))
220 static void ima_reset_appraise_flags(struct inode *inode)
222 struct integrity_iint_cache *iint;
224 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
227 iint = integrity_iint_find(inode);
231 iint->flags &= ~IMA_DONE_MASK;
235 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
236 const void *xattr_value, size_t xattr_value_len)
240 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
243 ima_reset_appraise_flags(dentry->d_inode);
249 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
253 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
255 ima_reset_appraise_flags(dentry->d_inode);