-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfortify.c
2352 lines (2035 loc) · 60.8 KB
/
fortify.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
/*
* FILE:
* fortify.c
*
* DESCRIPTION:
* A fortified shell for malloc, realloc, calloc, strdup, getcwd, tempnam
* and free.
* To use Fortify, each source file will need to #include "fortify.h". To
* enable Fortify, define the symbol FORTIFY. If FORTIFY is not defined, it
* will compile away to nothing. If you do not have stderr available, you may
* wish to set an alternate output function. See _Fortify_SetOutputFunc(),
* below.
* You will also need to link in fortify.o
*
* None of the functions in this file should really be called
* directly; they really should be called through the macros
* defined in fortify.h
*
*/
#if defined FORTIFY
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdlib.h>
#if defined MSDOS || defined __BORLANDC__ || defined WIN32 || defined __HIGHC__
# include <direct.h>
#endif
extern int EndOfPgr(int);
#define __FORTIFY_C__ /* So fortify.h knows to not define the fortify macros */
#include "fortify.h"
#include "ufortify.h" /* the user's options */
char *_Fortify_file=NULL;
int _Fortify_line=0;
#ifndef FORTIFY_TRANSPARENT
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#if defined MSDOS || defined __BORLANDC__ || defined WIN32 || defined __HIGHC__
# if !defined WIN32
# undef MSDOS
# define MSDOS
# endif
# include <conio.h>
#else
# include <unistd.h>
# include <termio.h>
# define getch() getchar()
#endif
#if defined _WINDOWS
# include "windows.h"
# if !defined WIN32
# include "toolhelp.h"
# endif
#endif
#if defined LONGNAME
# include "longname.h"
#endif
#if !defined MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
struct Header
{
char *File; /* The sourcefile of the caller */
unsigned short Line; /* The sourceline of the caller */
size_t Size; /* The size of the malloc'd block */
struct Header *Prev, /* List pointers */
*Next;
int Checksum; /* For validating the Header structure; see ChecksumHeader() */
unsigned char Scope;
};
#if defined AViiON || defined __GNUC__ || defined _MSC_VER
# define _static static
#else
# define _static
#endif
_static char *address __OF((void *addr));
_static int TimeToCheck __OF((void));
_static int CheckBlock __OF((struct Header *h, char *file, unsigned long line));
_static int CheckPointer __OF((unsigned char *ptr, unsigned long size, char *file, unsigned long line));
_static int CheckFortification __OF((unsigned char *ptr, unsigned char value, size_t size));
_static void SetFortification __OF((unsigned char *ptr, unsigned char value, size_t size));
_static void OutputFortification __OF((unsigned char *ptr, unsigned char value, size_t size));
_static int IsHeaderValid __OF((struct Header *h));
_static void MakeHeaderValid __OF((struct Header *h));
_static int ChecksumHeader __OF((struct Header *h));
_static int IsOnList __OF((struct Header *h));
_static void OutputHeader __OF((struct Header *h));
_static void OutputMemory __OF((struct Header *h));
_static void st_DefaultOutput __OF((char *String));
_static void WaitIfstdOutput __OF((void));
static char stdOutput = 0; /* If true, did some stderr output */
static OutputFuncPtr st_Output = st_DefaultOutput; /* Output function for errors */
#if !defined MSDOS && !defined WIN32
static int strnicmp(s1,s2,maxlen)
char *s1,*s2;
size_t maxlen;
{
while ((maxlen) && (*s1) && (*s2) && (toupper(*s1)==toupper(*s2))) {
maxlen--;
s1++;
s2++;
}
return((maxlen) ? toupper(*s1)-toupper(*s2) : 0);
}
static int stricmp(s1,s2)
char *s1,*s2;
{
return(strnicmp(s1,s2,strlen(s1)+1));
}
#endif
static char *address(void *addr)
{
static char str[80];
#if defined KNOWS_POINTER_TYPE
sprintf(str,"%p",addr);
#else
sprintf(str,"%lx",(unsigned long) addr);
#endif
return(str);
}
#ifdef FORTIFY_CheckInterval
int TimeToCheck()
{
static time_t lastcheck=0L;
time_t t;
int ret = 0;
time(&t);
if ((lastcheck==0L) || (t-lastcheck>=FORTIFY_CheckInterval))
{
lastcheck = t;
ret = 1;
}
return(ret);
}
#endif
static FILE *gfile=NULL;
static int Nchars=0,Nlines=0;
static char flag=0;
static void _Fortify_NoOutput()
{
}
static void st_DefaultOutput(char *String)
{
static FILE *file;
static char first=1;
if (first) {
file=stderr;
first=0;
}
if (stdOutput==0) {
Nchars=Nlines=0;
if (gfile!=NULL) rewind(gfile);
}
if (flag==0)
{
char *ptr;
file=stderr;
flag = 1;
if ((ptr=getenv("FORTIFY_OUTPUT"))!=NULL)
{
if ((stricmp(ptr,"null")==0) || (stricmp(ptr,"nul")==0))
file=NULL;
else if (stricmp(ptr,"stderr")==0)
file=stderr;
else if (stricmp(ptr,"stdout")==0)
file=stdout;
#if defined MSDOS && !defined _WINDOWS && !defined WIN32
else if (stricmp(ptr,"stdprn")==0)
file=stdprn;
#endif
else if ((file=fopen(ptr,"w"))==NULL)
{
#if !defined _WINDOWS
fprintf(stderr,"\r\nFortify: Unable to create logfile %s\r\n",ptr);
EndOfPgr(4);
#else
{
char str[255];
sprintf(str,"Unable to create logfile\n \"%s\"",ptr);
MessageBox((HWND) NULL,(LPCSTR) str,(LPCSTR) "Fortify",(UINT) MB_ICONSTOP);
#if 0
#if defined WIN32
/* TerminateProcess(GetCurrentProcess(),65535); */
ExitProcess(65535);
#else
TerminateApp((HTASK) NULL,(WORD) NO_UAE_BOX);
#endif
#else
EndOfPgr(1);
#endif
}
#endif
}
}
if ((file!=NULL) && (file!=stderr) && (file!=stdout))
{
time_t t;
time(&t);
fprintf(file,"Generated on: %s%s\n",
ctime(&t),
(file==stdout) || (file==stderr) ? "\r" : ""
);
}
}
if (file!=NULL)
{
#if defined _WINDOWS
if ((file==stdout) || (file==stderr)) {
#if defined LINE_BY_LINE
if (MessageBox((HWND) NULL,(LPCSTR) String,(LPCSTR) "Fortify",(UINT) MB_OKCANCEL /* |MB_ICONINFORMATION */)==IDCANCEL)
#if 0
#if defined WIN32
/* TerminateProcess(GetCurrentProcess(),65535); */
ExitProcess(65535);
#else
TerminateApp((HTASK) NULL,(WORD) NO_UAE_BOX);
#endif
#else
EndOfPgr(1);
#endif
#else
{
char *ptr;
ptr="fortify.tmp";
if ((ptr==NULL) || ((file=gfile=fopen(ptr,"w+"))==NULL))
{
char str[255];
sprintf(str,"Unable to create temporary file\n \"%s\"",(ptr==NULL) ? "(NULL)" : ptr);
MessageBox((HWND) NULL,(LPCSTR) str,(LPCSTR) "Fortify",(UINT) MB_ICONSTOP);
#if 0
#if defined WIN32
/* TerminateProcess(GetCurrentProcess(),65535); */
ExitProcess(65535);
#else
TerminateApp((HTASK) NULL,(WORD) NO_UAE_BOX);
#endif
#else
EndOfPgr(1);
#endif
}
}
#endif
}
if ((file!=stdout) && (file!=stderr))
#endif
{
int i,ch=-1;
for (i=0;(String[i]) && (Nlines<30);i++)
if (String[i]=='\n') Nlines++;
if ((String[i]) && (String[i+1])) {
ch=String[i+1];
String[i+1]=0;
}
if ((file==stdout) || (file==stderr)) {
char *ptr=String;
int i;
do {
for (i=0;(ptr[i]) && (ptr[i]!='\r') && (ptr[i]!='\n');i++);
Nchars+=fprintf(file,"%-*.*s%s",
i,i,
ptr,
(ptr[i]) ? "\r\n" : ""
);
ptr+=i;
if (ptr[0]=='\r') ptr++;
if (ptr[0]=='\n') ptr++;
} while (*ptr);
}
else Nchars+=fprintf(file,String);
if (ch>=0) String[i+1]=(char)ch;
if (Nlines>=30) {
WaitIfstdOutput();
Nchars=Nlines=0;
stdOutput = 0;
if ((String[i]) && (String[i+1])) {
if ((file==stderr) || (file==stdout) || ((gfile!=NULL) && (Nchars)))
stdOutput = 1;
st_DefaultOutput(String+i);
}
}
}
if ((file==stderr) || (file==stdout) || ((gfile!=NULL) && (Nchars)))
stdOutput = 1;
}
}
static void WaitIfstdOutput()
{
#if !defined _WINDOWS
if((stdOutput) && (st_Output != (OutputFuncPtr) _Fortify_NoOutput))
{
#ifdef FORTIFY_WAIT_FOR_KEY
static signed char wait_on_key=-1;
if(wait_on_key<0)
{
char *ptr;
if (((ptr=getenv("FORTIFY_WAIT_FOR_KEY"))!=NULL) &&
(tolower(*ptr)=='n')) wait_on_key = 0;
else wait_on_key = 1;
}
if(wait_on_key)
{
char c;
#if !defined MSDOS && !defined WIN32
struct termio tio,tiobak;
char flag;
if ((flag=ioctl(0,TCGETA,&tio))==0) /* handle 0 is stdin */
{
tiobak=tio;
tio.c_lflag&=~ICANON;
tio.c_lflag&=~ECHO;
tio.c_cc[VMIN]=1;
ioctl(0,TCSETA,&tio);
}
#endif /* !MSDOS */
c = (char)getch();
#if !defined MSDOS && !defined WIN32
if (flag==0)
ioctl(0,TCSETA,&tiobak);
#endif /* !MSDOS */
if ((c == 3) || (c == 0x1b)) EndOfPgr(3);
}
#endif /* FORTIFY_WAIT_FOR_KEY */
}
#else
# if !defined LINE_BY_LINE
if ((stdOutput) && (gfile!=NULL) && (Nchars))
{
char *ptr;
ptr=(char *) malloc(Nchars+1);
if (ptr!=NULL)
{
int n=0,l=0;
rewind(gfile);
while ((n<Nchars) && (l<Nlines))
{
fgets(ptr+n,Nchars-n+1,gfile);
n+=(int)strlen(ptr+n);
l++;
}
if (MessageBox((HWND) NULL,(LPCSTR) ptr,(LPCSTR) "Fortify",(UINT) MB_OKCANCEL /* |MB_ICONINFORMATION */)==IDCANCEL)
#if 0
#if defined WIN32
/* TerminateProcess(GetCurrentProcess(),65535); */
ExitProcess(65535);
#else
TerminateApp((HTASK) NULL,(WORD) NO_UAE_BOX);
#endif
#else
EndOfPgr(1);
#endif
}
free(ptr);
}
# endif
#endif
stdOutput = 0;
}
static struct Header *st_Head = NULL; /* Head of alloc'd memory list */
static char st_Buffer[256]; /* Temporary buffer for sprintf's */
static int st_Disabled = 0; /* If true, Fortify is inactive */
static int st_MallocFailRate = 0; /* % of the time to fail mallocs */
static char *st_LastVerifiedFile = "unknown";
static unsigned short st_LastVerifiedLine = 0;
static unsigned char st_Scope = 0;
static void OutputLastVerifiedPoint __OF((void));
void FORTIFY_STORAGE
_Fortify_Init(char *file,unsigned long line)
{
if (gfile!=NULL) fclose(gfile);
gfile=NULL;
Nchars=Nlines=0;
flag=0;
st_Head=NULL;
stdOutput=0;
st_Output=st_DefaultOutput;
st_Disabled=0;
st_MallocFailRate=0;
st_LastVerifiedFile="unknown";
st_LastVerifiedLine=0;
st_Scope=0;
}
/*
* _Fortify_malloc() - Allocates a block of memory, with extra bits for
* misuse protection/detection.
*
* Features:
* + Adds the malloc'd memory onto Fortify's own private list.
* (With a checksum'd header to detect corruption of the memory list)
* + Places sentinals on either side of the user's memory with
* known data in them, to detect use outside of the bounds
* of the block
* + Initializes the malloc'd memory to some "nasty" value, so code
* can't rely on it's contents.
* + Can check all sentinals on every malloc.
* + Can generate a warning message on a malloc fail.
* + Can randomly "fail" at a set fail rate
*/
void *FORTIFY_STORAGE
_Fortify_malloc(size_t size,char *file,unsigned long line)
{
unsigned char *ptr;
struct Header *h;
stdOutput = 0;
FORTIFY_LOCK();
if(st_Disabled)
{
ptr = (unsigned char *) malloc(size);
FORTIFY_UNLOCK();
WaitIfstdOutput();
return((void *) ptr);
}
#ifdef CHECK_ALL_MEMORY_ON_MALLOC
#ifdef FORTIFY_CheckInterval
if (TimeToCheck())
#endif
_Fortify_CheckAllMemory(file, line);
#endif
if(size == 0)
{
#ifdef WARN_ON_ZERO_MALLOC
sprintf(st_Buffer,
"\nFortify: %s.%ld\n malloc(0) attempted failed\n",
file, line);
st_Output(st_Buffer);
#endif
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(0);
}
if(st_MallocFailRate > 0)
{
if(rand() % 100 < st_MallocFailRate)
{
#ifdef WARN_ON_FALSE_FAIL
sprintf(st_Buffer,
"\nFortify: %s.%ld\n malloc(%ld) \"false\" failed\n",
file, line, (unsigned long)size);
st_Output(st_Buffer);
#endif
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(0);
}
}
/*
* malloc the memory, including the space for the header and fortification
* buffers
*/
#ifdef WARN_ON_SIZE_T_OVERFLOW
{
size_t private_size = sizeof(struct Header)
+ FORTIFY_BEFORE_SIZE + size + FORTIFY_AFTER_SIZE;
if(private_size < size) /* Check to see if the added baggage is larger than size_t */
{
sprintf(st_Buffer,
"\nFortify: %s.%ld\n malloc(%ld) has overflowed size_t.\n",
file, line, (unsigned long)size);
st_Output(st_Buffer);
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(0);
}
}
#endif
ptr = (unsigned char *) malloc(sizeof(struct Header) +
FORTIFY_BEFORE_SIZE + size + FORTIFY_AFTER_SIZE);
if(!ptr)
{
#ifdef WARN_ON_MALLOC_FAIL
sprintf(st_Buffer, "\nFortify: %s.%ld\n malloc(%ld) failed\n",
file, line, (unsigned long)size);
st_Output(st_Buffer);
#endif
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(0);
}
/*
* Initialize and validate the header
*/
h = (struct Header *)ptr;
h->Size = size;
h->File = file;
h->Line = (unsigned short) line;
h->Next = st_Head;
h->Prev = 0;
h->Scope = st_Scope;
if(st_Head)
{
st_Head->Prev = h;
MakeHeaderValid(st_Head);
}
st_Head = h;
MakeHeaderValid(h);
/*
* Initialize the fortifications
*/
SetFortification(ptr + sizeof(struct Header),
FORTIFY_BEFORE_VALUE, FORTIFY_BEFORE_SIZE);
SetFortification(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE + size,
FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);
#ifdef FILL_ON_MALLOC
/*
* Fill the actual user memory
*/
SetFortification(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE,
FILL_ON_MALLOC_VALUE, size);
#endif
/*
* We return the address of the user's memory, not the start of the block,
* which points to our magic cookies
*/
FORTIFY_UNLOCK();
WaitIfstdOutput();
return((void *) (ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE));
}
/*
* _Fortify_free() - This free must be used for all memory allocated with
* _Fortify_malloc().
*
* Features:
* + Pointers are validated before attempting a free - the pointer
* must point to a valid malloc'd bit of memory.
* + Detects attempts at freeing the same block of memory twice
* + Can clear out memory as it is free'd, to prevent code from using
* the memory after it's been freed.
* + Checks the sentinals of the memory being freed.
* + Can check the sentinals of all memory.
*/
void FORTIFY_STORAGE
_Fortify_free(void *uptr,char *file,unsigned long line)
{
unsigned char *ptr;
struct Header *h;
if(uptr == NULL)
return;
ptr = (unsigned char *)uptr - sizeof(struct Header) - FORTIFY_BEFORE_SIZE;
h = (struct Header *)ptr;
stdOutput = 0;
FORTIFY_LOCK();
if(st_Disabled)
{
free(uptr);
FORTIFY_UNLOCK();
WaitIfstdOutput();
return;
}
#ifdef CHECK_ALL_MEMORY_ON_FREE
#ifdef FORTIFY_CheckInterval
if (TimeToCheck())
#endif
_Fortify_CheckAllMemory(file, line);
#endif
#ifdef PARANOID_FREE
if(!IsOnList(h))
{
sprintf(st_Buffer,
"\nFortify: %s.%ld\n Invalid pointer, corrupted header, or possible free twice\n",
file, line);
st_Output(st_Buffer);
OutputLastVerifiedPoint();
goto fail;
}
#endif
if(!CheckBlock(h, file, line))
goto fail;
/*
* Remove the block from the list
*/
if(h->Prev)
{
if(!CheckBlock(h->Prev, file, line))
goto fail;
h->Prev->Next = h->Next;
MakeHeaderValid(h->Prev);
}
else
st_Head = h->Next;
if(h->Next)
{
if(!CheckBlock(h->Next, file, line))
goto fail;
h->Next->Prev = h->Prev;
MakeHeaderValid(h->Next);
}
#ifdef FILL_ON_FREE
/*
* Nuke out all memory that is about to be freed
*/
SetFortification(ptr, FILL_ON_FREE_VALUE,
sizeof(struct Header) + FORTIFY_BEFORE_SIZE + h->Size + FORTIFY_AFTER_SIZE);
#endif
/*
* And do the actual free
*/
free(ptr);
FORTIFY_UNLOCK();
WaitIfstdOutput();
return;
fail:
sprintf(st_Buffer, " free(%s) failed\n", address(uptr));
st_Output(st_Buffer);
FORTIFY_UNLOCK();
WaitIfstdOutput();
}
/*
* _Fortify_realloc() - Uses _Fortify_malloc() and _Fortify_free() to implement
* realloc().
*
* Features:
* + The realloc'd block is ALWAYS moved.
* + The pointer passed to realloc() is verified in the same way that
* _Fortify_free() verifies pointers before it frees them.
* + All the _Fortify_malloc() and _Fortify_free() protection
*/
void *FORTIFY_STORAGE
_Fortify_realloc(void *ptr,size_t new_size,char *file,unsigned long line)
{
void *new_ptr;
struct Header *h;
if(new_size == 0)
{
_Fortify_free(ptr,file,line);
return(NULL);
}
h = (struct Header *)((unsigned char *)ptr - sizeof(struct Header) - FORTIFY_BEFORE_SIZE);
stdOutput = 0;
if(st_Disabled)
{
FORTIFY_LOCK();
new_ptr = (void *) realloc(ptr, new_size);
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(new_ptr);
}
if(!ptr)
{
void *FORTIFY_STORAGE ret = _Fortify_malloc(new_size, file, line);
WaitIfstdOutput();
return(ret);
}
FORTIFY_LOCK();
if(!IsOnList(h))
{
sprintf(st_Buffer,
"\nFortify: %s.%ld\n Invalid pointer or corrupted header passed to realloc\n",
file, line);
st_Output(st_Buffer);
goto fail;
}
if(!CheckBlock(h, file, line))
goto fail;
new_ptr = _Fortify_malloc(new_size, file, line);
if(!new_ptr)
{
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(0);
}
if(h->Size < new_size)
memcpy(new_ptr, ptr, h->Size);
else
memcpy(new_ptr, ptr, new_size);
_Fortify_free(ptr, file, line);
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(new_ptr);
fail:
sprintf(st_Buffer, " realloc(%s, %ld) failed\n", address(ptr), (unsigned long)new_size);
st_Output(st_Buffer);
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(NULL);
}
/*
* __Fortify_CheckPointer() - Returns true if the uptr points to a valid
* piece of _Fortify_malloc()'d memory. The memory must be on the malloc'd
* list, and it's sentinals must be in tact.
* If anything is wrong, an error message is issued.
*
* (Note - if fortify is disabled, this function always returns true).
*/
static int FORTIFY_STORAGE
__Fortify_CheckPointer(void *uptr,char OnlyStart,unsigned long size,char *file,unsigned long line)
{
unsigned char *ptr = (unsigned char *)uptr - sizeof(struct Header) - FORTIFY_BEFORE_SIZE;
int r = 1, StartPointer;
stdOutput = 0;
if(st_Disabled)
{
WaitIfstdOutput();
return(1);
}
FORTIFY_LOCK();
StartPointer = IsOnList((struct Header *)ptr);
if((OnlyStart) && (!StartPointer))
{
sprintf(st_Buffer,
"\nFortify: %s.%ld\n Invalid pointer or corrupted header detected (%s)\n",
file, line, address(uptr));
st_Output(st_Buffer);
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(0);
}
if((OnlyStart) || (StartPointer))
r = CheckBlock((struct Header *)ptr, file, line);
if(!OnlyStart)
r = CheckPointer((unsigned char *)uptr, size, file, line);
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(r);
}
int FORTIFY_STORAGE
_Fortify_CheckPointer(void *uptr,char *file,unsigned long line)
{
return(__Fortify_CheckPointer(uptr,1,0,file,line));
}
/*
* Fortify_SetOutputFunc(OutputFuncPtr Output) - Sets the function used to
* output all error and diagnostic messages by fortify. The output function
* takes a single unsigned char * argument, and must be able to handle newlines.
* The function returns the old pointer.
*/
Fortify_OutputFuncPtr FORTIFY_STORAGE
_Fortify_SetOutputFunc(Fortify_OutputFuncPtr Output)
{
OutputFuncPtr Old = st_Output;
st_Output = (OutputFuncPtr) Output;
return((Fortify_OutputFuncPtr FORTIFY_STORAGE) Old);
}
/*
* _Fortify_SetMallocFailRate(int Percent) - _Fortify_malloc() will make the
* malloc attempt fail this Percent of the time, even if the memory is
* available. Useful to "stress-test" an application. Returns the old
* value. The fail rate defaults to 0.
*/
int FORTIFY_STORAGE
_Fortify_SetMallocFailRate(int Percent)
{
int Old = st_MallocFailRate;
st_MallocFailRate = Percent;
return(Old);
}
/*
* _Fortify_CheckAllMemory() - Checks the sentinals of all malloc'd memory.
* Returns the number of blocks that failed.
*
* (If Fortify is disabled, this function always returns 0).
*/
int FORTIFY_STORAGE
_Fortify_CheckAllMemory(char *file,unsigned long line)
{
struct Header *curr = st_Head;
int count = 0;
stdOutput = 0;
if(st_Disabled)
{
WaitIfstdOutput();
return(0);
}
FORTIFY_LOCK();
while(curr)
{
if(!CheckBlock(curr, file, line))
count++;
curr = curr->Next;
}
if(file)
{
st_LastVerifiedFile = file;
st_LastVerifiedLine = (short) line;
}
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(count);
}
/* _Fortify_EnterScope - enters a new Fortify scope level.
* returns the new scope level.
*/
int FORTIFY_STORAGE
_Fortify_EnterScope(char *file,unsigned long line)
{
return((int) ++st_Scope);
}
/* _Fortify_LeaveScope - leaves a Fortify scope level,
* also prints a memory dump of all non-freed memory that was allocated
* during the scope being exited.
*/
int FORTIFY_STORAGE
_Fortify_LeaveScope(char *file,unsigned long line)
{
struct Header *curr = st_Head;
int count = 0;
size_t size = 0;
stdOutput = 0;
if(st_Disabled)
{
WaitIfstdOutput();
return(0);
}
FORTIFY_LOCK();
st_Scope--;
while(curr)
{
if(curr->Scope > st_Scope)
{
if(count == 0)
{
sprintf(st_Buffer, "\nFortify: Memory Dump at %s.%ld\n", file, line);
st_Output(st_Buffer);
OutputLastVerifiedPoint();
sprintf(st_Buffer, "%11s %8s %s\n", "Address", "Size", "Allocator");
st_Output(st_Buffer);
}
OutputHeader(curr);
count++;
size += curr->Size;
}
curr = curr->Next;
}
if(count)
{
sprintf(st_Buffer, "%11s %8ld bytes overhead\n", "and",
(unsigned long)(count * (sizeof(struct Header) + FORTIFY_BEFORE_SIZE + FORTIFY_AFTER_SIZE)));
st_Output(st_Buffer);
sprintf(st_Buffer,"%11s %8ld bytes in %d blocks\n", "total", size, count);
st_Output(st_Buffer);
}
FORTIFY_UNLOCK();
WaitIfstdOutput();
return(count);
}
/*
* _Fortify_OutputAllMemory() - Outputs the entire list of currently
* malloc'd memory. For each malloc'd block is output it's Address,
* Size, and the SourceFile and Line that allocated it.
*
* If there is no memory on the list, this function outputs nothing.
*
* It returns the number of blocks on the list, unless fortify has been
* disabled, in which case it always returns 0.
*/
int FORTIFY_STORAGE
_Fortify_OutputAllMemory(char *file,unsigned long line)
{
struct Header *curr = st_Head;
int count = 0;
size_t size = 0;
stdOutput = 0;
if(st_Disabled)
{
WaitIfstdOutput();
return(0);