Merge pull request #2738 from bmiklautz/ffuncs

hardening: check fread and fwrite return values
This commit is contained in:
Norbert Federa 2015-07-01 13:02:32 +02:00
commit 1c43a6e115
15 changed files with 336 additions and 267 deletions

View File

@ -91,7 +91,7 @@ static void printer_cups_write_printjob(rdpPrintJob* printjob, BYTE* data, int s
if (fwrite(data, 1, size, fp) < size) if (fwrite(data, 1, size, fp) < size)
{ {
// FIXME once this function doesn't return void anymore!
} }
fclose(fp); fclose(fp);

View File

@ -800,17 +800,28 @@ BOOL freerdp_client_write_rdp_file(const rdpFile* file, const char* name, BOOL u
ConvertToUnicode(CP_UTF8, 0, buffer, length, &unicodestr, 0); ConvertToUnicode(CP_UTF8, 0, buffer, length, &unicodestr, 0);
/* Write multi-byte header */ /* Write multi-byte header */
fwrite(BOM_UTF16_LE, sizeof(BYTE), 2, fp); if (fwrite(BOM_UTF16_LE, sizeof(BYTE), 2, fp) != 2 ||
fwrite(unicodestr, 2, length, fp); fwrite(unicodestr, 2, length, fp) != length)
{
free(buffer);
free(unicodestr);
fclose(fp);
return FALSE;
}
free(unicodestr); free(unicodestr);
} }
else else
{ {
fwrite(buffer, 1, length, fp); if (fwrite(buffer, 1, length, fp) != length)
{
free(buffer);
fclose(fp);
return FALSE;
}
} }
status = fflush(fp); fflush(fp);
status = fclose(fp); status = fclose(fp);
} }

View File

@ -684,16 +684,20 @@ rdpRsaKey* key_new(const char* keyfile)
goto out_free; goto out_free;
} }
fseek(fp, 0, SEEK_END); if (fseek(fp, 0, SEEK_END) < 0)
length = ftell(fp); goto out_free;
fseek(fp, 0, SEEK_SET); if ((length = ftell(fp)) < 0)
goto out_free;
if (fseek(fp, 0, SEEK_SET) < 0)
goto out_free;
buffer = (BYTE*) malloc(length); buffer = (BYTE*) malloc(length);
if (!buffer) if (!buffer)
goto out_free; goto out_free;
fread((void*) buffer, length, 1, fp); if (fread((void*) buffer, length, 1, fp) != 1)
goto out_free;
fclose(fp); fclose(fp);
fp = NULL; fp = NULL;
@ -703,7 +707,6 @@ rdpRsaKey* key_new(const char* keyfile)
goto out_free; goto out_free;
rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL); rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
BIO_free(bio); BIO_free(bio);
free(buffer); free(buffer);
buffer = NULL; buffer = NULL;
@ -756,6 +759,7 @@ rdpRsaKey* key_new(const char* keyfile)
crypto_reverse(key->exponent, sizeof(key->exponent)); crypto_reverse(key->exponent, sizeof(key->exponent));
RSA_free(rsa); RSA_free(rsa);
return key; return key;
out_free_modulus: out_free_modulus:
free(key->Modulus); free(key->Modulus);
out_free_rsa: out_free_rsa:

View File

@ -46,8 +46,9 @@ static int prepare(const char* currentFileV2, const char* legacyFileV2, const ch
for (i=0; i<sizeof(hosts)/sizeof(hosts[0]); i++) for (i=0; i<sizeof(hosts)/sizeof(hosts[0]); i++)
{ {
fwrite(hosts[i], strlen(hosts[i]), sizeof(char), fl); if (fwrite(hosts[i], strlen(hosts[i]), 1, fl) != 1 ||
fwrite(hosts[i], strlen(hosts[i]), sizeof(char), fc); fwrite(hosts[i], strlen(hosts[i]), 1, fc) != 1)
goto finish;
} }
fclose(fc); fclose(fc);
@ -61,14 +62,19 @@ static int prepare(const char* currentFileV2, const char* legacyFileV2, const ch
goto finish; goto finish;
for (i=0; i<sizeof(legacy)/sizeof(legacy[0]); i++) for (i=0; i<sizeof(legacy)/sizeof(legacy[0]); i++)
fwrite(legacy[i], strlen(legacy[i]), sizeof(char), fl); {
if (fwrite(legacy[i], strlen(legacy[i]), 1, fl) != 1)
goto finish;
}
fclose(fl); fclose(fl);
return 0; return 0;
finish: finish:
fclose(fl); if (fl)
fclose(fc); fclose(fl);
if (fc)
fclose(fc);
return -1; return -1;
} }

View File

@ -1547,7 +1547,12 @@ char* freerdp_get_unix_timezone_identifier()
return NULL; return NULL;
} }
fread(tzid, length, 1, fp); if (fread(tzid, length, 1, fp) != 1)
{
free(tzid);
fclose(fp);
return NULL;
}
tzid[length] = '\0'; tzid[length] = '\0';
if (tzid[length - 1] == '\n') if (tzid[length - 1] == '\n')

View File

