blob: 23b04c6521b25b33eb131935c8f9fb11627598cf [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (C) 2011 IBM Corporation
4 *
5 * Author:
6 * Mimi Zohar <zohar@us.ibm.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
David Brazdil0f672f62019-12-10 10:32:29 +00008#include <linux/init.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009#include <linux/file.h>
10#include <linux/fs.h>
11#include <linux/xattr.h>
12#include <linux/magic.h>
13#include <linux/ima.h>
14#include <linux/evm.h>
15
16#include "ima.h"
17
18static int __init default_appraise_setup(char *str)
19{
20#ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
Olivier Deprez0e641232021-09-23 10:07:05 +020021 if (arch_ima_get_secureboot()) {
22 pr_info("Secure boot enabled: ignoring ima_appraise=%s boot parameter option",
23 str);
24 return 1;
25 }
26
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027 if (strncmp(str, "off", 3) == 0)
28 ima_appraise = 0;
29 else if (strncmp(str, "log", 3) == 0)
30 ima_appraise = IMA_APPRAISE_LOG;
31 else if (strncmp(str, "fix", 3) == 0)
32 ima_appraise = IMA_APPRAISE_FIX;
33#endif
34 return 1;
35}
36
37__setup("ima_appraise=", default_appraise_setup);
38
39/*
40 * is_ima_appraise_enabled - return appraise status
41 *
42 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
43 */
44bool is_ima_appraise_enabled(void)
45{
46 return ima_appraise & IMA_APPRAISE_ENFORCE;
47}
48
49/*
50 * ima_must_appraise - set appraise flag
51 *
52 * Return 1 to appraise or hash
53 */
54int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
55{
56 u32 secid;
57
58 if (!ima_appraise)
59 return 0;
60
61 security_task_getsecid(current, &secid);
62 return ima_match_policy(inode, current_cred(), secid, func, mask,
David Brazdil0f672f62019-12-10 10:32:29 +000063 IMA_APPRAISE | IMA_HASH, NULL, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064}
65
66static int ima_fix_xattr(struct dentry *dentry,
67 struct integrity_iint_cache *iint)
68{
69 int rc, offset;
70 u8 algo = iint->ima_hash->algo;
71
72 if (algo <= HASH_ALGO_SHA1) {
73 offset = 1;
74 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
75 } else {
76 offset = 0;
77 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
78 iint->ima_hash->xattr.ng.algo = algo;
79 }
80 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
81 &iint->ima_hash->xattr.data[offset],
82 (sizeof(iint->ima_hash->xattr) - offset) +
83 iint->ima_hash->length, 0);
84 return rc;
85}
86
87/* Return specific func appraised cached result */
88enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
89 enum ima_hooks func)
90{
91 switch (func) {
92 case MMAP_CHECK:
93 return iint->ima_mmap_status;
94 case BPRM_CHECK:
95 return iint->ima_bprm_status;
96 case CREDS_CHECK:
97 return iint->ima_creds_status;
98 case FILE_CHECK:
99 case POST_SETATTR:
100 return iint->ima_file_status;
101 case MODULE_CHECK ... MAX_CHECK - 1:
102 default:
103 return iint->ima_read_status;
104 }
105}
106
107static void ima_set_cache_status(struct integrity_iint_cache *iint,
108 enum ima_hooks func,
109 enum integrity_status status)
110{
111 switch (func) {
112 case MMAP_CHECK:
113 iint->ima_mmap_status = status;
114 break;
115 case BPRM_CHECK:
116 iint->ima_bprm_status = status;
117 break;
118 case CREDS_CHECK:
119 iint->ima_creds_status = status;
David Brazdil0f672f62019-12-10 10:32:29 +0000120 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121 case FILE_CHECK:
122 case POST_SETATTR:
123 iint->ima_file_status = status;
124 break;
125 case MODULE_CHECK ... MAX_CHECK - 1:
126 default:
127 iint->ima_read_status = status;
128 break;
129 }
130}
131
132static void ima_cache_flags(struct integrity_iint_cache *iint,
133 enum ima_hooks func)
134{
135 switch (func) {
136 case MMAP_CHECK:
137 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
138 break;
139 case BPRM_CHECK:
140 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
141 break;
142 case CREDS_CHECK:
143 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
144 break;
145 case FILE_CHECK:
146 case POST_SETATTR:
147 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
148 break;
149 case MODULE_CHECK ... MAX_CHECK - 1:
150 default:
151 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
152 break;
153 }
154}
155
156enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
157 int xattr_len)
158{
159 struct signature_v2_hdr *sig;
160 enum hash_algo ret;
161
162 if (!xattr_value || xattr_len < 2)
163 /* return default hash algo */
164 return ima_hash_algo;
165
166 switch (xattr_value->type) {
167 case EVM_IMA_XATTR_DIGSIG:
168 sig = (typeof(sig))xattr_value;
169 if (sig->version != 2 || xattr_len <= sizeof(*sig))
170 return ima_hash_algo;
171 return sig->hash_algo;
172 break;
173 case IMA_XATTR_DIGEST_NG:
David Brazdil0f672f62019-12-10 10:32:29 +0000174 /* first byte contains algorithm id */
175 ret = xattr_value->data[0];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000176 if (ret < HASH_ALGO__LAST)
177 return ret;
178 break;
179 case IMA_XATTR_DIGEST:
180 /* this is for backward compatibility */
181 if (xattr_len == 21) {
182 unsigned int zero = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000183 if (!memcmp(&xattr_value->data[16], &zero, 4))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000184 return HASH_ALGO_MD5;
185 else
186 return HASH_ALGO_SHA1;
187 } else if (xattr_len == 17)
188 return HASH_ALGO_MD5;
189 break;
190 }
191
192 /* return default hash algo */
193 return ima_hash_algo;
194}
195
196int ima_read_xattr(struct dentry *dentry,
197 struct evm_ima_xattr_data **xattr_value)
198{
199 ssize_t ret;
200
201 ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
202 0, GFP_NOFS);
203 if (ret == -EOPNOTSUPP)
204 ret = 0;
205 return ret;
206}
207
208/*
David Brazdil0f672f62019-12-10 10:32:29 +0000209 * xattr_verify - verify xattr digest or signature
210 *
211 * Verify whether the hash or signature matches the file contents.
212 *
213 * Return 0 on success, error code otherwise.
214 */
215static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
216 struct evm_ima_xattr_data *xattr_value, int xattr_len,
217 enum integrity_status *status, const char **cause)
218{
219 int rc = -EINVAL, hash_start = 0;
220
221 switch (xattr_value->type) {
222 case IMA_XATTR_DIGEST_NG:
223 /* first byte contains algorithm id */
224 hash_start = 1;
225 /* fall through */
226 case IMA_XATTR_DIGEST:
227 if (iint->flags & IMA_DIGSIG_REQUIRED) {
228 *cause = "IMA-signature-required";
229 *status = INTEGRITY_FAIL;
230 break;
231 }
232 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
233 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
234 iint->ima_hash->length)
235 /*
236 * xattr length may be longer. md5 hash in previous
237 * version occupied 20 bytes in xattr, instead of 16
238 */
239 rc = memcmp(&xattr_value->data[hash_start],
240 iint->ima_hash->digest,
241 iint->ima_hash->length);
242 else
243 rc = -EINVAL;
244 if (rc) {
245 *cause = "invalid-hash";
246 *status = INTEGRITY_FAIL;
247 break;
248 }
249 *status = INTEGRITY_PASS;
250 break;
251 case EVM_IMA_XATTR_DIGSIG:
252 set_bit(IMA_DIGSIG, &iint->atomic_flags);
253 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
254 (const char *)xattr_value,
255 xattr_len,
256 iint->ima_hash->digest,
257 iint->ima_hash->length);
258 if (rc == -EOPNOTSUPP) {
259 *status = INTEGRITY_UNKNOWN;
260 break;
261 }
262 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
263 func == KEXEC_KERNEL_CHECK)
264 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
265 (const char *)xattr_value,
266 xattr_len,
267 iint->ima_hash->digest,
268 iint->ima_hash->length);
269 if (rc) {
270 *cause = "invalid-signature";
271 *status = INTEGRITY_FAIL;
272 } else {
273 *status = INTEGRITY_PASS;
274 }
275 break;
276 default:
277 *status = INTEGRITY_UNKNOWN;
278 *cause = "unknown-ima-data";
279 break;
280 }
281
282 return rc;
283}
284
285/*
286 * modsig_verify - verify modsig signature
287 *
288 * Verify whether the signature matches the file contents.
289 *
290 * Return 0 on success, error code otherwise.
291 */
292static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
293 enum integrity_status *status, const char **cause)
294{
295 int rc;
296
297 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
298 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
299 func == KEXEC_KERNEL_CHECK)
300 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
301 modsig);
302 if (rc) {
303 *cause = "invalid-signature";
304 *status = INTEGRITY_FAIL;
305 } else {
306 *status = INTEGRITY_PASS;
307 }
308
309 return rc;
310}
311
312/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000313 * ima_appraise_measurement - appraise file measurement
314 *
315 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
316 * Assuming success, compare the xattr hash with the collected measurement.
317 *
318 * Return 0 on success, error code otherwise
319 */
320int ima_appraise_measurement(enum ima_hooks func,
321 struct integrity_iint_cache *iint,
322 struct file *file, const unsigned char *filename,
323 struct evm_ima_xattr_data *xattr_value,
David Brazdil0f672f62019-12-10 10:32:29 +0000324 int xattr_len, const struct modsig *modsig)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000325{
326 static const char op[] = "appraise_data";
327 const char *cause = "unknown";
328 struct dentry *dentry = file_dentry(file);
329 struct inode *inode = d_backing_inode(dentry);
330 enum integrity_status status = INTEGRITY_UNKNOWN;
David Brazdil0f672f62019-12-10 10:32:29 +0000331 int rc = xattr_len;
332 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000333
David Brazdil0f672f62019-12-10 10:32:29 +0000334 /* If not appraising a modsig, we need an xattr. */
335 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000336 return INTEGRITY_UNKNOWN;
337
David Brazdil0f672f62019-12-10 10:32:29 +0000338 /* If reading the xattr failed and there's no modsig, error out. */
339 if (rc <= 0 && !try_modsig) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000340 if (rc && rc != -ENODATA)
341 goto out;
342
343 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
344 "IMA-signature-required" : "missing-hash";
345 status = INTEGRITY_NOLABEL;
346 if (file->f_mode & FMODE_CREATED)
347 iint->flags |= IMA_NEW_FILE;
348 if ((iint->flags & IMA_NEW_FILE) &&
349 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
350 (inode->i_size == 0)))
351 status = INTEGRITY_PASS;
352 goto out;
353 }
354
355 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
356 switch (status) {
357 case INTEGRITY_PASS:
358 case INTEGRITY_PASS_IMMUTABLE:
359 case INTEGRITY_UNKNOWN:
360 break;
361 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
David Brazdil0f672f62019-12-10 10:32:29 +0000362 /* It's fine not to have xattrs when using a modsig. */
363 if (try_modsig)
364 break;
365 /* fall through */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000366 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
367 cause = "missing-HMAC";
368 goto out;
369 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
370 cause = "invalid-HMAC";
371 goto out;
372 default:
373 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
374 }
375
David Brazdil0f672f62019-12-10 10:32:29 +0000376 if (xattr_value)
377 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
378 &cause);
379
380 /*
381 * If we have a modsig and either no imasig or the imasig's key isn't
382 * known, then try verifying the modsig.
383 */
384 if (try_modsig &&
385 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
386 rc == -ENOKEY))
387 rc = modsig_verify(func, modsig, &status, &cause);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000388
389out:
390 /*
391 * File signatures on some filesystems can not be properly verified.
392 * When such filesystems are mounted by an untrusted mounter or on a
393 * system not willing to accept such a risk, fail the file signature
394 * verification.
395 */
396 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
397 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
398 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
399 status = INTEGRITY_FAIL;
400 cause = "unverifiable-signature";
401 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
402 op, cause, rc, 0);
403 } else if (status != INTEGRITY_PASS) {
404 /* Fix mode, but don't replace file signatures. */
David Brazdil0f672f62019-12-10 10:32:29 +0000405 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000406 (!xattr_value ||
407 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
408 if (!ima_fix_xattr(dentry, iint))
409 status = INTEGRITY_PASS;
410 }
411
412 /* Permit new files with file signatures, but without data. */
413 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
414 xattr_value && xattr_value->type == EVM_IMA_XATTR_DIGSIG) {
415 status = INTEGRITY_PASS;
416 }
417
418 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
419 op, cause, rc, 0);
420 } else {
421 ima_cache_flags(iint, func);
422 }
423
424 ima_set_cache_status(iint, func, status);
425 return status;
426}
427
428/*
429 * ima_update_xattr - update 'security.ima' hash value
430 */
431void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
432{
433 struct dentry *dentry = file_dentry(file);
434 int rc = 0;
435
436 /* do not collect and update hash for digital signatures */
437 if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
438 return;
439
440 if ((iint->ima_file_status != INTEGRITY_PASS) &&
441 !(iint->flags & IMA_HASH))
442 return;
443
David Brazdil0f672f62019-12-10 10:32:29 +0000444 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000445 if (rc < 0)
446 return;
447
448 inode_lock(file_inode(file));
449 ima_fix_xattr(dentry, iint);
450 inode_unlock(file_inode(file));
451}
452
453/**
454 * ima_inode_post_setattr - reflect file metadata changes
455 * @dentry: pointer to the affected dentry
456 *
457 * Changes to a dentry's metadata might result in needing to appraise.
458 *
459 * This function is called from notify_change(), which expects the caller
460 * to lock the inode's i_mutex.
461 */
462void ima_inode_post_setattr(struct dentry *dentry)
463{
464 struct inode *inode = d_backing_inode(dentry);
465 struct integrity_iint_cache *iint;
466 int action;
467
468 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
469 || !(inode->i_opflags & IOP_XATTR))
470 return;
471
472 action = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
473 if (!action)
474 __vfs_removexattr(dentry, XATTR_NAME_IMA);
475 iint = integrity_iint_find(inode);
476 if (iint) {
477 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
478 if (!action)
479 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
480 }
481}
482
483/*
484 * ima_protect_xattr - protect 'security.ima'
485 *
486 * Ensure that not just anyone can modify or remove 'security.ima'.
487 */
488static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
489 const void *xattr_value, size_t xattr_value_len)
490{
491 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
492 if (!capable(CAP_SYS_ADMIN))
493 return -EPERM;
494 return 1;
495 }
496 return 0;
497}
498
499static void ima_reset_appraise_flags(struct inode *inode, int digsig)
500{
501 struct integrity_iint_cache *iint;
502
503 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
504 return;
505
506 iint = integrity_iint_find(inode);
507 if (!iint)
508 return;
509 iint->measured_pcrs = 0;
510 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
511 if (digsig)
512 set_bit(IMA_DIGSIG, &iint->atomic_flags);
513 else
514 clear_bit(IMA_DIGSIG, &iint->atomic_flags);
515}
516
517int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
518 const void *xattr_value, size_t xattr_value_len)
519{
520 const struct evm_ima_xattr_data *xvalue = xattr_value;
521 int result;
522
523 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
524 xattr_value_len);
525 if (result == 1) {
526 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
527 return -EINVAL;
528 ima_reset_appraise_flags(d_backing_inode(dentry),
529 xvalue->type == EVM_IMA_XATTR_DIGSIG);
530 result = 0;
531 }
532 return result;
533}
534
535int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
536{
537 int result;
538
539 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
540 if (result == 1) {
541 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
542 result = 0;
543 }
544 return result;
545}