/*
MIT License
Copyright ( c ) 2025 Catena cyber
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE .
*/
#ifndef NALLOC_H_
#define NALLOC_H_
#if defined (__clang__) && defined (__has_feature)
#if __has_feature(address_sanitizer)
#define NALLOC_ASAN 1
#endif
#if __has_feature(memory_sanitizer)
#define FUZZER_DISABLE_NALLOC 1
#endif
#endif
#if defined (FUZZER_DISABLE_NALLOC)
#define nalloc_init(x)
#define nalloc_restrict_file_prefix(x)
#define nalloc_start(x, y)
#define nalloc_end()
#else
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
static const uint32_t nalloc_crc32_table[] = {
0 x00000000, 0 x04c11db7, 0 x09823b6e, 0 x0d4326d9, 0 x130476dc, 0 x17c56b6b,
0 x1a864db2, 0 x1e475005, 0 x2608edb8, 0 x22c9f00f, 0 x2f8ad6d6, 0 x2b4bcb61,
0 x350c9b64, 0 x31cd86d3, 0 x3c8ea00a, 0 x384fbdbd, 0 x4c11db70, 0 x48d0c6c7,
0 x4593e01e, 0 x4152fda9, 0 x5f15adac, 0 x5bd4b01b, 0 x569796c2, 0 x52568b75,
0 x6a1936c8, 0 x6ed82b7f, 0 x639b0da6, 0 x675a1011, 0 x791d4014, 0 x7ddc5da3,
0 x709f7b7a, 0 x745e66cd, 0 x9823b6e0, 0 x9ce2ab57, 0 x91a18d8e, 0 x95609039,
0 x8b27c03c, 0 x8fe6dd8b, 0 x82a5fb52, 0 x8664e6e5, 0 xbe2b5b58, 0 xbaea46ef,
0 xb7a96036, 0 xb3687d81, 0 xad2f2d84, 0 xa9ee3033, 0 xa4ad16ea, 0 xa06c0b5d,
0 xd4326d90, 0 xd0f37027, 0 xddb056fe, 0 xd9714b49, 0 xc7361b4c, 0 xc3f706fb,
0 xceb42022, 0 xca753d95, 0 xf23a8028, 0 xf6fb9d9f, 0 xfbb8bb46, 0 xff79a6f1,
0 xe13ef6f4, 0 xe5ffeb43, 0 xe8bccd9a, 0 xec7dd02d, 0 x34867077, 0 x30476dc0,
0 x3d044b19, 0 x39c556ae, 0 x278206ab, 0 x23431b1c, 0 x2e003dc5, 0 x2ac12072,
0 x128e9dcf, 0 x164f8078, 0 x1b0ca6a1, 0 x1fcdbb16, 0 x018aeb13, 0 x054bf6a4,
0 x0808d07d, 0 x0cc9cdca, 0 x7897ab07, 0 x7c56b6b0, 0 x71159069, 0 x75d48dde,
0 x6b93dddb, 0 x6f52c06c, 0 x6211e6b5, 0 x66d0fb02, 0 x5e9f46bf, 0 x5a5e5b08,
0 x571d7dd1, 0 x53dc6066, 0 x4d9b3063, 0 x495a2dd4, 0 x44190b0d, 0 x40d816ba,
0 xaca5c697, 0 xa864db20, 0 xa527fdf9, 0 xa1e6e04e, 0 xbfa1b04b, 0 xbb60adfc,
0 xb6238b25, 0 xb2e29692, 0 x8aad2b2f, 0 x8e6c3698, 0 x832f1041, 0 x87ee0df6,
0 x99a95df3, 0 x9d684044, 0 x902b669d, 0 x94ea7b2a, 0 xe0b41de7, 0 xe4750050,
0 xe9362689, 0 xedf73b3e, 0 xf3b06b3b, 0 xf771768c, 0 xfa325055, 0 xfef34de2,
0 xc6bcf05f, 0 xc27dede8, 0 xcf3ecb31, 0 xcbffd686, 0 xd5b88683, 0 xd1799b34,
0 xdc3abded, 0 xd8fba05a, 0 x690ce0ee, 0 x6dcdfd59, 0 x608edb80, 0 x644fc637,
0 x7a089632, 0 x7ec98b85, 0 x738aad5c, 0 x774bb0eb, 0 x4f040d56, 0 x4bc510e1,
0 x46863638, 0 x42472b8f, 0 x5c007b8a, 0 x58c1663d, 0 x558240e4, 0 x51435d53,
0 x251d3b9e, 0 x21dc2629, 0 x2c9f00f0, 0 x285e1d47, 0 x36194d42, 0 x32d850f5,
0 x3f9b762c, 0 x3b5a6b9b, 0 x0315d626, 0 x07d4cb91, 0 x0a97ed48, 0 x0e56f0ff,
0 x1011a0fa, 0 x14d0bd4d, 0 x19939b94, 0 x1d528623, 0 xf12f560e, 0 xf5ee4bb9,
0 xf8ad6d60, 0 xfc6c70d7, 0 xe22b20d2, 0 xe6ea3d65, 0 xeba91bbc, 0 xef68060b,
0 xd727bbb6, 0 xd3e6a601, 0 xdea580d8, 0 xda649d6f, 0 xc423cd6a, 0 xc0e2d0dd,
0 xcda1f604, 0 xc960ebb3, 0 xbd3e8d7e, 0 xb9ff90c9, 0 xb4bcb610, 0 xb07daba7,
0 xae3afba2, 0 xaafbe615, 0 xa7b8c0cc, 0 xa379dd7b, 0 x9b3660c6, 0 x9ff77d71,
0 x92b45ba8, 0 x9675461f, 0 x8832161a, 0 x8cf30bad, 0 x81b02d74, 0 x857130c3,
0 x5d8a9099, 0 x594b8d2e, 0 x5408abf7, 0 x50c9b640, 0 x4e8ee645, 0 x4a4ffbf2,
0 x470cdd2b, 0 x43cdc09c, 0 x7b827d21, 0 x7f436096, 0 x7200464f, 0 x76c15bf8,
0 x68860bfd, 0 x6c47164a, 0 x61043093, 0 x65c52d24, 0 x119b4be9, 0 x155a565e,
0 x18197087, 0 x1cd86d30, 0 x029f3d35, 0 x065e2082, 0 x0b1d065b, 0 x0fdc1bec,
0 x3793a651, 0 x3352bbe6, 0 x3e119d3f, 0 x3ad08088, 0 x2497d08d, 0 x2056cd3a,
0 x2d15ebe3, 0 x29d4f654, 0 xc5a92679, 0 xc1683bce, 0 xcc2b1d17, 0 xc8ea00a0,
0 xd6ad50a5, 0 xd26c4d12, 0 xdf2f6bcb, 0 xdbee767c, 0 xe3a1cbc1, 0 xe760d676,
0 xea23f0af, 0 xeee2ed18, 0 xf0a5bd1d, 0 xf464a0aa, 0 xf9278673, 0 xfde69bc4,
0 x89b8fd09, 0 x8d79e0be, 0 x803ac667, 0 x84fbdbd0, 0 x9abc8bd5, 0 x9e7d9662,
0 x933eb0bb, 0 x97ffad0c, 0 xafb010b1, 0 xab710d06, 0 xa6322bdf, 0 xa2f33668,
0 xbcb4666d, 0 xb8757bda, 0 xb5365d03, 0 xb1f740b4
};
// Nallocfuzz data to take a decision
uint32_t nalloc_random_state = 0 ;
__thread unsigned int nalloc_running = 0 ;
bool nalloc_initialized = false ;
uint32_t nalloc_runs = 0 ;
// Nalloc fuzz parameters
uint32_t nalloc_bitmask = 0 xFF;
bool nalloc_random_bitmask = true;
uint32_t nalloc_magic = 0 x294cee63;
bool nalloc_verbose = false ;
#ifdef NALLOC_ASAN
extern void __sanitizer_print_stack_trace(void );
#endif
// Generic init, using env variables to get parameters
void nalloc_init(const char *prog) {
if (nalloc_initialized) {
return ;
}
nalloc_initialized = true;
char *bitmask = getenv("NALLOC_FREQ" );
if (bitmask) {
int shift = atoi(bitmask);
if (shift > 0 && shift < 31 ) {
nalloc_bitmask = 1 << shift;
nalloc_random_bitmask = false ;
} else if (shift == 0 ) {
nalloc_random_bitmask = false ;
nalloc_bitmask = 0 ;
}
} else if (prog == NULL || strstr(prog, "nalloc" ) == NULL) {
nalloc_random_bitmask = false ;
nalloc_bitmask = 0 ;
return ;
}
char *magic = getenv("NALLOC_MAGIC" );
if (magic) {
nalloc_magic = (uint32_t)strtol(magic, NULL, 0 );
}
char *verbose = getenv("NALLOC_VERBOSE" );
if (verbose) {
nalloc_verbose = true;
}
}
// add one byte to the CRC
static inline void nalloc_random_update(uint8_t b) {
nalloc_random_state =
((uint32_t)((uint32_t)nalloc_random_state << 8 )) ^
nalloc_crc32_table[((nalloc_random_state >> 24 ) ^ b) & 0 xFF];
}
// Start the failure injections, using a buffer as seed
static int nalloc_start(const uint8_t *data, size_t size) {
if (nalloc_random_bitmask) {
if (nalloc_random_state & 0 x10) {
nalloc_bitmask = 0 xFFFFFFFF;
} else {
nalloc_bitmask = 1 << (5 + (nalloc_random_state & 0 xF));
}
} else if (nalloc_bitmask == 0 ) {
// nalloc disabled
return 0 ;
}
nalloc_random_state = 0 ;
for (size_t i = 0 ; i < size; i++) {
nalloc_random_update(data[i]);
}
if (__sync_fetch_and_add(&nalloc_running, 1 )) {
__sync_fetch_and_sub(&nalloc_running, 1 );
return 0 ;
}
nalloc_runs++;
return 1 ;
}
// Stop the failure injections
static void nalloc_end() { __sync_fetch_and_sub(&nalloc_running, 1 ); }
static bool nalloc_backtrace_exclude(size_t size, const char *op) {
if (nalloc_verbose) {
fprintf(stderr, "failed %s(%zu) \n" , op, size);
#ifdef NALLOC_ASAN
__sanitizer_print_stack_trace();
#endif
}
return false ;
}
//
static bool nalloc_fail(size_t size, const char *op) {
// do not fail before thread init
if (nalloc_runs == 0 ) {
return false ;
}
if (__sync_fetch_and_add(&nalloc_running, 1 ) != 1 ) {
// do not fail allocations outside of fuzzer input
// and do not fail inside of this function
__sync_fetch_and_sub(&nalloc_running, 1 );
return false ;
}
nalloc_random_update((uint8_t)size);
if (size >= 0 x100) {
nalloc_random_update((uint8_t)(size >> 8 ));
if (size >= 0 x10000) {
nalloc_random_update((uint8_t)(size >> 16 ));
// bigger may already fail or oom
}
}
if (((nalloc_random_state ^ nalloc_magic) & nalloc_bitmask) == 0 ) {
if (nalloc_backtrace_exclude(size, op)) {
__sync_fetch_and_sub(&nalloc_running, 1 );
return false ;
}
__sync_fetch_and_sub(&nalloc_running, 1 );
return true;
}
__sync_fetch_and_sub(&nalloc_running, 1 );
return false ;
}
// ASAN interceptor for libc routines
#ifdef NALLOC_ASAN
extern void *__interceptor_malloc(size_t);
extern void *__interceptor_calloc(size_t, size_t);
extern void *__interceptor_realloc(void *, size_t);
extern void *__interceptor_reallocarray(void *, size_t, size_t);
extern ssize_t __interceptor_read(int , void *, size_t);
extern ssize_t __interceptor_write(int , const void *, size_t);
extern ssize_t __interceptor_recv(int , void *, size_t, int );
extern ssize_t __interceptor_send(int , const void *, size_t, int );
#define nalloc_malloc(s) __interceptor_malloc(s)
#define nalloc_calloc(s, n) __interceptor_calloc(s, n)
#define nalloc_realloc(p, s) __interceptor_realloc(p, s)
#define nalloc_reallocarray(p, s, n) __interceptor_reallocarray(p, s, n)
#define nalloc_read(f, b, s) __interceptor_read(f, b, s)
#define nalloc_write(f, b, s) __interceptor_write(f, b, s)
#define nalloc_recv(f, b, s, x) __interceptor_recv(f, b, s, x)
#define nalloc_send(f, b, s, x) __interceptor_send(f, b, s, x)
#else
extern void *__libc_malloc(size_t);
extern void *__libc_calloc(size_t, size_t);
extern void *__libc_realloc(void *, size_t);
extern void *__libc_reallocarray(void *, size_t, size_t);
extern ssize_t __read(int , void *, size_t);
extern ssize_t __write(int , const void *, size_t);
extern ssize_t __recv(int , void *, size_t, int );
extern ssize_t __send(int , const void *, size_t, int );
#define nalloc_malloc(s) __libc_malloc(s)
#define nalloc_calloc(s, n) __libc_calloc(s, n)
#define nalloc_realloc(p, s) __libc_realloc(p, s)
#define nalloc_reallocarray(p, s, n) __libc_reallocarray(p, s, n)
#define nalloc_read(f, b, s) __read(f, b, s)
#define nalloc_write(f, b, s) __write(f, b, s)
#define nalloc_recv(f, b, s, x) __recv(f, b, s, x)
#define nalloc_send(f, b, s, x) __send(f, b, s, x)
#endif
// nalloc standard function overwrites with pseudo-random failures
ssize_t read(int fd, void *buf, size_t count) {
if (nalloc_fail(count, "read" )) {
errno = EIO;
return -1 ;
}
return nalloc_read(fd, buf, count);
}
ssize_t write(int fd, const void *buf, size_t count) {
if (nalloc_fail(count, "write" )) {
errno = EIO;
return -1 ;
}
return nalloc_write(fd, buf, count);
}
ssize_t recv(int fd, void *buf, size_t count, int flags) {
if (nalloc_fail(count, "recv" )) {
errno = EIO;
return -1 ;
}
return nalloc_recv(fd, buf, count, flags);
}
ssize_t send(int fd, const void *buf, size_t count, int flags) {
if (nalloc_fail(count, "send" )) {
errno = EIO;
return -1 ;
}
return nalloc_send(fd, buf, count, flags);
}
void *calloc(size_t nmemb, size_t size) {
if (nalloc_fail(size, "calloc" )) {
errno = ENOMEM;
return NULL;
}
return nalloc_calloc(nmemb, size);
}
void *malloc(size_t size) {
if (nalloc_fail(size, "malloc" )) {
errno = ENOMEM;
return NULL;
}
return nalloc_malloc(size);
}
void *realloc(void *ptr, size_t size) {
if (nalloc_fail(size, "realloc" )) {
errno = ENOMEM;
return NULL;
}
return nalloc_realloc(ptr, size);
}
void *reallocarray(void *ptr, size_t nmemb, size_t size) {
if (nalloc_fail(size, "reallocarray" )) {
errno = ENOMEM;
return NULL;
}
return nalloc_reallocarray(ptr, nmemb, size);
}
#ifdef __cplusplus
} // extern "C" {
#endif
#endif // FUZZER_DISABLE_NALLOC
#endif // NALLOC_H_
Messung V0.5 in Prozent C=68 H=100 G=85
¤ Dauer der Verarbeitung: 0.5 Sekunden
¤
*© Formatika GbR, Deutschland