@ -52,27 +52,27 @@ int gettimeofday(struct timeval* tp, void* tz)
#define PCAP_MAGIC 0xA1B2C3D4 #define PCAP_MAGIC 0xA1B2C3D4
BOOL pcap_read_header(rdpPcap* pcap, pcap_header* header) static BOOL pcap_read_header(rdpPcap* pcap, pcap_header* header)
{ {
return fread((void*) header, sizeof(pcap_header), 1, pcap->fp) == 1; return fread((void*) header, sizeof(pcap_header), 1, pcap->fp) == 1;
} }
BOOL pcap_write_header(rdpPcap* pcap, pcap_header* header) static BOOL pcap_write_header(rdpPcap* pcap, pcap_header* header)
{ {
return fwrite((void*) header, sizeof(pcap_header), 1, pcap->fp) == 1; return fwrite((void*) header, sizeof(pcap_header), 1, pcap->fp) == 1;
} }
BOOL pcap_read_record_header(rdpPcap* pcap, pcap_record_header* record) static BOOL pcap_read_record_header(rdpPcap* pcap, pcap_record_header* record)
{ {
return fread((void*) record, sizeof(pcap_record_header), 1, pcap->fp) == 1; return fread((void*) record, sizeof(pcap_record_header), 1, pcap->fp) == 1;
} }
void pcap_write_record_header(rdpPcap* pcap, pcap_record_header* record) static BOOL pcap_write_record_header(rdpPcap* pcap, pcap_record_header* record)
{ {
fwrite((void*) record, sizeof(pcap_record_header), 1, pcap->fp); return fwrite((void*) record, sizeof(pcap_record_header), 1, pcap->fp) == 1;
} }
BOOL pcap_read_record(rdpPcap* pcap, pcap_record* record) static BOOL pcap_read_record(rdpPcap* pcap, pcap_record* record)
{ {
if (!pcap_read_record_header(pcap, &record->header)) if (!pcap_read_record_header(pcap, &record->header))
return FALSE; return FALSE;
@ -91,10 +91,10 @@ BOOL pcap_read_record(rdpPcap* pcap, pcap_record* record)
return TRUE; return TRUE;
} }
void pcap_write_record(rdpPcap* pcap, pcap_record* record) static BOOL pcap_write_record(rdpPcap* pcap, pcap_record* record)
{ {
pcap_write_record_header(pcap, &record->header); return pcap_write_record_header(pcap, &record->header) &&
fwrite(record->data, record->length, 1, pcap->fp); (fwrite(record->data, record->length, 1, pcap->fp) == 1);
} }
BOOL pcap_add_record(rdpPcap* pcap, void* data, UINT32 length) BOOL pcap_add_record(rdpPcap* pcap, void* data, UINT32 length)

View File

@ -764,21 +764,24 @@ int win_shadow_wds_init(winShadowSubsystem* subsystem)
WLog_INFO(TAG, "ConnectionString: %s", file->ConnectionString2); WLog_INFO(TAG, "ConnectionString: %s", file->ConnectionString2);
if (0) #if 0
FILE* fp;
size_t size;
fp = fopen("inv.xml", "w+b");
if (fp)
{ {
FILE* fp; size = strlen(file->ConnectionString2);
size_t size; if (fwrite(file->ConnectionString2, size, 1, fp) != 1 || fwrite("\r\n", 2, 1, fp) != 1)
fp = fopen("inv.xml", "w+b");
if (fp)
{ {
size = strlen(file->ConnectionString2);
fwrite(file->ConnectionString2, 1, size, fp);
fwrite("\r\n", 1, 2, fp);
fclose(fp); fclose(fp);
WLog_ERR(TAG, "Problem writing to inv.xml");
return -1;
} }
fclose(fp);
} }
#endif
status = win_shadow_rdp_init(subsystem); status = win_shadow_rdp_init(subsystem);

View File

@ -549,34 +549,42 @@ static void* schannel_test_server_thread(void* arg)
int dump_test_certificate_files() int dump_test_certificate_files()
{ {
FILE* fp; FILE* fp;
char* fullpath; char* fullpath = NULL;
int ret = -1;
/* /*
* Output Certificate File * Output Certificate File
*/ */
fullpath = GetCombinedPath("/tmp", "localhost.crt"); fullpath = GetCombinedPath("/tmp", "localhost.crt");
fp = fopen(fullpath, "w+"); if (!fullpath)
return -1;
fp = fopen(fullpath, "w+");
if (fp) if (fp)
{ {
fwrite((void*) test_localhost_crt, sizeof(test_localhost_crt), 1, fp); if (fwrite((void*) test_localhost_crt, sizeof(test_localhost_crt), 1, fp) != 1)
goto out_fail;
fclose(fp); fclose(fp);
fp = NULL;
} }
free(fullpath); free(fullpath);
/* /*
* Output Private Key File * Output Private Key File
*/ */
fullpath = GetCombinedPath("/tmp", "localhost.key"); fullpath = GetCombinedPath("/tmp", "localhost.key");
if (!fullpath)
return -1;
fp = fopen(fullpath, "w+"); fp = fopen(fullpath, "w+");
if (fp && fwrite((void*) test_localhost_key, sizeof(test_localhost_key), 1, fp) != 1)
goto out_fail;
if (fp) ret = 1;
{ out_fail:
fwrite((void*) test_localhost_key, sizeof(test_localhost_key), 1, fp);
fclose(fp);
}
free(fullpath); free(fullpath);
return 1; if (fp)
fclose(fp);
return ret;
} }
int TestSchannel(int argc, char* argv[]) int TestSchannel(int argc, char* argv[])

View File

