-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlkcd_km.c
8645 lines (8354 loc) · 287 KB
/
lkcd_km.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
#include <linux/module.h>
#include <linux/highmem.h>
#include <linux/kallsyms.h>
#include <linux/tty.h>
#include <linux/ptrace.h>
#include <linux/version.h>
#include <linux/binfmts.h>
#include <linux/sysrq.h>
#include <linux/slab.h>
#include <linux/hugetlb.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(6,0,0)
#ifdef CONFIG_SLAB
#include <linux/slab_def.h>
#endif
#ifdef CONFIG_SLUB
#include <linux/slub_def.h>
#endif
#endif
#include <asm/io.h>
#include <linux/vmalloc.h>
#include <linux/mman.h>
#include <linux/fs.h>
#include <linux/debugfs.h>
#include <linux/namei.h>
#include <linux/kernfs.h>
#include <linux/console.h>
#ifdef __x86_64__
#include <asm/segment.h>
#include <asm/uaccess.h>
#endif
#include <linux/rbtree.h>
#ifdef CONFIG_UPROBES
#include <linux/uprobes.h>
#endif
#ifdef CONFIG_KPROBES
#include <linux/kprobes.h>
#endif
#ifdef CONFIG_FSNOTIFY
#include <linux/fsnotify_backend.h>
#include <linux/mount.h>
#include "mnt.h"
#endif /* CONFIG_FSNOTIFY */
#include <linux/smp.h>
#include <linux/cpufreq.h>
#include <linux/clk.h>
#include <linux/devfreq.h>
#include <linux/miscdevice.h>
#include <linux/notifier.h>
#ifdef CONFIG_USER_RETURN_NOTIFIER
#include <linux/user-return-notifier.h>
#endif /* CONFIG_USER_RETURN_NOTIFIER */
#include <linux/percpu.h>
#include <linux/mm.h>
#include <linux/uaccess.h>
#include <linux/vmalloc.h>
#ifdef CONFIG_TRACING
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)
#include <linux/trace.h>
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,2,0)
#include <linux/trace_events.h>
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,5,0)
#include <linux/tracepoint-defs.h>
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,0,0)
#include <linux/ftrace_event.h>
#endif
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,0,0)
#include <linux/ftrace.h>
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,18,0)
#include <uapi/linux/btf.h>
#endif
#ifdef CONFIG_NETFILTER
#include <linux/netfilter/x_tables.h>
#endif
#include <linux/task_work.h>
#include "uprobes.h"
#include <net/net_namespace.h>
#include <net/sock.h>
#include <net/fib_rules.h>
#include <net/udp_tunnel.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <net/rtnetlink.h>
#include <net/genetlink.h>
#include <net/tcp.h>
#include <linux/sock_diag.h>
#include <net/protocol.h>
#include <linux/rhashtable.h>
#include "netlink.h"
#ifdef CONFIG_XFRM
#include <net/xfrm.h>
#endif
#include <linux/crypto.h>
#include <crypto/algapi.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,2,0)
#include <linux/lsm_hooks.h>
#endif
#include <linux/bpf.h>
#include <linux/filter.h>
#include <linux/input.h>
#include <linux/timer.h>
#include <linux/workqueue.h>
#include <linux/alarmtimer.h>
#ifdef CONFIG_NETFILTER
#include <net/netfilter/nf_log.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,15,0)
#include <net/netfilter/nf_tables.h>
#endif
#endif
#ifdef CONFIG_WIRELESS_EXT
#include <net/iw_handler.h>
#endif
#ifdef CONFIG_KEYS
#include <linux/key-type.h>
#include <linux/key.h>
#endif
#ifdef CONFIG_ZPOOL
#include <linux/zpool.h>
#endif
#include "timers.h"
#include "bpf.h"
#include "event.h"
#include "sub_priv.h"
#include "shared.h"
#include "arm64.bti/arm64bti.h"
MODULE_LICENSE("GPL");
// Char we show before each debug print
const char program_name[] = "lkcd";
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,10,0)
#define strlcpy strscpy
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,4,0)
static unsigned int *s_fib_notifier_net_id = NULL;
#endif
static struct mm_struct *s_init_mm = 0;
#ifdef __x86_64__
typedef pte_t *(*my_lookup_address)(unsigned long address, unsigned int *level);
static my_lookup_address s_lookup_address = 0;
#endif
#ifdef CONFIG_HUGETLB_PAGE
typedef int (*my_pmd_huge)(pmd_t pmd);
typedef int (*my_pud_huge)(pud_t pmd);
my_pmd_huge s_pmd_huge = 0;
my_pud_huge s_pud_huge = 0;
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(6,9,0)
static spinlock_t *s_vmap_area_lock = 0;
static spinlock_t *s_purge_vmap_area_lock = 0;
static struct list_head *s_vmap_area_list = 0;
static struct list_head *s_purge_vmap_area_list = 0;
#endif
static void **s_sys_table = 0;
#ifdef __x86_64__
static void **s_ia32_sys_table = 0;
static void **s_x32_sys_table = 0;
#endif
static struct cred *s_init_cred = 0;
static struct rw_semaphore *s_net = 0;
static rwlock_t *s_dev_base_lock = 0;
static struct sock_diag_handler **s_sock_diag_handlers = 0;
static struct mutex *s_sock_diag_table_mutex = 0;
// tcp congestion
static struct list_head *s_tcp_cong_list = 0;
static spinlock_t *s_tcp_cong_list_lock = 0;
#ifdef CONFIG_NETFILTER
static struct mutex *s_nf_hook_mutex = 0;
static struct mutex *s_nf_log_mutex = 0;
static struct xt_af *s_xt = 0;
#endif /* CONFIG_NETFILTER */
#ifdef CONFIG_XFRM
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,3,0)
static spinlock_t *s_xfrm_state_afinfo_lock = 0;
static struct xfrm_state_afinfo **s_xfrm_state_afinfo = 0;
#endif
static spinlock_t *s_xfrm_km_lock = 0;
static struct list_head *s_xfrm_km_list = 0;
static spinlock_t *s_xfrm_policy_afinfo_lock = 0;
static struct xfrm_policy_afinfo **s_xfrm_policy_afinfo = 0;
static spinlock_t *s_xfrm_input_afinfo_lock = 0;
// xfrm protocols
static struct mutex *s_xfrm4_protocol_mutex = 0;
static struct xfrm4_protocol **x4p[3] = { 0, 0, 0 }; // esp4_handlers, ah4_handlers, ipcomp4_handlers
static struct mutex *s_xfrm6_protocol_mutex = 0;
static struct xfrm6_protocol **x6p[3] = { 0, 0, 0 }; // esp6_handlers, ah6_handlers, ipcomp6_handlers
// xfrm tunnels
static struct mutex *s_tunnel4_mutex = 0;
static struct xfrm_tunnel **x4t[3] = { 0, 0, 0 }; // tunnel4_handlers, tunnel64_handlers, tunnelmpls4_handlers
static struct mutex *s_tunnel6_mutex = 0;
static struct xfrm6_tunnel **x6t[3] = { 0, 0, 0 }; // tunnel6_handlers, tunnel46_handlers, tunnelmpls6_handlers
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,11,0)
# define XFRM_MAX (AF_INET6 + 1)
#else
# define XFRM_MAX AF_MAX
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,10,0)
static spinlock_t *s_xfrm_translator_lock = 0;
static struct xfrm_translator **s_xfrm_translator = 0;
#endif
#endif /* CONFIG_XFRM */
#ifdef CONFIG_SECURITY_SELINUX
static struct avc_callback_node **s_avc_callbacks = NULL;
#endif
#ifdef CONFIG_KEYS
typedef struct key *(*my_key_lookup)(key_serial_t id);
static my_key_lookup f_key_lookup = 0;
static struct rw_semaphore *s_key_types_sem = 0;
static struct list_head *s_key_types_list = 0;
static struct rb_root *s_key_serial_tree = 0;
static spinlock_t *s_key_serial_lock = 0;
#endif /* CONFIG_KEYS */
static struct ftrace_ops *s_ftrace_end = 0;
static void *delayed_timer = 0;
static struct alarm_base *s_alarm = 0;
#ifdef CONFIG_INPUT
static struct list_head *s_input_handler_list = 0;
static struct list_head *s_input_dev_list = 0;
static struct mutex *s_input_mutex = 0;
#endif
#ifdef CONFIG_DYNAMIC_DEBUG
static struct list_head *s_ddebug_tables = 0;
static struct mutex *s_ddebug_lock = 0;
#endif
#ifdef CONFIG_MAGIC_SYSRQ
// size of sysrq_key_table is different depending on kernel version
// on 3.x & 4.x it is 36
// since 5.10 it is 62
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,10,0)
#define MAGIC_SIZE 62
#else
#define MAGIC_SIZE 32
#endif
static spinlock_t *s_sysrq_key_table_lock = 0;
static struct sysrq_key_op **s_sysrq_key_table = 0;
#endif
#ifdef CONFIG_PERF_EVENTS
struct perf_guest_info_callbacks **s_perf_guest_cbs = 0;
#endif
typedef int (*my_mprotect_pkey)(unsigned long start, size_t len, unsigned long prot, int pkey);
static my_mprotect_pkey s_mprotect = 0;
typedef int (*my_lookup)(unsigned long addr, char *symname);
my_lookup s_lookup = 0;
static struct mutex *s_module_mutex = 0;
static struct list_head *s_modules = 0;
static struct list_head *s_formats = 0; // totally uniq name, yeah
rwlock_t *s_binfmt_lock = 0;
typedef int (*my_vmalloc_or_module_addr)(const void *);
static my_vmalloc_or_module_addr s_vmalloc_or_module_addr = 0;
typedef struct callback_head *(*my_task_work_cancel)(struct task_struct *task, task_work_func_t func);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,10,0)
typedef int (*my_task_work_add)(struct task_struct *task, struct callback_head *work, enum task_work_notify_mode notify);
#else
typedef int (*my_task_work_add)(struct task_struct *task, struct callback_head *work, int);
#endif
static my_task_work_cancel s_my_task_work_cancel = 0;
static my_task_work_add s_task_work_add = 0;
#ifdef CONFIG_ZPOOL
static struct list_head *z_drivers_head = 0;
static spinlock_t *z_drivers_lock = 0;
#endif
static struct list_head *s_slab_caches = 0;
static struct mutex *s_slab_mutex = 0;
#define KPROBE_HASH_BITS 6
#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
#ifdef __x86_64__
// asm functions in getgs.asm
extern void *get_gs(long offset);
extern void *get_this_gs(long this_cpu, long offset);
extern unsigned int get_gs_dword(long offset);
extern unsigned short get_gs_word(long offset);
extern unsigned char get_gs_byte(long offset);
extern void *xchg_ptrs(void *, void *);
#endif /* __x86_64__ */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,10,0)
#include <linux/static_call.h>
static unsigned long lkcd_lookup_name_scinit(const char *name);
DEFINE_STATIC_CALL(lkcd_lookup_name_sc, lkcd_lookup_name_scinit);
#endif
// read kernel symbols from the /proc
#define KALLSYMS_PATH "/proc/kallsyms"
// Warning! When change this size you must patch size of ksym_params in lk.h too
#define BUFF_SIZE 256
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,4,0) && LINUX_VERSION_CODE < KERNEL_VERSION(5,10,0)
unsigned long lkcd_lookup_name(const char *name)
{
unsigned int i = 0, first_space_idx = 0, second_space_idx = 0; /* Read Index and indexes of spaces */
struct file *proc_ksyms = NULL;
loff_t pos = 0;
unsigned long ret = 0;
ssize_t read = 0;
int err = 0;
const size_t name_len = strlen(name);
/*
* Buffer for each line of kallsyms file.
* The symbol names are limited to KSYM_NAME_LEN=128. When Linux is
* compiled with clang's Control Flow Integrity, there are large symbols
* such as
* __typeid__ZTSFvPvP15ieee80211_local11set_key_cmdP21ieee80211_sub_if_dataP13ieee80211_staP18ieee80211_key_confE_global_addr
* which lead to a line with 142 characters.
* Some use a buffer which can hold 256 characters, to be safe.
*/
char proc_ksyms_entry[KSYM_NAME_LEN] = {0};
proc_ksyms = filp_open(KALLSYMS_PATH, O_RDONLY, 0);
if (proc_ksyms == NULL)
goto cleanup;
read = kernel_read(proc_ksyms, proc_ksyms_entry + i, 1, &pos);
while (read == 1) {
if (proc_ksyms_entry[i] == '\n' || (size_t)i == sizeof(proc_ksyms_entry) - 1) {
/* Prefix-match the name with the 3rd field of the line, after the second space */
if (second_space_idx > 0 &&
second_space_idx + 1 + name_len <= sizeof(proc_ksyms_entry) &&
!strncmp(proc_ksyms_entry + second_space_idx + 1, name, name_len)) {
printk(KERN_INFO "[+] %s: %.*s\n", name,
i, proc_ksyms_entry);
/* Decode the address, which is in hexadecimal */
proc_ksyms_entry[first_space_idx] = '\0';
err = kstrtoul(proc_ksyms_entry, 16, &ret);
if (err) {
printk(KERN_ERR "kstrtoul returned error %d while parsing %.*s\n",
err, first_space_idx, proc_ksyms_entry);
ret = 0;
goto cleanup;
}
goto cleanup;
}
i = 0;
first_space_idx = 0;
second_space_idx = 0;
memset(proc_ksyms_entry, 0, sizeof(proc_ksyms_entry));
} else {
if (proc_ksyms_entry[i] == ' ') {
if (first_space_idx == 0) {
first_space_idx = i;
} else if (second_space_idx == 0) {
second_space_idx = i;
}
}
i++;
}
read = kernel_read(proc_ksyms, proc_ksyms_entry + i, 1, &pos);
}
printk(KERN_ERR "symbol not found in kallsyms: %s\n", name);
cleanup:
if (proc_ksyms != NULL)
filp_close(proc_ksyms, 0);
return ret;
}
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5,10,0)
static struct kprobe kp = {
.symbol_name = "kallsyms_lookup_name",
.flags = KPROBE_FLAG_DISABLED
};
static unsigned long lkcd_lookup_name_scinit(const char *name)
{
unsigned long (*lkcd_lookup_name_fp)(const char *name) = NULL;
int kp_ret;
// try kprobes first, but have a fallback as they might be disabled
kp_ret = register_kprobe(&kp);
if (kp_ret < 0) {
printk(KERN_DEBUG "register_kprobe failed, returned %d", kp_ret);
} else {
lkcd_lookup_name_fp = (unsigned long (*) (const char *name))kp.addr;
unregister_kprobe(&kp);
}
// brute force by doing a symbolic search via sprint_symbol
if (!lkcd_lookup_name_fp) {
char name[KSYM_SYMBOL_LEN];
unsigned long start = (unsigned long) sprint_symbol;
unsigned long end = start - 32 * 1024;
unsigned long addr, offset;
char *off_ptr;
for (addr = start; addr > end; addr--) {
if (sprint_symbol(name, addr) <= 0)
break;
if (!strncmp(name, "0x", 2))
break;
off_ptr = strchr(name, '+');
if (!off_ptr)
break;
if (sscanf(off_ptr, "+%lx", &offset) != 1)
break;
addr -= offset;
if (off_ptr - name == 20 &&
!strncmp(name, "kallsyms_lookup_name", 20))
{
lkcd_lookup_name_fp = (void *)addr;
break;
}
}
if (!lkcd_lookup_name_fp)
printk(KERN_DEBUG "lookup via sprint_symbol() failed, too");
}
if (lkcd_lookup_name_fp) {
static_call_update(lkcd_lookup_name_sc, lkcd_lookup_name_fp);
return static_call(lkcd_lookup_name_sc)(name);
}
return 0;
}
static unsigned long lkcd_lookup_name(const char *name)
{
return static_call(lkcd_lookup_name_sc)(name);
}
#else
static unsigned long lkcd_lookup_name(const char *name)
{
return kallsyms_lookup_name(name);
}
#endif
static int open_lkcd(struct inode *inode, struct file *file)
{
try_module_get(THIS_MODULE);
return 0;
}
static int close_lkcd(struct inode *inode, struct file *file)
{
module_put(THIS_MODULE);
return 0;
}
#ifdef CONFIG_BPF
static const char *get_ioctl_name(unsigned int num)
{
switch(num)
{
case IOCTL_GET_BPF_USED_MAPS:
return "IOCTL_GET_BPF_USED_MAPS";
case IOCTL_GET_BPF_OPCODES:
return "IOCTL_GET_BPF_OPCODES";
case IOCTL_GET_BPF_PROG_BODY:
return "IOCTL_GET_BPF_PROG_BODY";
}
return "unknown";
}
#endif
// ripped from https://stackoverflow.com/questions/1184274/read-write-files-within-a-linux-kernel-module
static struct file *file_open(const char *path, int flags, int rights, int *err)
{
struct file *filp = NULL;
const struct cred *old_real = NULL, *old;
*err = 0;
if ( s_init_cred )
{
old_real = current->real_cred;
old = current->cred;
rcu_assign_pointer(current->real_cred, s_init_cred);
rcu_assign_pointer(current->cred, s_init_cred);
}
filp = filp_open(path, flags, rights);
if ( old_real != NULL )
{
rcu_assign_pointer(current->real_cred, old_real);
rcu_assign_pointer(current->cred, old);
}
if (IS_ERR(filp)) {
*err = PTR_ERR(filp);
return NULL;
}
return filp;
}
static inline void file_close(struct file *file)
{
filp_close(file, NULL);
}
static const struct file_operations *s_dbg_open = 0;
static const struct file_operations *s_dbg_full = 0;
static void *k_pre_handler_kretprobe = 0;
static inline int is_dbgfs(const struct file_operations *in)
{
return (in == s_dbg_open) || (in == s_dbg_full);
}
// css_next_child is not exported so css_for_each_child not compiling. as usually
typedef struct cgroup_subsys_state *(*kcss_next_child)(struct cgroup_subsys_state *pos, struct cgroup_subsys_state *parent);
static kcss_next_child css_next_child_ptr = 0;
#ifdef CONFIG_BPF
static struct undoc_btf_ops **s_kind_ops = NULL;
// bpf_prog_put was not exported till 4.1
typedef void (*my_bpf_prog_put)(struct bpf_prog *prog);
my_bpf_prog_put s_bpf_prog_put = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
static const char *s_verops = "bpf_verifier_ops";
#else
static const char *s_verops = "bpf_prog_types";
#endif
#if LINUX_VERSION_CODE > KERNEL_VERSION(4,11,0)
static const struct bpf_verifier_ops **s_bpf_verifier_ops = NULL;
#if LINUX_VERSION_CODE > KERNEL_VERSION(5,4,0)
#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) [_id] = 1,
#else
#define BPF_PROG_TYPE(_id, _ops) [_id] = 1,
#endif
#define BPF_MAP_TYPE(_id, _ops)
#define BPF_LINK_TYPE(_id, _name)
const char s_bpf_verops[] = {
#include <linux/bpf_types.h>
};
#undef BPF_PROG_TYPE
#undef BPF_MAP_TYPE
#undef BPF_LINK_TYPE
#else
static struct list_head *s_bpf_prog_types = NULL;
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
typedef int (*kcgroup_bpf_detach)(struct cgroup *cgrp, struct bpf_prog *prog, enum bpf_attach_type type);
static kcgroup_bpf_detach cgroup_bpf_detach_ptr = 0;
#endif
static void copy_bpf_verops(struct one_bpf_verops *to, const struct bpf_verifier_ops *op)
{
to->addr = (void *)op;
to->get_func_proto = (unsigned long)op->get_func_proto;
to->is_valid_access = (unsigned long)op->is_valid_access;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,1,0)
to->convert_ctx_access = (unsigned long)op->convert_ctx_access;
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,9,0)
to->gen_prologue = (unsigned long)op->gen_prologue;
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,18,0)
to->gen_ld_abs = (unsigned long)op->gen_ld_abs;
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)
to->btf_struct_access = (unsigned long)op->btf_struct_access;
#endif
}
#endif /* CONFIG_BPF */
// kernfs_node_from_dentry is not exported
typedef struct kernfs_node *(*krnf_node_type)(struct dentry *dentry);
static krnf_node_type krnf_node_ptr = 0;
typedef void (*und_iterate_supers)(void (*f)(struct super_block *, void *), void *arg);
static und_iterate_supers iterate_supers_ptr = 0;
static seqlock_t *mount_lock = 0;
static inline void lock_mount_hash(void)
{
write_seqlock(mount_lock);
}
static inline void unlock_mount_hash(void)
{
write_sequnlock(mount_lock);
}
// trace events list and semaphore
static struct rw_semaphore *s_trace_event_sem = 0;
static struct mutex *s_event_mutex = 0;
static struct list_head *s_ftrace_events = 0;
static struct mutex *s_bpf_event_mutex = 0;
static struct mutex *s_tracepoints_mutex = 0;
static struct mutex *s_tracepoint_module_list_mutex = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
typedef int (*und_bpf_prog_array_length)(struct bpf_prog_array *progs);
static und_bpf_prog_array_length bpf_prog_array_length_ptr = 0;
#endif
// x86 only
// on arm64 there is aarch64_insn_write but it accepts whole instruction with len 4 bytes
// on arm there is __patch_text and it also accepts whole instruction with len 4 bytes
#ifdef CONFIG_ARM64
typedef int (*t_patch_text)(void *addr, u32 insn);
static const char *s_patch_name = "aarch64_insn_patch_text_nosync";
#else
typedef void *(*t_patch_text)(void *addr, const void *opcode, size_t len);
static const char *s_patch_name = "text_poke_kgdb";
#endif
static t_patch_text s_patch_text = 0;
#if CONFIG_FSNOTIFY && LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
typedef struct fsnotify_mark *(*und_fsnotify_first_mark)(struct fsnotify_mark_connector **connp);
typedef struct fsnotify_mark *(*und_fsnotify_next_mark)(struct fsnotify_mark *mark);
static struct srcu_struct *fsnotify_mark_srcu_ptr = 0;
static und_fsnotify_first_mark fsnotify_first_mark_ptr = 0;
static und_fsnotify_next_mark fsnotify_next_mark_ptr = 0;
static struct fsnotify_mark *my_fsnotify_first_mark(struct fsnotify_mark_connector **connp)
{
struct fsnotify_mark_connector *conn;
struct hlist_node *node = NULL;
conn = srcu_dereference(*connp, fsnotify_mark_srcu_ptr);
if (conn)
node = srcu_dereference(conn->list.first, fsnotify_mark_srcu_ptr);
return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
}
static struct fsnotify_mark *my_fsnotify_next_mark(struct fsnotify_mark *mark)
{
struct hlist_node *node = NULL;
if (mark)
node = srcu_dereference(mark->obj_list.next,
fsnotify_mark_srcu_ptr);
return hlist_entry_safe(node, struct fsnotify_mark, obj_list);
}
struct super_mark_args
{
void *sb_addr;
int found;
unsigned long cnt;
unsigned long *curr;
struct one_fsnotify *data;
};
static void count_superblock_marks(struct super_block *sb, void *arg)
{
struct super_mark_args *args = (struct super_mark_args *)arg;
if ( (void *)sb != args->sb_addr )
return;
else {
struct fsnotify_mark *mark;
args->found |= 1;
for ( mark = fsnotify_first_mark_ptr(&sb->s_fsnotify_marks);
mark != NULL;
mark = fsnotify_next_mark_ptr(mark)
)
args->cnt++;
}
}
static void fill_superblock_marks(struct super_block *sb, void *arg)
{
struct super_mark_args *args = (struct super_mark_args *)arg;
if ( (void *)sb != args->sb_addr )
return;
else {
struct fsnotify_mark *mark;
args->found |= 1;
for ( mark = fsnotify_first_mark_ptr(&sb->s_fsnotify_marks);
mark != NULL && args->curr[0] < args->cnt;
mark = fsnotify_next_mark_ptr(mark), args->curr[0]++
)
{
unsigned long index = args->curr[0];
args->data[index].mark_addr = (void *)mark;
args->data[index].mask = mark->mask;
#if LINUX_VERSION_CODE < KERNEL_VERSION(6,0,0)
// you can't guess when those stupid morons chanhed ignored_mask to ignore_mask
// in ubuntu 5.15.125 it called ignore_mask
// on official site: https://elixir.bootlin.com/linux/v5.15.125/source/include/linux/fsnotify_backend.h#L400
// kill kill kill
args->data[index].ignored_mask = mark->ignore_mask;
#else
args->data[index].ignored_mask = mark->ignore_mask;
#endif
args->data[index].flags = mark->flags;
if ( mark->group )
{
args->data[index].group = (void *)mark->group;
args->data[index].ops = (void *)mark->group->ops;
} else {
args->data[index].group = NULL;
args->data[index].ops = NULL;
}
}
}
}
struct inode_mark_args
{
void *sb_addr;
void *inode_addr;
int found;
unsigned long cnt;
unsigned long *curr;
struct one_fsnotify *data;
};
static void fill_mount_marks(struct super_block *sb, void *arg)
{
struct inode_mark_args *args = (struct inode_mark_args *)arg;
if ( (void *)sb != args->sb_addr )
return;
else {
struct mount *mnt;
struct fsnotify_mark *mark;
args->found |= 1;
lock_mount_hash();
list_for_each_entry(mnt, &sb->s_mounts, mnt_instance)
{
if ( (void *)mnt != args->inode_addr )
continue;
args->found |= 2;
for ( mark = fsnotify_first_mark_ptr(&mnt->mnt_fsnotify_marks);
mark != NULL && args->curr[0] < args->cnt;
mark = fsnotify_next_mark_ptr(mark), args->curr[0]++
)
{
unsigned long index = args->curr[0];
args->data[index].mark_addr = (void *)mark;
args->data[index].mask = mark->mask;
#if LINUX_VERSION_CODE < KERNEL_VERSION(6,0,0)
// kill kill kill
args->data[index].ignored_mask = mark->ignore_mask;
#else
args->data[index].ignored_mask = mark->ignore_mask;
#endif
args->data[index].flags = mark->flags;
if ( mark->group )
{
args->data[index].group = (void *)mark->group;
args->data[index].ops = (void *)mark->group->ops;
} else {
args->data[index].group = NULL;
args->data[index].ops = NULL;
}
}
break;
}
unlock_mount_hash();
}
}
static void fill_inode_marks(struct super_block *sb, void *arg)
{
struct inode_mark_args *args = (struct inode_mark_args *)arg;
if ( (void *)sb != args->sb_addr )
return;
else {
struct inode *inode;
struct fsnotify_mark *mark;
args->found |= 1;
// iterate on inodes
spin_lock(&sb->s_inode_list_lock);
list_for_each_entry(inode, &sb->s_inodes, i_sb_list)
{
if ( (void *)inode != args->inode_addr )
continue;
args->found |= 2;
for ( mark = fsnotify_first_mark_ptr(&inode->i_fsnotify_marks);
mark != NULL && args->curr[0] < args->cnt;
mark = fsnotify_next_mark_ptr(mark), args->curr[0]++
)
{
unsigned long index = args->curr[0];
args->data[index].mark_addr = (void *)mark;
args->data[index].mask = mark->mask;
#if LINUX_VERSION_CODE < KERNEL_VERSION(6,0,0)
// kill kill kill
args->data[index].ignored_mask = mark->ignore_mask;
#else
args->data[index].ignored_mask = mark->ignore_mask;
#endif
args->data[index].flags = mark->flags;
if ( mark->group )
{
args->data[index].group = (void *)mark->group;
args->data[index].ops = (void *)mark->group->ops;
} else {
args->data[index].group = NULL;
args->data[index].ops = NULL;
}
}
break;
}
spin_unlock(&sb->s_inode_list_lock);
}
}
#endif /* CONFIG_FSNOTIFY */
struct super_inodes_args
{
void *sb_addr;
int found;
unsigned long cnt;
unsigned long *curr;
struct one_inode *data;
};
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
// used in fill_super_block_inodes & fill_super_blocks
static spinlock_t *s_inode_sb_list_lock = 0;
#endif
static void fill_super_block_inodes(struct super_block *sb, void *arg)
{
struct super_inodes_args *args = (struct super_inodes_args *)arg;
if ( (void *)sb != args->sb_addr )
return;
else {
struct inode *inode;
args->found++;
// iterate on inodes
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
spin_lock(s_inode_sb_list_lock);
#else
spin_lock(&sb->s_inode_list_lock);
#endif
list_for_each_entry(inode, &sb->s_inodes, i_sb_list)
{
unsigned long index = args->curr[0];
if ( args->curr[0] >= args->cnt )
break;
// copy data for this inode
args->data[index].addr = (void *)inode;
args->data[index].i_mode = inode->i_mode;
args->data[index].i_ino = inode->i_ino;
args->data[index].i_flags = inode->i_flags;
#if CONFIG_FSNOTIFY && LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
args->data[index].i_fsnotify_mask = inode->i_fsnotify_mask;
args->data[index].i_fsnotify_marks = (void *)inode->i_fsnotify_marks;
args->data[index].mark_count = 0;
// iterate on marks
if ( fsnotify_first_mark_ptr && fsnotify_next_mark_ptr )
{
struct fsnotify_mark *mark;
for ( mark = fsnotify_first_mark_ptr(&inode->i_fsnotify_marks); mark != NULL; mark = fsnotify_next_mark_ptr(mark) )
args->data[index].mark_count++;
}
#endif /* CONFIG_FSNOTIFY */
// inc count for next iteration
args->curr[0]++;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
spin_unlock(s_inode_sb_list_lock);
#else
spin_unlock(&sb->s_inode_list_lock);
#endif
}
}
struct super_mount_args
{
void *sb_addr;
int found;
unsigned long cnt;
unsigned long *curr;
struct one_mount *data;
};
static void fill_super_block_mounts(struct super_block *sb, void *arg)
{
struct super_mount_args *args = (struct super_mount_args *)arg;
if ( (void *)sb != args->sb_addr )
return;
else {
struct mount *mnt;
args->found++;
lock_mount_hash();
list_for_each_entry(mnt, &sb->s_mounts, mnt_instance)
{
unsigned long index = args->curr[0];
if ( args->curr[0] >= args->cnt )
break;
// copy data for this inode
args->data[index].addr = (void *)mnt;
args->data[index].mnt_id = mnt->mnt_id;
if ( mnt->mnt_mountpoint )
dentry_path_raw(mnt->mnt_mountpoint, args->data[index].root, sizeof(args->data[index].root));
else
args->data[index].root[0] = 0;
if ( mnt->mnt.mnt_root )
{
// struct path mnt_path = { .dentry = mnt->mnt.mnt_root, .mnt = &mnt->mnt };
// d_path(&mnt_path, args->data[index].mnt_root, sizeof(args->data[index].mnt_root));
dentry_path_raw(mnt->mnt.mnt_root, args->data[index].mnt_root, sizeof(args->data[index].mnt_root));
} else
args->data[index].mnt_root[0] = 0;
if ( mnt->mnt_mp )
dentry_path_raw(mnt->mnt_mp->m_dentry, args->data[index].mnt_mp, sizeof(args->data[index].mnt_mp));
else
args->data[index].mnt_mp[0] = 0;
args->data[index].mark_count = 0;
#if CONFIG_FSNOTIFY && LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
// iterate on marks
if ( fsnotify_first_mark_ptr && fsnotify_next_mark_ptr )
{
struct fsnotify_mark *mark;
for ( mark = fsnotify_first_mark_ptr(&mnt->mnt_fsnotify_marks); mark != NULL; mark = fsnotify_next_mark_ptr(mark) )
args->data[index].mark_count++;
}
#endif /* CONFIG_FSNOTIFY */
// inc count for next iteration
args->curr[0]++;
}
unlock_mount_hash();
}
}
struct super_args
{
unsigned long cnt;
unsigned long *curr;
struct one_super_block *data;
};
static void count_super_blocks(struct super_block *sb, void *arg)
{
(*(unsigned long *)arg)++;
}
static void fill_super_blocks(struct super_block *sb, void *arg)
{
struct super_args *args = (struct super_args *)arg;
unsigned long index = args->curr[0];
struct inode *inode;
struct mount *mnt;
if ( index >= args->cnt )
return;
// copy data from super-block
args->data[index].addr = sb;
args->data[index].dev = sb->s_dev;
args->data[index].s_flags = sb->s_flags;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,2,0)
args->data[index].s_iflags = sb->s_iflags;
#else
args->data[index].s_iflags = 0;
#endif
#ifdef CONFIG_FS_ENCRYPTION
args->data[index].s_cop = (void *)sb->s_cop;
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,7,0)
if ( sb->s_shrink ) {
args->data[index].count_objects = (void *)sb->s_shrink->count_objects;
args->data[index].scan_objects = (void *)sb->s_shrink->scan_objects;
}
#else
args->data[index].count_objects = (void *)sb->s_shrink.count_objects;
args->data[index].scan_objects = (void *)sb->s_shrink.scan_objects;
#endif
args->data[index].s_fs_info = (void *)sb->s_fs_info;
args->data[index].s_op = (void *)sb->s_op;
args->data[index].s_type = sb->s_type;
args->data[index].dq_op = (void *)sb->dq_op;
args->data[index].s_qcop = (void *)sb->s_qcop;
args->data[index].s_export_op = (void *)sb->s_export_op;
args->data[index].s_d_op = (void *)sb->s_d_op;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,2,0)
args->data[index].s_user_ns = (void *)sb->s_user_ns;
#else
args->data[index].s_user_ns = 0;
#endif
args->data[index].inodes_cnt = 0;
args->data[index].s_root = (void *)sb->s_root;
if ( sb->s_root )
dentry_path_raw(sb->s_root, args->data[index].root, sizeof(args->data[index].root));
else
args->data[index].root[0] = 0;
args->data[index].mount_count = 0;
list_for_each_entry(mnt, &sb->s_mounts, mnt_instance)
args->data[index].mount_count++;
#if CONFIG_FSNOTIFY && LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
args->data[index].s_fsnotify_mask = sb->s_fsnotify_mask;
args->data[index].s_fsnotify_marks = sb->s_fsnotify_marks;
#endif /* CONFIG_FSNOTIFY */
strncpy(args->data[index].s_id, sb->s_id, 31);
// iterate on inodes
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
spin_lock(s_inode_sb_list_lock);