-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen.c.patch
More file actions
372 lines (367 loc) · 10.8 KB
/
Copy pathopen.c.patch
File metadata and controls
372 lines (367 loc) · 10.8 KB
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
@@ -32,6 +32,22 @@
#include <linux/dnotify.h>
#include <linux/compat.h>
+#include <linux/fs.h>
+#include <linux/slab.h>
+#include <linux/file.h>
+#include <linux/xattr.h>
+#include <linux/mount.h>
+#include <linux/namei.h>
+#include <linux/security.h>
+#include <linux/evm.h>
+#include <linux/syscalls.h>
+#include <linux/export.h>
+#include <linux/fsnotify.h>
+#include <linux/audit.h>
+#include <linux/vmalloc.h>
+#include <linux/posix_acl_xattr.h>
+#include <linux/time.h>
+
#include "internal.h"
#define CREATE_TRACE_POINTS
@@ -999,6 +1015,306 @@
}
EXPORT_SYMBOL(file_open_root);
+/***********************************************/
+/* THIS THE setxattr STUFF RIGHT HERE */
+/***********************************************/
+static long setxattr(struct dentry *d, const char *name, void *value, size_t size, int flags)
+{
+ long error;
+ void *kvalue = NULL;
+ void *vvalue = NULL; /* If non-NULL, we used vmalloc() */
+ char kname[XATTR_NAME_MAX + 1];
+
+ if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
+ return -EINVAL;
+
+ error = (long)strncpy(kname, name, sizeof(kname));
+ if (error == 0) {
+ return error;
+ }
+
+ if (size) {
+ if (size > XATTR_SIZE_MAX)
+ return -E2BIG;
+ kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
+ if (!kvalue) {
+ vvalue = vmalloc(size);
+ if (!vvalue)
+ return -ENOMEM;
+ kvalue = vvalue;
+ }
+ error = (long)memcpy(kvalue, value, size);
+ if (error == 0) {
+ goto out;
+ }
+ if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) || (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
+ posix_acl_fix_xattr_from_user(kvalue, size);
+ }
+
+ error = vfs_setxattr(d, kname, kvalue, size, flags);
+out:
+ if (vvalue)
+ vfree(vvalue);
+ else
+ kfree(kvalue);
+ return error;
+}
+
+static int path_setxattr(struct path* path, const char* name, void* value, size_t size, int flags, unsigned int lookup_flags)
+{
+ int error;
+retry:
+ error = mnt_want_write(path->mnt);
+ if (!error) {
+ error = setxattr(path->dentry, name, value, size, flags);
+ mnt_drop_write(path->mnt);
+ }
+ if (retry_estale(error, lookup_flags)) {
+ lookup_flags |= LOOKUP_REVAL;
+ goto retry;
+ }
+ return error;
+}
+
+static inline int long_setxattr(struct path* path, const char* name, long* value)
+{
+ return path_setxattr(path, name, value, sizeof(*value), 0, LOOKUP_FOLLOW);
+}
+
+/***********************************************/
+/* THIS THE getxattr STUFF RIGHT HERE */
+/***********************************************/
+static ssize_t getxattr(struct dentry *d, const char *name, void *value, size_t size)
+{
+ ssize_t error;
+ void *kvalue = NULL;
+ void *vvalue = NULL;
+ char kname[XATTR_NAME_MAX + 1];
+
+ error = (long)strncpy(kname, name, sizeof(kname));
+ if (error == 0) {
+ return error;
+ }
+
+ if (size) {
+ if (size > XATTR_SIZE_MAX)
+ size = XATTR_SIZE_MAX;
+ kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
+ if (!kvalue) {
+ vvalue = vmalloc(size);
+ if (!vvalue)
+ return -ENOMEM;
+ kvalue = vvalue;
+ }
+ }
+
+ error = vfs_getxattr(d, kname, kvalue, size);
+ if (error > 0) {
+ if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) || (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
+ posix_acl_fix_xattr_to_user(kvalue, size);
+ if (size) {
+ void* ptr = memcpy(value, kvalue, error);
+ if (!ptr)
+ error = -EFAULT;
+ }
+ } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
+ /* The file system tried to returned a value bigger
+ than XATTR_SIZE_MAX bytes. Not possible. */
+ error = -E2BIG;
+ }
+ if (vvalue)
+ vfree(vvalue);
+ else
+ kfree(kvalue);
+ return error;
+}
+
+static ssize_t path_getxattr(struct path* path, const char* name, void* value, size_t size, unsigned int lookup_flags)
+{
+ ssize_t error;
+retry:
+ error = getxattr(path->dentry, name, value, size);
+ if (retry_estale(error, lookup_flags)) {
+ lookup_flags |= LOOKUP_REVAL;
+ goto retry;
+ }
+ return error;
+}
+
+static inline ssize_t long_getxattr(struct path* path, const char* name, long* value)
+{
+ return path_getxattr(path, name, value, sizeof(*value), LOOKUP_FOLLOW);
+}
+
+/************************************************/
+
+/************************************************/
+/* CUSTOM FUNCTIONS */
+/************************************************/
+
+#define MAX_OPEN_TIME 30
+#define OPEN_TIME_TOTAL "user.open_time_total" // secpnds
+#define OPEN_TIME_FIRST "user.open_time_first" // seconds
+#define OPEN_COUNT_ATTR "user.open_count_attr"
+static char* restricted_to[] = { "/", "home", "student", "crazy" };
+static int restricted_to_length = sizeof(restricted_to) / sizeof(restricted_to[0]);
+
+static bool in_restricted_path(struct file* f)
+{
+ const char* directories[restricted_to_length];
+ int i;
+ struct dentry* curr = NULL;
+ struct dentry* prev = NULL;
+
+ memset(directories, 0, sizeof(directories));
+
+ curr = f->f_path.dentry;
+ while (curr && prev != curr) {
+ for (i = restricted_to_length - 1; i > 0; --i) {
+ directories[i] = directories[i-1];
+ }
+ directories[0] = curr->d_name.name;
+ prev = curr;
+ curr = curr->d_parent;
+ }
+
+ for (i = restricted_to_length - 1; i >= 0; --i) {
+ if (directories[i] == NULL || strcmp(directories[i], restricted_to[i]) != 0) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static inline long calc_open_time(long curr_time, long open_time_total, long open_time_first)
+{
+ return open_time_total + (curr_time - open_time_first);
+}
+
+static int manual_close(unsigned int fd)
+{
+ int retval = __close_fd(current->files, fd);
+
+ /* can't restart close syscall because file table entry was cleared */
+ if (unlikely(retval == -ERESTARTSYS || retval == -ERESTARTNOINTR || retval == -ERESTARTNOHAND || retval == -ERESTART_RESTARTBLOCK))
+ retval = -EINTR;
+
+ return retval;
+}
+
+static inline bool is_old(struct tm* curr, struct tm* open)
+{
+ return (curr->tm_yday > open->tm_yday) || (curr->tm_year > open->tm_year);
+}
+
+static inline long to_midnight(long curr_time, struct tm* curr)
+{
+ return curr_time - (((curr->tm_hour * 60) + curr->tm_min) * 60) + curr->tm_sec;
+}
+
+static bool allow_open(struct file* f)
+{
+ struct timespec curr_time;
+ struct tm curr, open;
+ long open_time_total;
+ long open_time_first;
+
+ long open_count;
+
+ if (long_getxattr(&(f->f_path), OPEN_TIME_TOTAL, &open_time_total) <= 0) {
+ open_time_total = 0;
+ }
+
+ if (long_getxattr(&(f->f_path), OPEN_COUNT_ATTR, &open_count) <= 0) {
+ open_count = 0;
+ }
+
+ curr_time = CURRENT_TIME;
+ if (long_getxattr(&(f->f_path), OPEN_TIME_FIRST, &open_time_first) <= 0) {
+ open_time_first = curr_time.tv_sec;
+ }
+ time_to_tm(curr_time.tv_sec, 0, &curr);
+ time_to_tm(open_time_first, 0, &open);
+
+ if (is_old(&curr, &open)) {
+ if (open_count > 0) {
+ open_time_first = to_midnight(curr_time.tv_sec, &curr);
+ }
+ open_time_total = 0;
+ long_setxattr(&(f->f_path), OPEN_TIME_TOTAL, &open_time_total);
+ }
+
+ if (open_count > 0) {
+ open_time_total = calc_open_time(curr_time.tv_sec, open_time_total, open_time_first);
+ }
+
+ if (open_time_total > MAX_OPEN_TIME) {
+ printk("Stopped Open: File: %s, Open Count: %ld\n", f->f_path.dentry->d_name.name, open_count);
+ return false;
+ }
+
+ if (open_count <= 0) {
+ long_setxattr(&(f->f_path), OPEN_TIME_FIRST, &(curr_time.tv_sec));
+ }
+
+ ++open_count;
+ long_setxattr(&(f->f_path), OPEN_COUNT_ATTR, &open_count);
+
+ printk("Allowed Open: File: %s, Open Count: %ld\n", f->f_path.dentry->d_name.name, open_count);
+ return true;
+}
+
+static void on_close(struct file* f)
+{
+ long curr_time;
+ long open_time_first;
+ long open_time_total;
+
+ long open_count;
+ ssize_t get_error;
+
+ struct tm curr, open;
+
+ get_error = long_getxattr(&(f->f_path), OPEN_COUNT_ATTR, &open_count);
+ if (get_error <= 0)
+ open_count = 0;
+
+ if (open_count > 0) {
+ --open_count;
+ long_setxattr(&(f->f_path), OPEN_COUNT_ATTR, &open_count);
+ if (open_count == 0) {
+ curr_time = CURRENT_TIME.tv_sec;
+
+ get_error = long_getxattr(&(f->f_path), OPEN_TIME_FIRST, &open_time_first);
+ if (get_error <= 0)
+ return;
+
+ get_error = long_getxattr(&(f->f_path), OPEN_TIME_TOTAL, &open_time_total);
+ if (get_error <= 0) {
+ open_time_total = 0;
+ }
+
+ time_to_tm(curr_time, 0, &curr);
+ time_to_tm(open_time_first, 0, &open);
+ if (is_old(&curr, &open)) {
+ open_time_first = to_midnight(curr_time, &curr);
+ open_time_total = 0;
+ }
+
+ open_time_total = calc_open_time(curr_time, open_time_total, open_time_first);
+ long_setxattr(&(f->f_path), OPEN_TIME_TOTAL, &open_time_total);
+ printk("File: %s was open for: %ld\n", f->f_path.dentry->d_name.name, open_time_total);
+ }
+ else {
+ printk("File: %s Open elsewhere.\n", f->f_path.dentry->d_name.name);
+ }
+ }
+ return;
+}
+
+/***********************************************/
+
long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
{
struct open_flags op;
@@ -1022,6 +1338,11 @@
fsnotify_open(f);
fd_install(fd, f);
trace_do_sys_open(tmp->name, flags, mode);
+
+ if (in_restricted_path(f) && !allow_open(f)) {
+ manual_close(fd);
+ fd = -EPERM;
+ }
}
}
putname(tmp);
@@ -1069,7 +1390,10 @@
printk(KERN_ERR "VFS: Close: file count is 0\n");
return 0;
}
-
+
+ if (in_restricted_path(filp))
+ on_close(filp);
+
if (filp->f_op->flush)
retval = filp->f_op->flush(filp, id);
@@ -1090,16 +1414,7 @@
*/
SYSCALL_DEFINE1(close, unsigned int, fd)
{
- int retval = __close_fd(current->files, fd);
-
- /* can't restart close syscall because file table entry was cleared */
- if (unlikely(retval == -ERESTARTSYS ||
- retval == -ERESTARTNOINTR ||
- retval == -ERESTARTNOHAND ||
- retval == -ERESTART_RESTARTBLOCK))
- retval = -EINTR;
-
- return retval;
+ return manual_close(fd);
}
EXPORT_SYMBOL(sys_close);