Compare commits
7 Commits
master
...
nanopb-0.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45fc9f3ef6 | ||
|
|
115de6e555 | ||
|
|
9c92410e2a | ||
|
|
6df566859d | ||
|
|
cc1c3a7963 | ||
|
|
33585924de | ||
|
|
b7add1e577 |
@@ -1,3 +1,8 @@
|
||||
nanopb-0.2.9.1 (2014-09-11)
|
||||
Fix security issue due to size_t overflows. (issue 132)
|
||||
Fix memory leak with duplicated fields and PB_ENABLE_MALLOC
|
||||
Fix crash if pb_release() is called twice.
|
||||
|
||||
nanopb-0.2.9 (2014-08-09)
|
||||
NOTE: If you are using the -e option with the generator, you have
|
||||
to prepend . to the argument to get the same behaviour as before.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
'''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.'''
|
||||
nanopb_version = "nanopb-0.2.9"
|
||||
nanopb_version = "nanopb-0.2.9.1"
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
2
pb.h
2
pb.h
@@ -46,7 +46,7 @@
|
||||
|
||||
/* Version of the nanopb library. Just in case you want to check it in
|
||||
* your own program. */
|
||||
#define NANOPB_VERSION nanopb-0.2.9
|
||||
#define NANOPB_VERSION nanopb-0.2.9.1
|
||||
|
||||
/* Include all the system headers needed by nanopb. You will need the
|
||||
* definitions of the following:
|
||||
|
||||
137
pb_decode.c
137
pb_decode.c
@@ -57,6 +57,11 @@ static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t
|
||||
static bool checkreturn pb_skip_varint(pb_istream_t *stream);
|
||||
static bool checkreturn pb_skip_string(pb_istream_t *stream);
|
||||
|
||||
#ifdef PB_ENABLE_MALLOC
|
||||
static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
|
||||
static void pb_release_single_field(const pb_field_iterator_t *iter);
|
||||
#endif
|
||||
|
||||
/* --- Function pointers to field decoders ---
|
||||
* Order in the array must match pb_action_t LTYPE numbering.
|
||||
*/
|
||||
@@ -477,18 +482,23 @@ static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t
|
||||
{
|
||||
void *ptr = *(void**)pData;
|
||||
|
||||
if (data_size == 0 || array_size == 0)
|
||||
PB_RETURN_ERROR(stream, "invalid size");
|
||||
|
||||
/* Check for multiplication overflows.
|
||||
* This code avoids the costly division if the sizes are small enough.
|
||||
* Multiplication is safe as long as only half of bits are set
|
||||
* in either multiplicand.
|
||||
*/
|
||||
const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
|
||||
if (data_size >= check_limit || array_size >= check_limit)
|
||||
{
|
||||
const size_t size_max = (size_t)-1;
|
||||
if (size_max / array_size < data_size)
|
||||
const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
|
||||
if (data_size >= check_limit || array_size >= check_limit)
|
||||
{
|
||||
PB_RETURN_ERROR(stream, "size too large");
|
||||
const size_t size_max = (size_t)-1;
|
||||
if (size_max / array_size < data_size)
|
||||
{
|
||||
PB_RETURN_ERROR(stream, "size too large");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -535,6 +545,13 @@ static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_
|
||||
{
|
||||
case PB_HTYPE_REQUIRED:
|
||||
case PB_HTYPE_OPTIONAL:
|
||||
if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
|
||||
*(void**)iter->pData != NULL)
|
||||
{
|
||||
/* Duplicate field, have to release the old allocation first. */
|
||||
pb_release_single_field(iter);
|
||||
}
|
||||
|
||||
if (PB_LTYPE(type) == PB_LTYPE_STRING ||
|
||||
PB_LTYPE(type) == PB_LTYPE_BYTES)
|
||||
{
|
||||
@@ -934,57 +951,66 @@ bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *
|
||||
}
|
||||
|
||||
#ifdef PB_ENABLE_MALLOC
|
||||
static void pb_release_single_field(const pb_field_iterator_t *iter)
|
||||
{
|
||||
pb_type_t type;
|
||||
type = iter->pos->type;
|
||||
|
||||
if (PB_ATYPE(type) == PB_ATYPE_POINTER)
|
||||
{
|
||||
if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
|
||||
(PB_LTYPE(type) == PB_LTYPE_STRING ||
|
||||
PB_LTYPE(type) == PB_LTYPE_BYTES))
|
||||
{
|
||||
/* Release entries in repeated string or bytes array */
|
||||
void **pItem = *(void***)iter->pData;
|
||||
pb_size_t count = *(pb_size_t*)iter->pSize;
|
||||
while (count--)
|
||||
{
|
||||
pb_free(*pItem);
|
||||
*pItem++ = NULL;
|
||||
}
|
||||
*(pb_size_t*)iter->pSize = 0;
|
||||
}
|
||||
else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
|
||||
{
|
||||
/* Release fields in submessages */
|
||||
void *pItem = *(void**)iter->pData;
|
||||
if (pItem)
|
||||
{
|
||||
pb_size_t count = 1;
|
||||
|
||||
if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
|
||||
{
|
||||
count = *(pb_size_t*)iter->pSize;
|
||||
*(pb_size_t*)iter->pSize = 0;
|
||||
}
|
||||
|
||||
while (count--)
|
||||
{
|
||||
pb_release((const pb_field_t*)iter->pos->ptr, pItem);
|
||||
pItem = (uint8_t*)pItem + iter->pos->data_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Release main item */
|
||||
pb_free(*(void**)iter->pData);
|
||||
*(void**)iter->pData = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void pb_release(const pb_field_t fields[], void *dest_struct)
|
||||
{
|
||||
pb_field_iterator_t iter;
|
||||
pb_field_init(&iter, fields, dest_struct);
|
||||
|
||||
if (iter.pos->tag == 0)
|
||||
return; /* Empty message type */
|
||||
|
||||
do
|
||||
{
|
||||
pb_type_t type;
|
||||
type = iter.pos->type;
|
||||
|
||||
/* Avoid crash on empty message types (zero fields) */
|
||||
if (iter.pos->tag == 0)
|
||||
continue;
|
||||
|
||||
if (PB_ATYPE(type) == PB_ATYPE_POINTER)
|
||||
{
|
||||
if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
|
||||
(PB_LTYPE(type) == PB_LTYPE_STRING ||
|
||||
PB_LTYPE(type) == PB_LTYPE_BYTES))
|
||||
{
|
||||
/* Release entries in repeated string or bytes array */
|
||||
void **pItem = *(void***)iter.pData;
|
||||
size_t count = *(size_t*)iter.pSize;
|
||||
while (count--)
|
||||
{
|
||||
pb_free(*pItem);
|
||||
*pItem++ = NULL;
|
||||
}
|
||||
}
|
||||
else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
|
||||
{
|
||||
/* Release fields in submessages */
|
||||
void *pItem = *(void**)iter.pData;
|
||||
size_t count = (pItem ? 1 : 0);
|
||||
|
||||
if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
|
||||
{
|
||||
count = *(size_t*)iter.pSize;
|
||||
}
|
||||
|
||||
while (count--)
|
||||
{
|
||||
pb_release((const pb_field_t*)iter.pos->ptr, pItem);
|
||||
pItem = (uint8_t*)pItem + iter.pos->data_size;
|
||||
}
|
||||
}
|
||||
|
||||
/* Release main item */
|
||||
pb_free(*(void**)iter.pData);
|
||||
*(void**)iter.pData = NULL;
|
||||
}
|
||||
pb_release_single_field(&iter);
|
||||
} while (pb_field_next(&iter));
|
||||
}
|
||||
#endif
|
||||
@@ -1112,29 +1138,35 @@ static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *f
|
||||
static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
|
||||
{
|
||||
uint32_t size;
|
||||
size_t alloc_size;
|
||||
pb_bytes_array_t *bdest;
|
||||
|
||||
if (!pb_decode_varint32(stream, &size))
|
||||
return false;
|
||||
|
||||
alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
|
||||
if (size > alloc_size)
|
||||
PB_RETURN_ERROR(stream, "size too large");
|
||||
|
||||
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
|
||||
{
|
||||
#ifndef PB_ENABLE_MALLOC
|
||||
PB_RETURN_ERROR(stream, "no malloc support");
|
||||
#else
|
||||
if (!allocate_field(stream, dest, PB_BYTES_ARRAY_T_ALLOCSIZE(size), 1))
|
||||
if (!allocate_field(stream, dest, alloc_size, 1))
|
||||
return false;
|
||||
bdest = *(pb_bytes_array_t**)dest;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
if (PB_BYTES_ARRAY_T_ALLOCSIZE(size) > field->data_size)
|
||||
if (alloc_size > field->data_size)
|
||||
PB_RETURN_ERROR(stream, "bytes overflow");
|
||||
bdest = (pb_bytes_array_t*)dest;
|
||||
}
|
||||
|
||||
bdest->size = size;
|
||||
|
||||
return pb_read(stream, bdest->bytes, size);
|
||||
}
|
||||
|
||||
@@ -1149,6 +1181,9 @@ static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *fi
|
||||
/* Space for null terminator */
|
||||
alloc_size = size + 1;
|
||||
|
||||
if (alloc_size < size)
|
||||
PB_RETURN_ERROR(stream, "size too large");
|
||||
|
||||
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
|
||||
{
|
||||
#ifndef PB_ENABLE_MALLOC
|
||||
|
||||
@@ -57,6 +57,7 @@ if not env.GetOption('clean'):
|
||||
if not stdbool or not stdint or not stddef or not string:
|
||||
conf.env.Append(CPPDEFINES = {'PB_SYSTEM_HEADER': '\\"pb_syshdr.h\\"'})
|
||||
conf.env.Append(CPPPATH = "#../extra")
|
||||
conf.env.Append(SYSHDR = '\\"pb_syshdr.h\\"')
|
||||
|
||||
if stdbool: conf.env.Append(CPPDEFINES = {'HAVE_STDBOOL_H': 1})
|
||||
if stdint: conf.env.Append(CPPDEFINES = {'HAVE_STDINT_H': 1})
|
||||
@@ -101,7 +102,7 @@ if 'gcc' in env['CC']:
|
||||
# GNU Compiler Collection
|
||||
|
||||
# Debug info, warnings as errors
|
||||
env.Append(CFLAGS = '-ansi -pedantic -g -Wall -Werror -fprofile-arcs -ftest-coverage -fstack-protector-all')
|
||||
env.Append(CFLAGS = '-ansi -pedantic -g -Wall -Werror -fprofile-arcs -ftest-coverage ')
|
||||
env.Append(CORECFLAGS = '-Wextra')
|
||||
env.Append(LINKFLAGS = '-g --coverage')
|
||||
|
||||
|
||||
55
tests/fuzztest/SConscript
Normal file
55
tests/fuzztest/SConscript
Normal file
@@ -0,0 +1,55 @@
|
||||
# Run a fuzz test to verify robustness against corrupted/malicious data.
|
||||
|
||||
Import("env")
|
||||
|
||||
# We need our own pb_decode.o for the malloc support
|
||||
env = env.Clone()
|
||||
env.Append(CPPDEFINES = {'PB_ENABLE_MALLOC': 1,
|
||||
'PB_SYSTEM_HEADER': '\\"fuzz_syshdr.h\\"'})
|
||||
env.Append(CPPPATH = ".")
|
||||
|
||||
if 'SYSHDR' in env:
|
||||
env.Append(CPPDEFINES = {'PB_OLD_SYSHDR': env['SYSHDR']})
|
||||
|
||||
# Disable libmudflap, because it will confuse valgrind
|
||||
# and other memory leak detection tools.
|
||||
if '-fmudflap' in env["CCFLAGS"]:
|
||||
env["CCFLAGS"].remove("-fmudflap")
|
||||
env["LINKFLAGS"].remove("-fmudflap")
|
||||
env["LIBS"].remove("mudflap")
|
||||
|
||||
strict = env.Clone()
|
||||
strict.Append(CFLAGS = strict['CORECFLAGS'])
|
||||
strict.Object("pb_decode_with_malloc.o", "$NANOPB/pb_decode.c")
|
||||
strict.Object("pb_encode_with_malloc.o", "$NANOPB/pb_encode.c")
|
||||
|
||||
# We want both pointer and static versions of the AllTypes message
|
||||
env.Command("alltypes_static.proto", "#alltypes/alltypes.proto",
|
||||
lambda target, source, env:
|
||||
open(str(target[0]), 'w').write("package alltypes_static;\n"
|
||||
+ open(str(source[0])).read()))
|
||||
env.Command("alltypes_pointer.proto", "#alltypes/alltypes.proto",
|
||||
lambda target, source, env:
|
||||
open(str(target[0]), 'w').write("package alltypes_pointer;\n"
|
||||
+ open(str(source[0])).read()))
|
||||
|
||||
p1 = env.NanopbProto(["alltypes_pointer", "alltypes_pointer.options"])
|
||||
p2 = env.NanopbProto(["alltypes_static", "alltypes_static.options"])
|
||||
fuzz = env.Program(["fuzztest.c",
|
||||
"alltypes_pointer.pb.c",
|
||||
"alltypes_static.pb.c",
|
||||
"pb_encode_with_malloc.o",
|
||||
"pb_decode_with_malloc.o",
|
||||
"malloc_wrappers.c"])
|
||||
Depends([p1, p2, fuzz], ["fuzz_syshdr.h", "malloc_wrappers.h"])
|
||||
|
||||
env.RunTest(fuzz)
|
||||
|
||||
fuzzstub = env.Program(["fuzzstub.c",
|
||||
"alltypes_pointer.pb.c",
|
||||
"alltypes_static.pb.c",
|
||||
"pb_encode_with_malloc.o",
|
||||
"pb_decode_with_malloc.o",
|
||||
"malloc_wrappers.c"])
|
||||
|
||||
|
||||
3
tests/fuzztest/alltypes_pointer.options
Normal file
3
tests/fuzztest/alltypes_pointer.options
Normal file
@@ -0,0 +1,3 @@
|
||||
# Generate all fields as pointers.
|
||||
* type:FT_POINTER
|
||||
|
||||
3
tests/fuzztest/alltypes_static.options
Normal file
3
tests/fuzztest/alltypes_static.options
Normal file
@@ -0,0 +1,3 @@
|
||||
* max_size:32
|
||||
* max_count:8
|
||||
*.extensions type:FT_IGNORE
|
||||
15
tests/fuzztest/fuzz_syshdr.h
Normal file
15
tests/fuzztest/fuzz_syshdr.h
Normal file
@@ -0,0 +1,15 @@
|
||||
/* This is just a wrapper in order to get our own malloc wrappers into nanopb core. */
|
||||
|
||||
#define pb_realloc(ptr,size) counting_realloc(ptr,size)
|
||||
#define pb_free(ptr) counting_free(ptr)
|
||||
|
||||
#ifdef PB_OLD_SYSHDR
|
||||
#include PB_OLD_SYSHDR
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#include <malloc_wrappers.h>
|
||||
189
tests/fuzztest/fuzzstub.c
Normal file
189
tests/fuzztest/fuzzstub.c
Normal file
@@ -0,0 +1,189 @@
|
||||
/* Fuzz testing for the nanopb core.
|
||||
* This can be used with external fuzzers, e.g. radamsa.
|
||||
* It performs most of the same checks as fuzztest, but does not feature data generation.
|
||||
*/
|
||||
|
||||
#include <pb_decode.h>
|
||||
#include <pb_encode.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include "malloc_wrappers.h"
|
||||
#include "alltypes_static.pb.h"
|
||||
#include "alltypes_pointer.pb.h"
|
||||
|
||||
#define BUFSIZE 4096
|
||||
|
||||
static bool do_static_decode(uint8_t *buffer, size_t msglen, bool assert_success)
|
||||
{
|
||||
pb_istream_t stream;
|
||||
bool status;
|
||||
|
||||
alltypes_static_AllTypes *msg = malloc_with_check(sizeof(alltypes_static_AllTypes));
|
||||
stream = pb_istream_from_buffer(buffer, msglen);
|
||||
status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg);
|
||||
|
||||
if (!status && assert_success)
|
||||
{
|
||||
/* Anything that was successfully encoded, should be decodeable.
|
||||
* One exception: strings without null terminator are encoded up
|
||||
* to end of buffer, but refused on decode because the terminator
|
||||
* would not fit. */
|
||||
if (strcmp(stream.errmsg, "string overflow") != 0)
|
||||
assert(status);
|
||||
}
|
||||
|
||||
free_with_check(msg);
|
||||
return status;
|
||||
}
|
||||
|
||||
static bool do_pointer_decode(uint8_t *buffer, size_t msglen, bool assert_success)
|
||||
{
|
||||
pb_istream_t stream;
|
||||
bool status;
|
||||
alltypes_pointer_AllTypes *msg;
|
||||
|
||||
msg = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
|
||||
memset(msg, 0, sizeof(alltypes_pointer_AllTypes));
|
||||
stream = pb_istream_from_buffer(buffer, msglen);
|
||||
|
||||
assert(get_alloc_count() == 0);
|
||||
status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg);
|
||||
|
||||
if (assert_success)
|
||||
assert(status);
|
||||
|
||||
pb_release(alltypes_pointer_AllTypes_fields, msg);
|
||||
assert(get_alloc_count() == 0);
|
||||
|
||||
free_with_check(msg);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Do a decode -> encode -> decode -> encode roundtrip */
|
||||
static void do_static_roundtrip(uint8_t *buffer, size_t msglen)
|
||||
{
|
||||
bool status;
|
||||
uint8_t *buf2 = malloc_with_check(BUFSIZE);
|
||||
uint8_t *buf3 = malloc_with_check(BUFSIZE);
|
||||
size_t msglen2, msglen3;
|
||||
alltypes_static_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_static_AllTypes));
|
||||
alltypes_static_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_static_AllTypes));
|
||||
memset(msg1, 0, sizeof(alltypes_static_AllTypes));
|
||||
memset(msg2, 0, sizeof(alltypes_static_AllTypes));
|
||||
|
||||
{
|
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
|
||||
status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg1);
|
||||
assert(status);
|
||||
}
|
||||
|
||||
{
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
|
||||
status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg1);
|
||||
assert(status);
|
||||
msglen2 = stream.bytes_written;
|
||||
}
|
||||
|
||||
{
|
||||
pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
|
||||
status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg2);
|
||||
assert(status);
|
||||
}
|
||||
|
||||
{
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
|
||||
status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg2);
|
||||
assert(status);
|
||||
msglen3 = stream.bytes_written;
|
||||
}
|
||||
|
||||
assert(msglen2 == msglen3);
|
||||
assert(memcmp(buf2, buf3, msglen2) == 0);
|
||||
|
||||
free_with_check(msg1);
|
||||
free_with_check(msg2);
|
||||
free_with_check(buf2);
|
||||
free_with_check(buf3);
|
||||
}
|
||||
|
||||
/* Do decode -> encode -> decode -> encode roundtrip */
|
||||
static void do_pointer_roundtrip(uint8_t *buffer, size_t msglen)
|
||||
{
|
||||
bool status;
|
||||
uint8_t *buf2 = malloc_with_check(BUFSIZE);
|
||||
uint8_t *buf3 = malloc_with_check(BUFSIZE);
|
||||
size_t msglen2, msglen3;
|
||||
alltypes_pointer_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
|
||||
alltypes_pointer_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
|
||||
memset(msg1, 0, sizeof(alltypes_pointer_AllTypes));
|
||||
memset(msg2, 0, sizeof(alltypes_pointer_AllTypes));
|
||||
|
||||
{
|
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
|
||||
status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg1);
|
||||
assert(status);
|
||||
}
|
||||
|
||||
{
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
|
||||
status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg1);
|
||||
assert(status);
|
||||
msglen2 = stream.bytes_written;
|
||||
}
|
||||
|
||||
{
|
||||
pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
|
||||
status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg2);
|
||||
assert(status);
|
||||
}
|
||||
|
||||
{
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
|
||||
status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg2);
|
||||
assert(status);
|
||||
msglen3 = stream.bytes_written;
|
||||
}
|
||||
|
||||
assert(msglen2 == msglen3);
|
||||
assert(memcmp(buf2, buf3, msglen2) == 0);
|
||||
|
||||
pb_release(alltypes_pointer_AllTypes_fields, msg1);
|
||||
pb_release(alltypes_pointer_AllTypes_fields, msg2);
|
||||
free_with_check(msg1);
|
||||
free_with_check(msg2);
|
||||
free_with_check(buf2);
|
||||
free_with_check(buf3);
|
||||
}
|
||||
|
||||
static void run_iteration()
|
||||
{
|
||||
uint8_t *buffer = malloc_with_check(BUFSIZE);
|
||||
size_t msglen;
|
||||
bool status;
|
||||
|
||||
msglen = fread(buffer, BUFSIZE, 1, stdin);
|
||||
|
||||
status = do_static_decode(buffer, msglen, false);
|
||||
|
||||
if (status)
|
||||
do_static_roundtrip(buffer, msglen);
|
||||
|
||||
status = do_pointer_decode(buffer, msglen, false);
|
||||
|
||||
if (status)
|
||||
do_pointer_roundtrip(buffer, msglen);
|
||||
|
||||
free_with_check(buffer);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
run_iteration();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
431
tests/fuzztest/fuzztest.c
Normal file
431
tests/fuzztest/fuzztest.c
Normal file
@@ -0,0 +1,431 @@
|
||||
/* Fuzz testing for the nanopb core.
|
||||
* Attempts to verify all the properties defined in the security model document.
|
||||
*/
|
||||
|
||||
#include <pb_decode.h>
|
||||
#include <pb_encode.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include "malloc_wrappers.h"
|
||||
#include "alltypes_static.pb.h"
|
||||
#include "alltypes_pointer.pb.h"
|
||||
|
||||
static uint64_t random_seed;
|
||||
|
||||
/* Uses xorshift64 here instead of rand() for both speed and
|
||||
* reproducibility across platforms. */
|
||||
static uint32_t rand_word()
|
||||
{
|
||||
random_seed ^= random_seed >> 12;
|
||||
random_seed ^= random_seed << 25;
|
||||
random_seed ^= random_seed >> 27;
|
||||
return random_seed * 2685821657736338717ULL;
|
||||
}
|
||||
|
||||
/* Get a random integer in range, with approximately flat distribution. */
|
||||
static int rand_int(int min, int max)
|
||||
{
|
||||
return rand_word() % (max + 1 - min) + min;
|
||||
}
|
||||
|
||||
static bool rand_bool()
|
||||
{
|
||||
return rand_word() & 1;
|
||||
}
|
||||
|
||||
/* Get a random byte, with skewed distribution.
|
||||
* Important corner cases like 0xFF, 0x00 and 0xFE occur more
|
||||
* often than other values. */
|
||||
static uint8_t rand_byte()
|
||||
{
|
||||
uint32_t w = rand_word();
|
||||
uint8_t b = w & 0xFF;
|
||||
if (w & 0x100000)
|
||||
b >>= (w >> 8) & 7;
|
||||
if (w & 0x200000)
|
||||
b <<= (w >> 12) & 7;
|
||||
if (w & 0x400000)
|
||||
b ^= 0xFF;
|
||||
return b;
|
||||
}
|
||||
|
||||
/* Get a random length, with skewed distribution.
|
||||
* Favors the shorter lengths, but always atleast 1. */
|
||||
static size_t rand_len(size_t max)
|
||||
{
|
||||
uint32_t w = rand_word();
|
||||
size_t s;
|
||||
if (w & 0x800000)
|
||||
w &= 3;
|
||||
else if (w & 0x400000)
|
||||
w &= 15;
|
||||
else if (w & 0x200000)
|
||||
w &= 255;
|
||||
|
||||
s = (w % max);
|
||||
if (s == 0)
|
||||
s = 1;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/* Fills a buffer with random data with skewed distribution. */
|
||||
static void rand_fill(uint8_t *buf, size_t count)
|
||||
{
|
||||
while (count--)
|
||||
*buf++ = rand_byte();
|
||||
}
|
||||
|
||||
/* Fill with random protobuf-like data */
|
||||
static size_t rand_fill_protobuf(uint8_t *buf, size_t min_bytes, size_t max_bytes, int min_tag)
|
||||
{
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buf, max_bytes);
|
||||
|
||||
while(stream.bytes_written < min_bytes)
|
||||
{
|
||||
pb_wire_type_t wt = rand_int(0, 3);
|
||||
if (wt == 3) wt = 5; /* Gap in values */
|
||||
|
||||
if (!pb_encode_tag(&stream, wt, rand_int(min_tag, min_tag + 512)))
|
||||
break;
|
||||
|
||||
if (wt == PB_WT_VARINT)
|
||||
{
|
||||
uint64_t value;
|
||||
rand_fill((uint8_t*)&value, sizeof(value));
|
||||
pb_encode_varint(&stream, value);
|
||||
}
|
||||
else if (wt == PB_WT_64BIT)
|
||||
{
|
||||
uint64_t value;
|
||||
rand_fill((uint8_t*)&value, sizeof(value));
|
||||
pb_encode_fixed64(&stream, &value);
|
||||
}
|
||||
else if (wt == PB_WT_32BIT)
|
||||
{
|
||||
uint32_t value;
|
||||
rand_fill((uint8_t*)&value, sizeof(value));
|
||||
pb_encode_fixed32(&stream, &value);
|
||||
}
|
||||
else if (wt == PB_WT_STRING)
|
||||
{
|
||||
size_t len;
|
||||
uint8_t *buf;
|
||||
|
||||
if (min_bytes > stream.bytes_written)
|
||||
len = rand_len(min_bytes - stream.bytes_written);
|
||||
else
|
||||
len = 0;
|
||||
|
||||
buf = malloc(len);
|
||||
pb_encode_varint(&stream, len);
|
||||
rand_fill(buf, len);
|
||||
pb_write(&stream, buf, len);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
|
||||
return stream.bytes_written;
|
||||
}
|
||||
|
||||
/* Given a buffer of data, mess it up a bit */
|
||||
static void rand_mess(uint8_t *buf, size_t count)
|
||||
{
|
||||
int m = rand_int(0, 3);
|
||||
|
||||
if (m == 0)
|
||||
{
|
||||
/* Replace random substring */
|
||||
int s = rand_int(0, count - 1);
|
||||
int l = rand_len(count - s);
|
||||
rand_fill(buf + s, l);
|
||||
}
|
||||
else if (m == 1)
|
||||
{
|
||||
/* Swap random bytes */
|
||||
int a = rand_int(0, count - 1);
|
||||
int b = rand_int(0, count - 1);
|
||||
int x = buf[a];
|
||||
buf[a] = buf[b];
|
||||
buf[b] = x;
|
||||
}
|
||||
else if (m == 2)
|
||||
{
|
||||
/* Duplicate substring */
|
||||
int s = rand_int(0, count - 2);
|
||||
int l = rand_len((count - s) / 2);
|
||||
memcpy(buf + s + l, buf + s, l);
|
||||
}
|
||||
else if (m == 3)
|
||||
{
|
||||
/* Add random protobuf noise */
|
||||
int s = rand_int(0, count - 1);
|
||||
int l = rand_len(count - s);
|
||||
rand_fill_protobuf(buf + s, l, count - s, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Some default data to put in the message */
|
||||
static const alltypes_static_AllTypes initval = alltypes_static_AllTypes_init_default;
|
||||
|
||||
#define BUFSIZE 4096
|
||||
|
||||
static bool do_static_encode(uint8_t *buffer, size_t *msglen)
|
||||
{
|
||||
pb_ostream_t stream;
|
||||
bool status;
|
||||
|
||||
/* Allocate a message and fill it with defaults */
|
||||
alltypes_static_AllTypes *msg = malloc_with_check(sizeof(alltypes_static_AllTypes));
|
||||
memcpy(msg, &initval, sizeof(initval));
|
||||
|
||||
/* Apply randomness to the data before encoding */
|
||||
while (rand_int(0, 7))
|
||||
rand_mess((uint8_t*)msg, sizeof(alltypes_static_AllTypes));
|
||||
|
||||
stream = pb_ostream_from_buffer(buffer, BUFSIZE);
|
||||
status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg);
|
||||
assert(stream.bytes_written <= BUFSIZE);
|
||||
assert(stream.bytes_written <= alltypes_static_AllTypes_size);
|
||||
|
||||
*msglen = stream.bytes_written;
|
||||
free_with_check(msg);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Append or prepend protobuf noise */
|
||||
static void do_protobuf_noise(uint8_t *buffer, size_t *msglen)
|
||||
{
|
||||
int m = rand_int(0, 2);
|
||||
size_t max_size = BUFSIZE - 32 - *msglen;
|
||||
if (m == 1)
|
||||
{
|
||||
/* Prepend */
|
||||
uint8_t *tmp = malloc_with_check(BUFSIZE);
|
||||
size_t s = rand_fill_protobuf(tmp, rand_len(max_size), BUFSIZE - *msglen, 512);
|
||||
memmove(buffer + s, buffer, *msglen);
|
||||
memcpy(buffer, tmp, s);
|
||||
free_with_check(tmp);
|
||||
*msglen += s;
|
||||
}
|
||||
else if (m == 2)
|
||||
{
|
||||
/* Append */
|
||||
size_t s = rand_fill_protobuf(buffer + *msglen, rand_len(max_size), BUFSIZE - *msglen, 512);
|
||||
*msglen += s;
|
||||
}
|
||||
}
|
||||
|
||||
static bool do_static_decode(uint8_t *buffer, size_t msglen, bool assert_success)
|
||||
{
|
||||
pb_istream_t stream;
|
||||
bool status;
|
||||
|
||||
alltypes_static_AllTypes *msg = malloc_with_check(sizeof(alltypes_static_AllTypes));
|
||||
rand_fill((uint8_t*)msg, sizeof(alltypes_static_AllTypes));
|
||||
stream = pb_istream_from_buffer(buffer, msglen);
|
||||
status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg);
|
||||
|
||||
if (!status && assert_success)
|
||||
{
|
||||
/* Anything that was successfully encoded, should be decodeable.
|
||||
* One exception: strings without null terminator are encoded up
|
||||
* to end of buffer, but refused on decode because the terminator
|
||||
* would not fit. */
|
||||
if (strcmp(stream.errmsg, "string overflow") != 0)
|
||||
assert(status);
|
||||
}
|
||||
|
||||
free_with_check(msg);
|
||||
return status;
|
||||
}
|
||||
|
||||
static bool do_pointer_decode(uint8_t *buffer, size_t msglen, bool assert_success)
|
||||
{
|
||||
pb_istream_t stream;
|
||||
bool status;
|
||||
alltypes_pointer_AllTypes *msg;
|
||||
|
||||
msg = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
|
||||
memset(msg, 0, sizeof(alltypes_pointer_AllTypes));
|
||||
stream = pb_istream_from_buffer(buffer, msglen);
|
||||
|
||||
assert(get_alloc_count() == 0);
|
||||
status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg);
|
||||
|
||||
if (assert_success)
|
||||
assert(status);
|
||||
|
||||
pb_release(alltypes_pointer_AllTypes_fields, msg);
|
||||
assert(get_alloc_count() == 0);
|
||||
|
||||
free_with_check(msg);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Do a decode -> encode -> decode -> encode roundtrip */
|
||||
static void do_static_roundtrip(uint8_t *buffer, size_t msglen)
|
||||
{
|
||||
bool status;
|
||||
uint8_t *buf2 = malloc_with_check(BUFSIZE);
|
||||
uint8_t *buf3 = malloc_with_check(BUFSIZE);
|
||||
size_t msglen2, msglen3;
|
||||
alltypes_static_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_static_AllTypes));
|
||||
alltypes_static_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_static_AllTypes));
|
||||
memset(msg1, 0, sizeof(alltypes_static_AllTypes));
|
||||
memset(msg2, 0, sizeof(alltypes_static_AllTypes));
|
||||
|
||||
{
|
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
|
||||
status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg1);
|
||||
assert(status);
|
||||
}
|
||||
|
||||
{
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
|
||||
status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg1);
|
||||
assert(status);
|
||||
msglen2 = stream.bytes_written;
|
||||
}
|
||||
|
||||
{
|
||||
pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
|
||||
status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg2);
|
||||
assert(status);
|
||||
}
|
||||
|
||||
{
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
|
||||
status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg2);
|
||||
assert(status);
|
||||
msglen3 = stream.bytes_written;
|
||||
}
|
||||
|
||||
assert(msglen2 == msglen3);
|
||||
assert(memcmp(buf2, buf3, msglen2) == 0);
|
||||
|
||||
free_with_check(msg1);
|
||||
free_with_check(msg2);
|
||||
free_with_check(buf2);
|
||||
free_with_check(buf3);
|
||||
}
|
||||
|
||||
/* Do decode -> encode -> decode -> encode roundtrip */
|
||||
static void do_pointer_roundtrip(uint8_t *buffer, size_t msglen)
|
||||
{
|
||||
bool status;
|
||||
uint8_t *buf2 = malloc_with_check(BUFSIZE);
|
||||
uint8_t *buf3 = malloc_with_check(BUFSIZE);
|
||||
size_t msglen2, msglen3;
|
||||
alltypes_pointer_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
|
||||
alltypes_pointer_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
|
||||
memset(msg1, 0, sizeof(alltypes_pointer_AllTypes));
|
||||
memset(msg2, 0, sizeof(alltypes_pointer_AllTypes));
|
||||
|
||||
{
|
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
|
||||
status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg1);
|
||||
assert(status);
|
||||
}
|
||||
|
||||
{
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
|
||||
status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg1);
|
||||
assert(status);
|
||||
msglen2 = stream.bytes_written;
|
||||
}
|
||||
|
||||
{
|
||||
pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
|
||||
status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg2);
|
||||
assert(status);
|
||||
}
|
||||
|
||||
{
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
|
||||
status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg2);
|
||||
assert(status);
|
||||
msglen3 = stream.bytes_written;
|
||||
}
|
||||
|
||||
assert(msglen2 == msglen3);
|
||||
assert(memcmp(buf2, buf3, msglen2) == 0);
|
||||
|
||||
pb_release(alltypes_pointer_AllTypes_fields, msg1);
|
||||
pb_release(alltypes_pointer_AllTypes_fields, msg2);
|
||||
free_with_check(msg1);
|
||||
free_with_check(msg2);
|
||||
free_with_check(buf2);
|
||||
free_with_check(buf3);
|
||||
}
|
||||
|
||||
static void run_iteration()
|
||||
{
|
||||
uint8_t *buffer = malloc_with_check(BUFSIZE);
|
||||
size_t msglen;
|
||||
bool status;
|
||||
|
||||
rand_fill(buffer, BUFSIZE);
|
||||
|
||||
if (do_static_encode(buffer, &msglen))
|
||||
{
|
||||
do_protobuf_noise(buffer, &msglen);
|
||||
|
||||
status = do_static_decode(buffer, msglen, true);
|
||||
|
||||
if (status)
|
||||
do_static_roundtrip(buffer, msglen);
|
||||
|
||||
status = do_pointer_decode(buffer, msglen, true);
|
||||
|
||||
if (status)
|
||||
do_pointer_roundtrip(buffer, msglen);
|
||||
|
||||
/* Apply randomness to the encoded data */
|
||||
while (rand_bool())
|
||||
rand_mess(buffer, BUFSIZE);
|
||||
|
||||
/* Apply randomness to encoded data length */
|
||||
if (rand_bool())
|
||||
msglen = rand_int(0, BUFSIZE);
|
||||
|
||||
status = do_static_decode(buffer, msglen, false);
|
||||
do_pointer_decode(buffer, msglen, status);
|
||||
|
||||
if (status)
|
||||
{
|
||||
do_static_roundtrip(buffer, msglen);
|
||||
do_pointer_roundtrip(buffer, msglen);
|
||||
}
|
||||
}
|
||||
|
||||
free_with_check(buffer);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
if (argc > 1)
|
||||
{
|
||||
random_seed = atol(argv[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
random_seed = time(NULL);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Random seed: %llu\n", (long long unsigned)random_seed);
|
||||
|
||||
for (i = 0; i < 10000; i++)
|
||||
{
|
||||
run_iteration();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
54
tests/fuzztest/malloc_wrappers.c
Normal file
54
tests/fuzztest/malloc_wrappers.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "malloc_wrappers.h"
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
static size_t alloc_count = 0;
|
||||
|
||||
/* Allocate memory and place check values before and after. */
|
||||
void* malloc_with_check(size_t size)
|
||||
{
|
||||
size_t size32 = (size + 3) / 4 + 3;
|
||||
uint32_t *buf = malloc(size32 * sizeof(uint32_t));
|
||||
buf[0] = size32;
|
||||
buf[1] = 0xDEADBEEF;
|
||||
buf[size32 - 1] = 0xBADBAD;
|
||||
return buf + 2;
|
||||
}
|
||||
|
||||
/* Free memory allocated with malloc_with_check() and do the checks. */
|
||||
void free_with_check(void *mem)
|
||||
{
|
||||
uint32_t *buf = (uint32_t*)mem - 2;
|
||||
assert(buf[1] == 0xDEADBEEF);
|
||||
assert(buf[buf[0] - 1] == 0xBADBAD);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/* Track memory usage */
|
||||
void* counting_realloc(void *ptr, size_t size)
|
||||
{
|
||||
/* Don't allocate crazy amounts of RAM when fuzzing */
|
||||
if (size > 1000000)
|
||||
return NULL;
|
||||
|
||||
if (!ptr && size)
|
||||
alloc_count++;
|
||||
|
||||
return realloc(ptr, size);
|
||||
}
|
||||
|
||||
void counting_free(void *ptr)
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
assert(alloc_count > 0);
|
||||
alloc_count--;
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
size_t get_alloc_count()
|
||||
{
|
||||
return alloc_count;
|
||||
}
|
||||
7
tests/fuzztest/malloc_wrappers.h
Normal file
7
tests/fuzztest/malloc_wrappers.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
void* malloc_with_check(size_t size);
|
||||
void free_with_check(void *mem);
|
||||
void* counting_realloc(void *ptr, size_t size);
|
||||
void counting_free(void *ptr);
|
||||
size_t get_alloc_count();
|
||||
12
tests/fuzztest/run_radamsa.sh
Executable file
12
tests/fuzztest/run_radamsa.sh
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
|
||||
TMP=`tempfile`
|
||||
|
||||
echo $TMP
|
||||
while true
|
||||
do
|
||||
radamsa sample_data/* > $TMP
|
||||
$1 < $TMP
|
||||
test $? -gt 127 && break
|
||||
done
|
||||
|
||||
BIN
tests/fuzztest/sample_data/sample1.pb
Normal file
BIN
tests/fuzztest/sample_data/sample1.pb
Normal file
Binary file not shown.
BIN
tests/fuzztest/sample_data/sample2.pb
Normal file
BIN
tests/fuzztest/sample_data/sample2.pb
Normal file
Binary file not shown.
Reference in New Issue
Block a user