GNU Linux-libre 4.19.286-gnu1
[releases.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/module.h>
20 #include <linux/crypto.h>
21 #include <linux/audit.h>
22 #include <linux/xattr.h>
23 #include <linux/integrity.h>
24 #include <linux/evm.h>
25 #include <linux/magic.h>
26
27 #include <crypto/hash.h>
28 #include <crypto/hash_info.h>
29 #include <crypto/algapi.h>
30 #include "evm.h"
31
32 int evm_initialized;
33
34 static const char * const integrity_status_msg[] = {
35         "pass", "pass_immutable", "fail", "no_label", "no_xattrs", "unknown"
36 };
37 int evm_hmac_attrs;
38
39 static struct xattr_list evm_config_default_xattrnames[] = {
40 #ifdef CONFIG_SECURITY_SELINUX
41         {.name = XATTR_NAME_SELINUX},
42 #endif
43 #ifdef CONFIG_SECURITY_SMACK
44         {.name = XATTR_NAME_SMACK},
45 #ifdef CONFIG_EVM_EXTRA_SMACK_XATTRS
46         {.name = XATTR_NAME_SMACKEXEC},
47         {.name = XATTR_NAME_SMACKTRANSMUTE},
48         {.name = XATTR_NAME_SMACKMMAP},
49 #endif
50 #endif
51 #ifdef CONFIG_SECURITY_APPARMOR
52         {.name = XATTR_NAME_APPARMOR},
53 #endif
54 #ifdef CONFIG_IMA_APPRAISE
55         {.name = XATTR_NAME_IMA},
56 #endif
57         {.name = XATTR_NAME_CAPS},
58 };
59
60 LIST_HEAD(evm_config_xattrnames);
61
62 static int evm_fixmode __ro_after_init;
63 static int __init evm_set_fixmode(char *str)
64 {
65         if (strncmp(str, "fix", 3) == 0)
66                 evm_fixmode = 1;
67         return 0;
68 }
69 __setup("evm=", evm_set_fixmode);
70
71 static void __init evm_init_config(void)
72 {
73         int i, xattrs;
74
75         xattrs = ARRAY_SIZE(evm_config_default_xattrnames);
76
77         pr_info("Initialising EVM extended attributes:\n");
78         for (i = 0; i < xattrs; i++) {
79                 pr_info("%s\n", evm_config_default_xattrnames[i].name);
80                 list_add_tail(&evm_config_default_xattrnames[i].list,
81                               &evm_config_xattrnames);
82         }
83
84 #ifdef CONFIG_EVM_ATTR_FSUUID
85         evm_hmac_attrs |= EVM_ATTR_FSUUID;
86 #endif
87         pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs);
88 }
89
90 static bool evm_key_loaded(void)
91 {
92         return (bool)(evm_initialized & EVM_KEY_MASK);
93 }
94
95 static int evm_find_protected_xattrs(struct dentry *dentry)
96 {
97         struct inode *inode = d_backing_inode(dentry);
98         struct xattr_list *xattr;
99         int error;
100         int count = 0;
101
102         if (!(inode->i_opflags & IOP_XATTR))
103                 return -EOPNOTSUPP;
104
105         list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
106                 error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0);
107                 if (error < 0) {
108                         if (error == -ENODATA)
109                                 continue;
110                         return error;
111                 }
112                 count++;
113         }
114
115         return count;
116 }
117
118 /*
119  * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
120  *
121  * Compute the HMAC on the dentry's protected set of extended attributes
122  * and compare it against the stored security.evm xattr.
123  *
124  * For performance:
125  * - use the previoulsy retrieved xattr value and length to calculate the
126  *   HMAC.)
127  * - cache the verification result in the iint, when available.
128  *
129  * Returns integrity status
130  */
131 static enum integrity_status evm_verify_hmac(struct dentry *dentry,
132                                              const char *xattr_name,
133                                              char *xattr_value,
134                                              size_t xattr_value_len,
135                                              struct integrity_iint_cache *iint)
136 {
137         struct evm_ima_xattr_data *xattr_data = NULL;
138         struct signature_v2_hdr *hdr;
139         enum integrity_status evm_status = INTEGRITY_PASS;
140         struct evm_digest digest;
141         struct inode *inode;
142         int rc, xattr_len;
143
144         if (iint && (iint->evm_status == INTEGRITY_PASS ||
145                      iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
146                 return iint->evm_status;
147
148         /* if status is not PASS, try to check again - against -ENOMEM */
149
150         /* first need to know the sig type */
151         rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
152                                 GFP_NOFS);
153         if (rc <= 0) {
154                 evm_status = INTEGRITY_FAIL;
155                 if (rc == -ENODATA) {
156                         rc = evm_find_protected_xattrs(dentry);
157                         if (rc > 0)
158                                 evm_status = INTEGRITY_NOLABEL;
159                         else if (rc == 0)
160                                 evm_status = INTEGRITY_NOXATTRS; /* new file */
161                 } else if (rc == -EOPNOTSUPP) {
162                         evm_status = INTEGRITY_UNKNOWN;
163                 }
164                 goto out;
165         }
166
167         xattr_len = rc;
168
169         /* check value type */
170         switch (xattr_data->type) {
171         case EVM_XATTR_HMAC:
172                 if (xattr_len != sizeof(struct evm_ima_xattr_data)) {
173                         evm_status = INTEGRITY_FAIL;
174                         goto out;
175                 }
176
177                 digest.hdr.algo = HASH_ALGO_SHA1;
178                 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
179                                    xattr_value_len, &digest);
180                 if (rc)
181                         break;
182                 rc = crypto_memneq(xattr_data->digest, digest.digest,
183                                    SHA1_DIGEST_SIZE);
184                 if (rc)
185                         rc = -EINVAL;
186                 break;
187         case EVM_IMA_XATTR_DIGSIG:
188         case EVM_XATTR_PORTABLE_DIGSIG:
189                 /* accept xattr with non-empty signature field */
190                 if (xattr_len <= sizeof(struct signature_v2_hdr)) {
191                         evm_status = INTEGRITY_FAIL;
192                         goto out;
193                 }
194
195                 hdr = (struct signature_v2_hdr *)xattr_data;
196                 digest.hdr.algo = hdr->hash_algo;
197                 rc = evm_calc_hash(dentry, xattr_name, xattr_value,
198                                    xattr_value_len, xattr_data->type, &digest);
199                 if (rc)
200                         break;
201                 rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
202                                         (const char *)xattr_data, xattr_len,
203                                         digest.digest, digest.hdr.length);
204                 if (!rc) {
205                         inode = d_backing_inode(dentry);
206
207                         if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
208                                 if (iint)
209                                         iint->flags |= EVM_IMMUTABLE_DIGSIG;
210                                 evm_status = INTEGRITY_PASS_IMMUTABLE;
211                         } else if (!IS_RDONLY(inode) &&
212                                    !(inode->i_sb->s_readonly_remount) &&
213                                    !IS_IMMUTABLE(inode)) {
214                                 evm_update_evmxattr(dentry, xattr_name,
215                                                     xattr_value,
216                                                     xattr_value_len);
217                         }
218                 }
219                 break;
220         default:
221                 rc = -EINVAL;
222                 break;
223         }
224
225         if (rc)
226                 evm_status = (rc == -ENODATA) ?
227                                 INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
228 out:
229         if (iint)
230                 iint->evm_status = evm_status;
231         kfree(xattr_data);
232         return evm_status;
233 }
234
235 static int evm_protected_xattr(const char *req_xattr_name)
236 {
237         int namelen;
238         int found = 0;
239         struct xattr_list *xattr;
240
241         namelen = strlen(req_xattr_name);
242         list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
243                 if ((strlen(xattr->name) == namelen)
244                     && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
245                         found = 1;
246                         break;
247                 }
248                 if (strncmp(req_xattr_name,
249                             xattr->name + XATTR_SECURITY_PREFIX_LEN,
250                             strlen(req_xattr_name)) == 0) {
251                         found = 1;
252                         break;
253                 }
254         }
255
256         return found;
257 }
258
259 /**
260  * evm_verifyxattr - verify the integrity of the requested xattr
261  * @dentry: object of the verify xattr
262  * @xattr_name: requested xattr
263  * @xattr_value: requested xattr value
264  * @xattr_value_len: requested xattr value length
265  *
266  * Calculate the HMAC for the given dentry and verify it against the stored
267  * security.evm xattr. For performance, use the xattr value and length
268  * previously retrieved to calculate the HMAC.
269  *
270  * Returns the xattr integrity status.
271  *
272  * This function requires the caller to lock the inode's i_mutex before it
273  * is executed.
274  */
275 enum integrity_status evm_verifyxattr(struct dentry *dentry,
276                                       const char *xattr_name,
277                                       void *xattr_value, size_t xattr_value_len,
278                                       struct integrity_iint_cache *iint)
279 {
280         if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
281                 return INTEGRITY_UNKNOWN;
282
283         if (!iint) {
284                 iint = integrity_iint_find(d_backing_inode(dentry));
285                 if (!iint)
286                         return INTEGRITY_UNKNOWN;
287         }
288         return evm_verify_hmac(dentry, xattr_name, xattr_value,
289                                  xattr_value_len, iint);
290 }
291 EXPORT_SYMBOL_GPL(evm_verifyxattr);
292
293 /*
294  * evm_verify_current_integrity - verify the dentry's metadata integrity
295  * @dentry: pointer to the affected dentry
296  *
297  * Verify and return the dentry's metadata integrity. The exceptions are
298  * before EVM is initialized or in 'fix' mode.
299  */
300 static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
301 {
302         struct inode *inode = d_backing_inode(dentry);
303
304         if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
305                 return 0;
306         return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
307 }
308
309 /*
310  * evm_protect_xattr - protect the EVM extended attribute
311  *
312  * Prevent security.evm from being modified or removed without the
313  * necessary permissions or when the existing value is invalid.
314  *
315  * The posix xattr acls are 'system' prefixed, which normally would not
316  * affect security.evm.  An interesting side affect of writing posix xattr
317  * acls is their modifying of the i_mode, which is included in security.evm.
318  * For posix xattr acls only, permit security.evm, even if it currently
319  * doesn't exist, to be updated unless the EVM signature is immutable.
320  */
321 static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
322                              const void *xattr_value, size_t xattr_value_len)
323 {
324         enum integrity_status evm_status;
325
326         if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
327                 if (!capable(CAP_SYS_ADMIN))
328                         return -EPERM;
329         } else if (!evm_protected_xattr(xattr_name)) {
330                 if (!posix_xattr_acl(xattr_name))
331                         return 0;
332                 evm_status = evm_verify_current_integrity(dentry);
333                 if ((evm_status == INTEGRITY_PASS) ||
334                     (evm_status == INTEGRITY_NOXATTRS))
335                         return 0;
336                 goto out;
337         }
338
339         evm_status = evm_verify_current_integrity(dentry);
340         if (evm_status == INTEGRITY_NOXATTRS) {
341                 struct integrity_iint_cache *iint;
342
343                 iint = integrity_iint_find(d_backing_inode(dentry));
344                 if (iint && (iint->flags & IMA_NEW_FILE))
345                         return 0;
346
347                 /* exception for pseudo filesystems */
348                 if (dentry->d_sb->s_magic == TMPFS_MAGIC
349                     || dentry->d_sb->s_magic == SYSFS_MAGIC)
350                         return 0;
351
352                 integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
353                                     dentry->d_inode, dentry->d_name.name,
354                                     "update_metadata",
355                                     integrity_status_msg[evm_status],
356                                     -EPERM, 0);
357         }
358 out:
359         if (evm_status != INTEGRITY_PASS)
360                 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
361                                     dentry->d_name.name, "appraise_metadata",
362                                     integrity_status_msg[evm_status],
363                                     -EPERM, 0);
364         return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
365 }
366
367 /**
368  * evm_inode_setxattr - protect the EVM extended attribute
369  * @dentry: pointer to the affected dentry
370  * @xattr_name: pointer to the affected extended attribute name
371  * @xattr_value: pointer to the new extended attribute value
372  * @xattr_value_len: pointer to the new extended attribute value length
373  *
374  * Before allowing the 'security.evm' protected xattr to be updated,
375  * verify the existing value is valid.  As only the kernel should have
376  * access to the EVM encrypted key needed to calculate the HMAC, prevent
377  * userspace from writing HMAC value.  Writing 'security.evm' requires
378  * requires CAP_SYS_ADMIN privileges.
379  */
380 int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
381                        const void *xattr_value, size_t xattr_value_len)
382 {
383         const struct evm_ima_xattr_data *xattr_data = xattr_value;
384
385         /* Policy permits modification of the protected xattrs even though
386          * there's no HMAC key loaded
387          */
388         if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
389                 return 0;
390
391         if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
392                 if (!xattr_value_len)
393                         return -EINVAL;
394                 if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
395                     xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
396                         return -EPERM;
397         }
398         return evm_protect_xattr(dentry, xattr_name, xattr_value,
399                                  xattr_value_len);
400 }
401
402 /**
403  * evm_inode_removexattr - protect the EVM extended attribute
404  * @dentry: pointer to the affected dentry
405  * @xattr_name: pointer to the affected extended attribute name
406  *
407  * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
408  * the current value is valid.
409  */
410 int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
411 {
412         /* Policy permits modification of the protected xattrs even though
413          * there's no HMAC key loaded
414          */
415         if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
416                 return 0;
417
418         return evm_protect_xattr(dentry, xattr_name, NULL, 0);
419 }
420
421 static void evm_reset_status(struct inode *inode)
422 {
423         struct integrity_iint_cache *iint;
424
425         iint = integrity_iint_find(inode);
426         if (iint)
427                 iint->evm_status = INTEGRITY_UNKNOWN;
428 }
429
430 /**
431  * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
432  * @dentry: pointer to the affected dentry
433  * @xattr_name: pointer to the affected extended attribute name
434  * @xattr_value: pointer to the new extended attribute value
435  * @xattr_value_len: pointer to the new extended attribute value length
436  *
437  * Update the HMAC stored in 'security.evm' to reflect the change.
438  *
439  * No need to take the i_mutex lock here, as this function is called from
440  * __vfs_setxattr_noperm().  The caller of which has taken the inode's
441  * i_mutex lock.
442  */
443 void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
444                              const void *xattr_value, size_t xattr_value_len)
445 {
446         if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name)
447                                   && !posix_xattr_acl(xattr_name)))
448                 return;
449
450         evm_reset_status(dentry->d_inode);
451
452         evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
453 }
454
455 /**
456  * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
457  * @dentry: pointer to the affected dentry
458  * @xattr_name: pointer to the affected extended attribute name
459  *
460  * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
461  *
462  * No need to take the i_mutex lock here, as this function is called from
463  * vfs_removexattr() which takes the i_mutex.
464  */
465 void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
466 {
467         if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
468                 return;
469
470         evm_reset_status(dentry->d_inode);
471
472         evm_update_evmxattr(dentry, xattr_name, NULL, 0);
473 }
474
475 /**
476  * evm_inode_setattr - prevent updating an invalid EVM extended attribute
477  * @dentry: pointer to the affected dentry
478  *
479  * Permit update of file attributes when files have a valid EVM signature,
480  * except in the case of them having an immutable portable signature.
481  */
482 int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
483 {
484         unsigned int ia_valid = attr->ia_valid;
485         enum integrity_status evm_status;
486
487         /* Policy permits modification of the protected attrs even though
488          * there's no HMAC key loaded
489          */
490         if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
491                 return 0;
492
493         if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
494                 return 0;
495         evm_status = evm_verify_current_integrity(dentry);
496         if ((evm_status == INTEGRITY_PASS) ||
497             (evm_status == INTEGRITY_NOXATTRS))
498                 return 0;
499         integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
500                             dentry->d_name.name, "appraise_metadata",
501                             integrity_status_msg[evm_status], -EPERM, 0);
502         return -EPERM;
503 }
504
505 /**
506  * evm_inode_post_setattr - update 'security.evm' after modifying metadata
507  * @dentry: pointer to the affected dentry
508  * @ia_valid: for the UID and GID status
509  *
510  * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
511  * changes.
512  *
513  * This function is called from notify_change(), which expects the caller
514  * to lock the inode's i_mutex.
515  */
516 void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
517 {
518         if (!evm_key_loaded())
519                 return;
520
521         if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
522                 evm_update_evmxattr(dentry, NULL, NULL, 0);
523 }
524
525 /*
526  * evm_inode_init_security - initializes security.evm HMAC value
527  */
528 int evm_inode_init_security(struct inode *inode,
529                                  const struct xattr *lsm_xattr,
530                                  struct xattr *evm_xattr)
531 {
532         struct evm_ima_xattr_data *xattr_data;
533         int rc;
534
535         if (!(evm_initialized & EVM_INIT_HMAC) ||
536             !evm_protected_xattr(lsm_xattr->name))
537                 return 0;
538
539         xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
540         if (!xattr_data)
541                 return -ENOMEM;
542
543         xattr_data->type = EVM_XATTR_HMAC;
544         rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
545         if (rc < 0)
546                 goto out;
547
548         evm_xattr->value = xattr_data;
549         evm_xattr->value_len = sizeof(*xattr_data);
550         evm_xattr->name = XATTR_EVM_SUFFIX;
551         return 0;
552 out:
553         kfree(xattr_data);
554         return rc;
555 }
556 EXPORT_SYMBOL_GPL(evm_inode_init_security);
557
558 #ifdef CONFIG_EVM_LOAD_X509
559 void __init evm_load_x509(void)
560 {
561         int rc;
562
563         rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH);
564         if (!rc)
565                 evm_initialized |= EVM_INIT_X509;
566 }
567 #endif
568
569 static int __init init_evm(void)
570 {
571         int error;
572         struct list_head *pos, *q;
573         struct xattr_list *xattr;
574
575         evm_init_config();
576
577         error = integrity_init_keyring(INTEGRITY_KEYRING_EVM);
578         if (error)
579                 goto error;
580
581         error = evm_init_secfs();
582         if (error < 0) {
583                 pr_info("Error registering secfs\n");
584                 goto error;
585         }
586
587 error:
588         if (error != 0) {
589                 if (!list_empty(&evm_config_xattrnames)) {
590                         list_for_each_safe(pos, q, &evm_config_xattrnames) {
591                                 xattr = list_entry(pos, struct xattr_list,
592                                                    list);
593                                 list_del(pos);
594                         }
595                 }
596         }
597
598         return error;
599 }
600
601 late_initcall(init_evm);
602
603 MODULE_DESCRIPTION("Extended Verification Module");
604 MODULE_LICENSE("GPL");