1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include <linux/module.h>
21#include <linux/file.h>
22#include <linux/binfmts.h>
23#include <linux/mount.h>
24#include <linux/mman.h>
25#include <linux/slab.h>
26#include <linux/xattr.h>
27#include <linux/ima.h>
28#include <linux/iversion.h>
29#include <linux/fs.h>
30
31#include "ima.h"
32
33#ifdef CONFIG_IMA_APPRAISE
34int ima_appraise = IMA_APPRAISE_ENFORCE;
35#else
36int ima_appraise;
37#endif
38
39int ima_hash_algo = HASH_ALGO_SHA1;
40static int hash_setup_done;
41
42static struct notifier_block ima_lsm_policy_notifier = {
43 .notifier_call = ima_lsm_policy_change,
44};
45
46static int __init hash_setup(char *str)
47{
48 struct ima_template_desc *template_desc = ima_template_desc_current();
49 int i;
50
51 if (hash_setup_done)
52 return 1;
53
54 if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
55 if (strncmp(str, "sha1", 4) == 0)
56 ima_hash_algo = HASH_ALGO_SHA1;
57 else if (strncmp(str, "md5", 3) == 0)
58 ima_hash_algo = HASH_ALGO_MD5;
59 else
60 return 1;
61 goto out;
62 }
63
64 i = match_string(hash_algo_name, HASH_ALGO__LAST, str);
65 if (i < 0)
66 return 1;
67
68 ima_hash_algo = i;
69out:
70 hash_setup_done = 1;
71 return 1;
72}
73__setup("ima_hash=", hash_setup);
74
75
76static int mmap_violation_check(enum ima_hooks func, struct file *file,
77 char **pathbuf, const char **pathname,
78 char *filename)
79{
80 struct inode *inode;
81 int rc = 0;
82
83 if ((func == MMAP_CHECK) && mapping_writably_mapped(file->f_mapping)) {
84 rc = -ETXTBSY;
85 inode = file_inode(file);
86
87 if (!*pathbuf)
88 *pathname = ima_d_path(&file->f_path, pathbuf,
89 filename);
90 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, *pathname,
91 "mmap_file", "mmapped_writers", rc, 0);
92 }
93 return rc;
94}
95
96
97
98
99
100
101
102
103
104
105
106static void ima_rdwr_violation_check(struct file *file,
107 struct integrity_iint_cache *iint,
108 int must_measure,
109 char **pathbuf,
110 const char **pathname,
111 char *filename)
112{
113 struct inode *inode = file_inode(file);
114 fmode_t mode = file->f_mode;
115 bool send_tomtou = false, send_writers = false;
116
117 if (mode & FMODE_WRITE) {
118 if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
119 if (!iint)
120 iint = integrity_iint_find(inode);
121
122 if (iint && test_bit(IMA_MUST_MEASURE,
123 &iint->atomic_flags))
124 send_tomtou = true;
125 }
126 } else {
127 if (must_measure)
128 set_bit(IMA_MUST_MEASURE, &iint->atomic_flags);
129 if (inode_is_open_for_write(inode) && must_measure)
130 send_writers = true;
131 }
132
133 if (!send_tomtou && !send_writers)
134 return;
135
136 *pathname = ima_d_path(&file->f_path, pathbuf, filename);
137
138 if (send_tomtou)
139 ima_add_violation(file, *pathname, iint,
140 "invalid_pcr", "ToMToU");
141 if (send_writers)
142 ima_add_violation(file, *pathname, iint,
143 "invalid_pcr", "open_writers");
144}
145
146static void ima_check_last_writer(struct integrity_iint_cache *iint,
147 struct inode *inode, struct file *file)
148{
149 fmode_t mode = file->f_mode;
150 bool update;
151
152 if (!(mode & FMODE_WRITE))
153 return;
154
155 mutex_lock(&iint->mutex);
156 if (atomic_read(&inode->i_writecount) == 1) {
157 update = test_and_clear_bit(IMA_UPDATE_XATTR,
158 &iint->atomic_flags);
159 if (!IS_I_VERSION(inode) ||
160 !inode_eq_iversion(inode, iint->version) ||
161 (iint->flags & IMA_NEW_FILE)) {
162 iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
163 iint->measured_pcrs = 0;
164 if (update)
165 ima_update_xattr(iint, file);
166 }
167 }
168 mutex_unlock(&iint->mutex);
169}
170
171
172
173
174
175
176
177void ima_file_free(struct file *file)
178{
179 struct inode *inode = file_inode(file);
180 struct integrity_iint_cache *iint;
181
182 if (!ima_policy_flag || !S_ISREG(inode->i_mode))
183 return;
184
185 iint = integrity_iint_find(inode);
186 if (!iint)
187 return;
188
189 ima_check_last_writer(iint, inode, file);
190}
191
192static int process_measurement(struct file *file, const struct cred *cred,
193 u32 secid, char *buf, loff_t size, int mask,
194 enum ima_hooks func)
195{
196 struct inode *inode = file_inode(file);
197 struct integrity_iint_cache *iint = NULL;
198 struct ima_template_desc *template_desc = NULL;
199 char *pathbuf = NULL;
200 char filename[NAME_MAX];
201 const char *pathname = NULL;
202 int rc = 0, action, must_appraise = 0;
203 int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
204 struct evm_ima_xattr_data *xattr_value = NULL;
205 int xattr_len = 0;
206 bool violation_check;
207 enum hash_algo hash_algo;
208
209 if (!ima_policy_flag || !S_ISREG(inode->i_mode))
210 return 0;
211
212
213
214
215
216 action = ima_get_action(inode, cred, secid, mask, func, &pcr,
217 &template_desc);
218 violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
219 (ima_policy_flag & IMA_MEASURE));
220 if (!action && !violation_check)
221 return 0;
222
223 must_appraise = action & IMA_APPRAISE;
224
225
226 if (action & IMA_FILE_APPRAISE)
227 func = FILE_CHECK;
228
229 inode_lock(inode);
230
231 if (action) {
232 iint = integrity_inode_get(inode);
233 if (!iint)
234 rc = -ENOMEM;
235 }
236
237 if (!rc && violation_check)
238 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
239 &pathbuf, &pathname, filename);
240
241 inode_unlock(inode);
242
243 if (rc)
244 goto out;
245 if (!action)
246 goto out;
247
248 mutex_lock(&iint->mutex);
249
250 if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags))
251
252 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
253 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
254 IMA_ACTION_FLAGS);
255
256
257
258
259
260
261 if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags) ||
262 ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
263 !(inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) &&
264 !(action & IMA_FAIL_UNVERIFIABLE_SIGS))) {
265 iint->flags &= ~IMA_DONE_MASK;
266 iint->measured_pcrs = 0;
267 }
268
269
270
271
272
273 iint->flags |= action;
274 action &= IMA_DO_MASK;
275 action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);
276
277
278 if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
279 action ^= IMA_MEASURE;
280
281
282 if ((action & IMA_HASH) &&
283 !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) {
284 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
285 if ((xattr_value && xattr_len > 2) &&
286 (xattr_value->type == EVM_IMA_XATTR_DIGSIG))
287 set_bit(IMA_DIGSIG, &iint->atomic_flags);
288 iint->flags |= IMA_HASHED;
289 action ^= IMA_HASH;
290 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
291 }
292
293
294 if (!action) {
295 if (must_appraise) {
296 rc = mmap_violation_check(func, file, &pathbuf,
297 &pathname, filename);
298 if (!rc)
299 rc = ima_get_cache_status(iint, func);
300 }
301 goto out_locked;
302 }
303
304 if ((action & IMA_APPRAISE_SUBMASK) ||
305 strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
306
307 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
308
309 hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
310
311 rc = ima_collect_measurement(iint, file, buf, size, hash_algo);
312 if (rc != 0 && rc != -EBADF && rc != -EINVAL)
313 goto out_locked;
314
315 if (!pathbuf)
316 pathname = ima_d_path(&file->f_path, &pathbuf, filename);
317
318 if (action & IMA_MEASURE)
319 ima_store_measurement(iint, file, pathname,
320 xattr_value, xattr_len, pcr,
321 template_desc);
322 if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
323 inode_lock(inode);
324 rc = ima_appraise_measurement(func, iint, file, pathname,
325 xattr_value, xattr_len);
326 inode_unlock(inode);
327 if (!rc)
328 rc = mmap_violation_check(func, file, &pathbuf,
329 &pathname, filename);
330 }
331 if (action & IMA_AUDIT)
332 ima_audit_measurement(iint, pathname);
333
334 if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO))
335 rc = 0;
336out_locked:
337 if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) &&
338 !(iint->flags & IMA_NEW_FILE))
339 rc = -EACCES;
340 mutex_unlock(&iint->mutex);
341 kfree(xattr_value);
342out:
343 if (pathbuf)
344 __putname(pathbuf);
345 if (must_appraise) {
346 if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE))
347 return -EACCES;
348 if (file->f_mode & FMODE_WRITE)
349 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
350 }
351 return 0;
352}
353
354
355
356
357
358
359
360
361
362
363
364
365int ima_file_mmap(struct file *file, unsigned long prot)
366{
367 u32 secid;
368
369 if (file && (prot & PROT_EXEC)) {
370 security_task_getsecid(current, &secid);
371 return process_measurement(file, current_cred(), secid, NULL,
372 0, MAY_EXEC, MMAP_CHECK);
373 }
374
375 return 0;
376}
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391int ima_bprm_check(struct linux_binprm *bprm)
392{
393 int ret;
394 u32 secid;
395
396 security_task_getsecid(current, &secid);
397 ret = process_measurement(bprm->file, current_cred(), secid, NULL, 0,
398 MAY_EXEC, BPRM_CHECK);
399 if (ret)
400 return ret;
401
402 security_cred_getsecid(bprm->cred, &secid);
403 return process_measurement(bprm->file, bprm->cred, secid, NULL, 0,
404 MAY_EXEC, CREDS_CHECK);
405}
406
407
408
409
410
411
412
413
414
415
416
417int ima_file_check(struct file *file, int mask)
418{
419 u32 secid;
420
421 security_task_getsecid(current, &secid);
422 return process_measurement(file, current_cred(), secid, NULL, 0,
423 mask & (MAY_READ | MAY_WRITE | MAY_EXEC |
424 MAY_APPEND), FILE_CHECK);
425}
426EXPORT_SYMBOL_GPL(ima_file_check);
427
428
429
430
431
432
433
434
435
436void ima_post_create_tmpfile(struct inode *inode)
437{
438 struct integrity_iint_cache *iint;
439 int must_appraise;
440
441 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
442 if (!must_appraise)
443 return;
444
445
446 iint = integrity_inode_get(inode);
447 if (!iint)
448 return;
449
450
451 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
452 iint->ima_file_status = INTEGRITY_PASS;
453}
454
455
456
457
458
459
460
461
462void ima_post_path_mknod(struct dentry *dentry)
463{
464 struct integrity_iint_cache *iint;
465 struct inode *inode = dentry->d_inode;
466 int must_appraise;
467
468 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
469 if (!must_appraise)
470 return;
471
472
473 iint = integrity_inode_get(inode);
474 if (!iint)
475 return;
476
477
478 iint->flags |= IMA_NEW_FILE;
479}
480
481
482
483
484
485
486
487
488
489
490
491
492int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
493{
494
495
496
497
498
499
500
501
502 return 0;
503}
504
505static const int read_idmap[READING_MAX_ID] = {
506 [READING_FIRMWARE] = FIRMWARE_CHECK,
507 [READING_FIRMWARE_PREALLOC_BUFFER] = FIRMWARE_CHECK,
508 [READING_MODULE] = MODULE_CHECK,
509 [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
510 [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
511 [READING_POLICY] = POLICY_CHECK
512};
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527int ima_post_read_file(struct file *file, void *buf, loff_t size,
528 enum kernel_read_file_id read_id)
529{
530 enum ima_hooks func;
531 u32 secid;
532
533 if (!file && read_id == READING_FIRMWARE) {
534 if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
535 (ima_appraise & IMA_APPRAISE_ENFORCE)) {
536 pr_err("Prevent firmware loading_store.\n");
537 return -EACCES;
538 }
539 return 0;
540 }
541
542
543 if (!file && read_id == READING_X509_CERTIFICATE)
544 return 0;
545
546 if (!file || !buf || size == 0) {
547 if (ima_appraise & IMA_APPRAISE_ENFORCE)
548 return -EACCES;
549 return 0;
550 }
551
552 func = read_idmap[read_id] ?: FILE_CHECK;
553 security_task_getsecid(current, &secid);
554 return process_measurement(file, current_cred(), secid, buf, size,
555 MAY_READ, func);
556}
557
558
559
560
561
562
563
564
565
566
567
568int ima_load_data(enum kernel_load_data_id id)
569{
570 bool ima_enforce, sig_enforce;
571
572 ima_enforce =
573 (ima_appraise & IMA_APPRAISE_ENFORCE) == IMA_APPRAISE_ENFORCE;
574
575 switch (id) {
576 case LOADING_KEXEC_IMAGE:
577 if (IS_ENABLED(CONFIG_KEXEC_VERIFY_SIG)
578 && arch_ima_get_secureboot()) {
579 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
580 return -EACCES;
581 }
582
583 if (ima_enforce && (ima_appraise & IMA_APPRAISE_KEXEC)) {
584 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
585 return -EACCES;
586 }
587 break;
588 case LOADING_FIRMWARE:
589 if (ima_enforce && (ima_appraise & IMA_APPRAISE_FIRMWARE)) {
590 pr_err("Prevent firmware sysfs fallback loading.\n");
591 return -EACCES;
592 }
593 break;
594 case LOADING_MODULE:
595 sig_enforce = is_module_sig_enforced();
596
597 if (ima_enforce && (!sig_enforce
598 && (ima_appraise & IMA_APPRAISE_MODULES))) {
599 pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
600 return -EACCES;
601 }
602 default:
603 break;
604 }
605 return 0;
606}
607
608
609
610
611
612
613
614
615
616
617
618static void process_buffer_measurement(const void *buf, int size,
619 const char *eventname,
620 const struct cred *cred, u32 secid)
621{
622 int ret = 0;
623 struct ima_template_entry *entry = NULL;
624 struct integrity_iint_cache iint = {};
625 struct ima_event_data event_data = {.iint = &iint,
626 .filename = eventname,
627 .buf = buf,
628 .buf_len = size};
629 struct ima_template_desc *template_desc = NULL;
630 struct {
631 struct ima_digest_data hdr;
632 char digest[IMA_MAX_DIGEST_SIZE];
633 } hash = {};
634 int violation = 0;
635 int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
636 int action = 0;
637
638 action = ima_get_action(NULL, cred, secid, 0, KEXEC_CMDLINE, &pcr,
639 &template_desc);
640 if (!(action & IMA_MEASURE))
641 return;
642
643 iint.ima_hash = &hash.hdr;
644 iint.ima_hash->algo = ima_hash_algo;
645 iint.ima_hash->length = hash_digest_size[ima_hash_algo];
646
647 ret = ima_calc_buffer_hash(buf, size, iint.ima_hash);
648 if (ret < 0)
649 goto out;
650
651 ret = ima_alloc_init_template(&event_data, &entry, template_desc);
652 if (ret < 0)
653 goto out;
654
655 ret = ima_store_template(entry, violation, NULL, buf, pcr);
656
657 if (ret < 0)
658 ima_free_template_entry(entry);
659
660out:
661 return;
662}
663
664
665
666
667
668
669
670
671void ima_kexec_cmdline(const void *buf, int size)
672{
673 u32 secid;
674
675 if (buf && size != 0) {
676 security_task_getsecid(current, &secid);
677 process_buffer_measurement(buf, size, "kexec-cmdline",
678 current_cred(), secid);
679 }
680}
681
682static int __init init_ima(void)
683{
684 int error;
685
686 ima_init_template_list();
687 hash_setup(CONFIG_IMA_DEFAULT_HASH);
688 error = ima_init();
689
690 if (error && strcmp(hash_algo_name[ima_hash_algo],
691 CONFIG_IMA_DEFAULT_HASH) != 0) {
692 pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
693 hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH);
694 hash_setup_done = 0;
695 hash_setup(CONFIG_IMA_DEFAULT_HASH);
696 error = ima_init();
697 }
698
699 error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
700 if (error)
701 pr_warn("Couldn't register LSM notifier, error %d\n", error);
702
703 if (!error)
704 ima_update_policy_flag();
705
706 return error;
707}
708
709late_initcall(init_ima);
710