@ -39,6 +39,7 @@ int winpr_bitmap_write(const char* filename, BYTE* data, int width, int height,
FILE* fp; FILE* fp;
WINPR_BITMAP_FILE_HEADER bf; WINPR_BITMAP_FILE_HEADER bf;
WINPR_BITMAP_INFO_HEADER bi; WINPR_BITMAP_INFO_HEADER bi;
int ret = 1;
fp = fopen(filename, "w+b"); fp = fopen(filename, "w+b");
@ -67,13 +68,14 @@ int winpr_bitmap_write(const char* filename, BYTE* data, int width, int height,
bi.biClrImportant = 0; bi.biClrImportant = 0;
bi.biSize = sizeof(WINPR_BITMAP_INFO_HEADER); bi.biSize = sizeof(WINPR_BITMAP_INFO_HEADER);
fwrite((void*) &bf, sizeof(WINPR_BITMAP_FILE_HEADER), 1, fp); if (fwrite((void*) &bf, sizeof(WINPR_BITMAP_FILE_HEADER), 1, fp) != 1 ||
fwrite((void*) &bi, sizeof(WINPR_BITMAP_INFO_HEADER), 1, fp); fwrite((void*) &bi, sizeof(WINPR_BITMAP_INFO_HEADER), 1, fp) != 1 ||
fwrite((void*) data, bi.biSizeImage, 1, fp); fwrite((void*) data, bi.biSizeImage, 1, fp) != 1)
ret = -1;
fclose(fp); fclose(fp);
return 1; return ret;
} }
int winpr_image_write(wImage* image, const char* filename) int winpr_image_write(wImage* image, const char* filename)
@ -113,9 +115,8 @@ int winpr_image_png_read_fp(wImage* image, FILE* fp)
if (!data) if (!data)
return -1; return -1;
fread((void*) data, size, 1, fp); if (fread((void*) data, size, 1, fp) != 1)
return -1;
fclose(fp);
lodepng_status = lodepng_decode32(&(image->data), &width, &height, data, size); lodepng_status = lodepng_decode32(&(image->data), &width, &height, data, size);
@ -163,14 +164,16 @@ int winpr_image_bitmap_read_fp(wImage* image, FILE* fp)
WINPR_BITMAP_FILE_HEADER bf; WINPR_BITMAP_FILE_HEADER bf;
WINPR_BITMAP_INFO_HEADER bi; WINPR_BITMAP_INFO_HEADER bi;
fread((void*) &bf, sizeof(WINPR_BITMAP_FILE_HEADER), 1, fp); if (fread((void*) &bf, sizeof(WINPR_BITMAP_FILE_HEADER), 1, fp) != 1)
return -1;
if ((bf.bfType[0] != 'B') || (bf.bfType[1] != 'M')) if ((bf.bfType[0] != 'B') || (bf.bfType[1] != 'M'))
return -1; return -1;
image->type = WINPR_IMAGE_BITMAP; image->type = WINPR_IMAGE_BITMAP;
fread((void*) &bi, sizeof(WINPR_BITMAP_INFO_HEADER), 1, fp); if (fread((void*) &bi, sizeof(WINPR_BITMAP_INFO_HEADER), 1, fp) != 1)
return -1;
if (ftell(fp) != bf.bfOffBits) if (ftell(fp) != bf.bfOffBits)
{ {
@ -201,7 +204,12 @@ int winpr_image_bitmap_read_fp(wImage* image, FILE* fp)
if (!vFlip) if (!vFlip)
{ {
fread((void*) image->data, bi.biSizeImage, 1, fp); if (fread((void*) image->data, bi.biSizeImage, 1, fp) != 1)
{
free(image->data);
image->data = NULL;
return -1;
}
} }
else else
{ {
@ -209,13 +217,16 @@ int winpr_image_bitmap_read_fp(wImage* image, FILE* fp)
for (index = 0; index < image->height; index++) for (index = 0; index < image->height; index++)
{ {
fread((void*) pDstData, image->scanline, 1, fp); if (fread((void*) pDstData, image->scanline, 1, fp) != 1)
{
free(image->data);
image->data = NULL;
return -1;
}
pDstData -= image->scanline; pDstData -= image->scanline;
} }
} }
fclose(fp);
return 1; return 1;
} }
@ -302,8 +313,11 @@ int winpr_image_read(wImage* image, const char* filename)
return -1; return -1;
} }
fread((void*) &sig, sizeof(sig), 1, fp); if (fread((void*) &sig, sizeof(sig), 1, fp) != 1 || fseek(fp, 0, SEEK_SET) < 0)
fseek(fp, 0, SEEK_SET); {
fclose(fp);
return -1;
}
if ((sig[0] == 'B') && (sig[1] == 'M')) if ((sig[0] == 'B') && (sig[1] == 'M'))
{ {
@ -316,10 +330,7 @@ int winpr_image_read(wImage* image, const char* filename)
image->type = WINPR_IMAGE_PNG; image->type = WINPR_IMAGE_PNG;
status = winpr_image_png_read_fp(image, fp); status = winpr_image_png_read_fp(image, fp);
} }
else fclose(fp);
{
fclose(fp);
}
return status; return status;
} }

View File

