⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fortify.c

📁 利用c语言编写
💻 C
📖 第 1 页 / 共 3 页
字号:
		if(curr->Scope >= 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);			OutputMemory(curr);			st_Output("\n");			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_Disable() - This function provides a mechanism to disable Fortify *   without recompiling all the sourcecode. *   If 'how' is zero then it can only be called when there is no memory on the *   Fortify malloc'd list. (Ideally, at the start of the program before any *   memory has been allocated). If you call this function when there IS *   memory on the Fortify malloc'd list, it will issue an error, and fortify *   will not be disabled. *   If 'how' is nonzero then output will only be disabled. This can always be *   done. */int FORTIFY_STORAGE_Fortify_Disable(file,line,how) char *file; unsigned long line; int how;{	int result;        if (how == 0)        {    	    stdOutput = 0;    	    FORTIFY_LOCK();    	    if(st_Head)    	    {    		    sprintf(st_Buffer, "Fortify: %s.%d\n", file, line);    		    st_Output(st_Buffer);    		    st_Output("         Fortify_Disable failed\n");    		    st_Output("         (because there is memory on the Fortify memory list)\n");    		    _Fortify_OutputAllMemory(file, line);    		    result = 0;    	    }    	    else    	    {    		    st_Disabled = 1;    		    result = 1;    	    }    	    FORTIFY_UNLOCK();            WaitIfstdOutput();        }        else        {            _Fortify_SetOutputFunc((Fortify_OutputFuncPtr) _Fortify_NoOutput);            result = 1;        }	return(result);}/* * Check a block's header and fortifications. */static int CheckBlock(h,file,line) struct Header *h; char *file; unsigned long line;{	unsigned char *ptr = (unsigned char *)h;	int result = 1;        stdOutput = 0;	if(!IsHeaderValid(h))	{		sprintf(st_Buffer,				"\nFortify: %s.%ld\n         Invalid pointer or corrupted header detected (%s)\n",				file, line, address(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE));		st_Output(st_Buffer);		OutputLastVerifiedPoint();                WaitIfstdOutput();                return(0);	}	if(!CheckFortification(ptr + sizeof(struct Header),		                   FORTIFY_BEFORE_VALUE, FORTIFY_BEFORE_SIZE))	{		sprintf(st_Buffer,		        "\nFortify: %s.%ld\n         Memory overrun detected before block\n",		        file, line);		st_Output(st_Buffer);		sprintf(st_Buffer,"         (%s,%ld,%s.%u)\n",			address(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE),		       (unsigned long)h->Size, h->File, h->Line);		st_Output(st_Buffer);		OutputFortification(ptr + sizeof(struct Header),		                    FORTIFY_BEFORE_VALUE, FORTIFY_BEFORE_SIZE);		OutputLastVerifiedPoint();		result = 0;	}	if(!CheckFortification(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE + h->Size,	                       FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE))	{		sprintf(st_Buffer, "\nFortify: %s.%ld\n         Memory overrun detected after block\n",		                   file, line);		st_Output(st_Buffer);		sprintf(st_Buffer,"         (%s,%ld,%s.%u)\n",				  address(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE),				 (unsigned long)h->Size, h->File, h->Line);		st_Output(st_Buffer);		OutputFortification(ptr + sizeof(struct Header) + FORTIFY_BEFORE_SIZE + h->Size,		                    FORTIFY_AFTER_VALUE, FORTIFY_AFTER_SIZE);		OutputLastVerifiedPoint();		result = 0;	}        WaitIfstdOutput();	return(result);}/* * Checks if the _size_ bytes from _ptr_ are all set to _value_ */static int CheckFortification(ptr,value,size) unsigned char *ptr; unsigned char value; size_t size;{	while(size--)		if(*ptr++ != value)			return(0);	return(1);}/* * Set the _size_ bytes from _ptr_ to _value_. */static void SetFortification(ptr,value,size) unsigned char *ptr; unsigned char value; size_t size;{	memset(ptr, value, size);}/* * Output the corrupted section of the fortification *//* Output the corrupted section of the fortification */static voidOutputFortification(ptr,value,size) unsigned char *ptr; unsigned char value; size_t size;{	unsigned long offset, column;	char	ascii[17];	st_Output("Address     Offset Data");	offset = 0;	column = 0;	while(offset < size)	{		if(column == 0)		{			sprintf(st_Buffer, "\n%8s %8d ", address(ptr), offset);			st_Output(st_Buffer);		}		sprintf(st_Buffer, "%02x ", *ptr);		st_Output(st_Buffer);		ascii[ (int) column ] = isprint( *ptr ) ? (char)(*ptr) : (char)(' ');		ascii[ (int) (column + 1) ] = '\0';		ptr++;		offset++;		column++;		if(column == 16)		{			st_Output( "   \"" );			st_Output( ascii );			st_Output( "\"" );			column = 0;		}	}	if ( column != 0 )	{		while ( column ++ < 16 )		{			st_Output( "   " );		}		st_Output( "   \"" );		st_Output( ascii );		st_Output( "\"" );	}	st_Output("\n");}/* * Returns true if the supplied pointer does indeed point to a real Header */static int IsHeaderValid(h) struct Header *h;{	return(!ChecksumHeader(h));}/* * Updates the checksum to make the header valid */static void MakeHeaderValid(h) struct Header *h;{	h->Checksum = 0;	h->Checksum = -ChecksumHeader(h);}/* * Calculate (and return) the checksum of the header. (Including the Checksum * variable itself. If all is well, the checksum returned by this function should * be 0. */static int ChecksumHeader(h) struct Header *h;{	register int c, checksum, *p;	for(c = 0, checksum = 0, p = (int *)h; c < sizeof(struct Header)/sizeof(int); c++)		checksum += *p++;	return(checksum);}/* * Examines the malloc'd list to see if the given header is on it. */static int IsOnList(h) struct Header *h;{	struct Header *curr;	curr = st_Head;	while(curr)	{		if(curr == h)			return(1);		curr = curr->Next;	}	return(0);}/* * Hex and ascii dump the memory */static voidOutputMemory(h) struct Header *h;{	OutputFortification((unsigned char*)h + sizeof(struct Header) + FORTIFY_BEFORE_SIZE,						0, h->Size);}/* * Output the header... */static void OutputHeader(h) struct Header *h;{	sprintf(st_Buffer, "%11s %8ld %s.%u (%d)\n",			   address((unsigned char*)h + sizeof(struct Header) + FORTIFY_BEFORE_SIZE),			   (unsigned long)h->Size,			   h->File, h->Line, (int) h->Scope);	st_Output(st_Buffer);}static void OutputLastVerifiedPoint(){	sprintf(st_Buffer, "\nLast Verified point: %s.%u\n",			   st_LastVerifiedFile,			   st_LastVerifiedLine);	st_Output(st_Buffer);}#else  /* FORTIFY_TRANSPARENT */void *FORTIFY_STORAGE_Fortify_malloc(size,file,line) size_t size; char *file; unsigned long line;{	return(malloc(size));}void FORTIFY_STORAGE_Fortify_free(uptr,file,line) void *uptr; char *file; unsigned long line;{	free(uptr);}void *FORTIFY_STORAGE_Fortify_realloc(ptr,new_size,file,line) void *ptr; size_t new_size; char *file; unsigned long line;{	return(realloc(ptr, new_size));}int FORTIFY_STORAGE_Fortify_CheckPointer(uptr,file,line) void *uptr; char *file; unsigned long line;{	return(1);}Fortify_OutputFuncPtr FORTIFY_STORAGE_Fortify_SetOutputFunc(Output) Fortify_OutputFuncPtr Output;{	return(0);}int FORTIFY_STORAGE_Fortify_SetMallocFailRate(Percent) int Percent;{	return(0);}int FORTIFY_STORAGE_Fortify_CheckAllMemory(file,line) char *file; unsigned long line;{	return(0);}int FORTIFY_STORAGE_Fortify_EnterScope(file,line) char *file; unsigned long line;{	return(0);}int FORTIFY_STORAGE_Fortify_LeaveScope(file,line) char *file; unsigned long line;{	return(0);}int FORTIFY_STORAGE_Fortify_OutputAllMemory(file,line) char *file; unsigned long line;{	return(0);}int FORTIFY_STORAGE_Fortify_DumpAllMemory(scope,file,line) int scope; char *file; unsigned long line;{	return(0);}int FORTIFY_STORAGE_Fortify_Disable(file,line) char *file; unsigned long line;{	return(1);}#endif /* !FORTIFY_TRANSPARENT *//* function that use _Fortify_malloc(), _Fortify_realloc(), _Fortify_free() *//* * Fortifty_calloc() - Uses _Fortify_malloc() to implement calloc(). Much *                     the same protection as _Fortify_malloc(). */void *FORTIFY_STORAGE_Fortify_calloc(nitems,size,file,line) size_t nitems; size_t size; char *file; unsigned long line;{	void *ptr;	ptr = _Fortify_malloc(nitems * size, file, line);	if(ptr)		memset(ptr, 0, nitems * size);	return(ptr);}/* * Fortifty_strdup() - Uses _Fortify_malloc() to implement strdup(). Much *                     the same protection as _Fortify_malloc(). * The library function is not used because it is not certain that getpwd * uses the library malloc function (if linked with an alternate library) * and if the memory is freed then strange things can happen */char *FORTIFY_STORAGE_Fortify_strdup(str,file,line) char *str; char *file; unsigned long line;{	char *ptr;	ptr = (char *) _Fortify_malloc(strlen(str) + 1, file, line);	if(ptr)		strcpy(ptr, str);	return(ptr);}/* * Fortifty_getpwd() - Uses _Fortify_malloc() to implement getpwd(). Much *                     the same protection as _Fortify_malloc(). * Memory is not allocated bu getcwd but by our routine for the same reason * as for strdup */char *FORTIFY_STORAGE_Fortify_getcwd(buf,size,file,line) char *buf; size_t size; char *file; unsigned long line;{	char *ptr;        if(buf!=NULL)                ptr = buf;	else        	ptr = (char *) _Fortify_malloc(size + 1, file, line);	if(ptr)		ptr = getcwd(ptr, size);	return(ptr);}/* * Fortifty_tempnam() - Uses _Fortify_strdup() to implement tempnam(). Much *                     the same protection as _Fortify_malloc(). */char *FORTIFY_STORAGE_Fortify_tempnam(dir,pfx,file,line) char *dir; char *pfx; char *file; unsigned long line;{	char *ptr1, *ptr2;        ptr1 = tempnam(dir,pfx);	if(ptr1)        {                ptr2=_Fortify_strdup(ptr1,file,line);                free(ptr1);                ptr1=ptr2;        }	return(ptr1);}#endif /* FORTIFY */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -