Skip to content

Commit

Permalink
Only use malloc wrapper if fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
mkj committed Mar 1, 2018
1 parent 8a4f7fe commit 26ad685
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 22 deletions.
80 changes: 59 additions & 21 deletions dbmalloc.c
@@ -1,6 +1,64 @@
#include "dbmalloc.h"
#include "dbutil.h"


void * m_calloc(size_t nmemb, size_t size) {
if (SIZE_T_MAX / nmemb < size) {
dropbear_exit("m_calloc failed");
}
return m_malloc(nmemb*size);
}

void * m_strdup(const char * str) {
char* ret;
unsigned int len;
len = strlen(str);

ret = m_malloc(len+1);
if (ret == NULL) {
dropbear_exit("m_strdup failed");
}
memcpy(ret, str, len+1);
return ret;
}

#if !DROPBEAR_TRACKING_MALLOC

/* Simple wrappers around malloc etc */
void * m_malloc(size_t size) {

void* ret;

if (size == 0) {
dropbear_exit("m_malloc failed");
}
ret = calloc(1, size);
if (ret == NULL) {
dropbear_exit("m_malloc failed");
}
return ret;

}

void * m_realloc(void* ptr, size_t size) {

void *ret;

if (size == 0) {
dropbear_exit("m_realloc failed");
}
ret = realloc(ptr, size);
if (ret == NULL) {
dropbear_exit("m_realloc failed");
}
return ret;
}


#else

/* For fuzzing */

struct dbmalloc_header {
unsigned int epoch;
struct dbmalloc_header *prev;
Expand Down Expand Up @@ -90,13 +148,6 @@ void * m_malloc(size_t size) {
return &mem[sizeof(struct dbmalloc_header)];
}

void * m_calloc(size_t nmemb, size_t size) {
if (SIZE_T_MAX / nmemb < size) {
dropbear_exit("m_calloc failed");
}
return m_malloc(nmemb*size);
}

void * m_realloc(void* ptr, size_t size) {
char* mem = NULL;
struct dbmalloc_header* header = NULL;
Expand Down Expand Up @@ -128,17 +179,4 @@ void m_free_direct(void* ptr) {
free(header);
}

void * m_strdup(const char * str) {
char* ret;
unsigned int len;
len = strlen(str);

ret = m_malloc(len+1);
if (ret == NULL) {
dropbear_exit("m_strdup failed");
}
memcpy(ret, str, len+1);
return ret;
}


#endif /* DROPBEAR_TRACKING_MALLOC */
11 changes: 10 additions & 1 deletion dbmalloc.h
Expand Up @@ -7,10 +7,19 @@ void * m_malloc(size_t size);
void * m_calloc(size_t nmemb, size_t size);
void * m_strdup(const char * str);
void * m_realloc(void* ptr, size_t size);

#if DROPBEAR_TRACKING_MALLOC
void m_free_direct(void* ptr);
#define m_free(X) do {m_free_direct(X); (X) = NULL;} while (0)

void m_malloc_set_epoch(unsigned int epoch);
void m_malloc_free_epoch(unsigned int epoch, int dofree);

#else
/* plain wrapper */

#define m_free(X) do {free(X); (X) = NULL;} while (0)

#endif


#endif /* DBMALLOC_H_ */
2 changes: 2 additions & 0 deletions sysoptions.h
Expand Up @@ -316,4 +316,6 @@ If you test it please contact the Dropbear author */
#define DROPBEAR_CLIENT_TCP_FAST_OPEN 0
#endif

#define DROPBEAR_TRACKING_MALLOC (DROPBEAR_FUZZ)

/* no include guard for this file */

0 comments on commit 26ad685

Please sign in to comment.