@ -77,38 +77,28 @@ int IniFile_Load_File(wIniFile* ini, const char* filename)
if (IniFile_Open_File(ini, filename) < 0) if (IniFile_Open_File(ini, filename) < 0)
return -1; return -1;
fseek(ini->fp, 0, SEEK_END); if (fseek(ini->fp, 0, SEEK_END) < 0)
goto out_file;
fileSize = ftell(ini->fp); fileSize = ftell(ini->fp);
fseek(ini->fp, 0, SEEK_SET); if (fileSize < 0)
goto out_file;
if (fseek(ini->fp, 0, SEEK_SET) < 0)
goto out_file;
ini->line = NULL; ini->line = NULL;
ini->nextLine = NULL; ini->nextLine = NULL;
ini->buffer = NULL; ini->buffer = NULL;
if (fileSize < 1) if (fileSize < 1)
{ goto out_file;
fclose(ini->fp);
ini->fp = NULL;
return -1;
}
ini->buffer = (char*) malloc(fileSize + 2); ini->buffer = (char*) malloc(fileSize + 2);
if (!ini->buffer) if (!ini->buffer)
{ goto out_file;
fclose(ini->fp);
ini->fp = NULL;
return -1;
}
if (fread(ini->buffer, fileSize, 1, ini->fp) != 1) if (fread(ini->buffer, fileSize, 1, ini->fp) != 1)
{ goto out_buffer;
free(ini->buffer);
fclose(ini->fp);
ini->buffer = NULL;
ini->fp = NULL;
return -1;
}
fclose(ini->fp); fclose(ini->fp);
ini->fp = NULL; ini->fp = NULL;
@ -119,6 +109,14 @@ int IniFile_Load_File(wIniFile* ini, const char* filename)
ini->nextLine = strtok(ini->buffer, "\n"); ini->nextLine = strtok(ini->buffer, "\n");
return 1; return 1;
out_buffer:
free(ini->buffer);
ini->buffer = NULL;
out_file:
fclose(ini->fp);
ini->fp = NULL;
return -1;
} }
void IniFile_Load_Finish(wIniFile* ini) void IniFile_Load_Finish(wIniFile* ini)
@ -681,6 +679,7 @@ int IniFile_WriteFile(wIniFile* ini, const char* filename)
{ {
int length; int length;
char* buffer; char* buffer;
int ret = 1;
buffer = IniFile_WriteBuffer(ini); buffer = IniFile_WriteBuffer(ini);
@ -700,13 +699,14 @@ int IniFile_WriteFile(wIniFile* ini, const char* filename)
return -1; return -1;
} }
fwrite((void*) buffer, length, 1, ini->fp); if (fwrite((void*) buffer, length, 1, ini->fp) != 1)
ret = -1;
fclose(ini->fp); fclose(ini->fp);
free(buffer); free(buffer);
return 1; return ret;
} }
wIniFile* IniFile_New() wIniFile* IniFile_New()

View File

@ -362,6 +362,7 @@ unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* fil
if(size && (*out)) (*outsize) = fread(*out, 1, (size_t)size, file); if(size && (*out)) (*outsize) = fread(*out, 1, (size_t)size, file);
fclose(file); fclose(file);
if (*outsize != size) return 91;
if(!(*out) && size) return 83; /*the above malloc failed*/ if(!(*out) && size) return 83; /*the above malloc failed*/
return 0; return 0;
} }
@ -370,11 +371,13 @@ unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* fil
unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename)
{ {
FILE* file; FILE* file;
int ret = 0;
file = fopen(filename, "wb" ); file = fopen(filename, "wb" );
if(!file) return 79; if(!file) return 79;
fwrite((char*)buffer , 1 , buffersize, file); if (fwrite((char*)buffer , 1 , buffersize, file) != buffersize)
ret = 91;
fclose(file); fclose(file);
return 0; return ret;
} }
#endif /*LODEPNG_COMPILE_DISK*/ #endif /*LODEPNG_COMPILE_DISK*/
@ -5894,6 +5897,7 @@ const char* lodepng_error_text(unsigned code)
case 89: return "text chunk keyword too short or long: must have size 1-79"; case 89: return "text chunk keyword too short or long: must have size 1-79";
/*the windowsize in the LodePNGCompressSettings. Requiring POT(==> & instead of %) makes encoding 12% faster.*/ /*the windowsize in the LodePNGCompressSettings. Requiring POT(==> & instead of %) makes encoding 12% faster.*/
case 90: return "windowsize must be a power of two"; case 90: return "windowsize must be a power of two";
case 91: return "fwrite failed";
} }
return "unknown error code"; return "unknown error code";
} }

View File

@ -131,6 +131,7 @@ int WLog_BinaryAppender_WriteMessage(wLog* log, wLogBinaryAppender* appender, wL
int FileNameLength; int FileNameLength;
int FunctionNameLength; int FunctionNameLength;
int TextStringLength; int TextStringLength;
int ret = 1;
if (!log || !appender || !message) if (!log || !appender || !message)
return -1; return -1;
@ -171,11 +172,12 @@ int WLog_BinaryAppender_WriteMessage(wLog* log, wLogBinaryAppender* appender, wL
Stream_SealLength(s); Stream_SealLength(s);
fwrite(Stream_Buffer(s), MessageLength, 1, fp); if (fwrite(Stream_Buffer(s), MessageLength, 1, fp) != 1)
ret = -1;
Stream_Free(s, TRUE); Stream_Free(s, TRUE);
return 1; return ret;
} }
int WLog_BinaryAppender_WriteDataMessage(wLog* log, wLogBinaryAppender* appender, wLogMessage* message) int WLog_BinaryAppender_WriteDataMessage(wLog* log, wLogBinaryAppender* appender, wLogMessage* message)

View File

