forked from falcosecurity/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
3000 lines (2605 loc) · 80.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2022 The Falco Authors.
This file is dual licensed under either the MIT or GPL 2. See MIT.txt
or GPL2.txt for full copies of the license.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/version.h>
#include <trace/syscall.h>
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 20)
#include <linux/kobject.h>
#include <trace/sched.h>
#include "ppm_syscall.h"
#else
#include <asm/syscall.h>
#endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 20) */
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 37))
#include <asm/atomic.h>
#else
#include <linux/atomic.h>
#endif
#include <linux/cdev.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kdev_t.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0))
#include <linux/sched.h>
#else
#include <linux/sched/signal.h>
#include <linux/sched/cputime.h>
#endif
#include <linux/vmalloc.h>
#include <linux/wait.h>
#include <linux/tracepoint.h>
#include <linux/cpu.h>
#include <linux/jiffies.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
#include <linux/file.h>
#else
#include <linux/fdtable.h>
#endif
#include <net/sock.h>
#include <asm/unistd.h>
#include "driver_config.h"
#include "ppm_ringbuffer.h"
#include "ppm_events_public.h"
#include "ppm_events.h"
#include "ppm.h"
#include "ppm_tp.h"
#if defined(CONFIG_IA32_EMULATION) && !defined(__NR_ia32_socketcall)
#include "ppm_compat_unistd_32.h"
#endif
MODULE_LICENSE("GPL");
MODULE_AUTHOR("the Falco authors");
#if defined(CAPTURE_SCHED_PROC_EXEC) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
#error The kernel module CAPTURE_SCHED_PROC_EXEC support requires kernel versions greater or equal than '3.4'.
#endif
#if defined(CAPTURE_SCHED_PROC_FORK) && (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
#error The kernel module CAPTURE_SCHED_PROC_FORK support requires kernel versions greater or equal than '2.6'.
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35))
#define TRACEPOINT_PROBE_REGISTER(p1, p2) tracepoint_probe_register(p1, p2)
#define TRACEPOINT_PROBE_UNREGISTER(p1, p2) tracepoint_probe_unregister(p1, p2)
#define TRACEPOINT_PROBE(probe, args...) static void probe(args)
#else
#define TRACEPOINT_PROBE_REGISTER(p1, p2) tracepoint_probe_register(p1, p2, NULL)
#define TRACEPOINT_PROBE_UNREGISTER(p1, p2) tracepoint_probe_unregister(p1, p2, NULL)
#define TRACEPOINT_PROBE(probe, args...) static void probe(void *__data, args)
#endif
// Allow build even on arch where PAGE_ENC is not implemented
#ifndef _PAGE_ENC
#define _PAGE_ENC 0
#endif
struct ppm_device {
dev_t dev;
struct cdev cdev;
wait_queue_head_t read_queue;
};
struct event_data_t {
enum ppm_capture_category category;
int socketcall_syscall;
bool compat;
union {
struct {
struct pt_regs *regs;
long id;
const struct syscall_evt_pair *cur_g_syscall_table;
} syscall_data;
struct {
struct task_struct *sched_prev;
struct task_struct *sched_next;
} context_data;
struct {
int sig;
struct siginfo *info;
struct k_sigaction *ka;
} signal_data;
#ifdef CAPTURE_SCHED_PROC_FORK
/* Here we save only the child task struct since it is the
* unique parameter we will use in our `f_sched_prog_fork`
* filler. On the other side the `f_sched_prog_exec` filler
* won't need any tracepoint parameter so we don't need a
* internal struct here.
*/
struct {
struct task_struct *child;
} sched_proc_fork_data;
#endif
struct fault_data_t fault_data;
} event_info;
};
/*
* FORWARD DECLARATIONS
*/
static int ppm_open(struct inode *inode, struct file *filp);
static int ppm_release(struct inode *inode, struct file *filp);
static int force_tp_set(u32 new_tp_set, u32 max_val);
static long ppm_ioctl(struct file *f, unsigned int cmd, unsigned long arg);
static int ppm_mmap(struct file *filp, struct vm_area_struct *vma);
static int record_event_consumer(struct ppm_consumer_t *consumer,
enum ppm_event_type event_type,
enum syscall_flags drop_flags,
nanoseconds ns,
struct event_data_t *event_datap);
static void record_event_all_consumers(enum ppm_event_type event_type,
enum syscall_flags drop_flags,
struct event_data_t *event_datap);
static int init_ring_buffer(struct ppm_ring_buffer_context *ring, unsigned long buffer_bytes_dim);
static void free_ring_buffer(struct ppm_ring_buffer_context *ring);
static void reset_ring_buffer(struct ppm_ring_buffer_context *ring);
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0))
void ppm_task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st);
#endif
#ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
#error The kernel must have HAVE_SYSCALL_TRACEPOINTS in order to work
#endif
TRACEPOINT_PROBE(syscall_enter_probe, struct pt_regs *regs, long id);
TRACEPOINT_PROBE(syscall_exit_probe, struct pt_regs *regs, long ret);
TRACEPOINT_PROBE(syscall_procexit_probe, struct task_struct *p);
#ifdef CAPTURE_CONTEXT_SWITCHES
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35))
TRACEPOINT_PROBE(sched_switch_probe, struct rq *rq, struct task_struct *prev, struct task_struct *next);
#elif (LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0))
TRACEPOINT_PROBE(sched_switch_probe, struct task_struct *prev, struct task_struct *next);
#else
TRACEPOINT_PROBE(sched_switch_probe, bool preempt, struct task_struct *prev, struct task_struct *next);
#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)) */
#endif /* CAPTURE_CONTEXT_SWITCHES */
#ifdef CAPTURE_SIGNAL_DELIVERIES
TRACEPOINT_PROBE(signal_deliver_probe, int sig, struct siginfo *info, struct k_sigaction *ka);
#endif
/* tracepoints `page_fault_user/kernel` don't exist on some architectures.*/
#ifdef CAPTURE_PAGE_FAULTS
TRACEPOINT_PROBE(page_fault_probe, unsigned long address, struct pt_regs *regs, unsigned long error_code);
#endif
#ifdef CAPTURE_SCHED_PROC_FORK
TRACEPOINT_PROBE(sched_proc_fork_probe, struct task_struct *parent, struct task_struct *child);
#endif
#ifdef CAPTURE_SCHED_PROC_EXEC
TRACEPOINT_PROBE(sched_proc_exec_probe, struct task_struct *p, pid_t old_pid, struct linux_binprm *bprm);
#endif
static struct ppm_device *g_ppm_devs;
static struct class *g_ppm_class;
static unsigned int g_ppm_numdevs;
static int g_ppm_major;
bool g_tracers_enabled = false;
static DEFINE_PER_CPU(long, g_n_tracepoint_hit);
static const struct file_operations g_ppm_fops = {
.open = ppm_open,
.release = ppm_release,
.mmap = ppm_mmap,
.unlocked_ioctl = ppm_ioctl,
.owner = THIS_MODULE,
};
/*
* GLOBALS
*/
#define DEFAULT_BUFFER_BYTES_DIM 8 * 1024 * 1024;
LIST_HEAD(g_consumer_list);
static DEFINE_MUTEX(g_consumer_mutex);
static u32 g_tracepoints_attached; // list of attached tracepoints; bitmask using ppm_tp.h enum
static unsigned long g_buffer_bytes_dim = DEFAULT_BUFFER_BYTES_DIM; // dimension of a single per-CPU buffer in bytes.
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
static struct tracepoint *tp_sys_enter;
static struct tracepoint *tp_sys_exit;
#endif
static struct tracepoint *tp_sched_process_exit;
#ifdef CAPTURE_CONTEXT_SWITCHES
static struct tracepoint *tp_sched_switch;
#endif
#ifdef CAPTURE_SIGNAL_DELIVERIES
static struct tracepoint *tp_signal_deliver;
#endif
#ifdef CAPTURE_PAGE_FAULTS
// Even in kernels that can support page fault tracepoints, tracepoints may be
// disabled so check if g_fault_tracepoint_disabled is set.
static struct tracepoint *tp_page_fault_user;
static struct tracepoint *tp_page_fault_kernel;
static bool g_fault_tracepoint_disabled;
#endif
#ifdef CAPTURE_SCHED_PROC_FORK
static struct tracepoint *tp_sched_proc_fork;
#endif
#ifdef CAPTURE_SCHED_PROC_EXEC
static struct tracepoint *tp_sched_proc_exec;
#endif
#ifdef _DEBUG
static bool verbose = 1;
#else
static bool verbose = 0;
#endif
static unsigned int max_consumers = 5;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0))
static enum cpuhp_state hp_state = 0;
#endif
#define vpr_info(fmt, ...) \
do { \
if (verbose) \
pr_info(fmt, ##__VA_ARGS__); \
} while (0)
static inline nanoseconds ppm_nsecs(void)
{
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0))
return ktime_get_real_ns();
#else
/* Don't have ktime_get_real functions */
struct timespec ts;
getnstimeofday(&ts);
return SECOND_IN_NS * ts.tv_sec + ts.tv_nsec;
#endif
}
inline void ppm_syscall_get_arguments(struct task_struct *task, struct pt_regs *regs, unsigned long *args)
{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 1, 0))
syscall_get_arguments(task, regs, 0, 6, args);
#else
syscall_get_arguments(task, regs, args);
#endif
}
/* compat tracepoint functions */
static int compat_register_trace(void *func, const char *probename, struct tracepoint *tp)
{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0))
return TRACEPOINT_PROBE_REGISTER(probename, func);
#else
return tracepoint_probe_register(tp, func, NULL);
#endif
}
static void compat_unregister_trace(void *func, const char *probename, struct tracepoint *tp)
{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0))
TRACEPOINT_PROBE_UNREGISTER(probename, func);
#else
tracepoint_probe_unregister(tp, func, NULL);
#endif
}
static struct ppm_consumer_t *ppm_find_consumer(struct task_struct *consumer_id)
{
struct ppm_consumer_t *el = NULL;
rcu_read_lock();
list_for_each_entry_rcu(el, &g_consumer_list, node) {
if (el->consumer_id == consumer_id) {
rcu_read_unlock();
return el;
}
}
rcu_read_unlock();
return NULL;
}
static void check_remove_consumer(struct ppm_consumer_t *consumer, int remove_from_list)
{
int cpu;
int open_rings = 0;
for_each_possible_cpu(cpu) {
struct ppm_ring_buffer_context *ring = per_cpu_ptr(consumer->ring_buffers, cpu);
if (ring && ring->open)
++open_rings;
}
if (open_rings == 0) {
pr_info("deallocating consumer %p\n", consumer->consumer_id);
if (remove_from_list) {
list_del_rcu(&consumer->node);
synchronize_rcu();
}
for_each_possible_cpu(cpu) {
struct ppm_ring_buffer_context *ring = per_cpu_ptr(consumer->ring_buffers, cpu);
free_ring_buffer(ring);
}
free_percpu(consumer->ring_buffers);
vfree(consumer);
}
}
/*
* user I/O functions
*/
static int ppm_open(struct inode *inode, struct file *filp)
{
int ret;
u32 val;
int in_list = false;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
int ring_no = iminor(filp->f_path.dentry->d_inode);
#else
int ring_no = iminor(filp->f_dentry->d_inode);
#endif
struct task_struct *consumer_id = current;
struct ppm_consumer_t *consumer = NULL;
struct ppm_ring_buffer_context *ring = NULL;
/*
* Tricky: to identify a consumer, attach the thread id
* to the newly open file descriptor
*/
filp->private_data = consumer_id;
mutex_lock(&g_consumer_mutex);
consumer = ppm_find_consumer(consumer_id);
if (!consumer) {
unsigned int cpu;
unsigned int num_consumers = 0;
struct ppm_consumer_t *el = NULL;
rcu_read_lock();
list_for_each_entry_rcu(el, &g_consumer_list, node) {
++num_consumers;
}
rcu_read_unlock();
if (num_consumers >= max_consumers) {
pr_err("maximum number of consumers reached\n");
ret = -EBUSY;
goto cleanup_open;
}
pr_info("adding new consumer %p\n", consumer_id);
consumer = vmalloc(sizeof(struct ppm_consumer_t));
if (!consumer) {
pr_err("can't allocate consumer\n");
ret = -ENOMEM;
goto cleanup_open;
}
consumer->consumer_id = consumer_id;
consumer->buffer_bytes_dim = g_buffer_bytes_dim;
/*
* Initialize the ring buffers array
*/
consumer->ring_buffers = alloc_percpu(struct ppm_ring_buffer_context);
if (consumer->ring_buffers == NULL) {
pr_err("can't allocate the ring buffer array\n");
vfree(consumer);
ret = -ENOMEM;
goto cleanup_open;
}
/*
* Note, we have two loops here because the first one makes sure that ALL of the
* rings are properly initialized to null, since the second one could be interrupted
* and cause issues in the cleanup phase.
* This might not be necessary, because alloc_percpu memsets the allocated entries to
* 0, but better be extra safe.
*/
for_each_possible_cpu(cpu) {
ring = per_cpu_ptr(consumer->ring_buffers, cpu);
ring->cpu_online = false;
ring->str_storage = NULL;
ring->buffer = NULL;
ring->info = NULL;
}
/*
* If a cpu is offline when the consumer is first created, we
* will never get events for that cpu even if it later comes
* online via hotplug. We could allocate these rings on-demand
* later in this function if needed for hotplug, but that
* requires the consumer to know to call open again, and that is
* not supported.
*/
for_each_online_cpu(cpu) {
ring = per_cpu_ptr(consumer->ring_buffers, cpu);
pr_info("initializing ring buffer for CPU %u\n", cpu);
if (!init_ring_buffer(ring, consumer->buffer_bytes_dim)) {
pr_err("can't initialize the ring buffer for CPU %u\n", cpu);
ret = -ENOMEM;
goto err_init_ring_buffer;
}
ring->cpu_online = true;
}
list_add_rcu(&consumer->node, &g_consumer_list);
in_list = true;
} else {
vpr_info("found already existent consumer %p\n", consumer_id);
}
ring = per_cpu_ptr(consumer->ring_buffers, ring_no);
/*
* Check if the CPU pointed by this device is online. If it isn't stop here and
* return ENODEV. The cpu could be online while buffer is NULL if there's a cpu
* online hotplug callback between the first open on this consumer and the open
* for this particular device.
*/
if (ring->cpu_online == false || ring->buffer == NULL) {
ret = -ENODEV;
goto cleanup_open;
}
if (ring->open) {
pr_err("invalid operation: attempting to open device %d multiple times for consumer %p\n", ring_no, consumer->consumer_id);
ret = -EBUSY;
goto cleanup_open;
}
vpr_info("opening ring %d, consumer %p\n", ring_no, consumer->consumer_id);
/*
* ring->preempt_count is not reset to 0 on purpose, to prevent a race condition:
* if the same device is quickly closed and then reopened, record_event() might still be executing
* (with ring->preempt_count to 1) while ppm_open() resets ring->preempt_count to 0.
* When record_event() will exit, it will decrease
* ring->preempt_count which will become < 0, leading to the complete loss of all the events for that CPU.
*/
consumer->dropping_mode = 0;
consumer->snaplen = RW_SNAPLEN;
consumer->sampling_ratio = 1;
consumer->sampling_interval = 0;
consumer->is_dropping = 0;
consumer->do_dynamic_snaplen = false;
consumer->need_to_insert_drop_e = 0;
consumer->need_to_insert_drop_x = 0;
consumer->fullcapture_port_range_start = 0;
consumer->fullcapture_port_range_end = 0;
consumer->statsd_port = PPM_PORT_STATSD;
bitmap_fill(consumer->events_mask, PPM_EVENT_MAX); /* Enable all syscall to be passed to userspace */
reset_ring_buffer(ring);
ring->open = true;
if (g_tracepoints_attached == 0) {
pr_info("starting capture\n");
/*
* Enable the tracepoints
*/
val = (1 << TP_VAL_MAX) - 1;
ret = force_tp_set(val, TP_VAL_MAX);
if (ret != 0)
{
goto err_tp_set;
}
}
ret = 0;
goto cleanup_open;
err_tp_set:
ring->open = false;
err_init_ring_buffer:
check_remove_consumer(consumer, in_list);
cleanup_open:
mutex_unlock(&g_consumer_mutex);
return ret;
}
static int ppm_release(struct inode *inode, struct file *filp)
{
int cpu;
int ret;
struct ppm_ring_buffer_context *ring;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
int ring_no = iminor(filp->f_path.dentry->d_inode);
#else
int ring_no = iminor(filp->f_dentry->d_inode);
#endif
struct task_struct *consumer_id = filp->private_data;
struct ppm_consumer_t *consumer = NULL;
mutex_lock(&g_consumer_mutex);
consumer = ppm_find_consumer(consumer_id);
if (!consumer) {
pr_err("release: unknown consumer %p\n", consumer_id);
ret = -EBUSY;
goto cleanup_release;
}
ring = per_cpu_ptr(consumer->ring_buffers, ring_no);
if (!ring) {
ASSERT(false);
ret = -ENODEV;
goto cleanup_release;
}
if (!ring->open) {
pr_err("attempting to close unopened device %d for consumer %p\n", ring_no, consumer_id);
ret = -EBUSY;
goto cleanup_release;
}
ring->capture_enabled = false;
vpr_info("closing ring %d, consumer:%p evt:%llu, dr_buf:%llu, dr_buf_clone_fork_e:%llu, dr_buf_clone_fork_x:%llu, dr_buf_execve_e:%llu, dr_buf_execve_x:%llu, dr_buf_connect_e:%llu, dr_buf_connect_x:%llu, dr_buf_open_e:%llu, dr_buf_open_x:%llu, dr_buf_dir_file_e:%llu, dr_buf_dir_file_x:%llu, dr_buf_other_e:%llu, dr_buf_other_x:%llu, dr_pf:%llu, pr:%llu, cs:%llu\n",
ring_no,
consumer_id,
ring->info->n_evts,
ring->info->n_drops_buffer,
ring->info->n_drops_buffer_clone_fork_enter,
ring->info->n_drops_buffer_clone_fork_exit,
ring->info->n_drops_buffer_execve_enter,
ring->info->n_drops_buffer_execve_exit,
ring->info->n_drops_buffer_connect_enter,
ring->info->n_drops_buffer_connect_exit,
ring->info->n_drops_buffer_open_enter,
ring->info->n_drops_buffer_open_exit,
ring->info->n_drops_buffer_dir_file_enter,
ring->info->n_drops_buffer_dir_file_exit,
ring->info->n_drops_buffer_other_interest_enter,
ring->info->n_drops_buffer_other_interest_exit,
ring->info->n_drops_pf,
ring->info->n_preemptions,
ring->info->n_context_switches);
ring->open = false;
check_remove_consumer(consumer, true);
/*
* The last closed device stops event collection
*/
if (list_empty(&g_consumer_list)) {
if (g_tracepoints_attached != 0) {
pr_info("no more consumers, stopping capture\n");
force_tp_set(0, TP_VAL_MAX);
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
tracepoint_synchronize_unregister();
#endif
/*
* Reset tracepoint counter
*/
for_each_possible_cpu(cpu) {
per_cpu(g_n_tracepoint_hit, cpu) = 0;
}
} else {
ASSERT(false);
}
}
ret = 0;
cleanup_release:
mutex_unlock(&g_consumer_mutex);
return ret;
}
static int compat_set_tracepoint(void *func, const char *probename, struct tracepoint *tp, bool enabled)
{
int ret = 0;
if (enabled)
{
ret = compat_register_trace(func, probename, tp);
}
else
{
compat_unregister_trace(func, probename, tp);
}
return ret;
}
static int force_tp_set(u32 new_tp_set, u32 max_val)
{
u32 idx;
u32 new_val;
u32 curr_val;
int ret = 0;
if (new_tp_set == g_tracepoints_attached)
{
// ok already equal
return ret;
}
for(idx = 0; idx < max_val && ret == 0; idx++)
{
new_val = new_tp_set & (1 << idx);
curr_val = g_tracepoints_attached & (1 << idx);
if(new_val == curr_val)
{
// no change needed
continue;
}
switch(idx)
{
case SYS_ENTER:
if(new_val)
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
ret = compat_register_trace(syscall_enter_probe, tp_names[idx], tp_sys_enter);
#else
ret = register_trace_syscall_enter(syscall_enter_probe);
#endif
}
else
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
compat_unregister_trace(syscall_enter_probe, tp_names[idx], tp_sys_enter);
#else
unregister_trace_syscall_enter(syscall_enter_probe);
#endif
}
break;
case SYS_EXIT:
if(new_val)
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
ret = compat_register_trace(syscall_exit_probe, tp_names[idx], tp_sys_exit);
#else
ret = register_trace_syscall_exit(syscall_exit_probe);
#endif
}
else
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
compat_unregister_trace(syscall_exit_probe, tp_names[idx], tp_sys_exit);
#else
unregister_trace_syscall_exit(syscall_exit_probe);
#endif
}
break;
case SCHED_PROC_EXIT:
ret = compat_set_tracepoint(syscall_procexit_probe, tp_names[idx], tp_sched_process_exit, new_val);
break;
#ifdef CAPTURE_CONTEXT_SWITCHES
case SCHED_SWITCH:
ret = compat_set_tracepoint(sched_switch_probe, tp_names[idx], tp_sched_switch, new_val);
break;
#endif
#ifdef CAPTURE_PAGE_FAULTS
case PAGE_FAULT_USER:
if (!g_fault_tracepoint_disabled)
{
ret = compat_set_tracepoint(page_fault_probe, tp_names[idx], tp_page_fault_user, new_val);
}
break;
case PAGE_FAULT_KERN:
if (!g_fault_tracepoint_disabled)
{
ret = compat_set_tracepoint(page_fault_probe, tp_names[idx], tp_page_fault_kernel, new_val);
}
break;
#endif
#ifdef CAPTURE_SIGNAL_DELIVERIES
case SIGNAL_DELIVER:
ret = compat_set_tracepoint(signal_deliver_probe, tp_names[idx], tp_signal_deliver, new_val);
break;
#endif
#ifdef CAPTURE_SCHED_PROC_FORK
case SCHED_PROC_FORK:
ret = compat_set_tracepoint(sched_proc_fork_probe, tp_names[idx], tp_sched_proc_fork, new_val);
break;
#endif
#ifdef CAPTURE_SCHED_PROC_EXEC
case SCHED_PROC_EXEC:
ret = compat_set_tracepoint(sched_proc_exec_probe, tp_names[idx], tp_sched_proc_exec, new_val);
break;
#endif
default:
// unmanaged idx
break;
}
if (new_val)
{
if (ret == 0)
{
g_tracepoints_attached |= 1 << idx;
}
else
{
pr_err("can't attach the %s tracepoint\n", tp_names[idx]);
}
}
else
{
if (ret == 0)
{
g_tracepoints_attached &= ~(1 << idx);
}
else
{
pr_err("can't detach the %s tracepoint\n", tp_names[idx]);
}
}
}
if (ret != 0)
{
// Error: reset first idx-1 bits to 0.
// This means that we are requesting to unregister first
// idx-1 tracepoints, that are the succedeed ones before the error.
force_tp_set(0, idx - 1);
}
return ret;
}
static long ppm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
int cpu;
int ret;
struct task_struct *consumer_id = filp->private_data;
struct ppm_consumer_t *consumer = NULL;
if (cmd == PPM_IOCTL_GET_PROCLIST) {
struct ppm_proclist_info *proclist_info = NULL;
struct task_struct *p, *t;
u64 nentries = 0;
struct ppm_proclist_info pli;
u32 memsize;
if (copy_from_user(&pli, (void *)arg, sizeof(pli))) {
ret = -EINVAL;
goto cleanup_ioctl_nolock;
}
if(pli.max_entries < 0 || pli.max_entries > 1000000)
{
vpr_info("PPM_IOCTL_GET_PROCLIST: invalid max_entries %llu\n", pli.max_entries);
ret = -EINVAL;
goto cleanup_ioctl_procinfo;
}
vpr_info("PPM_IOCTL_GET_PROCLIST, size=%d\n", (int)pli.max_entries);
memsize = sizeof(struct ppm_proclist_info) + sizeof(struct ppm_proc_info) * pli.max_entries;
proclist_info = vmalloc(memsize);
if (!proclist_info) {
ret = -EINVAL;
goto cleanup_ioctl_nolock;
}
proclist_info->max_entries = pli.max_entries;
rcu_read_lock();
#ifdef for_each_process_thread
for_each_process_thread(p, t) {
#else
#ifdef for_each_process_all
for_each_process_all(p) {
#else
for_each_process(p) {
#endif
t = p;
do {
task_lock(p);
#endif
if (nentries < pli.max_entries) {
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0))
cputime_t utime, stime;
#else
u64 utime, stime;
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
task_cputime_adjusted(t, &utime, &stime);
#else
ppm_task_cputime_adjusted(t, &utime, &stime);
#endif
proclist_info->entries[nentries].pid = t->pid;
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0))
proclist_info->entries[nentries].utime = cputime_to_clock_t(utime);
proclist_info->entries[nentries].stime = cputime_to_clock_t(stime);
#else
proclist_info->entries[nentries].utime = nsec_to_clock_t(utime);
proclist_info->entries[nentries].stime = nsec_to_clock_t(stime);
#endif
}
nentries++;
#ifdef for_each_process_thread
}
#else
task_unlock(p);
#ifdef while_each_thread_all
} while_each_thread_all(p, t);
}
#else
} while_each_thread(p, t);
}
#endif
#endif
rcu_read_unlock();
proclist_info->n_entries = nentries;
if (nentries >= pli.max_entries) {
vpr_info("PPM_IOCTL_GET_PROCLIST: not enough space (%d avail, %d required)\n",
(int)pli.max_entries,
(int)nentries);
if (copy_to_user((void *)arg, proclist_info, sizeof(struct ppm_proclist_info))) {
ret = -EINVAL;
goto cleanup_ioctl_procinfo;
}
ret = -ENOSPC;
goto cleanup_ioctl_procinfo;
} else {
memsize = sizeof(struct ppm_proclist_info) + sizeof(struct ppm_proc_info) * nentries;
if (copy_to_user((void *)arg, proclist_info, memsize)) {
ret = -EINVAL;
goto cleanup_ioctl_procinfo;
}
}
ret = 0;
cleanup_ioctl_procinfo:
vfree((void *)proclist_info);
goto cleanup_ioctl_nolock;
}
if (cmd == PPM_IOCTL_GET_N_TRACEPOINT_HIT) {
long __user *counters = (long __user *) arg;
for_each_possible_cpu(cpu) {
if (put_user(per_cpu(g_n_tracepoint_hit, cpu), &counters[cpu])) {
ret = -EINVAL;
goto cleanup_ioctl_nolock;
}
}
ret = 0;
goto cleanup_ioctl_nolock;
} else if (cmd == PPM_IOCTL_GET_DRIVER_VERSION) {
if (copy_to_user((void *)arg, DRIVER_VERSION, sizeof(DRIVER_VERSION))) {
ret = -EINVAL;
goto cleanup_ioctl_nolock;
}
ret = 0;
goto cleanup_ioctl_nolock;
} else if (cmd == PPM_IOCTL_GET_API_VERSION) {
unsigned long long __user *out = (unsigned long long __user *) arg;
ret = 0;
if(put_user(PPM_API_CURRENT_VERSION, out))
ret = -EINVAL;
goto cleanup_ioctl_nolock;
} else if (cmd == PPM_IOCTL_GET_SCHEMA_VERSION) {
unsigned long long __user *out = (unsigned long long __user *) arg;
ret = 0;
if(put_user(PPM_SCHEMA_CURRENT_VERSION, out))
ret = -EINVAL;
goto cleanup_ioctl_nolock;
} else if (cmd == PPM_IOCTL_GET_TPMASK) {
u32 __user *out = (u32 __user *) arg;
ret = 0;
if(put_user(g_tracepoints_attached, out))
ret = -EINVAL;
goto cleanup_ioctl_nolock;
}
mutex_lock(&g_consumer_mutex);
consumer = ppm_find_consumer(consumer_id);
if (!consumer) {
pr_err("ioctl: unknown consumer %p\n", consumer_id);
ret = -EBUSY;
goto cleanup_ioctl;
}
switch (cmd) {
case PPM_IOCTL_DISABLE_CAPTURE:
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
int ring_no = iminor(filp->f_path.dentry->d_inode);
#else
int ring_no = iminor(filp->f_dentry->d_inode);
#endif
struct ppm_ring_buffer_context *ring = per_cpu_ptr(consumer->ring_buffers, ring_no);
if (!ring) {
ASSERT(false);
ret = -ENODEV;
goto cleanup_ioctl;
}
ring->capture_enabled = false;
vpr_info("PPM_IOCTL_DISABLE_CAPTURE for ring %d, consumer %p\n", ring_no, consumer_id);
ret = 0;
goto cleanup_ioctl;
}
case PPM_IOCTL_ENABLE_CAPTURE:
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
int ring_no = iminor(filp->f_path.dentry->d_inode);
#else
int ring_no = iminor(filp->f_dentry->d_inode);
#endif
struct ppm_ring_buffer_context *ring = per_cpu_ptr(consumer->ring_buffers, ring_no);
if (!ring) {
ASSERT(false);
ret = -ENODEV;
goto cleanup_ioctl;
}
ring->capture_enabled = true;
vpr_info("PPM_IOCTL_ENABLE_CAPTURE for ring %d, consumer %p\n", ring_no, consumer_id);
ret = 0;
goto cleanup_ioctl;
}
case PPM_IOCTL_DISABLE_DROPPING_MODE:
{
struct event_data_t event_data;
vpr_info("PPM_IOCTL_DISABLE_DROPPING_MODE, consumer %p\n", consumer_id);
consumer->dropping_mode = 0;
consumer->sampling_interval = 1000000000;
consumer->sampling_ratio = 1;
/*
* Push an event into the ring buffer so that the user can know that dropping
* mode has been disabled
*/
event_data.category = PPMC_CONTEXT_SWITCH;
event_data.event_info.context_data.sched_prev = (void *)DEI_DISABLE_DROPPING;
event_data.event_info.context_data.sched_next = (void *)0;