-
Notifications
You must be signed in to change notification settings - Fork 0
/
skull.c
279 lines (237 loc) · 6.97 KB
/
skull.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
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include<linux/string.h>
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/errno.h> /* EFAULT */
#define SKULL "skull"
#define Q_SET_SIZE 16
#define QUANTUM_SIZE 16
static int devNum;
static int min = 0;
static int count = 1;
const char* PREF = "[ skull ]";
struct node {
struct node* next;
void** data;
};
struct skull_d {
struct node* data; /* Pointer to first node of the linked lisit */
int quantum; /* the current quantum size */
int qset; /* the current array size */
unsigned long size; /* amount of data stored here */
struct cdev skull_cdev;
};
static struct skull_d skull = { .data = NULL, .qset = Q_SET_SIZE, .quantum = QUANTUM_SIZE, .size = 0 };
static struct node* getNodeByIndex(struct skull_d* dev, int index) {
struct node* targetNode = dev->data;
if (!targetNode) {
targetNode = dev->data = kmalloc(sizeof(struct node), GFP_KERNEL);
if (targetNode == NULL) return NULL;
memset(targetNode, 0, sizeof(struct node));
}
while (index--) {
if (!targetNode->next) {
targetNode->next = kmalloc(sizeof(struct node), GFP_KERNEL);
if (targetNode->next == NULL) return NULL;
memset(targetNode->next, 0, sizeof(struct node));
}
targetNode = targetNode->next;
continue;
}
return targetNode;
}
int skull_trim(struct skull_d* dev) {
struct node* currentNode;
struct node* nextNode;
int qset = dev->qset;
int i;
for (currentNode = dev->data; currentNode; currentNode = nextNode) {
if (currentNode->data) {
for (i = 0; i < qset; i++) {
kfree(currentNode->data[i]);
}
kfree(currentNode->data);
currentNode->data = NULL;
}
nextNode = currentNode->next;
kfree(currentNode);
}
dev->size = 0;
dev->qset = Q_SET_SIZE;
dev->quantum = QUANTUM_SIZE;
dev->data = NULL;
return 0;
}
static int open(struct inode* inode, struct file* filp) {
// Getting char device struct and adding it to private_data field
struct skull_d* dev;
dev = container_of(inode->i_cdev, struct skull_d, skull_cdev);
filp->private_data = dev;
// Checking access mode with f_flags
if ((filp->f_flags & O_ACCMODE) == O_WRONLY) {
pr_info("%s - About to trim on open\n", PREF);
skull_trim(dev);
}
return 0;
};
static ssize_t read(struct file* filp, char __user* buf, size_t len, loff_t* off) {
struct skull_d* dev;
struct node* targetNode;
int quantum, qset, pageSize, nodeIndex, s_pos, q_pos, rest;
ssize_t result;
dev = filp->private_data;
targetNode = dev->data;
quantum = dev->quantum;
qset = dev->qset;
pageSize = quantum * qset;
result = 0;
if (dev->size < *off) {
goto out;
}
if (*off + len > dev->size) {
len = dev->size - *off;
}
nodeIndex = (long)*off / pageSize;
rest = (long)*off % pageSize;
s_pos = rest / quantum;
q_pos = rest % quantum;
targetNode = getNodeByIndex(dev, nodeIndex);
if (targetNode == NULL || !targetNode->data || !targetNode->data[s_pos]) {
goto out;
}
if (len > quantum - q_pos) {
len = quantum - q_pos;
}
pr_info("%s - reading %ld bytes from quantum number %d\n", PREF, len, s_pos);
if (copy_to_user(buf, targetNode->data[s_pos] + q_pos, len)) {
result = -EFAULT;
goto out;
}
*off = *off + len;
result = len;
out:
return result;
}
static ssize_t write(struct file* filp, const char __user* buf, size_t len, loff_t* off) {
struct skull_d* dev;
struct node* targetNode;
int quantum, qset, pageSize, nodeIndex, s_pos, q_pos, rest;
ssize_t result;
dev = filp->private_data;
targetNode = dev->data;
quantum = dev->quantum;
qset = dev->qset;
pageSize = quantum * qset;
result = -ENOMEM;
nodeIndex = (long)*off / pageSize;
rest = (long)*off % pageSize;
s_pos = rest / quantum;
q_pos = rest % quantum;
targetNode = getNodeByIndex(dev, nodeIndex);
if (targetNode == NULL) {
goto out;
}
if (!targetNode->data) {
targetNode->data = kmalloc(qset * sizeof(char*), GFP_KERNEL);
if (targetNode->data == NULL) {
goto out;
}
memset(targetNode->data, 0, qset * sizeof(char*));
}
if (!targetNode->data[s_pos]) {
targetNode->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
if (!targetNode->data[s_pos]) {
goto out;
}
}
if (len > quantum - q_pos) {
len = quantum - q_pos;
}
if (copy_from_user(targetNode->data[s_pos] + q_pos, buf, len)) {
result = -EFAULT;
goto out;
}
*off = *off + len;
result = len;
if (dev->size < *off) {
dev->size = *off;
}
out:
return result;
}
static int release(struct inode* inode, struct file* filp) {
return 0;
}
static loff_t llseek(struct file* filp, loff_t off, int whence) {
struct skull_d* dev;
loff_t newpos;
dev = filp->private_data;
switch (whence) {
case 0: /* SEEK_SET */
newpos = off;
break;
case 1: /* SEEK_CUR */
newpos = filp->f_pos + off;
break;
case 2: /* SEEK_END */
newpos = dev->size + off;
break;
default: /* can't happen */
return -EINVAL;
}
if (newpos < 0) return -EINVAL;
filp->f_pos = newpos;
return newpos;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.open = open,
.read = read,
.write = write,
.release = release,
.llseek = llseek,
};
static int init_skull(void) {
int err;
err = alloc_chrdev_region(&devNum, min, count, SKULL);
if (err != 0) {
goto error;
}
pr_alert("%s - Char region allocated!\n", PREF);
cdev_init(&skull.skull_cdev, &fops);
pr_alert("%s - Character device struct initiated!\n", PREF);
skull.skull_cdev.owner = THIS_MODULE;
skull.skull_cdev.ops = &fops;
err = cdev_add(&skull.skull_cdev, devNum, 1);
if (err != 0) {
goto remove_cdev;
}
pr_alert("%s - Character device ready to use\n", PREF);
return 0;
remove_cdev:
cdev_del(&skull.skull_cdev);
unregister_chrdev_region(devNum, count);
error:
pr_alert("%s - THIS IS NO GOOD, ERROR\n", PREF);
return err;
}
static void exit_skull(void) {
skull_trim(&skull);
cdev_del(&skull.skull_cdev);
pr_alert("%s - Character device struct deallocated!\n", PREF);
unregister_chrdev_region(devNum, count);
pr_alert("%s - Char region deallocated\n", PREF);
}
MODULE_AUTHOR("Lautaro Jayat");
MODULE_DESCRIPTION("A cool module to interact with the undeads");
MODULE_VERSION("0.0.0");
MODULE_LICENSE("GPL");
module_init(init_skull);
module_exit(exit_skull);