This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
syscalls_proc.c
183 lines (151 loc) · 4.68 KB
/
syscalls_proc.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
/*
* Copyright (C) 2015 Francois Doray <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; only
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "syscalls.h"
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include "syscalls_abi.h"
#include "../wrapper/trace-clock.h"
static
void irq_wake(struct irq_work *entry)
{
struct syscall_tracker *syscall_priv = container_of(entry,
struct syscall_tracker, w_irq);
syscall_priv->reason = SYSCALL_TRACKER_WAKE_DATA;
wake_up_interruptible(&syscall_priv->read_wait);
syscall_priv->got_alert = true;
}
struct syscall_tracker *syscall_tracker_alloc_priv(void)
{
struct syscall_tracker *syscall_priv;
syscall_priv = kzalloc(sizeof(struct syscall_tracker), GFP_KERNEL);
if (!syscall_priv)
goto end;
syscall_priv->reason = SYSCALL_TRACKER_WAIT;
/* limit to 1 evt/sec */
syscall_priv->ns_rate_limit = 1000000000;
end:
return syscall_priv;
}
int syscall_tracker_setup_proc_priv(struct syscall_tracker *tracker_priv)
{
int ret;
init_irq_work(&tracker_priv->w_irq, irq_wake);
tracker_priv->proc_dentry = proc_create_data(SYSCALL_TRACKER_PROC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,
NULL, &syscall_tracker_fops, tracker_priv);
if (!tracker_priv->proc_dentry) {
printk(KERN_ERR "Error creating syscalls tracker control file.\n");
ret = -ENOMEM;
goto error;
}
init_waitqueue_head(&tracker_priv->read_wait);
ret = 0;
printk("Syscalls tracker module loaded successfully\n");
error:
return ret;
}
void syscall_tracker_destroy_proc_priv(struct syscall_tracker *tracker_priv)
{
irq_work_sync(&tracker_priv->w_irq);
if (tracker_priv->proc_dentry) {
remove_proc_entry(SYSCALL_TRACKER_PROC, NULL);
}
kfree(tracker_priv);
}
void syscall_tracker_handle_proc(struct syscall_tracker *tracker_priv)
{
uint64_t ts = trace_clock_read64();
/* Rate limiter */
if ((ts - tracker_priv->last_alert_ts) < tracker_priv->ns_rate_limit)
return;
if (tracker_priv->readers > 0)
irq_work_queue(&tracker_priv->w_irq);
tracker_priv->last_alert_ts = ts;
}
int syscall_tracker_proc_release(struct inode *inode, struct file *filp)
{
struct syscall_tracker *tracker_priv = filp->private_data;
tracker_priv->readers--;
module_put(THIS_MODULE);
return 0;
}
int syscall_tracker_proc_open(struct inode *inode, struct file *filp)
{
struct syscall_tracker *tracker_priv = PDE_DATA(inode);
int ret;
tracker_priv->got_alert = false;
tracker_priv->readers++;
filp->private_data = tracker_priv;
ret = try_module_get(THIS_MODULE);
if (!ret)
return -1;
return 0;
}
ssize_t syscall_tracker_proc_read(struct file *filp, char __user *buf, size_t n,
loff_t *offset)
{
struct syscall_tracker *tracker_priv = filp->private_data;
wait_event_interruptible(tracker_priv->read_wait,
tracker_priv->got_alert);
tracker_priv->reason = SYSCALL_TRACKER_WAIT;
tracker_priv->got_alert = false;
return 0;
}
unsigned int syscall_tracker_proc_poll(struct file *filp,
poll_table *wait)
{
struct syscall_tracker *tracker_priv = filp->private_data;
unsigned int mask = 0;
if (filp->f_mode & FMODE_READ) {
poll_wait(filp, &tracker_priv->read_wait, wait);
if (tracker_priv->reason == SYSCALL_TRACKER_WAKE_DATA)
mask |= POLLIN;
else
mask |= POLLHUP;
}
return mask;
}
long syscall_tracker_ioctl(
struct file *filp, unsigned int cmd, unsigned long arg)
{
struct syscall_tracker *tracker_priv = filp->private_data;
struct syscall_tracker_module_msg msg;
int ret = 0;
void __user *umsg = (void *) arg;
if (cmd != SYSCALLS_TRACKER_IOCTL)
return -ENOIOCTLCMD;
if (copy_from_user(&msg, umsg, sizeof(msg)))
return -EFAULT;
switch(msg.cmd) {
case SYSCALL_TRACKER_MODULE_REGISTER:
process_register(current->tgid);
break;
case SYSCALL_TRACKER_MODULE_UNREGISTER:
process_unregister(current->tgid);
break;
case SYSCALL_TRACKER_MODULE_STACK:
syscall_tracker_handle_proc(tracker_priv);
break;
default:
ret = -ENOTSUPP;
break;
}
return ret;
}