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, | |
e61b135f | 11 | * evm_inode_removexattr, evm_verifyxattr, and evm_inode_set_acl. |
66dbc325 MZ |
12 | */ |
13 | ||
87ac3d00 MZ |
14 | #define pr_fmt(fmt) "EVM: "fmt |
15 | ||
3aafb1fb | 16 | #include <linux/init.h> |
9b97b6cd | 17 | #include <linux/audit.h> |
66dbc325 MZ |
18 | #include <linux/xattr.h> |
19 | #include <linux/integrity.h> | |
3e1be52d | 20 | #include <linux/evm.h> |
50d34394 | 21 | #include <linux/magic.h> |
1886ab01 | 22 | #include <linux/posix_acl_xattr.h> |
6db7d1de | 23 | #include <linux/lsm_hooks.h> |
50d34394 | 24 | |
d46eb369 | 25 | #include <crypto/hash.h> |
5feeb611 | 26 | #include <crypto/hash_info.h> |
aa7c98b1 | 27 | #include <crypto/utils.h> |
66dbc325 MZ |
28 | #include "evm.h" |
29 | ||
30 | int evm_initialized; | |
31 | ||
17d7b0af | 32 | static const char * const integrity_status_msg[] = { |
cdef685b RS |
33 | "pass", "pass_immutable", "fail", "fail_immutable", "no_label", |
34 | "no_xattrs", "unknown" | |
9b97b6cd | 35 | }; |
d3b33679 | 36 | int evm_hmac_attrs; |
66dbc325 | 37 | |
fa516b66 | 38 | static struct xattr_list evm_config_default_xattrnames[] = { |
c808a6ec XJ |
39 | { |
40 | .name = XATTR_NAME_SELINUX, | |
41 | .enabled = IS_ENABLED(CONFIG_SECURITY_SELINUX) | |
8c7a703e | 42 | }, |
c808a6ec XJ |
43 | { |
44 | .name = XATTR_NAME_SMACK, | |
45 | .enabled = IS_ENABLED(CONFIG_SECURITY_SMACK) | |
8c7a703e | 46 | }, |
c808a6ec XJ |
47 | { |
48 | .name = XATTR_NAME_SMACKEXEC, | |
49 | .enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS) | |
8c7a703e | 50 | }, |
c808a6ec XJ |
51 | { |
52 | .name = XATTR_NAME_SMACKTRANSMUTE, | |
53 | .enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS) | |
8c7a703e | 54 | }, |
c808a6ec XJ |
55 | { |
56 | .name = XATTR_NAME_SMACKMMAP, | |
57 | .enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS) | |
8c7a703e | 58 | }, |
c808a6ec XJ |
59 | { |
60 | .name = XATTR_NAME_APPARMOR, | |
61 | .enabled = IS_ENABLED(CONFIG_SECURITY_APPARMOR) | |
8c7a703e | 62 | }, |
c808a6ec XJ |
63 | { |
64 | .name = XATTR_NAME_IMA, | |
65 | .enabled = IS_ENABLED(CONFIG_IMA_APPRAISE) | |
8c7a703e | 66 | }, |
c808a6ec XJ |
67 | { |
68 | .name = XATTR_NAME_CAPS, | |
8c7a703e RS |
69 | .enabled = true |
70 | }, | |
66dbc325 MZ |
71 | }; |
72 | ||
21af7663 MG |
73 | LIST_HEAD(evm_config_xattrnames); |
74 | ||
32ba540f | 75 | static int evm_fixmode __ro_after_init; |
7102ebcd MZ |
76 | static int __init evm_set_fixmode(char *str) |
77 | { | |
78 | if (strncmp(str, "fix", 3) == 0) | |
79 | evm_fixmode = 1; | |
7fe2bb7e BM |
80 | else |
81 | pr_err("invalid \"%s\" mode", str); | |
82 | ||
f2544f5e | 83 | return 1; |
7102ebcd MZ |
84 | } |
85 | __setup("evm=", evm_set_fixmode); | |
86 | ||
d3b33679 DK |
87 | static void __init evm_init_config(void) |
88 | { | |
21af7663 MG |
89 | int i, xattrs; |
90 | ||
91 | xattrs = ARRAY_SIZE(evm_config_default_xattrnames); | |
92 | ||
93 | pr_info("Initialising EVM extended attributes:\n"); | |
94 | for (i = 0; i < xattrs; i++) { | |
8c7a703e RS |
95 | pr_info("%s%s\n", evm_config_default_xattrnames[i].name, |
96 | !evm_config_default_xattrnames[i].enabled ? | |
97 | " (disabled)" : ""); | |
21af7663 MG |
98 | list_add_tail(&evm_config_default_xattrnames[i].list, |
99 | &evm_config_xattrnames); | |
100 | } | |
101 | ||
d3b33679 DK |
102 | #ifdef CONFIG_EVM_ATTR_FSUUID |
103 | evm_hmac_attrs |= EVM_ATTR_FSUUID; | |
104 | #endif | |
105 | pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs); | |
106 | } | |
107 | ||
ae1ba167 MG |
108 | static bool evm_key_loaded(void) |
109 | { | |
110 | return (bool)(evm_initialized & EVM_KEY_MASK); | |
111 | } | |
112 | ||
4a804b8a RS |
113 | /* |
114 | * This function determines whether or not it is safe to ignore verification | |
115 | * errors, based on the ability of EVM to calculate HMACs. If the HMAC key | |
116 | * is not loaded, and it cannot be loaded in the future due to the | |
117 | * EVM_SETUP_COMPLETE initialization flag, allowing an operation despite the | |
118 | * attrs/xattrs being found invalid will not make them valid. | |
119 | */ | |
120 | static bool evm_hmac_disabled(void) | |
121 | { | |
122 | if (evm_initialized & EVM_INIT_HMAC) | |
123 | return false; | |
124 | ||
125 | if (!(evm_initialized & EVM_SETUP_COMPLETE)) | |
126 | return false; | |
127 | ||
128 | return true; | |
129 | } | |
130 | ||
15647eb3 DK |
131 | static int evm_find_protected_xattrs(struct dentry *dentry) |
132 | { | |
c6f493d6 | 133 | struct inode *inode = d_backing_inode(dentry); |
21af7663 | 134 | struct xattr_list *xattr; |
15647eb3 DK |
135 | int error; |
136 | int count = 0; | |
137 | ||
5d6c3191 | 138 | if (!(inode->i_opflags & IOP_XATTR)) |
15647eb3 DK |
139 | return -EOPNOTSUPP; |
140 | ||
770f6058 | 141 | list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) { |
21af7663 | 142 | error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0); |
15647eb3 DK |
143 | if (error < 0) { |
144 | if (error == -ENODATA) | |
145 | continue; | |
146 | return error; | |
147 | } | |
148 | count++; | |
149 | } | |
150 | ||
151 | return count; | |
152 | } | |
153 | ||
cd708c93 MZ |
154 | static int is_unsupported_fs(struct dentry *dentry) |
155 | { | |
156 | struct inode *inode = d_backing_inode(dentry); | |
157 | ||
158 | if (inode->i_sb->s_iflags & SB_I_EVM_UNSUPPORTED) { | |
159 | pr_info_once("%s not supported\n", inode->i_sb->s_type->name); | |
160 | return 1; | |
161 | } | |
162 | return 0; | |
163 | } | |
164 | ||
66dbc325 MZ |
165 | /* |
166 | * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr | |
167 | * | |
168 | * Compute the HMAC on the dentry's protected set of extended attributes | |
7102ebcd MZ |
169 | * and compare it against the stored security.evm xattr. |
170 | * | |
171 | * For performance: | |
172 | * - use the previoulsy retrieved xattr value and length to calculate the | |
173 | * HMAC.) | |
174 | * - cache the verification result in the iint, when available. | |
66dbc325 MZ |
175 | * |
176 | * Returns integrity status | |
177 | */ | |
178 | static enum integrity_status evm_verify_hmac(struct dentry *dentry, | |
179 | const char *xattr_name, | |
180 | char *xattr_value, | |
75a323e6 | 181 | size_t xattr_value_len) |
66dbc325 | 182 | { |
15647eb3 | 183 | struct evm_ima_xattr_data *xattr_data = NULL; |
5feeb611 | 184 | struct signature_v2_hdr *hdr; |
566be59a | 185 | enum integrity_status evm_status = INTEGRITY_PASS; |
5feeb611 | 186 | struct evm_digest digest; |
75a323e6 RS |
187 | struct inode *inode = d_backing_inode(dentry); |
188 | struct evm_iint_cache *iint = evm_iint_inode(inode); | |
cdef685b | 189 | int rc, xattr_len, evm_immutable = 0; |
66dbc325 | 190 | |
50b97748 MG |
191 | if (iint && (iint->evm_status == INTEGRITY_PASS || |
192 | iint->evm_status == INTEGRITY_PASS_IMMUTABLE)) | |
24e0198e | 193 | return iint->evm_status; |
66dbc325 | 194 | |
cd708c93 MZ |
195 | if (is_unsupported_fs(dentry)) |
196 | return INTEGRITY_UNKNOWN; | |
197 | ||
6d38ca01 DK |
198 | /* if status is not PASS, try to check again - against -ENOMEM */ |
199 | ||
15647eb3 | 200 | /* first need to know the sig type */ |
4609e1f1 | 201 | rc = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, XATTR_NAME_EVM, |
c7c7a1a1 | 202 | (char **)&xattr_data, 0, GFP_NOFS); |
15647eb3 | 203 | if (rc <= 0) { |
1f100979 DK |
204 | evm_status = INTEGRITY_FAIL; |
205 | if (rc == -ENODATA) { | |
15647eb3 DK |
206 | rc = evm_find_protected_xattrs(dentry); |
207 | if (rc > 0) | |
208 | evm_status = INTEGRITY_NOLABEL; | |
209 | else if (rc == 0) | |
210 | evm_status = INTEGRITY_NOXATTRS; /* new file */ | |
1f100979 DK |
211 | } else if (rc == -EOPNOTSUPP) { |
212 | evm_status = INTEGRITY_UNKNOWN; | |
15647eb3 | 213 | } |
566be59a MZ |
214 | goto out; |
215 | } | |
66dbc325 | 216 | |
b1aaab22 | 217 | xattr_len = rc; |
15647eb3 DK |
218 | |
219 | /* check value type */ | |
220 | switch (xattr_data->type) { | |
221 | case EVM_XATTR_HMAC: | |
650b29db | 222 | if (xattr_len != sizeof(struct evm_xattr)) { |
b4bfec7f SF |
223 | evm_status = INTEGRITY_FAIL; |
224 | goto out; | |
225 | } | |
5feeb611 MG |
226 | |
227 | digest.hdr.algo = HASH_ALGO_SHA1; | |
15647eb3 | 228 | rc = evm_calc_hmac(dentry, xattr_name, xattr_value, |
a652aa59 | 229 | xattr_value_len, &digest, iint); |
15647eb3 DK |
230 | if (rc) |
231 | break; | |
650b29db | 232 | rc = crypto_memneq(xattr_data->data, digest.digest, |
5feeb611 | 233 | SHA1_DIGEST_SIZE); |
15647eb3 DK |
234 | if (rc) |
235 | rc = -EINVAL; | |
236 | break; | |
50b97748 | 237 | case EVM_XATTR_PORTABLE_DIGSIG: |
cdef685b RS |
238 | evm_immutable = 1; |
239 | fallthrough; | |
240 | case EVM_IMA_XATTR_DIGSIG: | |
455b6c91 RS |
241 | /* accept xattr with non-empty signature field */ |
242 | if (xattr_len <= sizeof(struct signature_v2_hdr)) { | |
243 | evm_status = INTEGRITY_FAIL; | |
244 | goto out; | |
245 | } | |
246 | ||
5feeb611 MG |
247 | hdr = (struct signature_v2_hdr *)xattr_data; |
248 | digest.hdr.algo = hdr->hash_algo; | |
15647eb3 | 249 | rc = evm_calc_hash(dentry, xattr_name, xattr_value, |
a652aa59 SB |
250 | xattr_value_len, xattr_data->type, &digest, |
251 | iint); | |
15647eb3 DK |
252 | if (rc) |
253 | break; | |
254 | rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM, | |
b1aaab22 | 255 | (const char *)xattr_data, xattr_len, |
5feeb611 | 256 | digest.digest, digest.hdr.length); |
15647eb3 | 257 | if (!rc) { |
50b97748 MG |
258 | if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) { |
259 | if (iint) | |
260 | iint->flags |= EVM_IMMUTABLE_DIGSIG; | |
261 | evm_status = INTEGRITY_PASS_IMMUTABLE; | |
70946c4a SH |
262 | } else if (!IS_RDONLY(inode) && |
263 | !(inode->i_sb->s_readonly_remount) && | |
264 | !IS_IMMUTABLE(inode)) { | |
c2baec7f DK |
265 | evm_update_evmxattr(dentry, xattr_name, |
266 | xattr_value, | |
267 | xattr_value_len); | |
50b97748 | 268 | } |
15647eb3 DK |
269 | } |
270 | break; | |
271 | default: | |
272 | rc = -EINVAL; | |
273 | break; | |
274 | } | |
275 | ||
cdef685b RS |
276 | if (rc) { |
277 | if (rc == -ENODATA) | |
278 | evm_status = INTEGRITY_NOXATTRS; | |
279 | else if (evm_immutable) | |
280 | evm_status = INTEGRITY_FAIL_IMMUTABLE; | |
281 | else | |
282 | evm_status = INTEGRITY_FAIL; | |
283 | } | |
87ac3d00 MZ |
284 | pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length, |
285 | digest.digest); | |
7102ebcd MZ |
286 | out: |
287 | if (iint) | |
288 | iint->evm_status = evm_status; | |
15647eb3 | 289 | kfree(xattr_data); |
7102ebcd | 290 | return evm_status; |
66dbc325 MZ |
291 | } |
292 | ||
8c7a703e RS |
293 | static int evm_protected_xattr_common(const char *req_xattr_name, |
294 | bool all_xattrs) | |
66dbc325 | 295 | { |
66dbc325 MZ |
296 | int namelen; |
297 | int found = 0; | |
21af7663 | 298 | struct xattr_list *xattr; |
66dbc325 MZ |
299 | |
300 | namelen = strlen(req_xattr_name); | |
770f6058 | 301 | list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) { |
8c7a703e RS |
302 | if (!all_xattrs && !xattr->enabled) |
303 | continue; | |
304 | ||
21af7663 MG |
305 | if ((strlen(xattr->name) == namelen) |
306 | && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) { | |
66dbc325 MZ |
307 | found = 1; |
308 | break; | |
309 | } | |
cb723180 | 310 | if (strncmp(req_xattr_name, |
21af7663 | 311 | xattr->name + XATTR_SECURITY_PREFIX_LEN, |
cb723180 MZ |
312 | strlen(req_xattr_name)) == 0) { |
313 | found = 1; | |
314 | break; | |
315 | } | |
66dbc325 | 316 | } |
21af7663 | 317 | |
66dbc325 MZ |
318 | return found; |
319 | } | |
320 | ||
c31288e5 | 321 | int evm_protected_xattr(const char *req_xattr_name) |
8c7a703e RS |
322 | { |
323 | return evm_protected_xattr_common(req_xattr_name, false); | |
324 | } | |
325 | ||
326 | int evm_protected_xattr_if_enabled(const char *req_xattr_name) | |
327 | { | |
328 | return evm_protected_xattr_common(req_xattr_name, true); | |
329 | } | |
330 | ||
8314b673 RS |
331 | /** |
332 | * evm_read_protected_xattrs - read EVM protected xattr names, lengths, values | |
333 | * @dentry: dentry of the read xattrs | |
8314b673 RS |
334 | * @buffer: buffer xattr names, lengths or values are copied to |
335 | * @buffer_size: size of buffer | |
336 | * @type: n: names, l: lengths, v: values | |
337 | * @canonical_fmt: data format (true: little endian, false: native format) | |
338 | * | |
339 | * Read protected xattr names (separated by |), lengths (u32) or values for a | |
340 | * given dentry and return the total size of copied data. If buffer is NULL, | |
341 | * just return the total size. | |
342 | * | |
343 | * Returns the total size on success, a negative value on error. | |
344 | */ | |
345 | int evm_read_protected_xattrs(struct dentry *dentry, u8 *buffer, | |
346 | int buffer_size, char type, bool canonical_fmt) | |
347 | { | |
348 | struct xattr_list *xattr; | |
349 | int rc, size, total_size = 0; | |
350 | ||
351 | list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) { | |
352 | rc = __vfs_getxattr(dentry, d_backing_inode(dentry), | |
353 | xattr->name, NULL, 0); | |
354 | if (rc < 0 && rc == -ENODATA) | |
355 | continue; | |
356 | else if (rc < 0) | |
357 | return rc; | |
358 | ||
359 | switch (type) { | |
360 | case 'n': | |
361 | size = strlen(xattr->name) + 1; | |
362 | if (buffer) { | |
363 | if (total_size) | |
364 | *(buffer + total_size - 1) = '|'; | |
365 | ||
366 | memcpy(buffer + total_size, xattr->name, size); | |
367 | } | |
368 | break; | |
369 | case 'l': | |
370 | size = sizeof(u32); | |
371 | if (buffer) { | |
372 | if (canonical_fmt) | |
6b26285f | 373 | rc = (__force int)cpu_to_le32(rc); |
8314b673 RS |
374 | |
375 | *(u32 *)(buffer + total_size) = rc; | |
376 | } | |
377 | break; | |
378 | case 'v': | |
379 | size = rc; | |
380 | if (buffer) { | |
381 | rc = __vfs_getxattr(dentry, | |
382 | d_backing_inode(dentry), xattr->name, | |
383 | buffer + total_size, | |
384 | buffer_size - total_size); | |
385 | if (rc < 0) | |
386 | return rc; | |
387 | } | |
388 | break; | |
389 | default: | |
390 | return -EINVAL; | |
391 | } | |
392 | ||
393 | total_size += size; | |
394 | } | |
395 | ||
396 | return total_size; | |
397 | } | |
398 | ||
66dbc325 MZ |
399 | /** |
400 | * evm_verifyxattr - verify the integrity of the requested xattr | |
401 | * @dentry: object of the verify xattr | |
402 | * @xattr_name: requested xattr | |
403 | * @xattr_value: requested xattr value | |
404 | * @xattr_value_len: requested xattr value length | |
405 | * | |
406 | * Calculate the HMAC for the given dentry and verify it against the stored | |
407 | * security.evm xattr. For performance, use the xattr value and length | |
408 | * previously retrieved to calculate the HMAC. | |
409 | * | |
410 | * Returns the xattr integrity status. | |
411 | * | |
412 | * This function requires the caller to lock the inode's i_mutex before it | |
413 | * is executed. | |
414 | */ | |
415 | enum integrity_status evm_verifyxattr(struct dentry *dentry, | |
416 | const char *xattr_name, | |
75a323e6 | 417 | void *xattr_value, size_t xattr_value_len) |
66dbc325 | 418 | { |
ae1ba167 | 419 | if (!evm_key_loaded() || !evm_protected_xattr(xattr_name)) |
66dbc325 MZ |
420 | return INTEGRITY_UNKNOWN; |
421 | ||
cd708c93 MZ |
422 | if (is_unsupported_fs(dentry)) |
423 | return INTEGRITY_UNKNOWN; | |
424 | ||
2960e6cb | 425 | return evm_verify_hmac(dentry, xattr_name, xattr_value, |
75a323e6 | 426 | xattr_value_len); |
66dbc325 MZ |
427 | } |
428 | EXPORT_SYMBOL_GPL(evm_verifyxattr); | |
429 | ||
7102ebcd MZ |
430 | /* |
431 | * evm_verify_current_integrity - verify the dentry's metadata integrity | |
432 | * @dentry: pointer to the affected dentry | |
433 | * | |
434 | * Verify and return the dentry's metadata integrity. The exceptions are | |
435 | * before EVM is initialized or in 'fix' mode. | |
436 | */ | |
437 | static enum integrity_status evm_verify_current_integrity(struct dentry *dentry) | |
438 | { | |
c6f493d6 | 439 | struct inode *inode = d_backing_inode(dentry); |
7102ebcd | 440 | |
ae1ba167 | 441 | if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode) |
e11afdbb | 442 | return INTEGRITY_PASS; |
75a323e6 | 443 | return evm_verify_hmac(dentry, NULL, NULL, 0); |
7102ebcd MZ |
444 | } |
445 | ||
1886ab01 RS |
446 | /* |
447 | * evm_xattr_change - check if passed xattr value differs from current value | |
39f60c1c | 448 | * @idmap: idmap of the mount |
1886ab01 RS |
449 | * @dentry: pointer to the affected dentry |
450 | * @xattr_name: requested xattr | |
451 | * @xattr_value: requested xattr value | |
452 | * @xattr_value_len: requested xattr value length | |
453 | * | |
454 | * Check if passed xattr value differs from current value. | |
455 | * | |
456 | * Returns 1 if passed xattr value differs from current value, 0 otherwise. | |
457 | */ | |
39f60c1c | 458 | static int evm_xattr_change(struct mnt_idmap *idmap, |
1886ab01 RS |
459 | struct dentry *dentry, const char *xattr_name, |
460 | const void *xattr_value, size_t xattr_value_len) | |
461 | { | |
462 | char *xattr_data = NULL; | |
463 | int rc = 0; | |
464 | ||
4609e1f1 | 465 | rc = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, xattr_name, &xattr_data, |
1886ab01 | 466 | 0, GFP_NOFS); |
f6fbd8cb PM |
467 | if (rc < 0) { |
468 | rc = 1; | |
469 | goto out; | |
470 | } | |
1886ab01 RS |
471 | |
472 | if (rc == xattr_value_len) | |
473 | rc = !!memcmp(xattr_value, xattr_data, rc); | |
474 | else | |
475 | rc = 1; | |
476 | ||
f6fbd8cb | 477 | out: |
1886ab01 RS |
478 | kfree(xattr_data); |
479 | return rc; | |
480 | } | |
481 | ||
a924ce0b MZ |
482 | /* |
483 | * evm_protect_xattr - protect the EVM extended attribute | |
484 | * | |
bf6d0f5d MZ |
485 | * Prevent security.evm from being modified or removed without the |
486 | * necessary permissions or when the existing value is invalid. | |
487 | * | |
488 | * The posix xattr acls are 'system' prefixed, which normally would not | |
489 | * affect security.evm. An interesting side affect of writing posix xattr | |
490 | * acls is their modifying of the i_mode, which is included in security.evm. | |
491 | * For posix xattr acls only, permit security.evm, even if it currently | |
50b97748 | 492 | * doesn't exist, to be updated unless the EVM signature is immutable. |
a924ce0b | 493 | */ |
39f60c1c | 494 | static int evm_protect_xattr(struct mnt_idmap *idmap, |
7e135dc7 | 495 | struct dentry *dentry, const char *xattr_name, |
a924ce0b MZ |
496 | const void *xattr_value, size_t xattr_value_len) |
497 | { | |
498 | enum integrity_status evm_status; | |
499 | ||
500 | if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) { | |
501 | if (!capable(CAP_SYS_ADMIN)) | |
502 | return -EPERM; | |
cd708c93 MZ |
503 | if (is_unsupported_fs(dentry)) |
504 | return -EPERM; | |
bf6d0f5d MZ |
505 | } else if (!evm_protected_xattr(xattr_name)) { |
506 | if (!posix_xattr_acl(xattr_name)) | |
507 | return 0; | |
cd708c93 MZ |
508 | if (is_unsupported_fs(dentry)) |
509 | return 0; | |
510 | ||
bf6d0f5d MZ |
511 | evm_status = evm_verify_current_integrity(dentry); |
512 | if ((evm_status == INTEGRITY_PASS) || | |
566be59a | 513 | (evm_status == INTEGRITY_NOXATTRS)) |
bf6d0f5d | 514 | return 0; |
9b97b6cd | 515 | goto out; |
cd708c93 MZ |
516 | } else if (is_unsupported_fs(dentry)) |
517 | return 0; | |
ae1ba167 | 518 | |
a924ce0b | 519 | evm_status = evm_verify_current_integrity(dentry); |
3dcbad52 | 520 | if (evm_status == INTEGRITY_NOXATTRS) { |
75a323e6 | 521 | struct evm_iint_cache *iint; |
3dcbad52 | 522 | |
4a804b8a RS |
523 | /* Exception if the HMAC is not going to be calculated. */ |
524 | if (evm_hmac_disabled()) | |
525 | return 0; | |
526 | ||
75a323e6 RS |
527 | iint = evm_iint_inode(d_backing_inode(dentry)); |
528 | if (iint && (iint->flags & EVM_NEW_FILE)) | |
3dcbad52 | 529 | return 0; |
5101a185 MZ |
530 | |
531 | /* exception for pseudo filesystems */ | |
fc64005c AV |
532 | if (dentry->d_sb->s_magic == TMPFS_MAGIC |
533 | || dentry->d_sb->s_magic == SYSFS_MAGIC) | |
5101a185 MZ |
534 | return 0; |
535 | ||
536 | integrity_audit_msg(AUDIT_INTEGRITY_METADATA, | |
537 | dentry->d_inode, dentry->d_name.name, | |
538 | "update_metadata", | |
539 | integrity_status_msg[evm_status], | |
540 | -EPERM, 0); | |
3dcbad52 | 541 | } |
9b97b6cd | 542 | out: |
4a804b8a RS |
543 | /* Exception if the HMAC is not going to be calculated. */ |
544 | if (evm_hmac_disabled() && (evm_status == INTEGRITY_NOLABEL || | |
545 | evm_status == INTEGRITY_UNKNOWN)) | |
546 | return 0; | |
cdef685b RS |
547 | |
548 | /* | |
549 | * Writing other xattrs is safe for portable signatures, as portable | |
550 | * signatures are immutable and can never be updated. | |
551 | */ | |
552 | if (evm_status == INTEGRITY_FAIL_IMMUTABLE) | |
553 | return 0; | |
554 | ||
1886ab01 | 555 | if (evm_status == INTEGRITY_PASS_IMMUTABLE && |
39f60c1c | 556 | !evm_xattr_change(idmap, dentry, xattr_name, xattr_value, |
1886ab01 RS |
557 | xattr_value_len)) |
558 | return 0; | |
559 | ||
560 | if (evm_status != INTEGRITY_PASS && | |
561 | evm_status != INTEGRITY_PASS_IMMUTABLE) | |
c6f493d6 | 562 | integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry), |
9b97b6cd MZ |
563 | dentry->d_name.name, "appraise_metadata", |
564 | integrity_status_msg[evm_status], | |
565 | -EPERM, 0); | |
a924ce0b MZ |
566 | return evm_status == INTEGRITY_PASS ? 0 : -EPERM; |
567 | } | |
568 | ||
66dbc325 MZ |
569 | /** |
570 | * evm_inode_setxattr - protect the EVM extended attribute | |
39f60c1c | 571 | * @idmap: idmap of the mount |
66dbc325 MZ |
572 | * @dentry: pointer to the affected dentry |
573 | * @xattr_name: pointer to the affected extended attribute name | |
574 | * @xattr_value: pointer to the new extended attribute value | |
575 | * @xattr_value_len: pointer to the new extended attribute value length | |
2b6a4054 | 576 | * @flags: flags to pass into filesystem operations |
66dbc325 | 577 | * |
2fb1c9a4 MZ |
578 | * Before allowing the 'security.evm' protected xattr to be updated, |
579 | * verify the existing value is valid. As only the kernel should have | |
580 | * access to the EVM encrypted key needed to calculate the HMAC, prevent | |
581 | * userspace from writing HMAC value. Writing 'security.evm' requires | |
582 | * requires CAP_SYS_ADMIN privileges. | |
66dbc325 | 583 | */ |
92383111 RS |
584 | static int evm_inode_setxattr(struct mnt_idmap *idmap, struct dentry *dentry, |
585 | const char *xattr_name, const void *xattr_value, | |
586 | size_t xattr_value_len, int flags) | |
66dbc325 | 587 | { |
2fb1c9a4 MZ |
588 | const struct evm_ima_xattr_data *xattr_data = xattr_value; |
589 | ||
ae1ba167 MG |
590 | /* Policy permits modification of the protected xattrs even though |
591 | * there's no HMAC key loaded | |
592 | */ | |
593 | if (evm_initialized & EVM_ALLOW_METADATA_WRITES) | |
594 | return 0; | |
595 | ||
3b1deef6 DK |
596 | if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) { |
597 | if (!xattr_value_len) | |
598 | return -EINVAL; | |
50b97748 MG |
599 | if (xattr_data->type != EVM_IMA_XATTR_DIGSIG && |
600 | xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG) | |
3b1deef6 DK |
601 | return -EPERM; |
602 | } | |
39f60c1c | 603 | return evm_protect_xattr(idmap, dentry, xattr_name, xattr_value, |
a924ce0b | 604 | xattr_value_len); |
66dbc325 MZ |
605 | } |
606 | ||
607 | /** | |
608 | * evm_inode_removexattr - protect the EVM extended attribute | |
39f60c1c | 609 | * @idmap: idmap of the mount |
66dbc325 MZ |
610 | * @dentry: pointer to the affected dentry |
611 | * @xattr_name: pointer to the affected extended attribute name | |
612 | * | |
7102ebcd MZ |
613 | * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that |
614 | * the current value is valid. | |
66dbc325 | 615 | */ |
92383111 RS |
616 | static int evm_inode_removexattr(struct mnt_idmap *idmap, struct dentry *dentry, |
617 | const char *xattr_name) | |
66dbc325 | 618 | { |
ae1ba167 MG |
619 | /* Policy permits modification of the protected xattrs even though |
620 | * there's no HMAC key loaded | |
621 | */ | |
622 | if (evm_initialized & EVM_ALLOW_METADATA_WRITES) | |
623 | return 0; | |
624 | ||
39f60c1c | 625 | return evm_protect_xattr(idmap, dentry, xattr_name, NULL, 0); |
66dbc325 MZ |
626 | } |
627 | ||
e61b135f | 628 | #ifdef CONFIG_FS_POSIX_ACL |
700b7940 | 629 | static int evm_inode_set_acl_change(struct mnt_idmap *idmap, |
e61b135f CB |
630 | struct dentry *dentry, const char *name, |
631 | struct posix_acl *kacl) | |
632 | { | |
633 | int rc; | |
634 | ||
635 | umode_t mode; | |
636 | struct inode *inode = d_backing_inode(dentry); | |
637 | ||
638 | if (!kacl) | |
639 | return 1; | |
640 | ||
700b7940 | 641 | rc = posix_acl_update_mode(idmap, inode, &mode, &kacl); |
e61b135f CB |
642 | if (rc || (inode->i_mode != mode)) |
643 | return 1; | |
644 | ||
645 | return 0; | |
646 | } | |
647 | #else | |
700b7940 | 648 | static inline int evm_inode_set_acl_change(struct mnt_idmap *idmap, |
e61b135f CB |
649 | struct dentry *dentry, |
650 | const char *name, | |
651 | struct posix_acl *kacl) | |
652 | { | |
653 | return 0; | |
654 | } | |
655 | #endif | |
656 | ||
657 | /** | |
658 | * evm_inode_set_acl - protect the EVM extended attribute from posix acls | |
700b7940 | 659 | * @idmap: idmap of the idmapped mount |
e61b135f CB |
660 | * @dentry: pointer to the affected dentry |
661 | * @acl_name: name of the posix acl | |
662 | * @kacl: pointer to the posix acls | |
663 | * | |
664 | * Prevent modifying posix acls causing the EVM HMAC to be re-calculated | |
665 | * and 'security.evm' xattr updated, unless the existing 'security.evm' is | |
666 | * valid. | |
92383111 RS |
667 | * |
668 | * Return: zero on success, -EPERM on failure. | |
e61b135f | 669 | */ |
92383111 RS |
670 | static int evm_inode_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, |
671 | const char *acl_name, struct posix_acl *kacl) | |
e61b135f CB |
672 | { |
673 | enum integrity_status evm_status; | |
674 | ||
675 | /* Policy permits modification of the protected xattrs even though | |
676 | * there's no HMAC key loaded | |
677 | */ | |
678 | if (evm_initialized & EVM_ALLOW_METADATA_WRITES) | |
679 | return 0; | |
680 | ||
681 | evm_status = evm_verify_current_integrity(dentry); | |
682 | if ((evm_status == INTEGRITY_PASS) || | |
683 | (evm_status == INTEGRITY_NOXATTRS)) | |
684 | return 0; | |
685 | ||
686 | /* Exception if the HMAC is not going to be calculated. */ | |
687 | if (evm_hmac_disabled() && (evm_status == INTEGRITY_NOLABEL || | |
688 | evm_status == INTEGRITY_UNKNOWN)) | |
689 | return 0; | |
690 | ||
691 | /* | |
692 | * Writing other xattrs is safe for portable signatures, as portable | |
693 | * signatures are immutable and can never be updated. | |
694 | */ | |
695 | if (evm_status == INTEGRITY_FAIL_IMMUTABLE) | |
696 | return 0; | |
697 | ||
698 | if (evm_status == INTEGRITY_PASS_IMMUTABLE && | |
700b7940 | 699 | !evm_inode_set_acl_change(idmap, dentry, acl_name, kacl)) |
e61b135f CB |
700 | return 0; |
701 | ||
16257cf6 | 702 | if (evm_status != INTEGRITY_PASS_IMMUTABLE) |
e61b135f CB |
703 | integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry), |
704 | dentry->d_name.name, "appraise_metadata", | |
705 | integrity_status_msg[evm_status], | |
706 | -EPERM, 0); | |
16257cf6 | 707 | return -EPERM; |
e61b135f CB |
708 | } |
709 | ||
92383111 RS |
710 | /** |
711 | * evm_inode_remove_acl - Protect the EVM extended attribute from posix acls | |
712 | * @idmap: idmap of the mount | |
713 | * @dentry: pointer to the affected dentry | |
714 | * @acl_name: name of the posix acl | |
715 | * | |
716 | * Prevent removing posix acls causing the EVM HMAC to be re-calculated | |
717 | * and 'security.evm' xattr updated, unless the existing 'security.evm' is | |
718 | * valid. | |
719 | * | |
720 | * Return: zero on success, -EPERM on failure. | |
721 | */ | |
722 | static int evm_inode_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry, | |
723 | const char *acl_name) | |
724 | { | |
725 | return evm_inode_set_acl(idmap, dentry, acl_name, NULL); | |
726 | } | |
727 | ||
523b74b1 DK |
728 | static void evm_reset_status(struct inode *inode) |
729 | { | |
75a323e6 | 730 | struct evm_iint_cache *iint; |
523b74b1 | 731 | |
75a323e6 | 732 | iint = evm_iint_inode(inode); |
523b74b1 DK |
733 | if (iint) |
734 | iint->evm_status = INTEGRITY_UNKNOWN; | |
735 | } | |
736 | ||
a652aa59 SB |
737 | /** |
738 | * evm_metadata_changed: Detect changes to the metadata | |
739 | * @inode: a file's inode | |
740 | * @metadata_inode: metadata inode | |
741 | * | |
742 | * On a stacked filesystem detect whether the metadata has changed. If this is | |
743 | * the case reset the evm_status associated with the inode that represents the | |
744 | * file. | |
745 | */ | |
746 | bool evm_metadata_changed(struct inode *inode, struct inode *metadata_inode) | |
747 | { | |
748 | struct evm_iint_cache *iint = evm_iint_inode(inode); | |
749 | bool ret = false; | |
750 | ||
751 | if (iint) { | |
752 | ret = (!IS_I_VERSION(metadata_inode) || | |
753 | integrity_inode_attrs_changed(&iint->metadata_inode, | |
754 | metadata_inode)); | |
755 | if (ret) | |
756 | iint->evm_status = INTEGRITY_UNKNOWN; | |
757 | } | |
758 | ||
759 | return ret; | |
760 | } | |
761 | ||
e3ccfe1a RS |
762 | /** |
763 | * evm_revalidate_status - report whether EVM status re-validation is necessary | |
764 | * @xattr_name: pointer to the affected extended attribute name | |
765 | * | |
766 | * Report whether callers of evm_verifyxattr() should re-validate the | |
767 | * EVM status. | |
768 | * | |
769 | * Return true if re-validation is necessary, false otherwise. | |
770 | */ | |
771 | bool evm_revalidate_status(const char *xattr_name) | |
772 | { | |
773 | if (!evm_key_loaded()) | |
774 | return false; | |
775 | ||
776 | /* evm_inode_post_setattr() passes NULL */ | |
777 | if (!xattr_name) | |
778 | return true; | |
779 | ||
780 | if (!evm_protected_xattr(xattr_name) && !posix_xattr_acl(xattr_name) && | |
781 | strcmp(xattr_name, XATTR_NAME_EVM)) | |
782 | return false; | |
783 | ||
784 | return true; | |
785 | } | |
786 | ||
66dbc325 MZ |
787 | /** |
788 | * evm_inode_post_setxattr - update 'security.evm' to reflect the changes | |
789 | * @dentry: pointer to the affected dentry | |
790 | * @xattr_name: pointer to the affected extended attribute name | |
791 | * @xattr_value: pointer to the new extended attribute value | |
792 | * @xattr_value_len: pointer to the new extended attribute value length | |
779cb194 | 793 | * @flags: flags to pass into filesystem operations |
66dbc325 MZ |
794 | * |
795 | * Update the HMAC stored in 'security.evm' to reflect the change. | |
796 | * | |
797 | * No need to take the i_mutex lock here, as this function is called from | |
798 | * __vfs_setxattr_noperm(). The caller of which has taken the inode's | |
799 | * i_mutex lock. | |
800 | */ | |
92383111 RS |
801 | static void evm_inode_post_setxattr(struct dentry *dentry, |
802 | const char *xattr_name, | |
803 | const void *xattr_value, | |
804 | size_t xattr_value_len, | |
805 | int flags) | |
66dbc325 | 806 | { |
e3ccfe1a | 807 | if (!evm_revalidate_status(xattr_name)) |
66dbc325 MZ |
808 | return; |
809 | ||
523b74b1 DK |
810 | evm_reset_status(dentry->d_inode); |
811 | ||
e3ccfe1a RS |
812 | if (!strcmp(xattr_name, XATTR_NAME_EVM)) |
813 | return; | |
814 | ||
4a804b8a RS |
815 | if (!(evm_initialized & EVM_INIT_HMAC)) |
816 | return; | |
817 | ||
cd708c93 MZ |
818 | if (is_unsupported_fs(dentry)) |
819 | return; | |
820 | ||
66dbc325 | 821 | evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len); |
66dbc325 MZ |
822 | } |
823 | ||
92383111 RS |
824 | /** |
825 | * evm_inode_post_set_acl - Update the EVM extended attribute from posix acls | |
826 | * @dentry: pointer to the affected dentry | |
827 | * @acl_name: name of the posix acl | |
828 | * @kacl: pointer to the posix acls | |
829 | * | |
830 | * Update the 'security.evm' xattr with the EVM HMAC re-calculated after setting | |
831 | * posix acls. | |
832 | */ | |
833 | static void evm_inode_post_set_acl(struct dentry *dentry, const char *acl_name, | |
834 | struct posix_acl *kacl) | |
835 | { | |
836 | return evm_inode_post_setxattr(dentry, acl_name, NULL, 0, 0); | |
837 | } | |
838 | ||
66dbc325 MZ |
839 | /** |
840 | * evm_inode_post_removexattr - update 'security.evm' after removing the xattr | |
841 | * @dentry: pointer to the affected dentry | |
842 | * @xattr_name: pointer to the affected extended attribute name | |
843 | * | |
844 | * Update the HMAC stored in 'security.evm' to reflect removal of the xattr. | |
7c51bb00 DK |
845 | * |
846 | * No need to take the i_mutex lock here, as this function is called from | |
847 | * vfs_removexattr() which takes the i_mutex. | |
66dbc325 | 848 | */ |
92383111 RS |
849 | static void evm_inode_post_removexattr(struct dentry *dentry, |
850 | const char *xattr_name) | |
66dbc325 | 851 | { |
e3ccfe1a | 852 | if (!evm_revalidate_status(xattr_name)) |
66dbc325 MZ |
853 | return; |
854 | ||
523b74b1 DK |
855 | evm_reset_status(dentry->d_inode); |
856 | ||
e3ccfe1a RS |
857 | if (!strcmp(xattr_name, XATTR_NAME_EVM)) |
858 | return; | |
859 | ||
4a804b8a RS |
860 | if (!(evm_initialized & EVM_INIT_HMAC)) |
861 | return; | |
862 | ||
66dbc325 | 863 | evm_update_evmxattr(dentry, xattr_name, NULL, 0); |
66dbc325 MZ |
864 | } |
865 | ||
92383111 RS |
866 | /** |
867 | * evm_inode_post_remove_acl - Update the EVM extended attribute from posix acls | |
868 | * @idmap: idmap of the mount | |
869 | * @dentry: pointer to the affected dentry | |
870 | * @acl_name: name of the posix acl | |
871 | * | |
872 | * Update the 'security.evm' xattr with the EVM HMAC re-calculated after | |
873 | * removing posix acls. | |
874 | */ | |
875 | static inline void evm_inode_post_remove_acl(struct mnt_idmap *idmap, | |
876 | struct dentry *dentry, | |
877 | const char *acl_name) | |
878 | { | |
879 | evm_inode_post_removexattr(dentry, acl_name); | |
880 | } | |
881 | ||
c1632a0f | 882 | static int evm_attr_change(struct mnt_idmap *idmap, |
0e363cf3 | 883 | struct dentry *dentry, struct iattr *attr) |
1886ab01 RS |
884 | { |
885 | struct inode *inode = d_backing_inode(dentry); | |
886 | unsigned int ia_valid = attr->ia_valid; | |
887 | ||
0dbe12f2 CB |
888 | if (!i_uid_needs_update(idmap, attr, inode) && |
889 | !i_gid_needs_update(idmap, attr, inode) && | |
1886ab01 RS |
890 | (!(ia_valid & ATTR_MODE) || attr->ia_mode == inode->i_mode)) |
891 | return 0; | |
892 | ||
893 | return 1; | |
894 | } | |
895 | ||
817b54aa MZ |
896 | /** |
897 | * evm_inode_setattr - prevent updating an invalid EVM extended attribute | |
b1de86d4 | 898 | * @idmap: idmap of the mount |
817b54aa | 899 | * @dentry: pointer to the affected dentry |
b1de86d4 | 900 | * @attr: iattr structure containing the new file attributes |
50b97748 MG |
901 | * |
902 | * Permit update of file attributes when files have a valid EVM signature, | |
903 | * except in the case of them having an immutable portable signature. | |
817b54aa | 904 | */ |
92383111 RS |
905 | static int evm_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
906 | struct iattr *attr) | |
817b54aa MZ |
907 | { |
908 | unsigned int ia_valid = attr->ia_valid; | |
909 | enum integrity_status evm_status; | |
910 | ||
ae1ba167 MG |
911 | /* Policy permits modification of the protected attrs even though |
912 | * there's no HMAC key loaded | |
913 | */ | |
914 | if (evm_initialized & EVM_ALLOW_METADATA_WRITES) | |
915 | return 0; | |
916 | ||
cd708c93 MZ |
917 | if (is_unsupported_fs(dentry)) |
918 | return 0; | |
919 | ||
a924ce0b | 920 | if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))) |
817b54aa | 921 | return 0; |
cd708c93 | 922 | |
817b54aa | 923 | evm_status = evm_verify_current_integrity(dentry); |
cdef685b RS |
924 | /* |
925 | * Writing attrs is safe for portable signatures, as portable signatures | |
926 | * are immutable and can never be updated. | |
927 | */ | |
566be59a | 928 | if ((evm_status == INTEGRITY_PASS) || |
4a804b8a | 929 | (evm_status == INTEGRITY_NOXATTRS) || |
cdef685b | 930 | (evm_status == INTEGRITY_FAIL_IMMUTABLE) || |
4a804b8a RS |
931 | (evm_hmac_disabled() && (evm_status == INTEGRITY_NOLABEL || |
932 | evm_status == INTEGRITY_UNKNOWN))) | |
566be59a | 933 | return 0; |
1886ab01 RS |
934 | |
935 | if (evm_status == INTEGRITY_PASS_IMMUTABLE && | |
c1632a0f | 936 | !evm_attr_change(idmap, dentry, attr)) |
1886ab01 RS |
937 | return 0; |
938 | ||
c6f493d6 | 939 | integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry), |
9b97b6cd MZ |
940 | dentry->d_name.name, "appraise_metadata", |
941 | integrity_status_msg[evm_status], -EPERM, 0); | |
566be59a | 942 | return -EPERM; |
817b54aa MZ |
943 | } |
944 | ||
66dbc325 MZ |
945 | /** |
946 | * evm_inode_post_setattr - update 'security.evm' after modifying metadata | |
784111d0 | 947 | * @idmap: idmap of the idmapped mount |
66dbc325 MZ |
948 | * @dentry: pointer to the affected dentry |
949 | * @ia_valid: for the UID and GID status | |
950 | * | |
951 | * For now, update the HMAC stored in 'security.evm' to reflect UID/GID | |
952 | * changes. | |
953 | * | |
954 | * This function is called from notify_change(), which expects the caller | |
955 | * to lock the inode's i_mutex. | |
956 | */ | |
92383111 RS |
957 | static void evm_inode_post_setattr(struct mnt_idmap *idmap, |
958 | struct dentry *dentry, int ia_valid) | |
66dbc325 | 959 | { |
e3ccfe1a | 960 | if (!evm_revalidate_status(NULL)) |
66dbc325 MZ |
961 | return; |
962 | ||
e3ccfe1a RS |
963 | evm_reset_status(dentry->d_inode); |
964 | ||
4a804b8a RS |
965 | if (!(evm_initialized & EVM_INIT_HMAC)) |
966 | return; | |
967 | ||
cd708c93 MZ |
968 | if (is_unsupported_fs(dentry)) |
969 | return; | |
970 | ||
66dbc325 MZ |
971 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) |
972 | evm_update_evmxattr(dentry, NULL, NULL, 0); | |
66dbc325 MZ |
973 | } |
974 | ||
32538047 | 975 | static int evm_inode_copy_up_xattr(struct dentry *src, const char *name) |
40ca4ee3 | 976 | { |
f2b3fc42 SB |
977 | struct evm_ima_xattr_data *xattr_data = NULL; |
978 | int rc; | |
979 | ||
980 | if (strcmp(name, XATTR_NAME_EVM) != 0) | |
981 | return -EOPNOTSUPP; | |
982 | ||
983 | /* first need to know the sig type */ | |
984 | rc = vfs_getxattr_alloc(&nop_mnt_idmap, src, XATTR_NAME_EVM, | |
985 | (char **)&xattr_data, 0, GFP_NOFS); | |
986 | if (rc <= 0) | |
987 | return -EPERM; | |
988 | ||
989 | if (rc < offsetof(struct evm_ima_xattr_data, type) + | |
990 | sizeof(xattr_data->type)) | |
991 | return -EPERM; | |
992 | ||
993 | switch (xattr_data->type) { | |
994 | case EVM_XATTR_PORTABLE_DIGSIG: | |
995 | rc = 0; /* allow copy-up */ | |
996 | break; | |
997 | case EVM_XATTR_HMAC: | |
998 | case EVM_IMA_XATTR_DIGSIG: | |
999 | default: | |
1000 | rc = 1; /* discard */ | |
1001 | } | |
1002 | ||
1003 | kfree(xattr_data); | |
1004 | return rc; | |
40ca4ee3 MZ |
1005 | } |
1006 | ||
cb723180 | 1007 | /* |
9eea2904 | 1008 | * evm_inode_init_security - initializes security.evm HMAC value |
cb723180 | 1009 | */ |
6db7d1de RS |
1010 | int evm_inode_init_security(struct inode *inode, struct inode *dir, |
1011 | const struct qstr *qstr, struct xattr *xattrs, | |
1012 | int *xattr_count) | |
cb723180 | 1013 | { |
650b29db | 1014 | struct evm_xattr *xattr_data; |
c31288e5 RS |
1015 | struct xattr *xattr, *evm_xattr; |
1016 | bool evm_protected_xattrs = false; | |
cb723180 MZ |
1017 | int rc; |
1018 | ||
c31288e5 RS |
1019 | if (!(evm_initialized & EVM_INIT_HMAC) || !xattrs) |
1020 | return 0; | |
1021 | ||
1022 | /* | |
1023 | * security_inode_init_security() makes sure that the xattrs array is | |
1024 | * contiguous, there is enough space for security.evm, and that there is | |
1025 | * a terminator at the end of the array. | |
1026 | */ | |
1027 | for (xattr = xattrs; xattr->name; xattr++) { | |
1028 | if (evm_protected_xattr(xattr->name)) | |
1029 | evm_protected_xattrs = true; | |
1030 | } | |
1031 | ||
1032 | /* EVM xattr not needed. */ | |
1033 | if (!evm_protected_xattrs) | |
5a4730ba | 1034 | return 0; |
cb723180 | 1035 | |
6db7d1de | 1036 | evm_xattr = lsm_get_xattr_slot(xattrs, xattr_count); |
c31288e5 RS |
1037 | /* |
1038 | * Array terminator (xattr name = NULL) must be the first non-filled | |
1039 | * xattr slot. | |
1040 | */ | |
1041 | WARN_ONCE(evm_xattr != xattr, | |
1042 | "%s: xattrs terminator is not the first non-filled slot\n", | |
1043 | __func__); | |
6db7d1de | 1044 | |
cb723180 MZ |
1045 | xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS); |
1046 | if (!xattr_data) | |
1047 | return -ENOMEM; | |
1048 | ||
650b29db | 1049 | xattr_data->data.type = EVM_XATTR_HMAC; |
6db7d1de | 1050 | rc = evm_init_hmac(inode, xattrs, xattr_data->digest); |
cb723180 MZ |
1051 | if (rc < 0) |
1052 | goto out; | |
1053 | ||
1054 | evm_xattr->value = xattr_data; | |
1055 | evm_xattr->value_len = sizeof(*xattr_data); | |
9548906b | 1056 | evm_xattr->name = XATTR_EVM_SUFFIX; |
cb723180 MZ |
1057 | return 0; |
1058 | out: | |
1059 | kfree(xattr_data); | |
1060 | return rc; | |
1061 | } | |
1062 | EXPORT_SYMBOL_GPL(evm_inode_init_security); | |
1063 | ||
75a323e6 RS |
1064 | static int evm_inode_alloc_security(struct inode *inode) |
1065 | { | |
1066 | struct evm_iint_cache *iint = evm_iint_inode(inode); | |
1067 | ||
1068 | /* Called by security_inode_alloc(), it cannot be NULL. */ | |
1069 | iint->flags = 0UL; | |
1070 | iint->evm_status = INTEGRITY_UNKNOWN; | |
1071 | ||
1072 | return 0; | |
1073 | } | |
1074 | ||
1075 | static void evm_file_release(struct file *file) | |
1076 | { | |
1077 | struct inode *inode = file_inode(file); | |
1078 | struct evm_iint_cache *iint = evm_iint_inode(inode); | |
1079 | fmode_t mode = file->f_mode; | |
1080 | ||
1081 | if (!S_ISREG(inode->i_mode) || !(mode & FMODE_WRITE)) | |
1082 | return; | |
1083 | ||
1084 | if (iint && atomic_read(&inode->i_writecount) == 1) | |
1085 | iint->flags &= ~EVM_NEW_FILE; | |
1086 | } | |
1087 | ||
1088 | static void evm_post_path_mknod(struct mnt_idmap *idmap, struct dentry *dentry) | |
1089 | { | |
1090 | struct inode *inode = d_backing_inode(dentry); | |
1091 | struct evm_iint_cache *iint = evm_iint_inode(inode); | |
1092 | ||
1093 | if (!S_ISREG(inode->i_mode)) | |
1094 | return; | |
1095 | ||
1096 | if (iint) | |
1097 | iint->flags |= EVM_NEW_FILE; | |
1098 | } | |
1099 | ||
2ce523eb DK |
1100 | #ifdef CONFIG_EVM_LOAD_X509 |
1101 | void __init evm_load_x509(void) | |
1102 | { | |
26ddabfe DK |
1103 | int rc; |
1104 | ||
1105 | rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH); | |
1106 | if (!rc) | |
1107 | evm_initialized |= EVM_INIT_X509; | |
2ce523eb DK |
1108 | } |
1109 | #endif | |
1110 | ||
66dbc325 MZ |
1111 | static int __init init_evm(void) |
1112 | { | |
1113 | int error; | |
21af7663 | 1114 | struct list_head *pos, *q; |
66dbc325 | 1115 | |
d3b33679 DK |
1116 | evm_init_config(); |
1117 | ||
f4dc3778 DK |
1118 | error = integrity_init_keyring(INTEGRITY_KEYRING_EVM); |
1119 | if (error) | |
21af7663 | 1120 | goto error; |
f4dc3778 | 1121 | |
66dbc325 MZ |
1122 | error = evm_init_secfs(); |
1123 | if (error < 0) { | |
20ee451f | 1124 | pr_info("Error registering secfs\n"); |
21af7663 | 1125 | goto error; |
66dbc325 | 1126 | } |
15647eb3 | 1127 | |
21af7663 MG |
1128 | error: |
1129 | if (error != 0) { | |
1130 | if (!list_empty(&evm_config_xattrnames)) { | |
c8b37524 | 1131 | list_for_each_safe(pos, q, &evm_config_xattrnames) |
21af7663 | 1132 | list_del(pos); |
21af7663 MG |
1133 | } |
1134 | } | |
66dbc325 | 1135 | |
21af7663 | 1136 | return error; |
66dbc325 MZ |
1137 | } |
1138 | ||
92383111 RS |
1139 | static struct security_hook_list evm_hooks[] __ro_after_init = { |
1140 | LSM_HOOK_INIT(inode_setattr, evm_inode_setattr), | |
1141 | LSM_HOOK_INIT(inode_post_setattr, evm_inode_post_setattr), | |
1142 | LSM_HOOK_INIT(inode_copy_up_xattr, evm_inode_copy_up_xattr), | |
1143 | LSM_HOOK_INIT(inode_setxattr, evm_inode_setxattr), | |
1144 | LSM_HOOK_INIT(inode_post_setxattr, evm_inode_post_setxattr), | |
1145 | LSM_HOOK_INIT(inode_set_acl, evm_inode_set_acl), | |
1146 | LSM_HOOK_INIT(inode_post_set_acl, evm_inode_post_set_acl), | |
1147 | LSM_HOOK_INIT(inode_remove_acl, evm_inode_remove_acl), | |
1148 | LSM_HOOK_INIT(inode_post_remove_acl, evm_inode_post_remove_acl), | |
1149 | LSM_HOOK_INIT(inode_removexattr, evm_inode_removexattr), | |
1150 | LSM_HOOK_INIT(inode_post_removexattr, evm_inode_post_removexattr), | |
1151 | LSM_HOOK_INIT(inode_init_security, evm_inode_init_security), | |
75a323e6 RS |
1152 | LSM_HOOK_INIT(inode_alloc_security, evm_inode_alloc_security), |
1153 | LSM_HOOK_INIT(file_release, evm_file_release), | |
1154 | LSM_HOOK_INIT(path_post_mknod, evm_post_path_mknod), | |
92383111 RS |
1155 | }; |
1156 | ||
1157 | static const struct lsm_id evm_lsmid = { | |
1158 | .name = "evm", | |
1159 | .id = LSM_ID_EVM, | |
1160 | }; | |
1161 | ||
1162 | static int __init init_evm_lsm(void) | |
1163 | { | |
1164 | security_add_hooks(evm_hooks, ARRAY_SIZE(evm_hooks), &evm_lsmid); | |
1165 | return 0; | |
1166 | } | |
1167 | ||
75a323e6 RS |
1168 | struct lsm_blob_sizes evm_blob_sizes __ro_after_init = { |
1169 | .lbs_inode = sizeof(struct evm_iint_cache), | |
1170 | .lbs_xattr_count = 1, | |
1171 | }; | |
1172 | ||
92383111 RS |
1173 | DEFINE_LSM(evm) = { |
1174 | .name = "evm", | |
1175 | .init = init_evm_lsm, | |
1176 | .order = LSM_ORDER_LAST, | |
75a323e6 | 1177 | .blobs = &evm_blob_sizes, |
92383111 RS |
1178 | }; |
1179 | ||
66dbc325 | 1180 | late_initcall(init_evm); |