@ -32,6 +32,7 @@ int WLog_DataMessage_Write(char* filename, void* data, int length)
{ {
FILE* fp; FILE* fp;
fp = fopen(filename, "w+b"); fp = fopen(filename, "w+b");
int ret = 0;
if (!fp) if (!fp)
{ {
@ -39,7 +40,8 @@ int WLog_DataMessage_Write(char* filename, void* data, int length)
return -1; return -1;
} }
fwrite(data, length, 1, fp); if (fwrite(data, length, 1, fp) != 1)
ret = -1;
fclose(fp); fclose(fp);
return 0; return ret;
} }

View File

@ -50,41 +50,28 @@ static int gettimeofday(struct timeval* tp, void* tz)
} }
#endif #endif
void Pcap_Read_Header(wPcap* pcap, wPcapHeader* header) static BOOL Pcap_Read_Header(wPcap* pcap, wPcapHeader* header)
{ {
if (pcap && pcap->fp) if (pcap && pcap->fp && fread((void*) header, sizeof(wPcapHeader), 1, pcap->fp) == 1)
fread((void*) header, sizeof(wPcapHeader), 1, pcap->fp); return TRUE;
return FALSE;
} }
void Pcap_Write_Header(wPcap* pcap, wPcapHeader* header) /* currently unused code */
# if 0
static BOOL Pcap_Read_RecordHeader(wPcap* pcap, wPcapRecordHeader* record)
{ {
if (pcap && pcap->fp) if (pcap && pcap->fp && (fread((void*) record, sizeof(wPcapRecordHeader), 1, pcap->fp) == 1))
fwrite((void*) header, sizeof(wPcapHeader), 1, pcap->fp); return TRUE;
return FALSE;
} }
void Pcap_Read_RecordHeader(wPcap* pcap, wPcapRecordHeader* record) static BOOL Pcap_Read_Record(wPcap* pcap, wPcapRecord* record)
{
if (pcap && pcap->fp)
fread((void*) record, sizeof(wPcapRecordHeader), 1, pcap->fp);
}
void Pcap_Write_RecordHeader(wPcap* pcap, wPcapRecordHeader* record)
{
if (pcap && pcap->fp)
fwrite((void*) record, sizeof(wPcapRecordHeader), 1, pcap->fp);
}
void Pcap_Write_RecordContent(wPcap* pcap, wPcapRecord* record)
{
if (pcap && pcap->fp)
fwrite(record->data, record->length, 1, pcap->fp);
}
BOOL Pcap_Read_Record(wPcap* pcap, wPcapRecord* record)
{ {
if (pcap && pcap->fp) if (pcap && pcap->fp)
{ {
Pcap_Read_RecordHeader(pcap, &record->header); if (!Pcap_Read_RecordHeader(pcap, &record->header))
return FALSE;
record->length = record->header.incl_len; record->length = record->header.incl_len;
record->data = malloc(record->length); record->data = malloc(record->length);
if (!record->data) if (!record->data)
@ -100,13 +87,7 @@ BOOL Pcap_Read_Record(wPcap* pcap, wPcapRecord* record)
return TRUE; return TRUE;
} }
void Pcap_Write_Record(wPcap* pcap, wPcapRecord* record) static BOOL Pcap_Add_Record(wPcap* pcap, void* data, UINT32 length)
{
Pcap_Write_RecordHeader(pcap, &record->header);
Pcap_Write_RecordContent(pcap, record);
}
void Pcap_Add_Record(wPcap* pcap, void* data, UINT32 length)
{ {
wPcapRecord* record; wPcapRecord* record;
struct timeval tp; struct timeval tp;
@ -115,7 +96,7 @@ void Pcap_Add_Record(wPcap* pcap, void* data, UINT32 length)
{ {
pcap->tail = (wPcapRecord*) calloc(1, sizeof(wPcapRecord)); pcap->tail = (wPcapRecord*) calloc(1, sizeof(wPcapRecord));
if (!pcap->tail) if (!pcap->tail)
return; return FALSE;
pcap->head = pcap->tail; pcap->head = pcap->tail;
pcap->record = pcap->head; pcap->record = pcap->head;
record = pcap->tail; record = pcap->tail;
@ -124,7 +105,7 @@ void Pcap_Add_Record(wPcap* pcap, void* data, UINT32 length)
{ {
record = (wPcapRecord*) calloc(1, sizeof(wPcapRecord)); record = (wPcapRecord*) calloc(1, sizeof(wPcapRecord));
if (!record) if (!record)
return; return FALSE;
pcap->tail->next = record; pcap->tail->next = record;
pcap->tail = record; pcap->tail = record;
} }
@ -139,9 +120,10 @@ void Pcap_Add_Record(wPcap* pcap, void* data, UINT32 length)
gettimeofday(&tp, 0); gettimeofday(&tp, 0);
record->header.ts_sec = tp.tv_sec; record->header.ts_sec = tp.tv_sec;
record->header.ts_usec = tp.tv_usec; record->header.ts_usec = tp.tv_usec;
return TRUE;
} }
BOOL Pcap_HasNext_Record(wPcap* pcap) static BOOL Pcap_HasNext_Record(wPcap* pcap)
{ {
if (pcap->file_size - (ftell(pcap->fp)) <= 16) if (pcap->file_size - (ftell(pcap->fp)) <= 16)
return FALSE; return FALSE;
@ -149,34 +131,59 @@ BOOL Pcap_HasNext_Record(wPcap* pcap)
return TRUE; return TRUE;
} }
BOOL Pcap_GetNext_RecordHeader(wPcap* pcap, wPcapRecord* record) static BOOL Pcap_GetNext_RecordHeader(wPcap* pcap, wPcapRecord* record)
{ {
if (Pcap_HasNext_Record(pcap) != TRUE) if (!Pcap_HasNext_Record(pcap) || !Pcap_Read_RecordHeader(pcap, &record->header))
return FALSE; return FALSE;
Pcap_Read_RecordHeader(pcap, &record->header);
record->length = record->header.incl_len; record->length = record->header.incl_len;
return TRUE; return TRUE;
} }
BOOL Pcap_GetNext_RecordContent(wPcap* pcap, wPcapRecord* record) static BOOL Pcap_GetNext_RecordContent(wPcap* pcap, wPcapRecord* record)
{ {
if (pcap && pcap->fp) if (pcap && pcap->fp && fread(record->data, record->length, 1, pcap->fp) == 1)
{
fread(record->data, record->length, 1, pcap->fp);
return TRUE; return TRUE;
}
return FALSE; return FALSE;
} }
BOOL Pcap_GetNext_Record(wPcap* pcap, wPcapRecord* record) static BOOL Pcap_GetNext_Record(wPcap* pcap, wPcapRecord* record)
{ {
if (Pcap_HasNext_Record(pcap) != TRUE) if (!Pcap_HasNext_Record(pcap))
return FALSE; return FALSE;
return Pcap_Read_Record(pcap, record); return Pcap_Read_Record(pcap, record);
} }
#endif
static BOOL Pcap_Write_Header(wPcap* pcap, wPcapHeader* header)
{
if (pcap && pcap->fp && fwrite((void*) header, sizeof(wPcapHeader), 1, pcap->fp) == 1)
return TRUE;
return FALSE;
}
static BOOL Pcap_Write_RecordHeader(wPcap* pcap, wPcapRecordHeader* record)
{
if (pcap && pcap->fp && fwrite((void *) record, sizeof(wPcapRecordHeader), 1, pcap->fp) == 1)
return TRUE;
return FALSE;
}
static BOOL Pcap_Write_RecordContent(wPcap* pcap, wPcapRecord* record)
{
if (pcap && pcap->fp && fwrite(record->data, record->length, 1, pcap->fp) == 1)
return TRUE;
return FALSE;
}
static BOOL Pcap_Write_Record(wPcap* pcap, wPcapRecord* record)
{
return Pcap_Write_RecordHeader(pcap, &record->header) &&
Pcap_Write_RecordContent(pcap, record);
}
wPcap* Pcap_Open(char* name, BOOL write) wPcap* Pcap_Open(char* name, BOOL write)
{ {
@ -191,33 +198,45 @@ wPcap* Pcap_Open(char* name, BOOL write)
pcap = (wPcap*) calloc(1, sizeof(wPcap)); pcap = (wPcap*) calloc(1, sizeof(wPcap));
if (pcap) if (!pcap)
{ return NULL;
pcap->name = name;
pcap->write = write;
pcap->record_count = 0;
pcap->fp = pcap_fp;
if (write) pcap->name = name;
{ pcap->write = write;
pcap->header.magic_number = PCAP_MAGIC_NUMBER; pcap->record_count = 0;
pcap->header.version_major = 2; pcap->fp = pcap_fp;
pcap->header.version_minor = 4;
pcap->header.thiszone = 0; if (write)
pcap->header.sigfigs = 0; {
pcap->header.snaplen = 0xFFFFFFFF; pcap->header.magic_number = PCAP_MAGIC_NUMBER;
pcap->header.network = 1; /* ethernet */ pcap->header.version_major = 2;
Pcap_Write_Header(pcap, &pcap->header); pcap->header.version_minor = 4;
} pcap->header.thiszone = 0;
else pcap->header.sigfigs = 0;
{ pcap->header.snaplen = 0xFFFFFFFF;
fseek(pcap->fp, 0, SEEK_END); pcap->header.network = 1; /* ethernet */
pcap->file_size = (int) ftell(pcap->fp); if (!Pcap_Write_Header(pcap, &pcap->header))
fseek(pcap->fp, 0, SEEK_SET); goto out_fail;
Pcap_Read_Header(pcap, &pcap->header);
}
} }
else
{
if (fseek(pcap->fp, 0, SEEK_END) < 0)
goto out_fail;
pcap->file_size = (int) ftell(pcap->fp);
if (pcap->file_size < 0)
goto out_fail;
if (fseek(pcap->fp, 0, SEEK_SET) < 0)
goto out_fail;
if (!Pcap_Read_Header(pcap, &pcap->header))
goto out_fail;
}
return pcap; return pcap;
out_fail:
fclose(pcap_fp);
free(pcap);
return NULL;
} }
void Pcap_Flush(wPcap* pcap) void Pcap_Flush(wPcap* pcap)
@ -227,11 +246,13 @@ void Pcap_Flush(wPcap* pcap)
while (pcap->record) while (pcap->record)
{ {
Pcap_Write_Record(pcap, pcap->record); if (!Pcap_Write_Record(pcap, pcap->record))
return;
pcap->record = pcap->record->next; pcap->record = pcap->record->next;
} }
fflush(pcap->fp); fflush(pcap->fp);
return;
} }
void Pcap_Close(wPcap* pcap) void Pcap_Close(wPcap* pcap)
@ -244,26 +265,28 @@ void Pcap_Close(wPcap* pcap)
free(pcap); free(pcap);
} }
int WLog_PacketMessage_Write_EthernetHeader(wPcap* pcap, wEthernetHeader* ethernet) static BOOL WLog_PacketMessage_Write_EthernetHeader(wPcap* pcap, wEthernetHeader* ethernet)
{ {
wStream* s; wStream* s;
BYTE buffer[14]; BYTE buffer[14];
BOOL ret = TRUE;
if (!pcap || !pcap->fp || !ethernet) if (!pcap || !pcap->fp || !ethernet)
return -1; return FALSE;
s = Stream_New(buffer, 14); s = Stream_New(buffer, 14);
if (!s) if (!s)
return -1; return FALSE;
Stream_Write(s, ethernet->Destination, 6); Stream_Write(s, ethernet->Destination, 6);
Stream_Write(s, ethernet->Source, 6); Stream_Write(s, ethernet->Source, 6);
Stream_Write_UINT16_BE(s, ethernet->Type); Stream_Write_UINT16_BE(s, ethernet->Type);
fwrite(buffer, 14, 1, pcap->fp); if (fwrite(buffer, 14, 1, pcap->fp) != 1)
ret = FALSE;
Stream_Free(s, FALSE); Stream_Free(s, FALSE);
return 0; return ret;
} }
UINT16 IPv4Checksum(BYTE* ipv4, int length) static UINT16 IPv4Checksum(BYTE* ipv4, int length)
{ {
UINT16 tmp16; UINT16 tmp16;
long checksum = 0; long checksum = 0;
@ -285,17 +308,18 @@ UINT16 IPv4Checksum(BYTE* ipv4, int length)
return (UINT16)(~checksum); return (UINT16)(~checksum);
} }
int WLog_PacketMessage_Write_IPv4Header(wPcap* pcap, wIPv4Header* ipv4) static BOOL WLog_PacketMessage_Write_IPv4Header(wPcap* pcap, wIPv4Header* ipv4)
{ {
wStream* s; wStream* s;
BYTE buffer[20]; BYTE buffer[20];
int ret = TRUE;
if (!pcap || !pcap->fp || !ipv4) if (!pcap || !pcap->fp || !ipv4)
return -1; return FALSE;
s = Stream_New(buffer, 20); s = Stream_New(buffer, 20);
if (!s) if (!s)
return -1; return FALSE;
Stream_Write_UINT8(s, (ipv4->Version << 4) | ipv4->InternetHeaderLength); Stream_Write_UINT8(s, (ipv4->Version << 4) | ipv4->InternetHeaderLength);
Stream_Write_UINT8(s, ipv4->TypeOfService); Stream_Write_UINT8(s, ipv4->TypeOfService);
Stream_Write_UINT16_BE(s, ipv4->TotalLength); Stream_Write_UINT16_BE(s, ipv4->TotalLength);
@ -310,22 +334,24 @@ int WLog_PacketMessage_Write_IPv4Header(wPcap* pcap, wIPv4Header* ipv4)
Stream_Rewind(s, 10); Stream_Rewind(s, 10);
Stream_Write_UINT16(s, ipv4->HeaderChecksum); Stream_Write_UINT16(s, ipv4->HeaderChecksum);
Stream_Seek(s, 8); Stream_Seek(s, 8);
fwrite(buffer, 20, 1, pcap->fp); if (fwrite(buffer, 20, 1, pcap->fp) != 1)
ret = FALSE;
Stream_Free(s, FALSE); Stream_Free(s, FALSE);
return 0; return ret;
} }
int WLog_PacketMessage_Write_TcpHeader(wPcap* pcap, wTcpHeader* tcp) static BOOL WLog_PacketMessage_Write_TcpHeader(wPcap* pcap, wTcpHeader* tcp)
{ {
wStream* s; wStream* s;
BYTE buffer[20]; BYTE buffer[20];
BOOL ret = TRUE;
if (!pcap || !pcap->fp || !tcp) if (!pcap || !pcap->fp || !tcp)
return -1; return FALSE;
s = Stream_New(buffer, 20); s = Stream_New(buffer, 20);
if (!s) if (!s)
return -1; return FALSE;
Stream_Write_UINT16_BE(s, tcp->SourcePort); Stream_Write_UINT16_BE(s, tcp->SourcePort);
Stream_Write_UINT16_BE(s, tcp->DestinationPort); Stream_Write_UINT16_BE(s, tcp->DestinationPort);
Stream_Write_UINT32_BE(s, tcp->SequenceNumber); Stream_Write_UINT32_BE(s, tcp->SequenceNumber);
@ -337,10 +363,13 @@ int WLog_PacketMessage_Write_TcpHeader(wPcap* pcap, wTcpHeader* tcp)
Stream_Write_UINT16_BE(s, tcp->UrgentPointer); Stream_Write_UINT16_BE(s, tcp->UrgentPointer);
if (pcap->fp) if (pcap->fp)
fwrite(buffer, 20, 1, pcap->fp); {
if (fwrite(buffer, 20, 1, pcap->fp) != 1)
ret = FALSE;
}
Stream_Free(s, FALSE); Stream_Free(s, FALSE);
return 0; return ret;
} }
static UINT32 g_InboundSequenceNumber = 0; static UINT32 g_InboundSequenceNumber = 0;
@ -445,11 +474,12 @@ int WLog_PacketMessage_Write(wPcap* pcap, void* data, DWORD length, DWORD flags)
gettimeofday(&tp, 0); gettimeofday(&tp, 0);
record.header.ts_sec = tp.tv_sec; record.header.ts_sec = tp.tv_sec;
record.header.ts_usec = tp.tv_usec; record.header.ts_usec = tp.tv_usec;
Pcap_Write_RecordHeader(pcap, &record.header); if (!Pcap_Write_RecordHeader(pcap, &record.header) ||
WLog_PacketMessage_Write_EthernetHeader(pcap, &ethernet); !WLog_PacketMessage_Write_EthernetHeader(pcap, &ethernet) ||
WLog_PacketMessage_Write_IPv4Header(pcap, &ipv4); !WLog_PacketMessage_Write_IPv4Header(pcap, &ipv4) ||
WLog_PacketMessage_Write_TcpHeader(pcap, &tcp); !WLog_PacketMessage_Write_TcpHeader(pcap, &tcp) ||
Pcap_Write_RecordContent(pcap, &record); !Pcap_Write_RecordContent(pcap, &record))
return -1;
fflush(pcap->fp); fflush(pcap->fp);
return 0; return 0;
} }

