blob: b6d9f14bc23477caa2f735172a89e6a059d1194d [file] [log] [blame]
Mimi Zohar66dbc3252011-03-15 16:12:09 -04001/*
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
Joe Perches20ee4512014-02-24 13:59:56 -080017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Paul Gortmaker3aafb1f2018-12-09 15:36:32 -050019#include <linux/init.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040020#include <linux/crypto.h>
Mimi Zohar9b97b6c2013-02-21 09:31:22 -050021#include <linux/audit.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040022#include <linux/xattr.h>
23#include <linux/integrity.h>
Mimi Zohar3e1be522011-03-09 14:38:26 -050024#include <linux/evm.h>
Ingo Molnar50d34392017-02-05 16:03:58 +010025#include <linux/magic.h>
26
Dmitry Kasatkind46eb362011-03-09 15:07:36 -050027#include <crypto/hash.h>
Matthew Garrett5feeb612018-06-08 14:57:43 -070028#include <crypto/hash_info.h>
Ryan Ware613317b2016-02-11 15:58:44 -080029#include <crypto/algapi.h>
Mimi Zohar66dbc3252011-03-15 16:12:09 -040030#include "evm.h"
31
32int evm_initialized;
33
HernĂ¡n Gonzalez17d7b0a2018-02-27 19:17:00 -030034static const char * const integrity_status_msg[] = {
Matthew Garrett50b97742017-11-07 07:17:42 -080035 "pass", "pass_immutable", "fail", "no_label", "no_xattrs", "unknown"
Mimi Zohar9b97b6c2013-02-21 09:31:22 -050036};
Dmitry Kasatkind3b33672014-03-28 14:31:04 +020037int evm_hmac_attrs;
Mimi Zohar66dbc3252011-03-15 16:12:09 -040038
Matthew Garrettfa516b62018-05-15 10:38:26 -070039static struct xattr_list evm_config_default_xattrnames[] = {
Mimi Zohar66dbc3252011-03-15 16:12:09 -040040#ifdef CONFIG_SECURITY_SELINUX
Matthew Garrett21af7662018-05-11 16:12:35 -070041 {.name = XATTR_NAME_SELINUX},
Mimi Zohar66dbc3252011-03-15 16:12:09 -040042#endif
43#ifdef CONFIG_SECURITY_SMACK
Matthew Garrett21af7662018-05-11 16:12:35 -070044 {.name = XATTR_NAME_SMACK},
Dmitry Kasatkin3e38df52014-03-28 14:31:14 +020045#ifdef CONFIG_EVM_EXTRA_SMACK_XATTRS
Matthew Garrett21af7662018-05-11 16:12:35 -070046 {.name = XATTR_NAME_SMACKEXEC},
47 {.name = XATTR_NAME_SMACKTRANSMUTE},
48 {.name = XATTR_NAME_SMACKMMAP},
Dmitry Kasatkin3e38df52014-03-28 14:31:14 +020049#endif
Mimi Zohar66dbc3252011-03-15 16:12:09 -040050#endif
Matthew Garrett096b8542017-10-13 15:09:25 -070051#ifdef CONFIG_SECURITY_APPARMOR
Matthew Garrett21af7662018-05-11 16:12:35 -070052 {.name = XATTR_NAME_APPARMOR},
Matthew Garrett096b8542017-10-13 15:09:25 -070053#endif
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050054#ifdef CONFIG_IMA_APPRAISE
Matthew Garrett21af7662018-05-11 16:12:35 -070055 {.name = XATTR_NAME_IMA},
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050056#endif
Matthew Garrett21af7662018-05-11 16:12:35 -070057 {.name = XATTR_NAME_CAPS},
Mimi Zohar66dbc3252011-03-15 16:12:09 -040058};
59
Matthew Garrett21af7662018-05-11 16:12:35 -070060LIST_HEAD(evm_config_xattrnames);
61
Mimi Zohar7102ebc2011-05-12 18:33:20 -040062static int evm_fixmode;
63static 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
Dmitry Kasatkind3b33672014-03-28 14:31:04 +020071static void __init evm_init_config(void)
72{
Matthew Garrett21af7662018-05-11 16:12:35 -070073 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
Dmitry Kasatkind3b33672014-03-28 14:31:04 +020084#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
Matthew Garrettae1ba162017-11-07 07:18:35 -080090static bool evm_key_loaded(void)
91{
92 return (bool)(evm_initialized & EVM_KEY_MASK);
93}
94
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030095static int evm_find_protected_xattrs(struct dentry *dentry)
96{
David Howellsc6f493d2015-03-17 22:26:22 +000097 struct inode *inode = d_backing_inode(dentry);
Matthew Garrett21af7662018-05-11 16:12:35 -070098 struct xattr_list *xattr;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +030099 int error;
100 int count = 0;
101
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200102 if (!(inode->i_opflags & IOP_XATTR))
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300103 return -EOPNOTSUPP;
104
Matthew Garrettfa516b62018-05-15 10:38:26 -0700105 list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
Matthew Garrett21af7662018-05-11 16:12:35 -0700106 error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300107 if (error < 0) {
108 if (error == -ENODATA)
109 continue;
110 return error;
111 }
112 count++;
113 }
114
115 return count;
116}
117
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400118/*
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
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400122 * 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.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400128 *
129 * Returns integrity status
130 */
131static 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{
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300137 struct evm_ima_xattr_data *xattr_data = NULL;
Matthew Garrett5feeb612018-06-08 14:57:43 -0700138 struct signature_v2_hdr *hdr;
Mimi Zohar566be592011-08-22 09:14:18 -0400139 enum integrity_status evm_status = INTEGRITY_PASS;
Matthew Garrett5feeb612018-06-08 14:57:43 -0700140 struct evm_digest digest;
Sascha Hauer70946c4a2018-03-01 13:38:45 +0100141 struct inode *inode;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300142 int rc, xattr_len;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400143
Matthew Garrett50b97742017-11-07 07:17:42 -0800144 if (iint && (iint->evm_status == INTEGRITY_PASS ||
145 iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
Dmitry Kasatkin24e01982011-05-06 11:34:17 +0300146 return iint->evm_status;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400147
Dmitry Kasatkin6d38ca012011-05-06 11:34:14 +0300148 /* if status is not PASS, try to check again - against -ENOMEM */
149
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300150 /* 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) {
Dmitry Kasatkin1f100972014-08-15 13:49:22 +0300154 evm_status = INTEGRITY_FAIL;
155 if (rc == -ENODATA) {
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300156 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 */
Dmitry Kasatkin1f100972014-08-15 13:49:22 +0300161 } else if (rc == -EOPNOTSUPP) {
162 evm_status = INTEGRITY_UNKNOWN;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300163 }
Mimi Zohar566be592011-08-22 09:14:18 -0400164 goto out;
165 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400166
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +0900167 xattr_len = rc;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300168
169 /* check value type */
170 switch (xattr_data->type) {
171 case EVM_XATTR_HMAC:
Seth Forsheeb4bfec72016-08-01 08:19:10 -0500172 if (xattr_len != sizeof(struct evm_ima_xattr_data)) {
173 evm_status = INTEGRITY_FAIL;
174 goto out;
175 }
Matthew Garrett5feeb612018-06-08 14:57:43 -0700176
177 digest.hdr.algo = HASH_ALGO_SHA1;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300178 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700179 xattr_value_len, &digest);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300180 if (rc)
181 break;
Matthew Garrett5feeb612018-06-08 14:57:43 -0700182 rc = crypto_memneq(xattr_data->digest, digest.digest,
183 SHA1_DIGEST_SIZE);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300184 if (rc)
185 rc = -EINVAL;
186 break;
187 case EVM_IMA_XATTR_DIGSIG:
Matthew Garrett50b97742017-11-07 07:17:42 -0800188 case EVM_XATTR_PORTABLE_DIGSIG:
Matthew Garrett5feeb612018-06-08 14:57:43 -0700189 hdr = (struct signature_v2_hdr *)xattr_data;
190 digest.hdr.algo = hdr->hash_algo;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300191 rc = evm_calc_hash(dentry, xattr_name, xattr_value,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700192 xattr_value_len, xattr_data->type, &digest);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300193 if (rc)
194 break;
195 rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +0900196 (const char *)xattr_data, xattr_len,
Matthew Garrett5feeb612018-06-08 14:57:43 -0700197 digest.digest, digest.hdr.length);
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300198 if (!rc) {
Sascha Hauer70946c4a2018-03-01 13:38:45 +0100199 inode = d_backing_inode(dentry);
200
Matthew Garrett50b97742017-11-07 07:17:42 -0800201 if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
202 if (iint)
203 iint->flags |= EVM_IMMUTABLE_DIGSIG;
204 evm_status = INTEGRITY_PASS_IMMUTABLE;
Sascha Hauer70946c4a2018-03-01 13:38:45 +0100205 } else if (!IS_RDONLY(inode) &&
206 !(inode->i_sb->s_readonly_remount) &&
207 !IS_IMMUTABLE(inode)) {
Dmitry Kasatkinc2baec72014-10-01 21:43:08 +0300208 evm_update_evmxattr(dentry, xattr_name,
209 xattr_value,
210 xattr_value_len);
Matthew Garrett50b97742017-11-07 07:17:42 -0800211 }
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300212 }
213 break;
214 default:
215 rc = -EINVAL;
216 break;
217 }
218
219 if (rc)
220 evm_status = (rc == -ENODATA) ?
221 INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400222out:
223 if (iint)
224 iint->evm_status = evm_status;
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300225 kfree(xattr_data);
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400226 return evm_status;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400227}
228
229static int evm_protected_xattr(const char *req_xattr_name)
230{
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400231 int namelen;
232 int found = 0;
Matthew Garrett21af7662018-05-11 16:12:35 -0700233 struct xattr_list *xattr;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400234
235 namelen = strlen(req_xattr_name);
Matthew Garrettfa516b62018-05-15 10:38:26 -0700236 list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
Matthew Garrett21af7662018-05-11 16:12:35 -0700237 if ((strlen(xattr->name) == namelen)
238 && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400239 found = 1;
240 break;
241 }
Mimi Zoharcb7231802011-03-09 14:40:44 -0500242 if (strncmp(req_xattr_name,
Matthew Garrett21af7662018-05-11 16:12:35 -0700243 xattr->name + XATTR_SECURITY_PREFIX_LEN,
Mimi Zoharcb7231802011-03-09 14:40:44 -0500244 strlen(req_xattr_name)) == 0) {
245 found = 1;
246 break;
247 }
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400248 }
Matthew Garrett21af7662018-05-11 16:12:35 -0700249
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400250 return found;
251}
252
253/**
254 * evm_verifyxattr - verify the integrity of the requested xattr
255 * @dentry: object of the verify xattr
256 * @xattr_name: requested xattr
257 * @xattr_value: requested xattr value
258 * @xattr_value_len: requested xattr value length
259 *
260 * Calculate the HMAC for the given dentry and verify it against the stored
261 * security.evm xattr. For performance, use the xattr value and length
262 * previously retrieved to calculate the HMAC.
263 *
264 * Returns the xattr integrity status.
265 *
266 * This function requires the caller to lock the inode's i_mutex before it
267 * is executed.
268 */
269enum integrity_status evm_verifyxattr(struct dentry *dentry,
270 const char *xattr_name,
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300271 void *xattr_value, size_t xattr_value_len,
272 struct integrity_iint_cache *iint)
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400273{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800274 if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400275 return INTEGRITY_UNKNOWN;
276
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300277 if (!iint) {
David Howellsc6f493d2015-03-17 22:26:22 +0000278 iint = integrity_iint_find(d_backing_inode(dentry));
Dmitry Kasatkin2960e6c2011-05-06 11:34:13 +0300279 if (!iint)
280 return INTEGRITY_UNKNOWN;
281 }
282 return evm_verify_hmac(dentry, xattr_name, xattr_value,
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400283 xattr_value_len, iint);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400284}
285EXPORT_SYMBOL_GPL(evm_verifyxattr);
286
287/*
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400288 * evm_verify_current_integrity - verify the dentry's metadata integrity
289 * @dentry: pointer to the affected dentry
290 *
291 * Verify and return the dentry's metadata integrity. The exceptions are
292 * before EVM is initialized or in 'fix' mode.
293 */
294static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
295{
David Howellsc6f493d2015-03-17 22:26:22 +0000296 struct inode *inode = d_backing_inode(dentry);
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400297
Matthew Garrettae1ba162017-11-07 07:18:35 -0800298 if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400299 return 0;
300 return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
301}
302
Mimi Zohara924ce02011-08-11 01:22:30 -0400303/*
304 * evm_protect_xattr - protect the EVM extended attribute
305 *
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400306 * Prevent security.evm from being modified or removed without the
307 * necessary permissions or when the existing value is invalid.
308 *
309 * The posix xattr acls are 'system' prefixed, which normally would not
310 * affect security.evm. An interesting side affect of writing posix xattr
311 * acls is their modifying of the i_mode, which is included in security.evm.
312 * For posix xattr acls only, permit security.evm, even if it currently
Matthew Garrett50b97742017-11-07 07:17:42 -0800313 * doesn't exist, to be updated unless the EVM signature is immutable.
Mimi Zohara924ce02011-08-11 01:22:30 -0400314 */
315static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
316 const void *xattr_value, size_t xattr_value_len)
317{
318 enum integrity_status evm_status;
319
320 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
321 if (!capable(CAP_SYS_ADMIN))
322 return -EPERM;
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400323 } else if (!evm_protected_xattr(xattr_name)) {
324 if (!posix_xattr_acl(xattr_name))
325 return 0;
326 evm_status = evm_verify_current_integrity(dentry);
327 if ((evm_status == INTEGRITY_PASS) ||
Mimi Zohar566be592011-08-22 09:14:18 -0400328 (evm_status == INTEGRITY_NOXATTRS))
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400329 return 0;
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500330 goto out;
Mimi Zoharbf6d0f52011-08-18 18:07:44 -0400331 }
Matthew Garrettae1ba162017-11-07 07:18:35 -0800332
Mimi Zohara924ce02011-08-11 01:22:30 -0400333 evm_status = evm_verify_current_integrity(dentry);
Dmitry Kasatkin3dcbad52014-09-02 16:31:43 +0300334 if (evm_status == INTEGRITY_NOXATTRS) {
335 struct integrity_iint_cache *iint;
336
David Howellsc6f493d2015-03-17 22:26:22 +0000337 iint = integrity_iint_find(d_backing_inode(dentry));
Dmitry Kasatkin3dcbad52014-09-02 16:31:43 +0300338 if (iint && (iint->flags & IMA_NEW_FILE))
339 return 0;
Mimi Zohar5101a182015-04-21 13:59:31 -0400340
341 /* exception for pseudo filesystems */
Al Virofc640052016-04-10 01:33:30 -0400342 if (dentry->d_sb->s_magic == TMPFS_MAGIC
343 || dentry->d_sb->s_magic == SYSFS_MAGIC)
Mimi Zohar5101a182015-04-21 13:59:31 -0400344 return 0;
345
346 integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
347 dentry->d_inode, dentry->d_name.name,
348 "update_metadata",
349 integrity_status_msg[evm_status],
350 -EPERM, 0);
Dmitry Kasatkin3dcbad52014-09-02 16:31:43 +0300351 }
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500352out:
353 if (evm_status != INTEGRITY_PASS)
David Howellsc6f493d2015-03-17 22:26:22 +0000354 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500355 dentry->d_name.name, "appraise_metadata",
356 integrity_status_msg[evm_status],
357 -EPERM, 0);
Mimi Zohara924ce02011-08-11 01:22:30 -0400358 return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
359}
360
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400361/**
362 * evm_inode_setxattr - protect the EVM extended attribute
363 * @dentry: pointer to the affected dentry
364 * @xattr_name: pointer to the affected extended attribute name
365 * @xattr_value: pointer to the new extended attribute value
366 * @xattr_value_len: pointer to the new extended attribute value length
367 *
Mimi Zohar2fb1c9a2014-05-11 00:05:23 -0400368 * Before allowing the 'security.evm' protected xattr to be updated,
369 * verify the existing value is valid. As only the kernel should have
370 * access to the EVM encrypted key needed to calculate the HMAC, prevent
371 * userspace from writing HMAC value. Writing 'security.evm' requires
372 * requires CAP_SYS_ADMIN privileges.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400373 */
374int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
375 const void *xattr_value, size_t xattr_value_len)
376{
Mimi Zohar2fb1c9a2014-05-11 00:05:23 -0400377 const struct evm_ima_xattr_data *xattr_data = xattr_value;
378
Matthew Garrettae1ba162017-11-07 07:18:35 -0800379 /* Policy permits modification of the protected xattrs even though
380 * there's no HMAC key loaded
381 */
382 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
383 return 0;
384
Dmitry Kasatkin3b1deef2014-10-28 14:28:49 +0200385 if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
386 if (!xattr_value_len)
387 return -EINVAL;
Matthew Garrett50b97742017-11-07 07:17:42 -0800388 if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
389 xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
Dmitry Kasatkin3b1deef2014-10-28 14:28:49 +0200390 return -EPERM;
391 }
Mimi Zohara924ce02011-08-11 01:22:30 -0400392 return evm_protect_xattr(dentry, xattr_name, xattr_value,
393 xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400394}
395
396/**
397 * evm_inode_removexattr - protect the EVM extended attribute
398 * @dentry: pointer to the affected dentry
399 * @xattr_name: pointer to the affected extended attribute name
400 *
Mimi Zohar7102ebc2011-05-12 18:33:20 -0400401 * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
402 * the current value is valid.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400403 */
404int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
405{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800406 /* Policy permits modification of the protected xattrs even though
407 * there's no HMAC key loaded
408 */
409 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
410 return 0;
411
Mimi Zohara924ce02011-08-11 01:22:30 -0400412 return evm_protect_xattr(dentry, xattr_name, NULL, 0);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400413}
414
Dmitry Kasatkin523b74b2015-10-22 21:26:42 +0300415static void evm_reset_status(struct inode *inode)
416{
417 struct integrity_iint_cache *iint;
418
419 iint = integrity_iint_find(inode);
420 if (iint)
421 iint->evm_status = INTEGRITY_UNKNOWN;
422}
423
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400424/**
425 * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
426 * @dentry: pointer to the affected dentry
427 * @xattr_name: pointer to the affected extended attribute name
428 * @xattr_value: pointer to the new extended attribute value
429 * @xattr_value_len: pointer to the new extended attribute value length
430 *
431 * Update the HMAC stored in 'security.evm' to reflect the change.
432 *
433 * No need to take the i_mutex lock here, as this function is called from
434 * __vfs_setxattr_noperm(). The caller of which has taken the inode's
435 * i_mutex lock.
436 */
437void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
438 const void *xattr_value, size_t xattr_value_len)
439{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800440 if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name)
441 && !posix_xattr_acl(xattr_name)))
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400442 return;
443
Dmitry Kasatkin523b74b2015-10-22 21:26:42 +0300444 evm_reset_status(dentry->d_inode);
445
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400446 evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400447}
448
449/**
450 * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
451 * @dentry: pointer to the affected dentry
452 * @xattr_name: pointer to the affected extended attribute name
453 *
454 * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
Dmitry Kasatkin7c51bb002014-11-20 16:31:01 +0200455 *
456 * No need to take the i_mutex lock here, as this function is called from
457 * vfs_removexattr() which takes the i_mutex.
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400458 */
459void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
460{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800461 if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400462 return;
463
Dmitry Kasatkin523b74b2015-10-22 21:26:42 +0300464 evm_reset_status(dentry->d_inode);
465
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400466 evm_update_evmxattr(dentry, xattr_name, NULL, 0);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400467}
468
469/**
Mimi Zohar817b54a2011-05-13 12:53:38 -0400470 * evm_inode_setattr - prevent updating an invalid EVM extended attribute
471 * @dentry: pointer to the affected dentry
Matthew Garrett50b97742017-11-07 07:17:42 -0800472 *
473 * Permit update of file attributes when files have a valid EVM signature,
474 * except in the case of them having an immutable portable signature.
Mimi Zohar817b54a2011-05-13 12:53:38 -0400475 */
476int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
477{
478 unsigned int ia_valid = attr->ia_valid;
479 enum integrity_status evm_status;
480
Matthew Garrettae1ba162017-11-07 07:18:35 -0800481 /* Policy permits modification of the protected attrs even though
482 * there's no HMAC key loaded
483 */
484 if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
485 return 0;
486
Mimi Zohara924ce02011-08-11 01:22:30 -0400487 if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
Mimi Zohar817b54a2011-05-13 12:53:38 -0400488 return 0;
489 evm_status = evm_verify_current_integrity(dentry);
Mimi Zohar566be592011-08-22 09:14:18 -0400490 if ((evm_status == INTEGRITY_PASS) ||
491 (evm_status == INTEGRITY_NOXATTRS))
492 return 0;
David Howellsc6f493d2015-03-17 22:26:22 +0000493 integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
Mimi Zohar9b97b6c2013-02-21 09:31:22 -0500494 dentry->d_name.name, "appraise_metadata",
495 integrity_status_msg[evm_status], -EPERM, 0);
Mimi Zohar566be592011-08-22 09:14:18 -0400496 return -EPERM;
Mimi Zohar817b54a2011-05-13 12:53:38 -0400497}
498
499/**
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400500 * evm_inode_post_setattr - update 'security.evm' after modifying metadata
501 * @dentry: pointer to the affected dentry
502 * @ia_valid: for the UID and GID status
503 *
504 * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
505 * changes.
506 *
507 * This function is called from notify_change(), which expects the caller
508 * to lock the inode's i_mutex.
509 */
510void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
511{
Matthew Garrettae1ba162017-11-07 07:18:35 -0800512 if (!evm_key_loaded())
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400513 return;
514
515 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
516 evm_update_evmxattr(dentry, NULL, NULL, 0);
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400517}
518
Mimi Zoharcb7231802011-03-09 14:40:44 -0500519/*
520 * evm_inode_init_security - initializes security.evm
521 */
522int evm_inode_init_security(struct inode *inode,
523 const struct xattr *lsm_xattr,
524 struct xattr *evm_xattr)
525{
526 struct evm_ima_xattr_data *xattr_data;
527 int rc;
528
Matthew Garrettae1ba162017-11-07 07:18:35 -0800529 if (!evm_key_loaded() || !evm_protected_xattr(lsm_xattr->name))
Mimi Zohar5a4730b2011-08-11 00:22:52 -0400530 return 0;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500531
532 xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
533 if (!xattr_data)
534 return -ENOMEM;
535
536 xattr_data->type = EVM_XATTR_HMAC;
537 rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
538 if (rc < 0)
539 goto out;
540
541 evm_xattr->value = xattr_data;
542 evm_xattr->value_len = sizeof(*xattr_data);
Tetsuo Handa95489062013-07-25 05:44:02 +0900543 evm_xattr->name = XATTR_EVM_SUFFIX;
Mimi Zoharcb7231802011-03-09 14:40:44 -0500544 return 0;
545out:
546 kfree(xattr_data);
547 return rc;
548}
549EXPORT_SYMBOL_GPL(evm_inode_init_security);
550
Dmitry Kasatkin2ce523e2015-10-22 21:26:21 +0300551#ifdef CONFIG_EVM_LOAD_X509
552void __init evm_load_x509(void)
553{
Dmitry Kasatkin26ddabf2015-10-22 21:26:26 +0300554 int rc;
555
556 rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH);
557 if (!rc)
558 evm_initialized |= EVM_INIT_X509;
Dmitry Kasatkin2ce523e2015-10-22 21:26:21 +0300559}
560#endif
561
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400562static int __init init_evm(void)
563{
564 int error;
Matthew Garrett21af7662018-05-11 16:12:35 -0700565 struct list_head *pos, *q;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400566
Dmitry Kasatkind3b33672014-03-28 14:31:04 +0200567 evm_init_config();
568
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +0300569 error = integrity_init_keyring(INTEGRITY_KEYRING_EVM);
570 if (error)
Matthew Garrett21af7662018-05-11 16:12:35 -0700571 goto error;
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +0300572
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400573 error = evm_init_secfs();
574 if (error < 0) {
Joe Perches20ee4512014-02-24 13:59:56 -0800575 pr_info("Error registering secfs\n");
Matthew Garrett21af7662018-05-11 16:12:35 -0700576 goto error;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400577 }
Dmitry Kasatkin15647eb2011-09-01 14:41:40 +0300578
Matthew Garrett21af7662018-05-11 16:12:35 -0700579error:
580 if (error != 0) {
581 if (!list_empty(&evm_config_xattrnames)) {
YueHaibingc8b37522018-12-15 10:06:10 +0000582 list_for_each_safe(pos, q, &evm_config_xattrnames)
Matthew Garrett21af7662018-05-11 16:12:35 -0700583 list_del(pos);
Matthew Garrett21af7662018-05-11 16:12:35 -0700584 }
585 }
586
587 return error;
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400588}
589
Mimi Zohar66dbc3252011-03-15 16:12:09 -0400590late_initcall(init_evm);