-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfend.c
More file actions
460 lines (410 loc) · 10 KB
/
fend.c
File metadata and controls
460 lines (410 loc) · 10 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
Author: Siddharth Sharma, NCSU
Unity ID: ssharm24
Class: CS501 2016
Instructor: Vincent Freeh
Assignment 1: Sandbox
*/
#include <sys/user.h> // for register user_regs_struct
#include <stdio.h> //fprintf, fscanf, printf, scanf, perror
#include <unistd.h> // fork exec
#include <sys/types.h> //pid_t
#include <stdlib.h> //for exit and EXIT, realpath
#include <sys/wait.h> //wait waitpid
#include <string.h> // strcmp, strcpy, strerror
#include <errno.h> //for errno
#include <sys/ptrace.h> //for ptrace
#include <err.h> //for err
#include <asm/unistd.h> // for __NR_read constants
#include <fcntl.h> // for O_RD constants
#include <fnmatch.h> // for matching wildcards
#include <sys/stat.h> // for checking if directory or file
typedef unsigned long long ull;
typedef struct user_regs_struct uregs;
typedef struct {
pid_t child;
int insyscall;
} sandbox;
typedef struct {
int syscall;
void (*callback)(sandbox *, uregs *,int);
int fileindex;
} sandb_syscall;
int exec_once = 0;
char global_config_path[1000];
char *config_name = ".fendrc";
char *restricted_file = "/root/z";
const int word_length = 8; //for x86_64 , 4 for x86
void handle_exec(sandbox *, uregs *, int);
void handle_open(sandbox *, uregs *, int);
void handle_write(sandbox *, uregs *, int);
void handle_rename(sandbox *, uregs *, int);
void sandb_init(sandbox *, int, char**);
void sandb_run(sandbox *);
void sandb_kill(sandbox *);
void sandb_handle_syscall(sandbox *);
int parse_input(int, char **);
int find_config(char *);
int find_local_config();
void get_string(sandbox *, ull *, char *);
void set_string(sandbox *, ull *, char *);
int rw_perm(int);
ull get_addr(uregs *, int);
int block_file(char *, int);
int block_file_exec(char *);
sandb_syscall sandb_syscalls[] = {
{__NR_creat, handle_write, 0},
{__NR_openat, handle_open, 1},
{__NR_open, handle_open, 0},
{__NR_mkdir, handle_write, 0},
{__NR_rmdir, handle_write, 0},
{__NR_unlink, handle_write, 0},
{__NR_unlinkat, handle_write, 1},
{__NR_execve, handle_exec, 0},
{__NR_rename, handle_rename, 0},
{__NR_renameat, handle_rename, 1},
{__NR_renameat2, handle_rename, 1},
};
int rw_perm(int p){
if(p==11) return 3;
else if(p==10) return 0;
else if(p==0) return -1;
else if(p==1) return 1;
}
ull get_addr(uregs *regs, int index){
switch(index){
case(0):
return regs->rdi;
case(1):
return regs->rsi;
case(2):
return regs->rdx;
case(3):
return regs->rcx;
default:
return 0;
}
}
int block_file(char *path, int demand){
int rwx,rw, block = 0;
char pattern[1000];
FILE *config = fopen(global_config_path,"r");
while(fscanf(config, "%d %s", &rwx, pattern) != EOF) {
rw = rw_perm(rwx/10);
if(fnmatch(pattern, path, 0) == 0){
block = (rw != 3) && (rw != demand);
}
}
fclose(config);
return block;
}
int block_file_exec(char *path){
int x, rwx,block = 0;
char pattern[1000];
FILE *config = fopen(global_config_path,"r");
while(fscanf(config, "%d %s", &rwx, pattern) != EOF) {
x = rwx%10;
if(fnmatch(pattern, path, 0) == 0){
block = (x != 1);
}
}
fclose(config);
return block;
}
char filepathex[1000];
char realfilepathex[1000];
void handle_exec(sandbox *sandb, uregs *regs, int index){
if(exec_once){
return;
}
ull fileaddr = get_addr(regs, sandb_syscalls[index].fileindex);
if(sandb->insyscall == 0){//syscall entry
get_string(sandb, &fileaddr, filepathex);
realpath(filepathex,realfilepathex);
if(block_file_exec(realfilepathex)){
set_string(sandb,&fileaddr,restricted_file);
sandb->insyscall = 2;
}
else{
sandb->insyscall = 1;
}
}
else{
if(sandb->insyscall == 2){
set_string(sandb, &fileaddr, filepathex);
}
sandb->insyscall = 0;
exec_once = 1;
}
}
char realfilepathrm1[1000];
char realfilepathrm2[1000];
char filepathrm1[1000];
char filepathrm2[1000];
void handle_rename(sandbox *sandb, uregs *regs, int index){
int m1=0,m2=1;
if(sandb_syscalls[index].fileindex == 1){
m1 = 1;
m2 = 3;
}
ull fileaddr1 = get_addr(regs, m1);
ull fileaddr2 = get_addr(regs, m2);
if(sandb->insyscall == 0){//syscall entry
int mode = 1; //write permission
get_string(sandb, &fileaddr1, filepathrm1);
get_string(sandb, &fileaddr2, filepathrm2);
realpath(filepathrm1,realfilepathrm1);
realpath(filepathrm2,realfilepathrm2);
if(block_file(realfilepathrm1,mode) || block_file(realfilepathrm2,mode)){
set_string(sandb,&fileaddr1,restricted_file);
sandb->insyscall = 2 ;
}
else{
sandb->insyscall = 1;
}
}
else{
if(sandb->insyscall==2){
set_string(sandb, &fileaddr1, filepathrm1);
}
sandb->insyscall = 0;
}
}
char filepathwrite[1000];
char realfilepathwrite[1000];
void handle_write(sandbox *sandb, uregs *regs, int index){
ull fileaddr = get_addr(regs, sandb_syscalls[index].fileindex);
if(sandb->insyscall == 0){//syscall entry
int mode = 1; //write permission
get_string(sandb, &fileaddr, filepathwrite);
realpath(filepathwrite,realfilepathwrite);
if(block_file(realfilepathwrite,mode)){
set_string(sandb,&fileaddr,restricted_file);
sandb->insyscall = 2 ;
}
else{
sandb->insyscall = 1;
}
}
else{
if(sandb->insyscall==2){
set_string(sandb, &fileaddr, filepathwrite);
}
sandb->insyscall=0;
}
}
char filepathop[1000];
char realfilepathop[1000];
void handle_open(sandbox *sandb, uregs *regs, int index){
ull fileaddr = get_addr(regs, sandb_syscalls[index].fileindex);
if(sandb->insyscall == 0){//syscall entry
ull modeaddr = get_addr(regs, sandb_syscalls[index].fileindex+1);
int creat = ((modeaddr & 67) > 3);
int mode = modeaddr & O_ACCMODE;
get_string(sandb, &fileaddr, filepathop);
realpath(filepathop,realfilepathop);
int filenotexist = (access(realfilepathop, F_OK ) == -1);
if(creat && filenotexist && (mode == 0)){
mode = 3;
}
if(block_file(realfilepathop,mode)){
set_string(sandb,&fileaddr,restricted_file);
sandb->insyscall = 2;
}
else{
sandb->insyscall = 1;
}
}
else{
if(sandb->insyscall==2){
set_string(sandb, &fileaddr, filepathop);
}
sandb->insyscall=0;
}
}
int find_config(char *config_path){
if(access(config_path, F_OK ) == -1){
return(EXIT_FAILURE);
}
strcpy(global_config_path,config_path);
return(EXIT_SUCCESS);
}
int find_local_config(){
int status;
char home_config_path[1000];
status = find_config(global_config_path);
if(status == EXIT_FAILURE){
strcpy(home_config_path, getenv("HOME"));
strcat(home_config_path, "/");
strcat(home_config_path, global_config_path);
status = find_config(home_config_path);
if(status==EXIT_FAILURE){
err(EXIT_FAILURE, "Must provide a config file.");
}
}
return(EXIT_SUCCESS);
}
int parse_input(int argc, char **argv){
int status;
int config_local = 1;
if(argc < 2){
errx(EXIT_FAILURE, "Input error. Usage : %s [-c config] <command>", argv[0]);
}
if(strcmp(argv[1],"-c")==0){
if(argc > 3){
status = find_config(argv[2]);
if(status == EXIT_FAILURE){
err(EXIT_FAILURE, "Must provide a config file.");
}
config_local = 0;
}
else{
errx(EXIT_FAILURE, "Input error. Usage : %s [-c config] <command>", argv[0]);
}
}
else{
find_local_config();
}
return config_local;
}
void get_string(sandbox *sandb, ull *addr, char *str){
char *laddr = str;
int i = 0;
union u {
long val;
char chars[sizeof(long)];
}data;
while(1){
data.val = ptrace(PTRACE_PEEKDATA,sandb->child, *addr + (i*word_length),NULL);
if(data.val < 0) {
if(errno == ESRCH) {
sandb_kill(sandb);
}
else{
exit(EXIT_FAILURE);
}
}
memcpy(laddr, data.chars, word_length);
if(data.chars[word_length-1]=='\0'){
break;
}
if(i>100){
break;
}
i += 1;
laddr += word_length;
}
}
void set_string(sandbox *sandb, ull *addr, char *str){
char *laddr = str;
int i = 0, status;
union u {
long val;
char chars[sizeof(long)];
}data;
while(1) {
memcpy(data.chars, laddr, word_length);
status = ptrace(PTRACE_POKEDATA,sandb->child, *addr + (i*word_length), data.val);
if(data.val < 0) {
if(errno == ESRCH) {
sandb_kill(sandb);
}
else{
exit(EXIT_FAILURE);
}
}
if(data.chars[word_length-1]=='\0'){
break;
}
if(i>100){
break;
}
i += 1;
laddr += word_length;
}
}
void sandb_handle_syscall(sandbox *sandb) {
int i, status;
uregs regs;
status = ptrace(PTRACE_GETREGS, sandb->child, NULL, ®s);
if(status < 0){
exit(EXIT_FAILURE);
}
for(i = 0; i < sizeof(sandb_syscalls)/sizeof(*sandb_syscalls); i++) {
if(regs.orig_rax == sandb_syscalls[i].syscall) {
if(sandb_syscalls[i].callback != NULL){
sandb_syscalls[i].callback(sandb,®s,i);
}
return;
}
}
if(regs.orig_rax == -1){
sandb_kill(sandb);
}
return;
}
void sandb_run(sandbox *sandb) {
int status, pstatus, ch;
pstatus = ptrace(PTRACE_SYSCALL, sandb->child, NULL, NULL);
if(pstatus < 0) {
if(errno == ESRCH) {
waitpid(sandb->child, &status, __WALL | WNOHANG);
sandb_kill(sandb);
}
else{
exit(EXIT_FAILURE);
}
}
wait(&status);
if(WIFEXITED(status)){
exit(EXIT_SUCCESS);
}
if(WIFSTOPPED(status)){
sandb_handle_syscall(sandb);
}
}
void sandb_kill(sandbox *sandb) {
kill(sandb->child, SIGKILL);
wait(NULL);
exit(EXIT_FAILURE);
}
void sandb_init(sandbox *sandb, int argc, char **argv) {
int status;
pid_t childpid;
childpid = fork();
switch(childpid){
case(0):
status = ptrace(PTRACE_TRACEME, 0, NULL, NULL);
if(status < 0){
exit(EXIT_FAILURE);
}
status = execvp(argv[0], argv);
if(status < 0){
exit(EXIT_FAILURE);
}
break;
case(-1):
exit(EXIT_FAILURE);
default:
sandb->child = childpid;
sandb->insyscall = 0;
wait(NULL);
}
}
int main(int argc, char **argv){
int config_local;
pid_t childpid;
strcpy(global_config_path,config_name);
config_local = parse_input(argc, argv);
sandbox sandb;
if(config_local){
sandb_init(&sandb, argc-1, argv+1);
}
else{
sandb_init(&sandb, argc-3, argv+3);
}
while(1){
sandb_run(&sandb);
}
return EXIT_SUCCESS;
}