View File

@ -588,7 +588,8 @@ int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context, char* pa
goto out_fail; goto out_fail;
length = offset; length = offset;
fwrite((void*) x509_str, length, 1, fp); if (fwrite((void*) x509_str, length, 1, fp) != 1)
goto out_fail;
} }
else else
@ -643,7 +644,8 @@ int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context, char* pa
length = offset; length = offset;
fwrite((void*) x509_str, length, 1, fp); if (fwrite((void*) x509_str, length, 1, fp) != 1)
goto out_fail;
free(x509_str); free(x509_str);
x509_str = NULL; x509_str = NULL;
@ -700,7 +702,8 @@ int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context, char* pa
length = offset; length = offset;
fwrite((void*) x509_str, length, 1, fp); if (fwrite((void*) x509_str, length, 1, fp) != 1)
goto out_fail;
} }
} }
@ -721,12 +724,15 @@ out_fail:
int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context, char* path) int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context, char* path)
{ {
FILE* fp; FILE* fp = NULL;
int status; int status;
int length; int length;
int offset; int offset;
char* filename; char* filename = NULL;
char* fullpath; char* fullpath = NULL;
int ret = -1;
BIO* bio = NULL;
BYTE* x509_str = NULL;
if (!context->crtFormat) if (!context->crtFormat)
return 1; return 1;
@ -748,7 +754,6 @@ int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context, char* pa
return -1; return -1;
strcpy(filename, context->output_file); strcpy(filename, context->output_file);
strcpy(&filename[length], ".key"); strcpy(&filename[length], ".key");
length = strlen(filename);
if (path) if (path)
fullpath = GetCombinedPath(path, filename); fullpath = GetCombinedPath(path, filename);
@ -756,99 +761,77 @@ int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context, char* pa
fullpath = _strdup(filename); fullpath = _strdup(filename);
if (!fullpath) if (!fullpath)
{ goto out_fail;
free(filename);
return -1;
}
fp = fopen(fullpath, "w+"); fp = fopen(fullpath, "w+");
if (!fp)
goto out_fail;
if (fp) bio = BIO_new(BIO_s_mem());
if (!bio)
goto out_fail;
if (!PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL))
goto out_fail;
offset = 0;
length = 2048;
x509_str = (BYTE*) malloc(length);
if (!x509_str)
goto out_fail;
status = BIO_read(bio, x509_str, length);
if (status < 0)
goto out_fail;
offset += status;
while (offset >= length)
{ {
BIO* bio; int new_len;
BYTE* x509_str; BYTE *new_str;
bio = BIO_new(BIO_s_mem()); new_len = length * 2;
new_str = (BYTE*) realloc(x509_str, new_len);
if (!bio) if (!new_str)
{ {
free (filename); status = -1;
free(fullpath); break;
fclose(fp);
return -1;
} }
status = PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL); length = new_len;
x509_str = new_str;
offset = 0; status = BIO_read(bio, &x509_str[offset], length);
length = 2048;
x509_str = (BYTE*) malloc(length);
if (!x509_str)
{
free (filename);
free(fullpath);
fclose(fp);
return -1;
}
status = BIO_read(bio, x509_str, length);
if (status < 0) if (status < 0)
{ break;
free (filename);
free(fullpath);
fclose(fp);
return -1;
}
offset += status; offset += status;
while (offset >= length)
{
int new_len;
BYTE *new_str;
new_len = length * 2;
new_str = (BYTE*) realloc(x509_str, new_len);
if (!new_str)
{
status = -1;
break;
}
length = new_len;
x509_str = new_str;
status = BIO_read(bio, &x509_str[offset], length);
if (status < 0)
break;
offset += status;
}
if (status < 0)
{
free (filename);
free(fullpath);
fclose(fp);
return -1;
}
length = offset;
fwrite((void*) x509_str, length, 1, fp);
free(x509_str);
BIO_free(bio);
fclose(fp);
} }
if (status < 0)
goto out_fail;
length = offset;
if (fwrite((void*) x509_str, length, 1, fp) != 1)
goto out_fail;
ret = 1;
out_fail:
if (fp)
fclose(fp);
if (bio)
BIO_free(bio);
free(x509_str);
free(filename); free(filename);
free(fullpath); free(fullpath);
return 1; return ret;
} }
int makecert_context_process(MAKECERT_CONTEXT* context, int argc, char** argv) int makecert_context_process(MAKECERT_CONTEXT* context, int argc, char** argv)