blob: 6ae00fee1d348f407a17413b9fee6fc661657c00 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Mimi Zohar66dbc3252011-03-15 16:12:09 -04002/*
3 * Copyright (C) 2005-2010 IBM Corporation
4 *
5 * Author:
6 * Mimi Zohar <zohar@us.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
8 *
Mimi Zohar66dbc3252011-03-15 16:12:09 -04009 * File: evm_main.c
10 * implements evm_inode_setxattr, evm_inode_post_setxattr,
11 * evm_inode_removexattr, and evm_verifyxattr
12 */
13
Paul Gortmaker3aafb1f2018-12-09 15:36:32 -050014#include <linux/init.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040015#include <linux/crypto.h>
Mimi Zohar9b97b6c2013-02-21 09:31:22 -050016#include <linux/audit.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040017#include <linux/xattr.h>
18#include <linux/integrity.h>
Mimi Zohar3e1be522011-03-09 14:38:26 -050019#include <linux/evm.h>
Ingo Molnar50d34392017-02-05 16:03:58 +010020#include <linux/magic.h>
21
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050022#include <crypto/hash.h>
Matthew Garrett5feeb612018-06-08 14:57:43 -070023#include <crypto/hash_info.h>
Ryan Ware613317b2016-02-11 15:58:44 -080024#include <crypto/algapi.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040025#include "evm.h"
26
27int evm_initialized;
28
HernĂ¡n Gonzalez17d7b0a2018-02-27 19:17:00 -030029static const char * const integrity_status_msg[] = {
Matthew Garrett50b97742017-11-07 07:17:42 -080030 "pass", "pass_immutable", "fail", "no_label", "no_xattrs", "unknown"
Mimi Zohar9b97b6c2013-02-21 09:31:22 -050031};
Dmitry Kasatkind3b33672014-03-28 14:31:04 +020032int evm_hmac_attrs;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040033
Matthew Garrettfa516b62018-05-15 10:38:26 -070034static struct xattr_list evm_config_default_xattrnames[] = {
Mimi Zohar66dbc3252011-03-15 16:12:09 -040035#ifdef CONFIG_SECURITY_SELINUX
Matthew Garrett21af7662018-05-11 16:12:35 -070036 {.name = XATTR_NAME_SELINUX},
Mimi Zohar66dbc3252011-03-15 16:12:09 -040037#endif
38#ifdef CONFIG_SECURITY_SMACK
Matthew Garrett21af7662018-05-11 16:12:35 -070039 {.name = XATTR_NAME_SMACK},
Dmitry Kasatkin3e38df52014-03-28 14:31:14 +020040#ifdef CONFIG_EVM_EXTRA_SMACK_XATTRS
Matthew Garrett21af7662018-05-11 16:12:35 -070041 {.name = XATTR_NAME_SMACKEXEC},
42 {.name = XATTR_NAME_SMACKTRANSMUTE},
43 {.name = XATTR_NAME_SMACKMMAP},
Dmitry Kasatkin3e38df52014-03-28 14:31:14 +020044#endif
Mimi Zohar66dbc3252011-03-15 16:12:09 -040045#endif
Matthew Garrett096b8542017-10-13 15:09:25 -070046#ifdef CONFIG_SECURITY_APPARMOR
Matthew Garrett21af7662018-05-11 16:12:35 -070047 {.name = XATTR_NAME_APPARMOR},
Matthew Garrett096b8542017-10-13 15:09:25 -070048#endif
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050049#ifdef CONFIG_IMA_APPRAISE
Matthew Garrett21af7662018-05-11 16:12:35 -070050 {.name = XATTR_NAME_IMA},
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050051#endif
Matthew Garrett21af7662018-05-11 16:12:35 -070052 {.name = XATTR_NAME_CAPS},
Mimi Zohar66dbc3252011-03-15 16:12:09 -040053};
54
Matthew Garrett21af7662018-05-11 16:12:35 -070055LIST_HEAD(evm_config_xattrnames);
56
Mimi Zohar7102ebc2011-05-12 18:33:20 -040057static int evm_fixmode;
58static int __init evm_set_fixmode(char *str)
59{
60 if (strncmp(str, "fix", 3) == 0)
61 evm_fixmode = 1;
Bruno Meneguele7fe2bb72020-09-04 16:40:58 -030062 else
63 pr_err("invalid \"%s\" mode", str);
64
Mimi Zohar7102ebc2011-05-12 18:33:20 -040065 return 0;
66}
67__setup("evm=", evm_set_fixmode);
68
Dmitry Kasatkind3b33672014-03-28 14:31:04 +020069static void __init evm_init_config(void)
70{
Matthew Garrett21af7662018-05-11 16:12:35 -070071 int i, xattrs;
72
73 xattrs = ARRAY_SIZE(evm_config_default_xattrnames);
74
75 pr_info("Initialising EVM extended attributes:\n");
76 for (i = 0; i < xattrs; i++) {
77 pr_info("%s\n", evm_config_default_xattrnames[i].name);
78 list_add_tail(&evm_config_default_xattrnames[i].list,
79 &evm_config_xattrnames);
80 }
81
Dmitry Kasatkind3b33672014-03-28 14:31:04 +020082#ifdef CONFIG_EVM_ATTR_FSUUID
83 evm_hmac_attrs |= EVM_ATTR_FSUUID;
84#endif
85 pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs);
86}
87
Matthew Garrettae1ba162017-11-07 07:18:35 -080088static bool evm_key_loaded(void)
89{
90 return (bool)(evm_initialized & EVM_KEY_MASK);
91}
92
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030093static int evm_find_protected_xattrs(struct dentry *dentry)
94{
David Howellsc6f493d2015-03-17 22:26:22 +000095 struct inode *inode = d_backing_inode(dentry);
Matthew Garrett21af7662018-05-11 16:12:35 -070096 struct xattr_list *xattr;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030097 int error;
98 int count = 0;
99
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200100 if (!(inode->i_opflags & IOP_XATTR))
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300101 return -EOPNOTSUPP;
102
Madhuparna Bhowmik770f6052020-04-30 21:32:05 +0530103 list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
Matthew Garrett21af7662018-05-11 16:12:35 -0700104 error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300105 if (error < 0) {
106 if (error == -ENODATA)
107 continue;
108 return error;
109 }
110 count++;
111 }
112
113 return count;
114}
115
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400116/*
117 * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
118 *
119 * Compute the HMAC on the dentry's protected set of extended attributes
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400120 * and compare it against the stored security.evm xattr.
121 *
122 * For performance:
123 * - use the previoulsy retrieved xattr value and length to calculate the
124 * HMAC.)
125 * - cache the verification result in the iint, when available.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400126 *
127 * Returns integrity status
128 */
129static enum integrity_status evm_verify_hmac(struct dentry *dentry,
130 const char *xattr_name,
131 char *xattr_value,
132 size_t xattr_value_len,
133 struct integrity_iint_cache *iint)
134{
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300135 struct evm_ima_xattr_data *xattr_data = NULL;
Matthew Garrett5feeb612018-06-08 14:57:43 -0700136 struct signature_v2_hdr *hdr;
Mimi Zohar566be592011-08-22 09:14:18 -0400137 enum integrity_status evm_status = INTEGRITY_PASS;
Matthew Garrett5feeb612018-06-08 14:57:43 -0700138 struct evm_digest digest;
Sascha Hauer70946c4a2018-03-01 13:38:45 +0100139 struct inode *inode;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300140 int rc, xattr_len;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400141
Matthew Garrett50b97742017-11-07 07:17:42 -0800142 if (iint && (iint->evm_status == INTEGRITY_PASS ||
143 iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
Dmitry Kasatkin24e01982011-05-06 11:34:17 +0300144 return iint->evm_status;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400145
Dmitry Kasatkin6d38ca012011-05-06 11:34:14 +0300146 /* if status is not PASS, try to check again - against -ENOMEM */
147
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300148 /* first need to know the sig type */
149 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
150 GFP_NOFS);
151 if (rc <= 0) {
Dmitry Kasatkin1f100972014-08-15 13:49:22 +0300152 evm_status = INTEGRITY_FAIL;
153 if (rc == -ENODATA) {
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300154 rc = evm_find_protected_xattrs(dentry);
155 if (rc > 0)
156 evm_status = INTEGRITY_NOLABEL;
157 else if (rc == 0)
158 evm_status = INTEGRITY_NOXATTRS; /* new file */
Dmitry Kasatkin1f100972014-08-15 13:49:22 +0300159 } else if (rc == -EOPNOTSUPP) {
160 evm_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300161 }
Mimi Zohar566be592011-08-22 09:14:18 -0400162 goto out;
163 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400164
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +0900165 xattr_len = rc;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300166
167 /* check value type */
168 switch (xattr_data->type) {
169 case EVM_XATTR_HMAC:
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300170 if (xattr_len != sizeof(struct evm_xattr)) {
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500171 evm_status = INTEGRITY_FAIL;
172 goto out;
173 }
Matthew Garrett5feeb612018-06-08 14:57:43 -0700174
175 digest.hdr.algo = HASH_ALGO_SHA1;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300176 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700177 xattr_value_len, &digest);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300178 if (rc)
179 break;
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300180 rc = crypto_memneq(xattr_data->data, digest.digest,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700181 SHA1_DIGEST_SIZE);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300182 if (rc)
183 rc = -EINVAL;
184 break;
185 case EVM_IMA_XATTR_DIGSIG:
Matthew Garrett50b97742017-11-07 07:17:42 -0800186 case EVM_XATTR_PORTABLE_DIGSIG:
Matthew Garrett5feeb612018-06-08 14:57:43 -0700187 hdr = (struct signature_v2_hdr *)xattr_data;
188 digest.hdr.algo = hdr->hash_algo;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300189 rc = evm_calc_hash(dentry, xattr_name, xattr_value,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700190 xattr_value_len, xattr_data->type, &digest);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300191 if (rc)
192 break;
193 rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +0900194 (const char *)xattr_data, xattr_len,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700195 digest.digest, digest.hdr.length);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300196 if (!rc) {
Sascha Hauer70946c4a2018-03-01 13:38:45 +0100197 inode = d_backing_inode(dentry);
198
Matthew Garrett50b97742017-11-07 07:17:42 -0800199 if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
200 if (iint)
201 iint->flags |= EVM_IMMUTABLE_DIGSIG;
202 evm_status = INTEGRITY_PASS_IMMUTABLE;
Sascha Hauer70946c4a2018-03-01 13:38:45 +0100203 } else if (!IS_RDONLY(inode) &&
204 !(inode->i_sb->s_readonly_remount) &&
205 !IS_IMMUTABLE(inode)) {
Dmitry Kasatkinc2baec72014-10-01 21:43:08 +0300206 evm_update_evmxattr(dentry, xattr_name,
207 xattr_value,
208 xattr_value_len);
Matthew Garrett50b97742017-11-07 07:17:42 -0800209 }
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300210 }
211 break;
212 default:
213 rc = -EINVAL;
214 break;
215 }
216
217 if (rc)
218 evm_status = (rc == -ENODATA) ?
219 INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400220out:
221 if (iint)
222 iint->evm_status = evm_status;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300223 kfree(xattr_data);
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400224 return evm_status;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400225}
226
227static int evm_protected_xattr(const char *req_xattr_name)
228{
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400229 int namelen;
230 int found = 0;
Matthew Garrett21af7662018-05-11 16:12:35 -0700231 struct xattr_list *xattr;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400232
233 namelen = strlen(req_xattr_name);
Madhuparna Bhowmik770f6052020-04-30 21:32:05 +0530234 list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
Matthew Garrett21af7662018-05-11 16:12:35 -0700235 if ((strlen(xattr->name) == namelen)
236 && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400237 found = 1;
238 break;
239 }
Mimi Zoharcb7231802011-03-09 14:40:44 -0500240 if (strncmp(req_xattr_name,
Matthew Garrett21af7662018-05-11 16:12:35 -0700241 xattr->name + XATTR_SECURITY_PREFIX_LEN,
Mimi Zoharcb7231802011-03-09 14:40:44 -0500242 strlen(req_xattr_name)) == 0) {
243 found = 1;
244 break;
245 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400246 }
Matthew Garrett21af7662018-05-11 16:12:35 -0700247
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400248 return found;
249}
250
251/**
252 * evm_verifyxattr - verify the integrity of the requested xattr
253 * @dentry: object of the verify xattr
254 * @xattr_name: requested xattr
255 * @xattr_value: requested xattr value
256 * @xattr_value_len: requested xattr value length
257 *
258 * Calculate the HMAC for the given dentry and verify it against the stored
259 * security.evm xattr. For performance, use the xattr value and length
260 * previously retrieved to calculate the HMAC.
261 *
262 * Returns the xattr integrity status.
263 *
264 * This function requires the caller to lock the inode's i_mutex before it
265 * is executed.
266 */
267enum integrity_status evm_verifyxattr(struct dentry *dentry,
268 const char *xattr_name,
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300269 void *xattr_value, size_t xattr_value_len,
270 struct integrity_iint_cache *iint)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400271{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800272 if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400273 return INTEGRITY_UNKNOWN;
274
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300275 if (!iint) {
David Howellsc6f493d2015-03-17 22:26:22 +0000276 iint = integrity_iint_find(d_backing_inode(dentry));
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300277 if (!iint)
278 return INTEGRITY_UNKNOWN;
279 }
280 return evm_verify_hmac(dentry, xattr_name, xattr_value,
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400281 xattr_value_len, iint);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400282}
283EXPORT_SYMBOL_GPL(evm_verifyxattr);
284
285/*
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400286 * evm_verify_current_integrity - verify the dentry's metadata integrity
287 * @dentry: pointer to the affected dentry
288 *
289 * Verify and return the dentry's metadata integrity. The exceptions are
290 * before EVM is initialized or in 'fix' mode.
291 */
292static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
293{
David Howellsc6f493d2015-03-17 22:26:22 +0000294 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400295
Matthew Garrettae1ba162017-11-07 07:18:35 -0800296 if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400297 return 0;
298 return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
299}
300
Mimi Zohara924ce02011-08-11 01:22:30 -0400301/*
302 * evm_protect_xattr - protect the EVM extended attribute
303 *
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400304 * Prevent security.evm from being modified or removed without the
305 * necessary permissions or when the existing value is invalid.
306 *
307 * The posix xattr acls are 'system' prefixed, which normally would not
308 * affect security.evm. An interesting side affect of writing posix xattr
309 * acls is their modifying of the i_mode, which is included in security.evm.
310 * For posix xattr acls only, permit security.evm, even if it currently
Matthew Garrett50b97742017-11-07 07:17:42 -0800311 * doesn't exist, to be updated unless the EVM signature is immutable.
Mimi Zohara924ce02011-08-11 01:22:30 -0400312 */
313static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
314 const void *xattr_value, size_t xattr_value_len)
315{
316 enum integrity_status evm_status;
317
318 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
319 if (!capable(CAP_SYS_ADMIN))
320 return -EPERM;
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400321 } else if (!evm_protected_xattr(xattr_name)) {
322 if (!posix_xattr_acl(xattr_name))
323 return 0;
324 evm_status = evm_verify_current_integrity(dentry);
325 if ((evm_status == INTEGRITY_PASS) ||
Mimi Zohar566be592011-08-22 09:14:18 -0400326 (evm_status == INTEGRITY_NOXATTRS))
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400327 return 0;
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500328 goto out;
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400329 }
Matthew Garrettae1ba162017-11-07 07:18:35 -0800330
Mimi Zohara924ce02011-08-11 01:22:30 -0400331 evm_status = evm_verify_current_integrity(dentry);
Dmitry Kasatkin3dcbad52014-09-02 16:31:43 +0300332 if (evm_status == INTEGRITY_NOXATTRS) {
333 struct integrity_iint_cache *iint;
334
David Howellsc6f493d2015-03-17 22:26:22 +0000335 iint = integrity_iint_find(d_backing_inode(dentry));
Dmitry Kasatkin3dcbad52014-09-02 16:31:43 +0300336 if (iint && (iint->flags & IMA_NEW_FILE))
337 return 0;
Mimi Zohar5101a182015-04-21 13:59:31 -0400338
339 /* exception for pseudo filesystems */
Al Virofc640052016-04-10 01:33:30 -0400340 if (dentry->d_sb->s_magic == TMPFS_MAGIC
341 || dentry->d_sb->s_magic == SYSFS_MAGIC)
Mimi Zohar5101a182015-04-21 13:59:31 -0400342 return 0;
343
344 integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
345 dentry->d_inode, dentry->d_name.name,
346 "update_metadata",
347 integrity_status_msg[evm_status],
348 -EPERM, 0);
Dmitry Kasatkin3dcbad52014-09-02 16:31:43 +0300349 }
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500350out:
351 if (evm_status != INTEGRITY_PASS)
David Howellsc6f493d2015-03-17 22:26:22 +0000352 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500353 dentry->d_name.name, "appraise_metadata",
354 integrity_status_msg[evm_status],
355 -EPERM, 0);
Mimi Zohara924ce02011-08-11 01:22:30 -0400356 return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
357}
358
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400359/**
360 * evm_inode_setxattr - protect the EVM extended attribute
361 * @dentry: pointer to the affected dentry
362 * @xattr_name: pointer to the affected extended attribute name
363 * @xattr_value: pointer to the new extended attribute value
364 * @xattr_value_len: pointer to the new extended attribute value length
365 *
Mimi Zohar2fb1c9a2014-05-11 00:05:23 -0400366 * Before allowing the 'security.evm' protected xattr to be updated,
367 * verify the existing value is valid. As only the kernel should have
368 * access to the EVM encrypted key needed to calculate the HMAC, prevent
369 * userspace from writing HMAC value. Writing 'security.evm' requires
370 * requires CAP_SYS_ADMIN privileges.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400371 */
372int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
373 const void *xattr_value, size_t xattr_value_len)
374{
Mimi Zohar2fb1c9a2014-05-11 00:05:23 -0400375 const struct evm_ima_xattr_data *xattr_data = xattr_value;
376
Matthew Garrettae1ba162017-11-07 07:18:35 -0800377 /* Policy permits modification of the protected xattrs even though
378 * there's no HMAC key loaded
379 */
380 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
381 return 0;
382
Dmitry Kasatkin3b1deef2014-10-28 14:28:49 +0200383 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
384 if (!xattr_value_len)
385 return -EINVAL;
Matthew Garrett50b97742017-11-07 07:17:42 -0800386 if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
387 xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
Dmitry Kasatkin3b1deef2014-10-28 14:28:49 +0200388 return -EPERM;
389 }
Mimi Zohara924ce02011-08-11 01:22:30 -0400390 return evm_protect_xattr(dentry, xattr_name, xattr_value,
391 xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400392}
393
394/**
395 * evm_inode_removexattr - protect the EVM extended attribute
396 * @dentry: pointer to the affected dentry
397 * @xattr_name: pointer to the affected extended attribute name
398 *
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400399 * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
400 * the current value is valid.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400401 */
402int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
403{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800404 /* Policy permits modification of the protected xattrs even though
405 * there's no HMAC key loaded
406 */
407 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
408 return 0;
409
Mimi Zohara924ce02011-08-11 01:22:30 -0400410 return evm_protect_xattr(dentry, xattr_name, NULL, 0);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400411}
412
Dmitry Kasatkin523b74b2015-10-22 21:26:42 +0300413static void evm_reset_status(struct inode *inode)
414{
415 struct integrity_iint_cache *iint;
416
417 iint = integrity_iint_find(inode);
418 if (iint)
419 iint->evm_status = INTEGRITY_UNKNOWN;
420}
421
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400422/**
423 * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
424 * @dentry: pointer to the affected dentry
425 * @xattr_name: pointer to the affected extended attribute name
426 * @xattr_value: pointer to the new extended attribute value
427 * @xattr_value_len: pointer to the new extended attribute value length
428 *
429 * Update the HMAC stored in 'security.evm' to reflect the change.
430 *
431 * No need to take the i_mutex lock here, as this function is called from
432 * __vfs_setxattr_noperm(). The caller of which has taken the inode's
433 * i_mutex lock.
434 */
435void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
436 const void *xattr_value, size_t xattr_value_len)
437{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800438 if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name)
439 && !posix_xattr_acl(xattr_name)))
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400440 return;
441
Dmitry Kasatkin523b74b2015-10-22 21:26:42 +0300442 evm_reset_status(dentry->d_inode);
443
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400444 evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400445}
446
447/**
448 * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
449 * @dentry: pointer to the affected dentry
450 * @xattr_name: pointer to the affected extended attribute name
451 *
452 * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
Dmitry Kasatkin7c51bb002014-11-20 16:31:01 +0200453 *
454 * No need to take the i_mutex lock here, as this function is called from
455 * vfs_removexattr() which takes the i_mutex.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400456 */
457void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
458{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800459 if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400460 return;
461
Dmitry Kasatkin523b74b2015-10-22 21:26:42 +0300462 evm_reset_status(dentry->d_inode);
463
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400464 evm_update_evmxattr(dentry, xattr_name, NULL, 0);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400465}
466
467/**
Mimi Zohar817b54a2011-05-13 12:53:38 -0400468 * evm_inode_setattr - prevent updating an invalid EVM extended attribute
469 * @dentry: pointer to the affected dentry
Matthew Garrett50b97742017-11-07 07:17:42 -0800470 *
471 * Permit update of file attributes when files have a valid EVM signature,
472 * except in the case of them having an immutable portable signature.
Mimi Zohar817b54a2011-05-13 12:53:38 -0400473 */
474int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
475{
476 unsigned int ia_valid = attr->ia_valid;
477 enum integrity_status evm_status;
478
Matthew Garrettae1ba162017-11-07 07:18:35 -0800479 /* Policy permits modification of the protected attrs even though
480 * there's no HMAC key loaded
481 */
482 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
483 return 0;
484
Mimi Zohara924ce02011-08-11 01:22:30 -0400485 if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
Mimi Zohar817b54a2011-05-13 12:53:38 -0400486 return 0;
487 evm_status = evm_verify_current_integrity(dentry);
Mimi Zohar566be592011-08-22 09:14:18 -0400488 if ((evm_status == INTEGRITY_PASS) ||
489 (evm_status == INTEGRITY_NOXATTRS))
490 return 0;
David Howellsc6f493d2015-03-17 22:26:22 +0000491 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500492 dentry->d_name.name, "appraise_metadata",
493 integrity_status_msg[evm_status], -EPERM, 0);
Mimi Zohar566be592011-08-22 09:14:18 -0400494 return -EPERM;
Mimi Zohar817b54a2011-05-13 12:53:38 -0400495}
496
497/**
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400498 * evm_inode_post_setattr - update 'security.evm' after modifying metadata
499 * @dentry: pointer to the affected dentry
500 * @ia_valid: for the UID and GID status
501 *
502 * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
503 * changes.
504 *
505 * This function is called from notify_change(), which expects the caller
506 * to lock the inode's i_mutex.
507 */
508void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
509{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800510 if (!evm_key_loaded())
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400511 return;
512
513 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
514 evm_update_evmxattr(dentry, NULL, NULL, 0);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400515}
516
Mimi Zoharcb7231802011-03-09 14:40:44 -0500517/*
518 * evm_inode_init_security - initializes security.evm
519 */
520int evm_inode_init_security(struct inode *inode,
521 const struct xattr *lsm_xattr,
522 struct xattr *evm_xattr)
523{
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300524 struct evm_xattr *xattr_data;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500525 int rc;
526
Matthew Garrettae1ba162017-11-07 07:18:35 -0800527 if (!evm_key_loaded() || !evm_protected_xattr(lsm_xattr->name))
Mimi Zohar5a4730b2011-08-11 00:22:52 -0400528 return 0;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500529
530 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
531 if (!xattr_data)
532 return -ENOMEM;
533
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300534 xattr_data->data.type = EVM_XATTR_HMAC;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500535 rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
536 if (rc < 0)
537 goto out;
538
539 evm_xattr->value = xattr_data;
540 evm_xattr->value_len = sizeof(*xattr_data);
Tetsuo Handa95489062013-07-25 05:44:02 +0900541 evm_xattr->name = XATTR_EVM_SUFFIX;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500542 return 0;
543out:
544 kfree(xattr_data);
545 return rc;
546}
547EXPORT_SYMBOL_GPL(evm_inode_init_security);
548
Dmitry Kasatkin2ce523e2015-10-22 21:26:21 +0300549#ifdef CONFIG_EVM_LOAD_X509
550void __init evm_load_x509(void)
551{
Dmitry Kasatkin26ddabf2015-10-22 21:26:26 +0300552 int rc;
553
554 rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH);
555 if (!rc)
556 evm_initialized |= EVM_INIT_X509;
Dmitry Kasatkin2ce523e2015-10-22 21:26:21 +0300557}
558#endif
559
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400560static int __init init_evm(void)
561{
562 int error;
Matthew Garrett21af7662018-05-11 16:12:35 -0700563 struct list_head *pos, *q;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400564
Dmitry Kasatkind3b33672014-03-28 14:31:04 +0200565 evm_init_config();
566
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +0300567 error = integrity_init_keyring(INTEGRITY_KEYRING_EVM);
568 if (error)
Matthew Garrett21af7662018-05-11 16:12:35 -0700569 goto error;
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +0300570
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400571 error = evm_init_secfs();
572 if (error < 0) {
Joe Perches20ee4512014-02-24 13:59:56 -0800573 pr_info("Error registering secfs\n");
Matthew Garrett21af7662018-05-11 16:12:35 -0700574 goto error;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400575 }
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300576
Matthew Garrett21af7662018-05-11 16:12:35 -0700577error:
578 if (error != 0) {
579 if (!list_empty(&evm_config_xattrnames)) {
YueHaibingc8b37522018-12-15 10:06:10 +0000580 list_for_each_safe(pos, q, &evm_config_xattrnames)
Matthew Garrett21af7662018-05-11 16:12:35 -0700581 list_del(pos);
Matthew Garrett21af7662018-05-11 16:12:35 -0700582 }
583 }
584
585 return error;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400586}
587
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400588late_initcall(init_evm);