-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfile_structures.c
More file actions
494 lines (482 loc) · 18.1 KB
/
file_structures.c
File metadata and controls
494 lines (482 loc) · 18.1 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "file_structures.h"
#include "ast.h"
#include "my_main.h"
/**
* Reads all bult-in functions of php in an array. The file is sorted in the first place
*/
void function_file_read( char * file, char *** flist, int *fcount) {
FILE * fp = fopen(file, "r");
if (fp==NULL) die("Could not open file.");
if (fp == NULL) die("Failed to read PHP built in functions list\n");
*fcount=0;
while (!feof(fp)) {
if (fgetc(fp) == '\n') (*fcount)++;
}
if (*fcount == 0) die("PHP built in functions list is empty?!\n");
*flist = (char **) malloc((*fcount) * sizeof (char *));
if (*flist == NULL) die("Malloc failed to hold all PHP functions\n");
char line[500]="";
rewind(fp);
int i = 0;
for (i = 0; i<*fcount; i++) {
fscanf(fp, "%s", (char*) line);
(*flist)[i] = (char *) malloc((strlen(line) + 1) * sizeof (char));
if ((*flist)[i]==0) die("Malloc failed to store name.");
strcpy((*flist)[i], line);
}
if (fclose(fp)!=0) die("Could not close file?");
}
/**
* Reads all taints from the taint file in two arrays. The file is sorted
* (per catrgory) in the first place
*/
void taint_file_read(char * file, char *** flist, int *fcount,char *** clist, int *ccount) {
FILE * fp = fopen(file, "r");
if (fp == NULL) die("Failed to read taints list\n");
int cf=0;
int cc=0;
int * cp=&cf;
while (!feof(fp)) {
char ch=fgetc(fp);
if (ch==EOF) break;
if (cf==0 && cc==0 && ch=='>') continue;
if (ch=='>') {
cp=&cc;
continue;
}
if (ch == '\n') (*cp)++;
}
cf--; cc--; //one extra line break from the category name
if (cf == 0 && cc==0) die("Taint functions list is empty?!\n");
*flist = (char **) malloc((cf) * sizeof (char *));
if (*flist == NULL) die("Malloc failed to hold all function taints\n");
*clist = (char **) malloc((cc) * sizeof (char *));
if (*clist == NULL) die("Malloc failed to hold all class taints\n");
*fcount=cf;
*ccount=cc;
char line[200];
rewind(fp);
int inclasses=0;
int i = 0;
while (!feof(fp)) {
int eof=fscanf(fp, "%s", (char*) line);
if (eof==EOF) break;
if (i++==0) continue; //ignore first line
if (strcmp(line,">classes")==0) {
inclasses=1;
i=1;
continue;
}
if (!inclasses) {
(*flist)[i-2] = (char *) malloc((strlen(line) + 1) * sizeof (char));
strcpy((*flist)[i-2], line);
//printf("Added1(%d): %s\n",i-2,line);
} else {
if (i-2==cc) break;
(*clist)[i-2] = (char *) malloc((strlen(line) + 1) * sizeof (char));
strcpy((*clist)[i-2], line);
//printf("Added2(%d): %s\n",i-2,line);
}
}
fclose(fp);
}
/**
* Binary search of a single function name in the list
*/
int list_search(char** flist, int fbase, int fcount, int fmax, const char * str) {
if (fmax==0) return -1;
if (fmax==1) {
if (strcmp(flist[0], str)==0) return 0;
else return -1;
}
int my_count = (fcount - fbase) / 2 + fbase; //the middle element in the range
int res = strcmp(flist[my_count], str);
if (res == 0) return my_count;
else if (fcount - fbase <= 1) {
int i = strcmp(flist[fbase], str);
if (i == 0) return fbase;
if (fcount==fmax) return -1;
i = strcmp(flist[fcount], str);
if (i == 0) return fcount;
else return -1;
} else if (res < 0) return list_search(flist, my_count, fcount, fmax, str);
else if (res > 0) return list_search(flist, fbase, my_count, fmax, str);
}
int list_print(char** flist, int fcount) {
int i=0;
for (i=0;i<fcount;i++) {
printf("FUNCTION: %s\n",flist[i]);
fflush(stdout);
}
}
/*
* Assumes a cetrainf format form the input files:
* f_name
* \tparam_type
* \tparamtype
* ...
* \t\treturntype
*/
void prototypes_file_read(char * file, prototype ** plist, int *pcount) {
FILE * fp = fopen(file, "r");
if (fp == NULL) die("Failed to read PHP function proptypes\n");
char line[200];
int i = 0;
*plist=NULL;
prototype * list=*plist;
*pcount=0;
for (i = 0; fgets(line,sizeof line,fp)!=NULL; i++) {
if (line==NULL || strlen(line)<2) break;
if (line[0]!='\t') {
(*pcount)++;
list=(prototype *)realloc(list, (*pcount) * sizeof(prototype));
if (list == NULL) die("realloc failed\n");
(list[*pcount-1]).name=strcpy_malloc(line);
int t=strlen((list[*pcount-1]).name);
for (t=t;t>=0;t--) {
if ((list[*pcount-1]).name[t]=='\n') {
(list[*pcount-1]).name[t]='\0';
break;
}
}
//printf("%s\n",list[*pcount-1]);
list[*pcount-1].parameters.total_parameters=0;
list[*pcount-1].parameters.types=NULL;
list[*pcount-1].return_type=NULL;
}
else if (line[0]=='\t' && line[1]!='\t') {
int total=++(list[*pcount-1].parameters.total_parameters);
list[*pcount-1].parameters.types=(char **)realloc(list[*pcount-1].parameters.types, total*sizeof(char **) );
if (list[*pcount-1].parameters.types == NULL) die("realloc failed\n");
list[*pcount-1].parameters.types[total-1] = strcpy_malloc(&(line[1]));
//kill the trailing \n;
list[*pcount-1].parameters.types[total-1][strlen(list[*pcount-1].parameters.types[total-1])-1]='\0';
}
else if (line[0]=='\t' && line[1]=='\t') {
list[*pcount-1].return_type=strcpy_malloc(&(line[2]));
//kill the trailing \n;
list[*pcount-1].return_type[strlen(list[*pcount-1].return_type)-1]='\0';
}
}
*plist=list;
fclose(fp);
}
void uprototypes_read_functions(char * file, prototype ** plist, int *pcount) {
FILE * fp = fopen(file, "r");
if (fp == NULL) die("Failed to read PHP user function proptypes\n");
char line[200];
int i = 0;
*plist=NULL;
prototype * list=*plist;
*pcount=0;
for (i = 0; fgets(line,sizeof line,fp)!=NULL; i++) {
if (line==NULL || strlen(line)<2) break;
if (strcmp(line,">functions\n")==0) continue;
else if (strcmp(line,">methods\n")==0) break;
if (line[0]!='\t') {
(*pcount)++;
list=(prototype *)realloc(list, (*pcount) * sizeof(prototype));
if (list == NULL) die("realloc failed\n");
(list[*pcount-1]).name=strcpy_malloc(line);
int t=strlen((list[*pcount-1]).name);
for (t=t;t>=0;t--) {
if ((list[*pcount-1]).name[t]=='\n') {
(list[*pcount-1]).name[t]='\0';
break;
}
}
//printf("%s\n",list[*pcount-1]);
list[*pcount-1].parameters.total_parameters=0;
list[*pcount-1].parameters.types=NULL;
list[*pcount-1].return_type=NULL;
}
else if (line[0]=='\t' && line[1]!='\t') {
int total=++(list[*pcount-1].parameters.total_parameters);
list[*pcount-1].parameters.types=(char **)realloc(list[*pcount-1].parameters.types, total*sizeof(char **) );
if (list[*pcount-1].parameters.types == NULL) die("realloc failed\n");
list[*pcount-1].parameters.types[total-1] = strcpy_malloc(&(line[1]));
//kill the trailing \n;
list[*pcount-1].parameters.types[total-1][strlen(list[*pcount-1].parameters.types[total-1])-1]='\0';
}
else if (line[0]=='\t' && line[1]=='\t') {
list[*pcount-1].return_type=strcpy_malloc(&(line[2]));
//kill the trailing \n;
list[*pcount-1].return_type[strlen(list[*pcount-1].return_type)-1]='\0';
}
}
*plist=list;
fclose(fp);
}
void uprototypes_read_methods(char * file, prototype ** plist, int *pcount) {
FILE * fp = fopen(file, "r");
if (fp == NULL) die("Failed to read PHP user method proptypes\n");
char line[200];
int i = 0;
*plist=NULL;
prototype * list=*plist;
*pcount=0;
int methods_started=0;
for (i = 0; fgets(line,sizeof line,fp)!=NULL; i++) {
//skip the first part of the file
if (!methods_started) {
if (strcmp(line,">methods\n")==0) methods_started=1;
continue;
}
if (line==NULL || strlen(line)<2) break;
if (line[0]!='\t') {
(*pcount)++;
list=(prototype *)realloc(list, (*pcount) * sizeof(prototype));
if (list == NULL) die("realloc failed\n");
(list[*pcount-1]).name=strcpy_malloc(line);
int t=strlen((list[*pcount-1]).name);
for (t=t;t>=0;t--) {
if ((list[*pcount-1]).name[t]=='\n') {
(list[*pcount-1]).name[t]='\0';
break;
}
}
//printf("%s\n",list[*pcount-1]);
list[*pcount-1].parameters.total_parameters=0;
list[*pcount-1].parameters.types=NULL;
list[*pcount-1].return_type=NULL;
}
else if (line[0]=='\t' && line[1]!='\t') {
int total=++(list[*pcount-1].parameters.total_parameters);
list[*pcount-1].parameters.types=(char **)realloc(list[*pcount-1].parameters.types, total*sizeof(char **) );
if (list[*pcount-1].parameters.types == NULL) die("realloc failed\n");
list[*pcount-1].parameters.types[total-1] = strcpy_malloc(&(line[1]));
//kill the trailing \n;
list[*pcount-1].parameters.types[total-1][strlen(list[*pcount-1].parameters.types[total-1])-1]='\0';
}
else if (line[0]=='\t' && line[1]=='\t') {
list[*pcount-1].return_type=strcpy_malloc(&(line[2]));
//kill the trailing \n;
list[*pcount-1].return_type[strlen(list[*pcount-1].return_type)-1]='\0';
}
}
*plist=list;
fclose(fp);
}
void prototypes_print(prototype * plist, int pcount) {
int i=0;
for (i=0;i<pcount;i++) {
printf("Prototype: %s\n",plist[i].name);
printf(" ret: %s\n",plist[i].return_type);
int j=0;
for (j=0;j<plist[i].parameters.total_parameters;j++) {
printf(" p%d: %s\n",j,plist[i].parameters.types[j]);
}
}
}
int prototypes_find(prototype * plist, int pcount, char* function) {
int i=0;
int res=-1;
for (i=0;i<pcount;i++) {
int cmp=strcmp(plist[i].name,function);
if (cmp==0) {
res=i;
break;
}
else if (cmp>0) break;
}
return res;
}
int prototype_total_parameters(prototype * plist, int pcount, char* function) {
int index=prototypes_find(plist,pcount,function);
if (index>-1) return plist[index].parameters.total_parameters;
else return -1;
}
char * prototype_parameter_type(prototype *plist, int pcount, char* function, int pnum, int ptotal) {
int index=prototypes_find(plist,pcount,function);
int total_parameters;
if (index>-1) {
total_parameters=plist[index].parameters.total_parameters;
char* rp_type=NULL;
int i=0;
for (i=0;i<total_parameters;i++) {
if (strstr(plist[index].parameters.types[i],"rp")!=NULL) rp_type=plist[index].parameters.types[i];
if (rp_type==NULL) {
if (i==pnum) {
return strcpy_malloc(plist[index].parameters.types[pnum]);
}
}
else {
if (pnum==ptotal-1) {
//printf("READ1:%s %d-%d\n",plist[index].parameters.types[pnum],pnum,ptotal);
return strcpy_malloc(plist[index].parameters.types[total_parameters-1]);
}
else {
//printf("READ2:%s\n",rp_type);
return strcpy_malloc(rp_type);
}
}
}
}
return strcpy_malloc("");
}
int prototype_has_ref_param(prototype *plist, int pcount, char* function) {
int res=0;
int index=prototypes_find(plist,pcount,function);
if (index>-1) {
int i=0;
for (i=0;i<plist[index].parameters.total_parameters;i++) {
if (strchr(plist[index].parameters.types[i],'&')!=NULL) {
res=1;
break;
}
}
}
return res;
}
char * prototype_return_type(prototype * plist, int pcount, char* function) {
int index=prototypes_find(plist,pcount,function);
if (index>-1) return strcpy_malloc(plist[index].return_type);
else return strcpy_malloc("");
}
int is_scalar(char * type) {
return strstr(type,"int")!=NULL || strstr(type,"bool")!=NULL || strstr(type,"string")!=NULL
|| strstr(type,"resource")!=NULL || strstr(type,"float")!=NULL;
}
int is_void(char * type) {
return strstr(type,"void")!=NULL;
}
int is_ref(char * type) {
return strchr(type,'&')!=NULL ;
}
int is_callback(char * type) {
return strstr(type,"callback")!=NULL;
}
int is_object(char * type) {
return strstr(type,"object")!=NULL;
}
//taint category functions
taint_category_list * category_file_read(char * file) {
taint_category_list *ret=(taint_category_list *)malloc(sizeof(taint_category_list));
ret->count=0;
ret->categories=NULL;
if (file==NULL) return ret;
FILE * fp = fopen(file, "r");
if (fp == NULL) die("Failed to read the taint category file provided\n");
char line[200];
while (fscanf(fp, "%s\n", (char*) line)>0) { //category loop
if (strcmp(line,"begin")==0) {
taint_category *tc=(taint_category *)malloc(sizeof(taint_category));
tc->glist=NULL;
tc->gcount=0;
tc->flist=NULL;
tc->fcount=0;
tc->slist=NULL;
tc->scount=0;
int reading_sanitisation=0;
int reading_sinks=0;
int reading_sources=0;
while (1) {
if (fscanf(fp,"%s\n",(char *)line)==0) break;
if (strcmp(line,"end")==0) break;
else if (strcmp(line,">sanitisation")==0) {
reading_sanitisation=1;
continue;
}
else if (strcmp(line,">sinks")==0) {
reading_sanitisation=0;
reading_sinks=1;
continue;
}
else if (strcmp(line,">sources")==0) {
reading_sanitisation=0;
reading_sinks=0;
reading_sources=1;
continue;
}
if (reading_sanitisation) {
tc->fcount++;
tc->flist=(char **)realloc(tc->flist, tc->fcount * (sizeof(char *)) );
if (tc->flist == NULL) die("realloc failed\n");
tc->flist[tc->fcount-1]=strcpy_malloc(line);
}
else if (reading_sinks || reading_sources) {
char *tok1=strtok((char *)line,"->");
char *tok2=strtok((char *)NULL,"->");
if (tok1!=NULL && tok2!=NULL) {
guard * g=(guard *)malloc(sizeof(guard));
g->name = strcpy_malloc(tok1);
g->sink = strcpy_malloc(tok2);
if (reading_sinks) {
tc->gcount++;
tc->glist = (guard **) realloc(tc->glist, tc->gcount * (sizeof (guard *)));
if (tc->glist == NULL) die("realloc failed\n");
tc->glist[tc->gcount - 1] = g;
} else if (reading_sources) {
tc->scount++;
tc->slist = (guard **) realloc(tc->glist, tc->scount * (sizeof (guard *)));
if (tc->slist == NULL) die("realloc failed\n");
tc->slist[tc->scount - 1] = g;
}
}
else die("Invalid guard in the category file");
}
}
//add to the category list
ret->count++;
ret->categories=(taint_category **)realloc(ret->categories, ret->count*sizeof(taint_category *));
ret->categories[ret->count-1]=tc;
}
}
fclose(fp);
return ret;
}
int category_file_count(char * file) {
int res=0;
if (file==NULL) return res;
FILE * fp = fopen(file, "r");
if (fp == NULL) die("Failed to read the taint category file provided\n");
char line[200];
while (fscanf(fp, "%s\n", (char*) line)>0) { //category loop
if (strcmp(line,"begin")==0) res++;
}
fclose(fp);
return res;
}
/*
* Given a function name, returns the index of the taint category.
* Returns -1 if the function is not a sanitisation function for any category.
*/
int category_find_index(taint_category_list *tc_list, char * function) {
//linear search, I expect taint categories to be very sort.
for (int i=0; i<tc_list->count; i++) {
taint_category * tc = tc_list->categories[i];
for ( int j=0; j<tc->fcount; j++ ) {
if (strcmp(function,tc->flist[j])==0) return i;
}
}
return -1;
}
char * category_find_guard(taint_category_list *tc_list, char * function, int is_sink) {
//linear search, I expect taint categories to be very sort.
for (int i=0; i<tc_list->count; i++) {
taint_category * tc = tc_list->categories[i];
if (is_sink) {
for (int j = 0; j < tc->gcount; j++) {
if (strcmp(function, tc->glist[j]->name) == 0)
return strcpy_malloc(tc->glist[j]->sink);
}
}
else {
for (int j = 0; j < tc->scount; j++) {
if (strcmp(function, tc->slist[j]->name) == 0)
return strcpy_malloc(tc->slist[j]->sink);
}
}
}
return NULL;
}
char * category_find_sink_guard(taint_category_list *tc_list, char * function) {
return category_find_guard(tc_list,function,1);
}
char * category_find_source_guard(taint_category_list *tc_list, char * function) {
return category_find_guard(tc_list,function,0);
}