evm: add evm_inode_init_security to initialize new files
[pandora-kernel.git] / security / integrity / evm / evm_main.c
1 /*
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>
21 #include <linux/evm.h>
22 #include "evm.h"
23
24 int evm_initialized;
25
26 char *evm_hmac = "hmac(sha1)";
27
28 char *evm_config_xattrnames[] = {
29 #ifdef CONFIG_SECURITY_SELINUX
30         XATTR_NAME_SELINUX,
31 #endif
32 #ifdef CONFIG_SECURITY_SMACK
33         XATTR_NAME_SMACK,
34 #endif
35         XATTR_NAME_CAPS,
36         NULL
37 };
38
39 /*
40  * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
41  *
42  * Compute the HMAC on the dentry's protected set of extended attributes
43  * and compare it against the stored security.evm xattr. (For performance,
44  * use the previoulsy retrieved xattr value and length to calculate the
45  * HMAC.)
46  *
47  * Returns integrity status
48  */
49 static enum integrity_status evm_verify_hmac(struct dentry *dentry,
50                                              const char *xattr_name,
51                                              char *xattr_value,
52                                              size_t xattr_value_len,
53                                              struct integrity_iint_cache *iint)
54 {
55         struct evm_ima_xattr_data xattr_data;
56         int rc;
57
58         if (iint->hmac_status != INTEGRITY_UNKNOWN)
59                 return iint->hmac_status;
60
61         rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
62                            xattr_value_len, xattr_data.digest);
63         if (rc < 0)
64                 return INTEGRITY_UNKNOWN;
65
66         xattr_data.type = EVM_XATTR_HMAC;
67         rc = vfs_xattr_cmp(dentry, XATTR_NAME_EVM, (u8 *)&xattr_data,
68                            sizeof xattr_data, GFP_NOFS);
69         if (rc < 0)
70                 goto err_out;
71         iint->hmac_status = INTEGRITY_PASS;
72         return iint->hmac_status;
73
74 err_out:
75         switch (rc) {
76         case -ENODATA:          /* file not labelled */
77                 iint->hmac_status = INTEGRITY_NOLABEL;
78                 break;
79         case -EINVAL:
80                 iint->hmac_status = INTEGRITY_FAIL;
81                 break;
82         default:
83                 iint->hmac_status = INTEGRITY_UNKNOWN;
84         }
85         return iint->hmac_status;
86 }
87
88 static 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                 }
101                 if (strncmp(req_xattr_name,
102                             *xattrname + XATTR_SECURITY_PREFIX_LEN,
103                             strlen(req_xattr_name)) == 0) {
104                         found = 1;
105                         break;
106                 }
107         }
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  */
127 enum integrity_status evm_verifyxattr(struct dentry *dentry,
128                                       const char *xattr_name,
129                                       void *xattr_value, size_t xattr_value_len)
130 {
131         struct inode *inode = dentry->d_inode;
132         struct integrity_iint_cache *iint;
133         enum integrity_status status;
134
135         if (!evm_initialized || !evm_protected_xattr(xattr_name))
136                 return INTEGRITY_UNKNOWN;
137
138         iint = integrity_iint_find(inode);
139         if (!iint)
140                 return INTEGRITY_UNKNOWN;
141         status = evm_verify_hmac(dentry, xattr_name, xattr_value,
142                                  xattr_value_len, iint);
143         return status;
144 }
145 EXPORT_SYMBOL_GPL(evm_verifyxattr);
146
147 /*
148  * evm_protect_xattr - protect the EVM extended attribute
149  *
150  * Prevent security.evm from being modified or removed.
151  */
152 static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
153                              const void *xattr_value, size_t xattr_value_len)
154 {
155         if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
156                 if (!capable(CAP_SYS_ADMIN))
157                         return -EPERM;
158         }
159         return 0;
160 }
161
162 /**
163  * evm_inode_setxattr - protect the EVM extended attribute
164  * @dentry: pointer to the affected dentry
165  * @xattr_name: pointer to the affected extended attribute name
166  * @xattr_value: pointer to the new extended attribute value
167  * @xattr_value_len: pointer to the new extended attribute value length
168  *
169  * Prevent 'security.evm' from being modified
170  */
171 int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
172                        const void *xattr_value, size_t xattr_value_len)
173 {
174         return evm_protect_xattr(dentry, xattr_name, xattr_value,
175                                  xattr_value_len);
176 }
177
178 /**
179  * evm_inode_removexattr - protect the EVM extended attribute
180  * @dentry: pointer to the affected dentry
181  * @xattr_name: pointer to the affected extended attribute name
182  *
183  * Prevent 'security.evm' from being removed.
184  */
185 int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
186 {
187         return evm_protect_xattr(dentry, xattr_name, NULL, 0);
188 }
189
190 /**
191  * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
192  * @dentry: pointer to the affected dentry
193  * @xattr_name: pointer to the affected extended attribute name
194  * @xattr_value: pointer to the new extended attribute value
195  * @xattr_value_len: pointer to the new extended attribute value length
196  *
197  * Update the HMAC stored in 'security.evm' to reflect the change.
198  *
199  * No need to take the i_mutex lock here, as this function is called from
200  * __vfs_setxattr_noperm().  The caller of which has taken the inode's
201  * i_mutex lock.
202  */
203 void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
204                              const void *xattr_value, size_t xattr_value_len)
205 {
206         if (!evm_initialized || !evm_protected_xattr(xattr_name))
207                 return;
208
209         evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
210         return;
211 }
212
213 /**
214  * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
215  * @dentry: pointer to the affected dentry
216  * @xattr_name: pointer to the affected extended attribute name
217  *
218  * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
219  */
220 void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
221 {
222         struct inode *inode = dentry->d_inode;
223
224         if (!evm_initialized || !evm_protected_xattr(xattr_name))
225                 return;
226
227         mutex_lock(&inode->i_mutex);
228         evm_update_evmxattr(dentry, xattr_name, NULL, 0);
229         mutex_unlock(&inode->i_mutex);
230         return;
231 }
232
233 /**
234  * evm_inode_post_setattr - update 'security.evm' after modifying metadata
235  * @dentry: pointer to the affected dentry
236  * @ia_valid: for the UID and GID status
237  *
238  * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
239  * changes.
240  *
241  * This function is called from notify_change(), which expects the caller
242  * to lock the inode's i_mutex.
243  */
244 void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
245 {
246         if (!evm_initialized)
247                 return;
248
249         if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
250                 evm_update_evmxattr(dentry, NULL, NULL, 0);
251         return;
252 }
253
254 /*
255  * evm_inode_init_security - initializes security.evm
256  */
257 int evm_inode_init_security(struct inode *inode,
258                                  const struct xattr *lsm_xattr,
259                                  struct xattr *evm_xattr)
260 {
261         struct evm_ima_xattr_data *xattr_data;
262         int rc;
263
264         if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
265                 return -EOPNOTSUPP;
266
267         xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
268         if (!xattr_data)
269                 return -ENOMEM;
270
271         xattr_data->type = EVM_XATTR_HMAC;
272         rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
273         if (rc < 0)
274                 goto out;
275
276         evm_xattr->value = xattr_data;
277         evm_xattr->value_len = sizeof(*xattr_data);
278         evm_xattr->name = kstrdup(XATTR_EVM_SUFFIX, GFP_NOFS);
279         return 0;
280 out:
281         kfree(xattr_data);
282         return rc;
283 }
284 EXPORT_SYMBOL_GPL(evm_inode_init_security);
285
286 static struct crypto_hash *tfm_hmac;    /* preload crypto alg */
287 static int __init init_evm(void)
288 {
289         int error;
290
291         tfm_hmac = crypto_alloc_hash(evm_hmac, 0, CRYPTO_ALG_ASYNC);
292         error = evm_init_secfs();
293         if (error < 0) {
294                 printk(KERN_INFO "EVM: Error registering secfs\n");
295                 goto err;
296         }
297 err:
298         return error;
299 }
300
301 static void __exit cleanup_evm(void)
302 {
303         evm_cleanup_secfs();
304         crypto_free_hash(tfm_hmac);
305 }
306
307 /*
308  * evm_display_config - list the EVM protected security extended attributes
309  */
310 static int __init evm_display_config(void)
311 {
312         char **xattrname;
313
314         for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
315                 printk(KERN_INFO "EVM: %s\n", *xattrname);
316         return 0;
317 }
318
319 pure_initcall(evm_display_config);
320 late_initcall(init_evm);
321
322 MODULE_DESCRIPTION("Extended Verification Module");
323 MODULE_LICENSE("GPL");