blob: eb07f9d13c247f35a2b1bfe6a9cc84f6863924de [file] [log] [blame]
Mimi Zohar66dbc3252011-03-15 16:12:09 -04001/*
2 * Copyright (C) 2005-2010 IBM Corporation
3 *
4 * Author:
5 * Mimi Zohar <zohar@us.ibm.com>
6 * Kylene Hall <kjhall@us.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, version 2 of the License.
11 *
12 * File: evm_main.c
13 * implements evm_inode_setxattr, evm_inode_post_setxattr,
14 * evm_inode_removexattr, and evm_verifyxattr
15 */
16
17#include <linux/module.h>
18#include <linux/crypto.h>
19#include <linux/xattr.h>
20#include <linux/integrity.h>
Mimi Zohar3e1be522011-03-09 14:38:26 -050021#include <linux/evm.h>
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050022#include <crypto/hash.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040023#include "evm.h"
24
25int evm_initialized;
26
27char *evm_hmac = "hmac(sha1)";
28
29char *evm_config_xattrnames[] = {
30#ifdef CONFIG_SECURITY_SELINUX
31 XATTR_NAME_SELINUX,
32#endif
33#ifdef CONFIG_SECURITY_SMACK
34 XATTR_NAME_SMACK,
35#endif
36 XATTR_NAME_CAPS,
37 NULL
38};
39
40/*
41 * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
42 *
43 * Compute the HMAC on the dentry's protected set of extended attributes
44 * and compare it against the stored security.evm xattr. (For performance,
45 * use the previoulsy retrieved xattr value and length to calculate the
46 * HMAC.)
47 *
48 * Returns integrity status
49 */
50static enum integrity_status evm_verify_hmac(struct dentry *dentry,
51 const char *xattr_name,
52 char *xattr_value,
53 size_t xattr_value_len,
54 struct integrity_iint_cache *iint)
55{
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -050056 struct evm_ima_xattr_data xattr_data;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040057 int rc;
58
Dmitry Kasatkin24e01982011-05-06 11:34:17 +030059 if (iint->evm_status == INTEGRITY_PASS)
60 return iint->evm_status;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040061
Dmitry Kasatkin6d38ca012011-05-06 11:34:14 +030062 /* if status is not PASS, try to check again - against -ENOMEM */
63
Mimi Zohar66dbc3252011-03-15 16:12:09 -040064 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -050065 xattr_value_len, xattr_data.digest);
Mimi Zohar66dbc3252011-03-15 16:12:09 -040066 if (rc < 0)
Dmitry Kasatkin6d38ca012011-05-06 11:34:14 +030067 goto err_out;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040068
Dmitry Kasatkin6be5cc52011-03-09 14:28:20 -050069 xattr_data.type = EVM_XATTR_HMAC;
70 rc = vfs_xattr_cmp(dentry, XATTR_NAME_EVM, (u8 *)&xattr_data,
71 sizeof xattr_data, GFP_NOFS);
Mimi Zohar66dbc3252011-03-15 16:12:09 -040072 if (rc < 0)
73 goto err_out;
Dmitry Kasatkin24e01982011-05-06 11:34:17 +030074 iint->evm_status = INTEGRITY_PASS;
75 return iint->evm_status;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040076
77err_out:
78 switch (rc) {
79 case -ENODATA: /* file not labelled */
Dmitry Kasatkin24e01982011-05-06 11:34:17 +030080 iint->evm_status = INTEGRITY_NOLABEL;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040081 break;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040082 default:
Dmitry Kasatkin24e01982011-05-06 11:34:17 +030083 iint->evm_status = INTEGRITY_FAIL;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040084 }
Dmitry Kasatkin24e01982011-05-06 11:34:17 +030085 return iint->evm_status;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040086}
87
88static int evm_protected_xattr(const char *req_xattr_name)
89{
90 char **xattrname;
91 int namelen;
92 int found = 0;
93
94 namelen = strlen(req_xattr_name);
95 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
96 if ((strlen(*xattrname) == namelen)
97 && (strncmp(req_xattr_name, *xattrname, namelen) == 0)) {
98 found = 1;
99 break;
100 }
Mimi Zoharcb7231802011-03-09 14:40:44 -0500101 if (strncmp(req_xattr_name,
102 *xattrname + XATTR_SECURITY_PREFIX_LEN,
103 strlen(req_xattr_name)) == 0) {
104 found = 1;
105 break;
106 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400107 }
108 return found;
109}
110
111/**
112 * evm_verifyxattr - verify the integrity of the requested xattr
113 * @dentry: object of the verify xattr
114 * @xattr_name: requested xattr
115 * @xattr_value: requested xattr value
116 * @xattr_value_len: requested xattr value length
117 *
118 * Calculate the HMAC for the given dentry and verify it against the stored
119 * security.evm xattr. For performance, use the xattr value and length
120 * previously retrieved to calculate the HMAC.
121 *
122 * Returns the xattr integrity status.
123 *
124 * This function requires the caller to lock the inode's i_mutex before it
125 * is executed.
126 */
127enum integrity_status evm_verifyxattr(struct dentry *dentry,
128 const char *xattr_name,
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300129 void *xattr_value, size_t xattr_value_len,
130 struct integrity_iint_cache *iint)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400131{
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400132 if (!evm_initialized || !evm_protected_xattr(xattr_name))
133 return INTEGRITY_UNKNOWN;
134
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300135 if (!iint) {
136 iint = integrity_iint_find(dentry->d_inode);
137 if (!iint)
138 return INTEGRITY_UNKNOWN;
139 }
140 return evm_verify_hmac(dentry, xattr_name, xattr_value,
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400141 xattr_value_len, iint);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400142}
143EXPORT_SYMBOL_GPL(evm_verifyxattr);
144
145/*
146 * evm_protect_xattr - protect the EVM extended attribute
147 *
148 * Prevent security.evm from being modified or removed.
149 */
150static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
151 const void *xattr_value, size_t xattr_value_len)
152{
153 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
154 if (!capable(CAP_SYS_ADMIN))
155 return -EPERM;
156 }
157 return 0;
158}
159
160/**
161 * evm_inode_setxattr - protect the EVM extended attribute
162 * @dentry: pointer to the affected dentry
163 * @xattr_name: pointer to the affected extended attribute name
164 * @xattr_value: pointer to the new extended attribute value
165 * @xattr_value_len: pointer to the new extended attribute value length
166 *
167 * Prevent 'security.evm' from being modified
168 */
169int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
170 const void *xattr_value, size_t xattr_value_len)
171{
172 return evm_protect_xattr(dentry, xattr_name, xattr_value,
173 xattr_value_len);
174}
175
176/**
177 * evm_inode_removexattr - protect the EVM extended attribute
178 * @dentry: pointer to the affected dentry
179 * @xattr_name: pointer to the affected extended attribute name
180 *
181 * Prevent 'security.evm' from being removed.
182 */
183int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
184{
185 return evm_protect_xattr(dentry, xattr_name, NULL, 0);
186}
187
188/**
189 * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
190 * @dentry: pointer to the affected dentry
191 * @xattr_name: pointer to the affected extended attribute name
192 * @xattr_value: pointer to the new extended attribute value
193 * @xattr_value_len: pointer to the new extended attribute value length
194 *
195 * Update the HMAC stored in 'security.evm' to reflect the change.
196 *
197 * No need to take the i_mutex lock here, as this function is called from
198 * __vfs_setxattr_noperm(). The caller of which has taken the inode's
199 * i_mutex lock.
200 */
201void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
202 const void *xattr_value, size_t xattr_value_len)
203{
204 if (!evm_initialized || !evm_protected_xattr(xattr_name))
205 return;
206
207 evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
208 return;
209}
210
211/**
212 * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
213 * @dentry: pointer to the affected dentry
214 * @xattr_name: pointer to the affected extended attribute name
215 *
216 * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
217 */
218void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
219{
220 struct inode *inode = dentry->d_inode;
221
222 if (!evm_initialized || !evm_protected_xattr(xattr_name))
223 return;
224
225 mutex_lock(&inode->i_mutex);
226 evm_update_evmxattr(dentry, xattr_name, NULL, 0);
227 mutex_unlock(&inode->i_mutex);
228 return;
229}
230
231/**
232 * evm_inode_post_setattr - update 'security.evm' after modifying metadata
233 * @dentry: pointer to the affected dentry
234 * @ia_valid: for the UID and GID status
235 *
236 * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
237 * changes.
238 *
239 * This function is called from notify_change(), which expects the caller
240 * to lock the inode's i_mutex.
241 */
242void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
243{
244 if (!evm_initialized)
245 return;
246
247 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
248 evm_update_evmxattr(dentry, NULL, NULL, 0);
249 return;
250}
251
Mimi Zoharcb7231802011-03-09 14:40:44 -0500252/*
253 * evm_inode_init_security - initializes security.evm
254 */
255int evm_inode_init_security(struct inode *inode,
256 const struct xattr *lsm_xattr,
257 struct xattr *evm_xattr)
258{
259 struct evm_ima_xattr_data *xattr_data;
260 int rc;
261
262 if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
263 return -EOPNOTSUPP;
264
265 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
266 if (!xattr_data)
267 return -ENOMEM;
268
269 xattr_data->type = EVM_XATTR_HMAC;
270 rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
271 if (rc < 0)
272 goto out;
273
274 evm_xattr->value = xattr_data;
275 evm_xattr->value_len = sizeof(*xattr_data);
276 evm_xattr->name = kstrdup(XATTR_EVM_SUFFIX, GFP_NOFS);
277 return 0;
278out:
279 kfree(xattr_data);
280 return rc;
281}
282EXPORT_SYMBOL_GPL(evm_inode_init_security);
283
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400284static int __init init_evm(void)
285{
286 int error;
287
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400288 error = evm_init_secfs();
289 if (error < 0) {
290 printk(KERN_INFO "EVM: Error registering secfs\n");
291 goto err;
292 }
293err:
294 return error;
295}
296
297static void __exit cleanup_evm(void)
298{
299 evm_cleanup_secfs();
Dmitry Kasatkind46eb362011-03-09 15:07:36 -0500300 if (hmac_tfm)
301 crypto_free_shash(hmac_tfm);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400302}
303
304/*
305 * evm_display_config - list the EVM protected security extended attributes
306 */
307static int __init evm_display_config(void)
308{
309 char **xattrname;
310
311 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
312 printk(KERN_INFO "EVM: %s\n", *xattrname);
313 return 0;
314}
315
316pure_initcall(evm_display_config);
317late_initcall(init_evm);
318
319MODULE_DESCRIPTION("Extended Verification Module");
320MODULE_LICENSE("GPL");