blob: c4e9a70a0be7a0b476ac360eb4f71287a21b265d [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
Austin Kimc7400e22021-10-28 12:26:42 +010057static int evm_fixmode __ro_after_init;
Mimi Zohar7102ebc2011-05-12 18:33:20 -040058static 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
Randy Dunlapd788ad42022-02-22 13:45:18 -080065 return 1;
Mimi Zohar7102ebc2011-05-12 18:33:20 -040066}
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) {
Mark Salyzyn3484eba2019-11-04 08:57:10 -0800104 error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0,
105 XATTR_NOSECURITY);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300106 if (error < 0) {
107 if (error == -ENODATA)
108 continue;
109 return error;
110 }
111 count++;
112 }
113
114 return count;
115}
116
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400117/*
118 * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
119 *
120 * Compute the HMAC on the dentry's protected set of extended attributes
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400121 * and compare it against the stored security.evm xattr.
122 *
123 * For performance:
124 * - use the previoulsy retrieved xattr value and length to calculate the
125 * HMAC.)
126 * - cache the verification result in the iint, when available.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400127 *
128 * Returns integrity status
129 */
130static enum integrity_status evm_verify_hmac(struct dentry *dentry,
131 const char *xattr_name,
132 char *xattr_value,
133 size_t xattr_value_len,
134 struct integrity_iint_cache *iint)
135{
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300136 struct evm_ima_xattr_data *xattr_data = NULL;
Matthew Garrett5feeb612018-06-08 14:57:43 -0700137 struct signature_v2_hdr *hdr;
Mimi Zohar566be592011-08-22 09:14:18 -0400138 enum integrity_status evm_status = INTEGRITY_PASS;
Matthew Garrett5feeb612018-06-08 14:57:43 -0700139 struct evm_digest digest;
Sascha Hauer70946c4a2018-03-01 13:38:45 +0100140 struct inode *inode;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300141 int rc, xattr_len;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400142
Matthew Garrett50b97742017-11-07 07:17:42 -0800143 if (iint && (iint->evm_status == INTEGRITY_PASS ||
144 iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
Dmitry Kasatkin24e01982011-05-06 11:34:17 +0300145 return iint->evm_status;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400146
Dmitry Kasatkin6d38ca012011-05-06 11:34:14 +0300147 /* if status is not PASS, try to check again - against -ENOMEM */
148
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300149 /* first need to know the sig type */
150 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
151 GFP_NOFS);
152 if (rc <= 0) {
Dmitry Kasatkin1f100972014-08-15 13:49:22 +0300153 evm_status = INTEGRITY_FAIL;
154 if (rc == -ENODATA) {
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300155 rc = evm_find_protected_xattrs(dentry);
156 if (rc > 0)
157 evm_status = INTEGRITY_NOLABEL;
158 else if (rc == 0)
159 evm_status = INTEGRITY_NOXATTRS; /* new file */
Dmitry Kasatkin1f100972014-08-15 13:49:22 +0300160 } else if (rc == -EOPNOTSUPP) {
161 evm_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300162 }
Mimi Zohar566be592011-08-22 09:14:18 -0400163 goto out;
164 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400165
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +0900166 xattr_len = rc;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300167
168 /* check value type */
169 switch (xattr_data->type) {
170 case EVM_XATTR_HMAC:
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300171 if (xattr_len != sizeof(struct evm_xattr)) {
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500172 evm_status = INTEGRITY_FAIL;
173 goto out;
174 }
Matthew Garrett5feeb612018-06-08 14:57:43 -0700175
176 digest.hdr.algo = HASH_ALGO_SHA1;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300177 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700178 xattr_value_len, &digest);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300179 if (rc)
180 break;
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300181 rc = crypto_memneq(xattr_data->data, digest.digest,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700182 SHA1_DIGEST_SIZE);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300183 if (rc)
184 rc = -EINVAL;
185 break;
186 case EVM_IMA_XATTR_DIGSIG:
Matthew Garrett50b97742017-11-07 07:17:42 -0800187 case EVM_XATTR_PORTABLE_DIGSIG:
Roberto Sassu455b6c92020-09-04 11:23:30 +0200188 /* accept xattr with non-empty signature field */
189 if (xattr_len <= sizeof(struct signature_v2_hdr)) {
190 evm_status = INTEGRITY_FAIL;
191 goto out;
192 }
193
Matthew Garrett5feeb612018-06-08 14:57:43 -0700194 hdr = (struct signature_v2_hdr *)xattr_data;
195 digest.hdr.algo = hdr->hash_algo;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300196 rc = evm_calc_hash(dentry, xattr_name, xattr_value,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700197 xattr_value_len, xattr_data->type, &digest);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300198 if (rc)
199 break;
200 rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +0900201 (const char *)xattr_data, xattr_len,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700202 digest.digest, digest.hdr.length);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300203 if (!rc) {
Sascha Hauer70946c4a2018-03-01 13:38:45 +0100204 inode = d_backing_inode(dentry);
205
Matthew Garrett50b97742017-11-07 07:17:42 -0800206 if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
207 if (iint)
208 iint->flags |= EVM_IMMUTABLE_DIGSIG;
209 evm_status = INTEGRITY_PASS_IMMUTABLE;
Sascha Hauer70946c4a2018-03-01 13:38:45 +0100210 } else if (!IS_RDONLY(inode) &&
211 !(inode->i_sb->s_readonly_remount) &&
212 !IS_IMMUTABLE(inode)) {
Dmitry Kasatkinc2baec72014-10-01 21:43:08 +0300213 evm_update_evmxattr(dentry, xattr_name,
214 xattr_value,
215 xattr_value_len);
Matthew Garrett50b97742017-11-07 07:17:42 -0800216 }
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300217 }
218 break;
219 default:
220 rc = -EINVAL;
221 break;
222 }
223
224 if (rc)
225 evm_status = (rc == -ENODATA) ?
226 INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400227out:
228 if (iint)
229 iint->evm_status = evm_status;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300230 kfree(xattr_data);
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400231 return evm_status;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400232}
233
234static int evm_protected_xattr(const char *req_xattr_name)
235{
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400236 int namelen;
237 int found = 0;
Matthew Garrett21af7662018-05-11 16:12:35 -0700238 struct xattr_list *xattr;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400239
240 namelen = strlen(req_xattr_name);
Madhuparna Bhowmik770f6052020-04-30 21:32:05 +0530241 list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
Matthew Garrett21af7662018-05-11 16:12:35 -0700242 if ((strlen(xattr->name) == namelen)
243 && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400244 found = 1;
245 break;
246 }
Mimi Zoharcb7231802011-03-09 14:40:44 -0500247 if (strncmp(req_xattr_name,
Matthew Garrett21af7662018-05-11 16:12:35 -0700248 xattr->name + XATTR_SECURITY_PREFIX_LEN,
Mimi Zoharcb7231802011-03-09 14:40:44 -0500249 strlen(req_xattr_name)) == 0) {
250 found = 1;
251 break;
252 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400253 }
Matthew Garrett21af7662018-05-11 16:12:35 -0700254
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400255 return found;
256}
257
258/**
259 * evm_verifyxattr - verify the integrity of the requested xattr
260 * @dentry: object of the verify xattr
261 * @xattr_name: requested xattr
262 * @xattr_value: requested xattr value
263 * @xattr_value_len: requested xattr value length
264 *
265 * Calculate the HMAC for the given dentry and verify it against the stored
266 * security.evm xattr. For performance, use the xattr value and length
267 * previously retrieved to calculate the HMAC.
268 *
269 * Returns the xattr integrity status.
270 *
271 * This function requires the caller to lock the inode's i_mutex before it
272 * is executed.
273 */
274enum integrity_status evm_verifyxattr(struct dentry *dentry,
275 const char *xattr_name,
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300276 void *xattr_value, size_t xattr_value_len,
277 struct integrity_iint_cache *iint)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400278{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800279 if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400280 return INTEGRITY_UNKNOWN;
281
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300282 if (!iint) {
David Howellsc6f493d2015-03-17 22:26:22 +0000283 iint = integrity_iint_find(d_backing_inode(dentry));
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300284 if (!iint)
285 return INTEGRITY_UNKNOWN;
286 }
287 return evm_verify_hmac(dentry, xattr_name, xattr_value,
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400288 xattr_value_len, iint);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400289}
290EXPORT_SYMBOL_GPL(evm_verifyxattr);
291
292/*
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400293 * evm_verify_current_integrity - verify the dentry's metadata integrity
294 * @dentry: pointer to the affected dentry
295 *
296 * Verify and return the dentry's metadata integrity. The exceptions are
297 * before EVM is initialized or in 'fix' mode.
298 */
299static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
300{
David Howellsc6f493d2015-03-17 22:26:22 +0000301 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400302
Matthew Garrettae1ba162017-11-07 07:18:35 -0800303 if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400304 return 0;
305 return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
306}
307
Mimi Zohara924ce02011-08-11 01:22:30 -0400308/*
309 * evm_protect_xattr - protect the EVM extended attribute
310 *
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400311 * Prevent security.evm from being modified or removed without the
312 * necessary permissions or when the existing value is invalid.
313 *
314 * The posix xattr acls are 'system' prefixed, which normally would not
315 * affect security.evm. An interesting side affect of writing posix xattr
316 * acls is their modifying of the i_mode, which is included in security.evm.
317 * For posix xattr acls only, permit security.evm, even if it currently
Matthew Garrett50b97742017-11-07 07:17:42 -0800318 * doesn't exist, to be updated unless the EVM signature is immutable.
Mimi Zohara924ce02011-08-11 01:22:30 -0400319 */
320static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
321 const void *xattr_value, size_t xattr_value_len)
322{
323 enum integrity_status evm_status;
324
325 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
326 if (!capable(CAP_SYS_ADMIN))
327 return -EPERM;
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400328 } else if (!evm_protected_xattr(xattr_name)) {
329 if (!posix_xattr_acl(xattr_name))
330 return 0;
331 evm_status = evm_verify_current_integrity(dentry);
332 if ((evm_status == INTEGRITY_PASS) ||
Mimi Zohar566be592011-08-22 09:14:18 -0400333 (evm_status == INTEGRITY_NOXATTRS))
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400334 return 0;
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500335 goto out;
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400336 }
Matthew Garrettae1ba162017-11-07 07:18:35 -0800337
Mimi Zohara924ce02011-08-11 01:22:30 -0400338 evm_status = evm_verify_current_integrity(dentry);
Dmitry Kasatkin3dcbad52014-09-02 16:31:43 +0300339 if (evm_status == INTEGRITY_NOXATTRS) {
340 struct integrity_iint_cache *iint;
341
David Howellsc6f493d2015-03-17 22:26:22 +0000342 iint = integrity_iint_find(d_backing_inode(dentry));
Dmitry Kasatkin3dcbad52014-09-02 16:31:43 +0300343 if (iint && (iint->flags & IMA_NEW_FILE))
344 return 0;
Mimi Zohar5101a182015-04-21 13:59:31 -0400345
346 /* exception for pseudo filesystems */
Al Virofc640052016-04-10 01:33:30 -0400347 if (dentry->d_sb->s_magic == TMPFS_MAGIC
348 || dentry->d_sb->s_magic == SYSFS_MAGIC)
Mimi Zohar5101a182015-04-21 13:59:31 -0400349 return 0;
350
351 integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
352 dentry->d_inode, dentry->d_name.name,
353 "update_metadata",
354 integrity_status_msg[evm_status],
355 -EPERM, 0);
Dmitry Kasatkin3dcbad52014-09-02 16:31:43 +0300356 }
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500357out:
358 if (evm_status != INTEGRITY_PASS)
David Howellsc6f493d2015-03-17 22:26:22 +0000359 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500360 dentry->d_name.name, "appraise_metadata",
361 integrity_status_msg[evm_status],
362 -EPERM, 0);
Mimi Zohara924ce02011-08-11 01:22:30 -0400363 return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
364}
365
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400366/**
367 * evm_inode_setxattr - protect the EVM extended attribute
368 * @dentry: pointer to the affected dentry
369 * @xattr_name: pointer to the affected extended attribute name
370 * @xattr_value: pointer to the new extended attribute value
371 * @xattr_value_len: pointer to the new extended attribute value length
372 *
Mimi Zohar2fb1c9a2014-05-11 00:05:23 -0400373 * Before allowing the 'security.evm' protected xattr to be updated,
374 * verify the existing value is valid. As only the kernel should have
375 * access to the EVM encrypted key needed to calculate the HMAC, prevent
376 * userspace from writing HMAC value. Writing 'security.evm' requires
377 * requires CAP_SYS_ADMIN privileges.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400378 */
379int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
380 const void *xattr_value, size_t xattr_value_len)
381{
Mimi Zohar2fb1c9a2014-05-11 00:05:23 -0400382 const struct evm_ima_xattr_data *xattr_data = xattr_value;
383
Matthew Garrettae1ba162017-11-07 07:18:35 -0800384 /* Policy permits modification of the protected xattrs even though
385 * there's no HMAC key loaded
386 */
387 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
388 return 0;
389
Dmitry Kasatkin3b1deef2014-10-28 14:28:49 +0200390 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
391 if (!xattr_value_len)
392 return -EINVAL;
Matthew Garrett50b97742017-11-07 07:17:42 -0800393 if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
394 xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
Dmitry Kasatkin3b1deef2014-10-28 14:28:49 +0200395 return -EPERM;
396 }
Mimi Zohara924ce02011-08-11 01:22:30 -0400397 return evm_protect_xattr(dentry, xattr_name, xattr_value,
398 xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400399}
400
401/**
402 * evm_inode_removexattr - protect the EVM extended attribute
403 * @dentry: pointer to the affected dentry
404 * @xattr_name: pointer to the affected extended attribute name
405 *
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400406 * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
407 * the current value is valid.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400408 */
409int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
410{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800411 /* Policy permits modification of the protected xattrs even though
412 * there's no HMAC key loaded
413 */
414 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
415 return 0;
416
Mimi Zohara924ce02011-08-11 01:22:30 -0400417 return evm_protect_xattr(dentry, xattr_name, NULL, 0);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400418}
419
Dmitry Kasatkin523b74b2015-10-22 21:26:42 +0300420static void evm_reset_status(struct inode *inode)
421{
422 struct integrity_iint_cache *iint;
423
424 iint = integrity_iint_find(inode);
425 if (iint)
426 iint->evm_status = INTEGRITY_UNKNOWN;
427}
428
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400429/**
430 * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
431 * @dentry: pointer to the affected dentry
432 * @xattr_name: pointer to the affected extended attribute name
433 * @xattr_value: pointer to the new extended attribute value
434 * @xattr_value_len: pointer to the new extended attribute value length
435 *
436 * Update the HMAC stored in 'security.evm' to reflect the change.
437 *
438 * No need to take the i_mutex lock here, as this function is called from
439 * __vfs_setxattr_noperm(). The caller of which has taken the inode's
440 * i_mutex lock.
441 */
442void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
443 const void *xattr_value, size_t xattr_value_len)
444{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800445 if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name)
446 && !posix_xattr_acl(xattr_name)))
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400447 return;
448
Dmitry Kasatkin523b74b2015-10-22 21:26:42 +0300449 evm_reset_status(dentry->d_inode);
450
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400451 evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400452}
453
454/**
455 * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
456 * @dentry: pointer to the affected dentry
457 * @xattr_name: pointer to the affected extended attribute name
458 *
459 * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
Dmitry Kasatkin7c51bb002014-11-20 16:31:01 +0200460 *
461 * No need to take the i_mutex lock here, as this function is called from
462 * vfs_removexattr() which takes the i_mutex.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400463 */
464void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
465{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800466 if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400467 return;
468
Dmitry Kasatkin523b74b2015-10-22 21:26:42 +0300469 evm_reset_status(dentry->d_inode);
470
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400471 evm_update_evmxattr(dentry, xattr_name, NULL, 0);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400472}
473
474/**
Mimi Zohar817b54a2011-05-13 12:53:38 -0400475 * evm_inode_setattr - prevent updating an invalid EVM extended attribute
476 * @dentry: pointer to the affected dentry
Matthew Garrett50b97742017-11-07 07:17:42 -0800477 *
478 * Permit update of file attributes when files have a valid EVM signature,
479 * except in the case of them having an immutable portable signature.
Mimi Zohar817b54a2011-05-13 12:53:38 -0400480 */
481int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
482{
483 unsigned int ia_valid = attr->ia_valid;
484 enum integrity_status evm_status;
485
Matthew Garrettae1ba162017-11-07 07:18:35 -0800486 /* Policy permits modification of the protected attrs even though
487 * there's no HMAC key loaded
488 */
489 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
490 return 0;
491
Mimi Zohara924ce02011-08-11 01:22:30 -0400492 if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
Mimi Zohar817b54a2011-05-13 12:53:38 -0400493 return 0;
494 evm_status = evm_verify_current_integrity(dentry);
Mimi Zohar566be592011-08-22 09:14:18 -0400495 if ((evm_status == INTEGRITY_PASS) ||
496 (evm_status == INTEGRITY_NOXATTRS))
497 return 0;
David Howellsc6f493d2015-03-17 22:26:22 +0000498 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500499 dentry->d_name.name, "appraise_metadata",
500 integrity_status_msg[evm_status], -EPERM, 0);
Mimi Zohar566be592011-08-22 09:14:18 -0400501 return -EPERM;
Mimi Zohar817b54a2011-05-13 12:53:38 -0400502}
503
504/**
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400505 * evm_inode_post_setattr - update 'security.evm' after modifying metadata
506 * @dentry: pointer to the affected dentry
507 * @ia_valid: for the UID and GID status
508 *
509 * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
510 * changes.
511 *
512 * This function is called from notify_change(), which expects the caller
513 * to lock the inode's i_mutex.
514 */
515void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
516{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800517 if (!evm_key_loaded())
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400518 return;
519
520 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
521 evm_update_evmxattr(dentry, NULL, NULL, 0);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400522}
523
Mimi Zoharcb7231802011-03-09 14:40:44 -0500524/*
Roberto Sassu7b84c7d2021-05-14 17:27:42 +0200525 * evm_inode_init_security - initializes security.evm HMAC value
Mimi Zoharcb7231802011-03-09 14:40:44 -0500526 */
527int evm_inode_init_security(struct inode *inode,
528 const struct xattr *lsm_xattr,
529 struct xattr *evm_xattr)
530{
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300531 struct evm_xattr *xattr_data;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500532 int rc;
533
Roberto Sassu7b84c7d2021-05-14 17:27:42 +0200534 if (!(evm_initialized & EVM_INIT_HMAC) ||
535 !evm_protected_xattr(lsm_xattr->name))
Mimi Zohar5a4730b2011-08-11 00:22:52 -0400536 return 0;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500537
538 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
539 if (!xattr_data)
540 return -ENOMEM;
541
Thiago Jung Bauermann650b29d2019-06-11 03:28:08 -0300542 xattr_data->data.type = EVM_XATTR_HMAC;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500543 rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
544 if (rc < 0)
545 goto out;
546
547 evm_xattr->value = xattr_data;
548 evm_xattr->value_len = sizeof(*xattr_data);
Tetsuo Handa95489062013-07-25 05:44:02 +0900549 evm_xattr->name = XATTR_EVM_SUFFIX;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500550 return 0;
551out:
552 kfree(xattr_data);
553 return rc;
554}
555EXPORT_SYMBOL_GPL(evm_inode_init_security);
556
Dmitry Kasatkin2ce523e2015-10-22 21:26:21 +0300557#ifdef CONFIG_EVM_LOAD_X509
558void __init evm_load_x509(void)
559{
Dmitry Kasatkin26ddabf2015-10-22 21:26:26 +0300560 int rc;
561
562 rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH);
563 if (!rc)
564 evm_initialized |= EVM_INIT_X509;
Dmitry Kasatkin2ce523e2015-10-22 21:26:21 +0300565}
566#endif
567
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400568static int __init init_evm(void)
569{
570 int error;
Matthew Garrett21af7662018-05-11 16:12:35 -0700571 struct list_head *pos, *q;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400572
Dmitry Kasatkind3b33672014-03-28 14:31:04 +0200573 evm_init_config();
574
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +0300575 error = integrity_init_keyring(INTEGRITY_KEYRING_EVM);
576 if (error)
Matthew Garrett21af7662018-05-11 16:12:35 -0700577 goto error;
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +0300578
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400579 error = evm_init_secfs();
580 if (error < 0) {
Joe Perches20ee4512014-02-24 13:59:56 -0800581 pr_info("Error registering secfs\n");
Matthew Garrett21af7662018-05-11 16:12:35 -0700582 goto error;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400583 }
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300584
Matthew Garrett21af7662018-05-11 16:12:35 -0700585error:
586 if (error != 0) {
587 if (!list_empty(&evm_config_xattrnames)) {
YueHaibingc8b37522018-12-15 10:06:10 +0000588 list_for_each_safe(pos, q, &evm_config_xattrnames)
Matthew Garrett21af7662018-05-11 16:12:35 -0700589 list_del(pos);
Matthew Garrett21af7662018-05-11 16:12:35 -0700590 }
591 }
592
593 return error;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400594}
595
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400596late_initcall(init_evm);