This source file includes following definitions.
- hash_setup
- mmap_violation_check
- ima_rdwr_violation_check
- ima_check_last_writer
- ima_file_free
- process_measurement
- ima_file_mmap
- ima_bprm_check
- ima_file_check
- ima_post_create_tmpfile
- ima_post_path_mknod
- ima_read_file
- ima_post_read_file
- ima_load_data
- process_buffer_measurement
- ima_kexec_cmdline
- init_ima
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
34 int ima_appraise = IMA_APPRAISE_ENFORCE;
35 #else
36 int ima_appraise;
37 #endif
38
39 int ima_hash_algo = HASH_ALGO_SHA1;
40 static int hash_setup_done;
41
42 static struct notifier_block ima_lsm_policy_notifier = {
43 .notifier_call = ima_lsm_policy_change,
44 };
45
46 static 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;
69 out:
70 hash_setup_done = 1;
71 return 1;
72 }
73 __setup("ima_hash=", hash_setup);
74
75
76 static 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
106 static 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
146 static 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
177 void 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
192 static 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 struct modsig *modsig = NULL;
206 int xattr_len = 0;
207 bool violation_check;
208 enum hash_algo hash_algo;
209
210 if (!ima_policy_flag || !S_ISREG(inode->i_mode))
211 return 0;
212
213
214
215
216
217 action = ima_get_action(inode, cred, secid, mask, func, &pcr,
218 &template_desc);
219 violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
220 (ima_policy_flag & IMA_MEASURE));
221 if (!action && !violation_check)
222 return 0;
223
224 must_appraise = action & IMA_APPRAISE;
225
226
227 if (action & IMA_FILE_APPRAISE)
228 func = FILE_CHECK;
229
230 inode_lock(inode);
231
232 if (action) {
233 iint = integrity_inode_get(inode);
234 if (!iint)
235 rc = -ENOMEM;
236 }
237
238 if (!rc && violation_check)
239 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
240 &pathbuf, &pathname, filename);
241
242 inode_unlock(inode);
243
244 if (rc)
245 goto out;
246 if (!action)
247 goto out;
248
249 mutex_lock(&iint->mutex);
250
251 if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags))
252
253 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
254 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
255 IMA_ACTION_FLAGS);
256
257
258
259
260
261
262 if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags) ||
263 ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
264 !(inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) &&
265 !(action & IMA_FAIL_UNVERIFIABLE_SIGS))) {
266 iint->flags &= ~IMA_DONE_MASK;
267 iint->measured_pcrs = 0;
268 }
269
270
271
272
273
274 iint->flags |= action;
275 action &= IMA_DO_MASK;
276 action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);
277
278
279 if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
280 action ^= IMA_MEASURE;
281
282
283 if ((action & IMA_HASH) &&
284 !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) {
285 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
286 if ((xattr_value && xattr_len > 2) &&
287 (xattr_value->type == EVM_IMA_XATTR_DIGSIG))
288 set_bit(IMA_DIGSIG, &iint->atomic_flags);
289 iint->flags |= IMA_HASHED;
290 action ^= IMA_HASH;
291 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
292 }
293
294
295 if (!action) {
296 if (must_appraise) {
297 rc = mmap_violation_check(func, file, &pathbuf,
298 &pathname, filename);
299 if (!rc)
300 rc = ima_get_cache_status(iint, func);
301 }
302 goto out_locked;
303 }
304
305 if ((action & IMA_APPRAISE_SUBMASK) ||
306 strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0) {
307
308 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
309
310
311
312
313
314
315 if (iint->flags & IMA_MODSIG_ALLOWED) {
316 rc = ima_read_modsig(func, buf, size, &modsig);
317
318 if (!rc && ima_template_has_modsig(template_desc) &&
319 iint->flags & IMA_MEASURED)
320 action |= IMA_MEASURE;
321 }
322 }
323
324 hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
325
326 rc = ima_collect_measurement(iint, file, buf, size, hash_algo, modsig);
327 if (rc != 0 && rc != -EBADF && rc != -EINVAL)
328 goto out_locked;
329
330 if (!pathbuf)
331 pathname = ima_d_path(&file->f_path, &pathbuf, filename);
332
333 if (action & IMA_MEASURE)
334 ima_store_measurement(iint, file, pathname,
335 xattr_value, xattr_len, modsig, pcr,
336 template_desc);
337 if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
338 inode_lock(inode);
339 rc = ima_appraise_measurement(func, iint, file, pathname,
340 xattr_value, xattr_len, modsig);
341 inode_unlock(inode);
342 if (!rc)
343 rc = mmap_violation_check(func, file, &pathbuf,
344 &pathname, filename);
345 }
346 if (action & IMA_AUDIT)
347 ima_audit_measurement(iint, pathname);
348
349 if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO))
350 rc = 0;
351 out_locked:
352 if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) &&
353 !(iint->flags & IMA_NEW_FILE))
354 rc = -EACCES;
355 mutex_unlock(&iint->mutex);
356 kfree(xattr_value);
357 ima_free_modsig(modsig);
358 out:
359 if (pathbuf)
360 __putname(pathbuf);
361 if (must_appraise) {
362 if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE))
363 return -EACCES;
364 if (file->f_mode & FMODE_WRITE)
365 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
366 }
367 return 0;
368 }
369
370
371
372
373
374
375
376
377
378
379
380
381 int ima_file_mmap(struct file *file, unsigned long prot)
382 {
383 u32 secid;
384
385 if (file && (prot & PROT_EXEC)) {
386 security_task_getsecid(current, &secid);
387 return process_measurement(file, current_cred(), secid, NULL,
388 0, MAY_EXEC, MMAP_CHECK);
389 }
390
391 return 0;
392 }
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407 int ima_bprm_check(struct linux_binprm *bprm)
408 {
409 int ret;
410 u32 secid;
411
412 security_task_getsecid(current, &secid);
413 ret = process_measurement(bprm->file, current_cred(), secid, NULL, 0,
414 MAY_EXEC, BPRM_CHECK);
415 if (ret)
416 return ret;
417
418 security_cred_getsecid(bprm->cred, &secid);
419 return process_measurement(bprm->file, bprm->cred, secid, NULL, 0,
420 MAY_EXEC, CREDS_CHECK);
421 }
422
423
424
425
426
427
428
429
430
431
432
433 int ima_file_check(struct file *file, int mask)
434 {
435 u32 secid;
436
437 security_task_getsecid(current, &secid);
438 return process_measurement(file, current_cred(), secid, NULL, 0,
439 mask & (MAY_READ | MAY_WRITE | MAY_EXEC |
440 MAY_APPEND), FILE_CHECK);
441 }
442 EXPORT_SYMBOL_GPL(ima_file_check);
443
444
445
446
447
448
449
450
451
452 void ima_post_create_tmpfile(struct inode *inode)
453 {
454 struct integrity_iint_cache *iint;
455 int must_appraise;
456
457 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
458 if (!must_appraise)
459 return;
460
461
462 iint = integrity_inode_get(inode);
463 if (!iint)
464 return;
465
466
467 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
468 iint->ima_file_status = INTEGRITY_PASS;
469 }
470
471
472
473
474
475
476
477
478 void ima_post_path_mknod(struct dentry *dentry)
479 {
480 struct integrity_iint_cache *iint;
481 struct inode *inode = dentry->d_inode;
482 int must_appraise;
483
484 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
485 if (!must_appraise)
486 return;
487
488
489 iint = integrity_inode_get(inode);
490 if (!iint)
491 return;
492
493
494 iint->flags |= IMA_NEW_FILE;
495 }
496
497
498
499
500
501
502
503
504
505
506
507
508 int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
509 {
510
511
512
513
514
515
516
517
518 return 0;
519 }
520
521 const int read_idmap[READING_MAX_ID] = {
522 [READING_FIRMWARE] = FIRMWARE_CHECK,
523 [READING_FIRMWARE_PREALLOC_BUFFER] = FIRMWARE_CHECK,
524 [READING_MODULE] = MODULE_CHECK,
525 [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
526 [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
527 [READING_POLICY] = POLICY_CHECK
528 };
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543 int ima_post_read_file(struct file *file, void *buf, loff_t size,
544 enum kernel_read_file_id read_id)
545 {
546 enum ima_hooks func;
547 u32 secid;
548
549 if (!file && read_id == READING_FIRMWARE) {
550 if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
551 (ima_appraise & IMA_APPRAISE_ENFORCE)) {
552 pr_err("Prevent firmware loading_store.\n");
553 return -EACCES;
554 }
555 return 0;
556 }
557
558
559 if (!file && read_id == READING_X509_CERTIFICATE)
560 return 0;
561
562 if (!file || !buf || size == 0) {
563 if (ima_appraise & IMA_APPRAISE_ENFORCE)
564 return -EACCES;
565 return 0;
566 }
567
568 func = read_idmap[read_id] ?: FILE_CHECK;
569 security_task_getsecid(current, &secid);
570 return process_measurement(file, current_cred(), secid, buf, size,
571 MAY_READ, func);
572 }
573
574
575
576
577
578
579
580
581
582
583
584 int ima_load_data(enum kernel_load_data_id id)
585 {
586 bool ima_enforce, sig_enforce;
587
588 ima_enforce =
589 (ima_appraise & IMA_APPRAISE_ENFORCE) == IMA_APPRAISE_ENFORCE;
590
591 switch (id) {
592 case LOADING_KEXEC_IMAGE:
593 if (IS_ENABLED(CONFIG_KEXEC_SIG)
594 && arch_ima_get_secureboot()) {
595 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
596 return -EACCES;
597 }
598
599 if (ima_enforce && (ima_appraise & IMA_APPRAISE_KEXEC)) {
600 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
601 return -EACCES;
602 }
603 break;
604 case LOADING_FIRMWARE:
605 if (ima_enforce && (ima_appraise & IMA_APPRAISE_FIRMWARE)) {
606 pr_err("Prevent firmware sysfs fallback loading.\n");
607 return -EACCES;
608 }
609 break;
610 case LOADING_MODULE:
611 sig_enforce = is_module_sig_enforced();
612
613 if (ima_enforce && (!sig_enforce
614 && (ima_appraise & IMA_APPRAISE_MODULES))) {
615 pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
616 return -EACCES;
617 }
618 default:
619 break;
620 }
621 return 0;
622 }
623
624
625
626
627
628
629
630
631
632
633
634 static void process_buffer_measurement(const void *buf, int size,
635 const char *eventname,
636 const struct cred *cred, u32 secid)
637 {
638 int ret = 0;
639 struct ima_template_entry *entry = NULL;
640 struct integrity_iint_cache iint = {};
641 struct ima_event_data event_data = {.iint = &iint,
642 .filename = eventname,
643 .buf = buf,
644 .buf_len = size};
645 struct ima_template_desc *template_desc = NULL;
646 struct {
647 struct ima_digest_data hdr;
648 char digest[IMA_MAX_DIGEST_SIZE];
649 } hash = {};
650 int violation = 0;
651 int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
652 int action = 0;
653
654 action = ima_get_action(NULL, cred, secid, 0, KEXEC_CMDLINE, &pcr,
655 &template_desc);
656 if (!(action & IMA_MEASURE))
657 return;
658
659 iint.ima_hash = &hash.hdr;
660 iint.ima_hash->algo = ima_hash_algo;
661 iint.ima_hash->length = hash_digest_size[ima_hash_algo];
662
663 ret = ima_calc_buffer_hash(buf, size, iint.ima_hash);
664 if (ret < 0)
665 goto out;
666
667 ret = ima_alloc_init_template(&event_data, &entry, template_desc);
668 if (ret < 0)
669 goto out;
670
671 ret = ima_store_template(entry, violation, NULL, buf, pcr);
672
673 if (ret < 0)
674 ima_free_template_entry(entry);
675
676 out:
677 return;
678 }
679
680
681
682
683
684
685
686
687 void ima_kexec_cmdline(const void *buf, int size)
688 {
689 u32 secid;
690
691 if (buf && size != 0) {
692 security_task_getsecid(current, &secid);
693 process_buffer_measurement(buf, size, "kexec-cmdline",
694 current_cred(), secid);
695 }
696 }
697
698 static int __init init_ima(void)
699 {
700 int error;
701
702 ima_init_template_list();
703 hash_setup(CONFIG_IMA_DEFAULT_HASH);
704 error = ima_init();
705
706 if (error && strcmp(hash_algo_name[ima_hash_algo],
707 CONFIG_IMA_DEFAULT_HASH) != 0) {
708 pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
709 hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH);
710 hash_setup_done = 0;
711 hash_setup(CONFIG_IMA_DEFAULT_HASH);
712 error = ima_init();
713 }
714
715 error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
716 if (error)
717 pr_warn("Couldn't register LSM notifier, error %d\n", error);
718
719 if (!error)
720 ima_update_policy_flag();
721
722 return error;
723 }
724
725 late_initcall(init_ima);