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