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