forked from ktap/ktap
/
ktap.c
231 lines (183 loc) · 4.61 KB
/
ktap.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
/*
* ktap.c - ktapvm kernel module main entry
*
* Copyright (C) 2012-2013 Jovi Zhang
*
* Author: Jovi Zhang <bookjovi@gmail.com>
* zhangwei(Jovi) <jovi.zhangwei@huawei.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/file.h>
#include <linux/fcntl.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/anon_inodes.h>
#include <linux/debugfs.h>
#include "ktap.h"
static int load_trunk(struct ktap_user_parm *uparm_ptr, unsigned long **buff)
{
int ret;
unsigned long *vmstart;
vmstart = vmalloc(uparm_ptr->trunk_len);
if (!vmstart)
return -ENOMEM;
ret = copy_from_user(vmstart, (void __user *)uparm_ptr->trunk,
uparm_ptr->trunk_len);
if (ret < 0) {
vfree(vmstart);
return -EFAULT;
}
*buff = vmstart;
return 0;
}
/* Ktap Main Entry */
static int ktap_main(struct file *file, struct ktap_user_parm *uparm_ptr)
{
unsigned long *buff = NULL;
ktap_State *ks;
Closure *cl;
int argc;
char **argv, *argstr;
int ret;
argstr = kmalloc(uparm_ptr->arglen, GFP_KERNEL);
if (!argstr)
return -ENOMEM;
ret = copy_from_user(argstr, (void __user *)uparm_ptr->argstr,
uparm_ptr->arglen);
if (ret < 0) {
kfree(argstr);
return -EFAULT;
}
argv = argv_split(GFP_KERNEL, argstr, &argc);
if (!argv) {
kfree(argstr);
pr_err("out of memory");
return -ENOMEM;
}
kfree(argstr);
ret = load_trunk(uparm_ptr, &buff);
if (ret) {
pr_err("cannot load file %s\n", argv[0]);
argv_free(argv);
return ret;
}
ks = kp_newstate((ktap_State **)&file->private_data, argc, argv);
argv_free(argv);
if (unlikely(!ks)) {
vfree(buff);
return -ENOEXEC;
}
cl = kp_load(ks, (unsigned char *)buff);
vfree(buff);
if (cl) {
/* optimize bytecode before excuting */
kp_optimize_code(ks, 0, cl->l.p);
kp_call(ks, ks->top - 1, 0);
}
kp_exit(ks);
return 0;
}
static void print_version(void)
{
}
static long ktap_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct ktap_user_parm uparm;
int ret;
switch (cmd) {
case KTAP_CMD_VERSION:
print_version();
return 0;
case KTAP_CMD_RUN:
ret = copy_from_user(&uparm, (void __user *)arg,
sizeof(struct ktap_user_parm));
if (ret < 0)
return -EFAULT;
return ktap_main(file, &uparm);
case KTAP_CMD_USER_COMPLETE: {
ktap_State *ks = file->private_data;
kp_user_complete(ks);
break;
}
default:
return -EINVAL;
};
return 0;
}
static unsigned int ktap_poll(struct file *file, poll_table *wait)
{
ktap_State *ks = file->private_data;
if (!ks)
return 0;
if (G(ks)->user_completion)
return POLLERR;
return 0;
}
static const struct file_operations ktap_fops = {
.llseek = no_llseek,
.unlocked_ioctl = ktap_ioctl,
.poll = ktap_poll
};
static long ktapvm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int new_fd, err;
struct file *new_file;
new_fd = get_unused_fd();
if (new_fd < 0)
return new_fd;
new_file = anon_inode_getfile("[ktap]", &ktap_fops, NULL, O_RDWR);
if (IS_ERR(new_file)) {
err = PTR_ERR(new_file);
put_unused_fd(new_fd);
return err;
}
file->private_data = 0;
fd_install(new_fd, new_file);
return new_fd;
}
static const struct file_operations ktapvm_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = ktapvm_ioctl,
};
struct dentry *ktap_dir;
static int __init init_ktap(void)
{
struct dentry *ktapvm_dentry;
ktap_dir = debugfs_create_dir("ktap", NULL);
if (!ktap_dir) {
pr_err("ktap: debugfs_create_dir failed\n");
return -1;
}
ktapvm_dentry = debugfs_create_file("ktapvm", 0444, ktap_dir, NULL,
&ktapvm_fops);
if (!ktapvm_dentry) {
pr_err("ktapvm: cannot create ktapvm file\n");
debugfs_remove_recursive(ktap_dir);
return -1;
}
return 0;
}
static void __exit exit_ktap(void)
{
debugfs_remove_recursive(ktap_dir);
}
module_init(init_ktap);
module_exit(exit_ktap);
MODULE_AUTHOR("Jovi Zhang <bookjovi@gmail.com>");
MODULE_DESCRIPTION("ktap");
MODULE_LICENSE("GPL");