-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.c
1490 lines (1280 loc) · 45.9 KB
/
report.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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2001-2002 Secure Software, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _MSC_VER
#include <windows.h>
#else
#include <sys/time.h>
#endif
#include <string.h>
#include "report.h"
int warning_level = 2;
static input_t * input_head = (input_t *)NULL;
static input_t * input_tail = (input_t *)NULL;
static ignore_t * ignore_list = (ignore_t *)NULL;
static vulnerability_t * list_head = (vulnerability_t *)NULL;
static vulnerability_t * list_tail = (vulnerability_t *)NULL;
static char *context_filename = NULL;
static FILE *context_fp = NULL;
static int context_line = 0;
static int lookup_ignore(char *filename, int lineno, char *token);
static int determine_ignorance(vulnerability_t *ptr);
static void diff_times(const struct timeval *, const struct timeval *, struct timeval *);
int total_lines = 0;
#ifdef _MSC_VER
DWORD time_started;
DWORD time_finished;
#else
struct timeval time_started;
struct timeval time_finished;
#endif
/* This function EXPECTS a MALLOCED BUFFER to be passed into it, as it will
* free it if it needs to be
*/
static char *
xml_escape(char *xstr)
{
char *result = NULL;
char *cntptr = NULL;
char *cpptr = NULL;
char *dstptr = NULL;
unsigned int newsz = 0;
newsz = strlen(xstr)+1;
cntptr = xstr;
while((cntptr = strchr(cntptr, '&')))
{
newsz += 4;
cntptr++;
}
cntptr = xstr;
while((cntptr = strchr(cntptr, '<')))
{
newsz += 3;
cntptr++;
}
cntptr = xstr;
while((cntptr = strchr(cntptr, '>')))
{
newsz += 3;
cntptr++;
}
if (newsz == strlen(xstr)+1)
{
return xstr;
}
result = malloc(newsz);
cpptr = xstr;
dstptr = result;
while(*cpptr && (dstptr < result+newsz))
{
if (*cpptr == '&')
{
strncat(dstptr, "&", 5);
dstptr += 5;
} else if (*cpptr == '<') {
strncat(dstptr, "<", 4);
dstptr += 4;
} else if (*cpptr == '>') {
strncat(dstptr, ">", 4);
dstptr += 4;
} else {
*dstptr = *cpptr;
*(dstptr+1) = 0;
dstptr++;
}
cpptr++;
}
free(xstr);
return result;
}
/* Exclusively for debugging vulnerabilities.
* - robbat2@gentoo.org 21/05/2006 */
static void debug_vuln_dump(vulnerability_t *ptr) {
fprintf(stderr,"vuln_dump: this=%x f=%s l=%d c=%d d=%x t=%d s=%d u=%x p=(%x,%x)\n",
(unsigned int) ptr,
ptr->filename,ptr->lineno,ptr->column,
(unsigned int) ptr->data,ptr->type,ptr->severity,
(unsigned int) ptr->uses, (unsigned int) ptr->next, (unsigned int) ptr->prev);
}
static void
replace_cfname(char *filename)
{
if (context_filename)
free(context_filename);
context_filename = malloc(strlen(filename)+1);
strncpy(context_filename, filename, strlen(filename));
context_filename[strlen(filename)] = 0;
}
static char *
getctx(char *filename, int lineno)
{
char *ret = NULL;
char buf[4096] = {0};
if (context_filename && strcmp(context_filename, filename))
{
replace_cfname(filename);
if (context_fp)
{
fclose(context_fp);
context_fp = NULL;
}
}
if (!context_filename)
{
replace_cfname(filename);
}
if (!context_fp)
{
context_fp = fopen(context_filename, "r");
context_line = 0;
if (!context_fp)
return NULL;
}
if (lineno <= context_line)
{
context_line = 0;
fseek(context_fp, 0, SEEK_SET);
}
while(fgets(buf, 4096, context_fp))
{
if(buf[strlen(buf)-1] == '\n')
{
context_line++;
}
if (context_line == lineno)
{
ret = malloc(strlen(buf)+1);
strncpy(ret, buf, strlen(buf));
ret[strlen(buf)] = 0;
return ret;
}
}
return NULL;
}
static
void insert_vulnerability(vulnerability_t *log)
{
int insert = 0;
vulnerability_t * ptr;
for (ptr = list_head; ptr != (vulnerability_t *)NULL; ptr = ptr->next)
{
if (ptr->severity < log->severity)
{
insert = 1;
ptr = ptr->prev;
break;
}
if (ptr->type == log->type && ptr->data == log->data)
{
for (ptr = ptr->next; ptr != (vulnerability_t *)NULL; ptr = ptr->next)
{
if (ptr->type != log->type || ptr->data != log->data)
{
ptr = ptr->prev;
break;
}
}
break;
}
}
if (ptr == (vulnerability_t *)NULL && !insert)
ptr = list_tail;
log->next = (ptr == (vulnerability_t *)NULL ? list_head : ptr->next);
log->prev = ptr;
if (log->next != (vulnerability_t *)NULL)
log->next->prev = log;
else
list_tail = log;
if (log->prev != (vulnerability_t *)NULL)
log->prev->next = log;
else
list_head = log;
}
void log_toctou(toctou_t **table, int first, int last, int check)
{
int i;
vulnerability_t * log;
if (check != -1)
{
int count = 0, index = 0;
for (i = first; i <= last; i++)
count += (table[i]->use);
log = (vulnerability_t *)malloc(sizeof(vulnerability_t));
log->filename = current_file;
log->lineno = table[check]->lineno;
log->column = table[check]->column;
log->data = table[check]->data;
log->type = RaceConditionCheck;
if (count > 0)
{
log->severity = Medium;
log->uses = (toctou_use_t *)malloc(sizeof(toctou_use_t) * (count + 1));
for (i = first; i <= last; i++)
{
if (table[i]->use)
{
log->uses[index].name = table[i]->data->Name;
log->uses[index].lineno = table[i]->lineno;
log->uses[index].column = table[i]->column;
index++;
}
}
log->uses[index].name = (char *)NULL;
log->uses[index].lineno = 0;
log->uses[index].column = 0;
}
else
{
log->severity = Low;
log->uses = (toctou_use_t *)NULL;
}
insert_vulnerability(log);
}
else
{
for (i = first; i <= last; i++)
{
log = (vulnerability_t *)malloc(sizeof(vulnerability_t));
log->filename = current_file;
log->column = table[i]->column;
log->lineno = table[i]->lineno;
log->data = table[i]->data;
log->type = RaceConditionUse;
log->severity = Low;
log->uses = (toctou_use_t *)NULL;
insert_vulnerability(log);
}
}
}
void log_vulnerability(type_t type, Severity_t severity)
{
vulnerability_t * log;
log = (vulnerability_t *)malloc(sizeof(vulnerability_t));
log->column = current_frame->column;
log->filename = current_file;
log->lineno = current_frame->lineno;
log->data = current_frame->data;
log->type = type;
log->severity = severity;
log->uses = (toctou_use_t *)NULL;
insert_vulnerability(log);
}
/* These are special static vulnerabilities because we don't
* want NULL data elements in the vulnerability_t->data
* field, because the HTML and XML output formats use that
* pointer without checking it for being null first.
* - robbat2@gentoo.org 21/05/2006 */
static struct Vuln_t vuln_PerlBacktick = {
.Name = "Perl Backtick"
};
static struct Vuln_t vuln_PhpBacktick = {
.Name = "PHP Backtick"
};
static struct Vuln_t vuln_PythonBacktick = {
.Name = "Python Backtick"
};
static struct Vuln_t vuln_StaticLocalBuffer = {
.Name = "Static Local Buffer"
};
static struct Vuln_t vuln_StaticGlobalBuffer = {
.Name = "Static Global Buffer"
};
void log_perlbacktick(int lineno, int column, Severity_t severity)
{
vulnerability_t * log;
log = (vulnerability_t *)malloc(sizeof(vulnerability_t));
log->filename = current_file;
log->column = column;
log->lineno = lineno;
log->data = &vuln_PerlBacktick;
log->type = PerlBacktick;
log->severity = severity;
log->uses = (toctou_use_t *)NULL;
insert_vulnerability(log);
}
void log_phpbacktick(int lineno, int column, Severity_t severity)
{
vulnerability_t * log;
log = (vulnerability_t *)malloc(sizeof(vulnerability_t));
log->filename = current_file;
log->column = column;
log->lineno = lineno;
log->data = &vuln_PhpBacktick;
log->type = PhpBacktick;
log->severity = severity;
log->uses = (toctou_use_t *)NULL;
insert_vulnerability(log);
}
void log_rubybacktick(int lineno, int column, Severity_t severity)
{
vulnerability_t * log;
log = (vulnerability_t *)malloc(sizeof(vulnerability_t));
log->filename = current_file;
log->column = column;
log->lineno = lineno;
log->data = (Vuln_t *)NULL;
log->type = RubyBacktick;
log->severity = severity;
log->uses = (toctou_use_t *)NULL;
insert_vulnerability(log);
}
void log_pythonbacktick(int lineno, int column, Severity_t severity)
{
vulnerability_t * log;
log = (vulnerability_t *)malloc(sizeof(vulnerability_t));
log->filename = current_file;
log->column = column;
log->lineno = lineno;
log->data = &vuln_PythonBacktick;
log->type = PythonBacktick;
log->severity = severity;
log->uses = (toctou_use_t *)NULL;
insert_vulnerability(log);
}
void log_staticbuffer(type_t type, int lineno, int column, Severity_t severity)
{
vulnerability_t * log;
log = (vulnerability_t *)malloc(sizeof(vulnerability_t));
log->filename = current_file;
log->column = column;
log->lineno = lineno;
switch(type) {
case StaticLocalBuffer:
log->data = &vuln_StaticLocalBuffer;
break;
case StaticGlobalBuffer:
log->data = &vuln_StaticGlobalBuffer;
break;
default:
log->data = (Vuln_t *)NULL;
}
log->type = type;
log->severity = severity;
log->uses = (toctou_use_t *)NULL;
insert_vulnerability(log);
}
void record_input(void)
{
input_t * input;
input = (input_t *)malloc(sizeof(input_t));
input->column = current_frame->column;
input->filename = current_file;
input->lineno = current_frame->lineno;
input->data = current_frame->data;
input->next = (input_t *)NULL;
if (input_tail != (input_t *)NULL)
input_tail->next = input;
else
input_head = input;
input_tail = input;
}
static
void cleanup_string(char *str)
{
int len;
char * c;
/* strip off leading a trailing whitespace */
for (c = str; *c && isspace(*c); c++);
for (len = strlen(c); len > 0 && isspace(*(c + len - 1)); len--);
*(c + len) = '\0';
memmove(str, c, len + 1);
/* squash occurences of multiple whitespace characters to a single one */
/* msg -- this code seems to corrupt memory on occasion */
//for (c = str + 1; *c; c++)
//{
// if (isspace(*c) && isspace(*(c - 1)))
// {
// char * start;
// for (start = c++; isspace(*c); c++);
// memmove(start, c, (len + 1) - (c - str));
// len -= (c - start);
// *(start - 1) = ' ';
// }
//}
}
static char *severities[] = { "Default", "Low", "Medium", "High" };
static void build_xml_vulnerability(vulnerability_t *ptr) {
int i;
/* Debugging - robbat2@gentoo.org 21/05/2006 */
if(ptr->data == NULL)
debug_vuln_dump(ptr);
printf("<vulnerability>\n");
/* Output the severity */
printf(" <severity>%s</severity>\n",
severities[ptr->severity]);
switch (ptr->type)
{
case BOProblem:
if (ptr->data->BOProblem->FormatArg > 0)
{
printf(" <type>%s</type>\n",
ptr->data->Name);
printf(" <message>\n");
printf(" Check to be sure that the format string passed as argument %d to this\n", ptr->data->BOProblem->FormatArg);
printf(" function call does not come from an untrusted source that could have added\n");
printf(" formatting characters that the code is not prepared to handle.\n");
printf(" Additionally, the format string could contain `%%s' without precision that\n");
printf(" could result in a buffer overflow.\n");
printf(" </message>\n");
}
if (ptr->data->BOProblem->SrcBufArg > 0)
{
printf(" <message>\n");
printf(" Check to be sure that argument %d passed to this function call will not\n", ptr->data->BOProblem->SrcBufArg);
printf(" copy more data than can be handled, resulting in a buffer overflow.\n");
printf(" </message>\n");
}
break;
case FSProblem:
printf(" <type>%s</type>\n",
ptr->data->Name);
printf(" <message>\n");
printf(" Check to be sure that the non-constant format string passed as argument %d \n", ptr->data->FSProblem->Arg);
printf(" to this function call does not come from an untrusted source that could\n");
printf(" have added formatting characters that the code is not prepared to handle.\n");
printf(" </message>\n");
break;
case InputProblem:
printf(" <type>%s</type>\n",
ptr->data->Name);
printf(" <message>\n");
printf(" Argument %d to this function call should be checked to ensure that it does\n", ptr->data->InputProblem->Arg);
printf(" not come from an untrusted source without first verifying that it contains\n");
printf(" nothing dangerous.\n");
printf(" </message>\n");
break;
case Info:
printf(" <type>%s</type>\n",
ptr->data->Name);
printf(" <message>\n");
if (ptr->data->Info->Description != (char *)NULL) {
cleanup_string(ptr->data->Info->Description);
printf(" %s\n", ptr->data->Info->Description);
}
if (ptr->data->Info->URL != (char *)NULL) {
cleanup_string(ptr->data->Info->URL);
/* This should possibly be made into it's own tag -- Robert */
printf(" foSee also:\n %s\n", ptr->data->Info->URL);
}
printf(" </message>\n");
break;
case RaceConditionCheck:
printf(" <type>%s</type>\n",
ptr->data->Name);
printf(" <message>\n");
printf(" A potential TOCTOU (Time Of Check, Time Of Use) vulnerability exists.\n");
printf(" This is the first line where a check has occured.");
if (ptr->uses != (toctou_use_t *)NULL && ptr->uses[0].lineno != 0)
{
printf("\n The following line(s) contain uses that may match up with this check:\n");
for (i = 0; ptr->uses[i].lineno != 0; i++)
printf(" %s%d (%s)", (i == 0 ? "" : ", "), ptr->uses[i].lineno, ptr->uses[i].name);
printf("\n");
}
else
{
printf(" No matching uses were detected.\n");
}
printf(" </message>\n");
break;
case RaceConditionUse:
printf(" <type>fixed size local buffer</type>\n");
printf(" <message>\n");
printf(" A potential race condition vulnerability exists here. Normally a call\n");
printf(" to this function is vulnerable only when a match check precedes it. No\n");
printf(" check was detected, however one could still exist that could not be\n");
printf(" detected.\n");
printf(" </message>\n");
break;
case StaticLocalBuffer:
printf(" <type>fixed size global buffer</type>\n");
printf(" <message>\n");
printf(" Extra care should be taken to ensure that character arrays that are\n");
printf(" allocated on the stack are used safely. They are prime targets for\n");
printf(" buffer overflow attacks.\n");
printf(" </message>\n");
break;
case StaticGlobalBuffer:
printf(" <type>%s</type>\n",
ptr->data->Name);
printf(" <message>\n");
printf(" Extra care should be taken to ensure that character arrays that are\n");
printf(" allocated with a static size are used safely. This appears to be a\n");
printf(" global allocation and is less dangerous than a similar one on the stack.\n");
printf(" Extra caution is still advised, however.\n");
printf(" </message>\n");
break;
case Reference:
printf(" <type>%s</type>\n",
ptr->data->Name);
printf(" <message>\n");
printf(" A function call is not being made here, but a reference is being made to\n");
printf(" a name that is normally a vulnerable function. It could be being\n");
printf(" assigned as a pointer to function.\n\n");
printf(" </message>\n");
break;
case PythonBacktick:
printf(" <type>Backtick</type>\n");
printf(" <message>\n");
printf(" Do not use a variable that has been derived from untrusted sources\n");
printf(" within a backtick. Doing so could allow an attacker to execute\n");
printf(" arbitrary python code.\n");
printf(" </message>\n");
break;
case PhpBacktick:
case PerlBacktick:
case RubyBacktick:
printf(" <type>Backtick</type>\n");
printf(" <message>\n");
printf(" The backtick will act just like an call to exec(), so care should be\n");
printf(" exercised that the string being backtick evaluated does not come from an\n");
printf(" untrusted source.\n");
printf(" </message>\n");
break;
case None:
printf(" <type>%s</type>\n",
ptr->data->Name);
printf(" <message>\n");
printf(" Unknown!?!?\n\n");
printf(" </message>\n");
break;
}
}
static
void report_vulnerability(vulnerability_t *ptr)
{
int i;
if(ptr->data == NULL)
debug_vuln_dump(ptr);
switch (ptr->type)
{
case BOProblem:
if (ptr->data->BOProblem->FormatArg > 0)
{
printf("Check to be sure that the format string passed as argument %d to this function\n", ptr->data->BOProblem->FormatArg);
printf("call does not come from an untrusted source that could have added formatting\n");
printf("characters that the code is not prepared to handle. Additionally, the format\n");
printf("string could contain `%%s' without precision that could result in a buffer\n");
printf("overflow.\n");
}
if (ptr->data->BOProblem->SrcBufArg > 0)
{
printf("Check to be sure that argument %d passed to this function call will not copy\n", ptr->data->BOProblem->SrcBufArg);
printf("more data than can be handled, resulting in a buffer overflow.\n");
}
printf("\n");
break;
case FSProblem:
printf("Check to be sure that the non-constant format string passed as argument %d to\n", ptr->data->FSProblem->Arg);
printf("this function call does not come from an untrusted source that could have added\n");
printf("formatting characters that the code is not prepared to handle.\n\n");
break;
case InputProblem:
printf("Argument %d to this function call should be checked to ensure that it does not\n", ptr->data->InputProblem->Arg);
printf("come from an untrusted source without first verifying that it contains nothing\n");
printf("dangerous.\n\n");
break;
case Info:
if (ptr->data->Info->Description != (char *)NULL)
{
cleanup_string(ptr->data->Info->Description);// comment out if causing problems
printf("%s\n", ptr->data->Info->Description);
}
if (ptr->data->Info->URL != (char *)NULL)
{
cleanup_string(ptr->data->Info->URL);
printf("See also: %s\n", ptr->data->Info->URL);
}
printf("\n");
break;
case RaceConditionCheck:
printf("A potential TOCTOU (Time Of Check, Time Of Use) vulnerability exists. This is\n");
printf("the first line where a check has occured.");
if (ptr->uses != (toctou_use_t *)NULL && ptr->uses[0].lineno != 0)
{
printf("\nThe following line(s) contain uses that may match up with this check:\n");
for (i = 0; ptr->uses[i].lineno != 0; i++)
printf("%s%d (%s)", (i == 0 ? "" : ", "), ptr->uses[i].lineno, ptr->uses[i].name);
printf("\n");
}
else
{
printf(" No matching uses were detected.\n");
}
printf("\n");
break;
case RaceConditionUse:
printf("A potential race condition vulnerability exists here. Normally a call to this\n");
printf("function is vulnerable only when a match check precedes it. No check was\n");
printf("detected, however one could still exist that could not be detected.\n\n");
break;
case StaticLocalBuffer:
printf("Extra care should be taken to ensure that character arrays that are allocated\n");
printf("on the stack are used safely. They are prime targets for buffer overflow\n");
printf("attacks.\n\n");
break;
case StaticGlobalBuffer:
printf("Extra care should be taken to ensure that character arrays that are allocated\n");
printf("with a static size are used safely. This appears to be a global allocation\n");
printf("and is less dangerous than a similar one on the stack. Extra caution is still\n");
printf("advised, however.\n\n");
break;
case Reference:
printf("A function call is not being made here, but a reference is being made to a name\n");
printf("that is normally a vulnerable function. It could be being assigned as a\n");
printf("pointer to function.\n\n");
break;
case PythonBacktick:
printf("Do not use a variable that has been derived from untrusted sources within a backtick.\n");
printf("Doing so could allow an attacker to execute arbitrary python code\n\n");
break;
case PhpBacktick:
case PerlBacktick:
case RubyBacktick:
printf("The backtick will act just like an call to exec(), so care should be exercised that the\n");
printf(" string being backtick evaluated does not come from an untrusted source\n\n");
break;
case None:
printf("Unknown!?!?\n\n");
break;
}
}
static
void html_report_inputs(void)
{
int count = 0;
input_t * next;
input_t * ptr;
if (!(flags & INPUT_MODE))
return;
for (ptr = input_head; ptr != (input_t *)NULL; ptr = next)
{
next = ptr->next;
if (!lookup_ignore(ptr->filename, ptr->lineno, ptr->data->Name))
{
count++;
printf("<b>%s</b>: Line %d: function %s<br>\n", ptr->filename, ptr->lineno, ptr->data->Name);
}
free(ptr);
}
input_head = input_tail = (input_t *)NULL;
if (count > 0)
{
printf("<br>Double check to be sure that all input accepted from an external data source\n");
printf("does not exceed the limits of the variable being used to hold it. Also make\n");
printf("sure that the input cannot be used in such a manner as to alter your program's\n");
printf("behaviour in an undesirable way.<br>\n");
}
}
static
void xml_report_inputs(void)
{
int count = 0;
input_t * next;
input_t * ptr;
if (!(flags & INPUT_MODE))
return;
for (ptr = input_head; ptr != (input_t *)NULL; ptr = next)
{
next = ptr->next;
if (!lookup_ignore(ptr->filename, ptr->lineno, ptr->data->Name))
{
count++;
printf("<input>\n");
printf("<message>");
printf("Double check to be sure that all input accepted from an external data source does not exceed the limits of the variable being used to hold it. Also make sure that the input cannot be used in such a manner as to alter your program's behaviour in an undesireable way");
printf("</message>\n") ;
printf("<function>%s</function>\n", ptr->data->Name);
printf("<file><name>%s</name><line>%d</line></file>\n", ptr->filename, ptr->lineno);
printf("</input>\n");
}
free(ptr);
}
input_head = input_tail = (input_t *)NULL;
}
static
void report_inputs(void)
{
int count = 0;
input_t * next;
input_t * ptr;
if (!(flags & INPUT_MODE))
return;
for (ptr = input_head; ptr != (input_t *)NULL; ptr = next)
{
next = ptr->next;
if (!lookup_ignore(ptr->filename, ptr->lineno, ptr->data->Name))
{
count++;
printf("%s: %d: %s\n", ptr->filename, ptr->lineno, ptr->data->Name);
}
free(ptr);
}
input_head = input_tail = (input_t *)NULL;
if (count > 0)
{
printf("Double check to be sure that all input accepted from an external data source\n");
printf("does not exceed the limits of the variable being used to hold it. Also make\n");
printf("sure that the input cannot be used in such a manner as to alter your program's\n");
printf("behaviour in an undesirable way.\n\n");
}
}
void generate_xml() {
char vuln_reported=0; /* Have we spewed the vuln message yet? */
vulnerability_t * ptr;
/* Initial necessary cruft */
/* Loop iterates through all of the problems found */
for (ptr = list_head; ptr != (vulnerability_t *)NULL; ptr = ptr->next) {
/* Check the severity of the vuln. If it's below our level, skip
* and go to the next one */
if (ptr->severity != Default && ptr->severity < warning_level) {
continue;
}
if (determine_ignorance(ptr))
{
continue;
}
/* If we haven't reported the vuln message yet for this type, do so. */
if(!vuln_reported) {
build_xml_vulnerability(ptr);
vuln_reported++;
}
/* If the filename of this vuln is different from the filename of the
* previous vuln, report the filename, or if the vuln type is different*/
if(ptr->prev==(vulnerability_t *)NULL||
strcmp(ptr->filename,ptr->prev->filename)|| ptr->type == RaceConditionCheck ||
ptr->prev->type != ptr->type || ptr->prev->data != ptr->data) {
printf(" <file>\n <name>%s</name>\n",
ptr->filename);
}
/* report the line number of the infraction */
printf(" <line>%d</line>\n",
ptr->lineno);
if (flags & SHOW_COLUMNS)
printf(" <column>%d</column>\n", ptr->column);
if (flags & SHOW_CONTEXT)
{
char *ctx = NULL;
ctx = getctx(ptr->filename, ptr->lineno);
if (ctx)
{
ctx = xml_escape(ctx);
printf("<context>%s</context>\n", ctx);
free(ctx);
}
}
/* If the next file or vuln type is different close the file tag */
if(ptr->next==(vulnerability_t *)NULL||
strcmp(ptr->filename,ptr->next->filename)|| ptr->type == RaceConditionCheck ||
ptr->next->type != ptr->type || ptr->next->data != ptr->data) {
printf(" </file>\n");
}
/* If the next vuln is different reset the vuln_reported variable to 0 so
* we know to report next time */
if (ptr->next == (vulnerability_t *)NULL || ptr->next->type != ptr->type ||
ptr->type == RaceConditionCheck || ptr->next->data != ptr->data) {
printf("</vulnerability>\n");
vuln_reported=0;
}
}
xml_report_inputs();
if (!(flags & NO_FOOTER))
{
#ifdef _MSC_VER
DWORD ttime;
#else
struct timeval ttime;
#endif
float fsecs;
#ifdef _MSC_VER
ttime = time_finished - time_started;
fsecs = ttime/1000 + (float)((ttime%1000)/1000);
#else
diff_times(&time_finished, &time_started, &ttime);
fsecs = ttime.tv_sec+ (ttime.tv_usec/(double)1000000);
#endif
printf("<timing>\n");
printf("<total_lines>%d</total_lines>\n", total_lines);
printf("<total_time>%f</total_time>\n", fsecs);
printf("<lines_per_second>%d</lines_per_second>\n", (int)(total_lines/fsecs));
printf("</timing>\n");
}
printf("</rats_output>\n");
}
static void build_html_vulnerability(vulnerability_t *ptr) {
int i;
/* Debugging - robbat2@gentoo.org 21/05/2006 */
if(ptr->data == NULL)
debug_vuln_dump(ptr);
/* Output the severity */
printf(" <b>Severity: %s</b><br/>\n",
severities[ptr->severity]);
switch (ptr->type)
{
case BOProblem:
if (ptr->data->BOProblem->FormatArg > 0)
{
printf(" Issue: %s<br/>\n",
ptr->data->Name);
printf(" Check to be sure that the format string passed as argument %d to this\n", ptr->data->BOProblem->FormatArg);
printf(" function call does not come from an untrusted source that could have added\n");
printf(" formatting characters that the code is not prepared to handle.\n");
printf(" Additionally, the format string could contain `%%s' without precision that\n");
printf(" could result in a buffer overflow.\n");
printf(" <br/>\n");
}
if (ptr->data->BOProblem->SrcBufArg > 0)
{
printf(" Issue: %s<br/>\n",
ptr->data->Name);
printf(" Check to be sure that argument %d passed to this function call will not\n", ptr->data->BOProblem->SrcBufArg);
printf(" copy more data than can be handled, resulting in a buffer overflow.\n");
printf(" <br/>\n");
}
break;
case FSProblem:
printf(" Issue: %s<br/>\n",
ptr->data->Name);
printf(" Check to be sure that the non-constant format string passed as argument %d \n", ptr->data->FSProblem->Arg);
printf(" to this function call does not come from an untrusted source that could\n");
printf(" have added formatting characters that the code is not prepared to handle.\n");
printf(" <br/>\n");
break;
case InputProblem:
printf(" Issue: %s<br/>\n",
ptr->data->Name);
printf(" Argument %d to this function call should be checked to ensure that it does\n", ptr->data->InputProblem->Arg);
printf(" not come from an untrusted source without first verifying that it contains\n");