429 Commits

Author SHA1 Message Date
Petteri Aimonen
ef422656a5 Fix oneof submessage initialization bug.
Update issue 149
Status: FixedInGit
2015-03-07 10:25:09 +02:00
Petteri Aimonen
5c16a116ec Better error messages for syntax errors in .options file 2015-02-26 18:16:25 +02:00
Petteri Aimonen
4a6580726e Fix generator error when long_names:false is combined with Oneofs.
Update issue 147
Status: FixedInGit
2015-02-26 17:33:36 +02:00
Petteri Aimonen
651e97456b Include libprotobuf in linux binary package.
Previously this got included by bbfreeze, but apparently no more.

Update issue 146
Status: FixedInGit
2015-02-22 15:28:26 +02:00
Petteri Aimonen
b836ac29dd Lower required CMake version in example 2015-02-13 18:57:46 +02:00
Petteri Aimonen
93364463ac Update cmake_simple example readme 2015-02-13 18:42:46 +02:00
Oliver Lee
7c00b90910 Add simple example built with CMake 2015-02-13 18:42:46 +02:00
Oliver Lee
02bd49bc93 Fix search for Python 2 with CMake
Do not assume that Python has already been found by CMake. Fix value of
CMake variable PYTHON_EXECUTABLE if Python 3 was found. Change minimum
supported Python version to 2.6.

This fixes a bug introduced by this commit:
d8d3b75e2e
2015-02-13 18:42:46 +02:00
Oliver Lee
d8d3b75e2e Updates for the CMake rule file.
1) Search explicitly for python2.7

In systems where python3 is default or in build cases where the user has
already searched for and found python3 in CMake, store the python3
executable and search for python2.7.

2) Generate nanopb core protobuf files with CMake

Generate python output files used in turn by the nanopb generator
script. This removes the requirement of manually calling 'make' in the
nanopb/generator/proto directory.

3) Use nanopb options file if it exists

Look for nanopb options file and use in protobuf source and header
generation if it exists. The options file must have the same name and
path as the proto file, excluding the extension.
2015-02-13 17:31:12 +02:00
Petteri Aimonen
25b92c5b4e Fix generator bug when oneof is first field in a message.
Added test case for the same.

Update issue 142
Status: FixedInGit
2015-01-27 17:47:25 +02:00
Petteri Aimonen
5aa8207ab1 Setting version to nanopb-0.3.3-dev 2015-01-24 17:40:42 +02:00
Petteri Aimonen
acd7291791 Publishing nanopb-0.3.2 nanopb-0.3.2 2015-01-24 17:33:01 +02:00
Petteri Aimonen
d32d04ba10 Fix encoded_size #defines for oneof messages.
The sizes are represented as EncodedSize() instances, which cause
max() operation to sort them by address instead of value. This caused
pretty much random item to be selected for the maximum.

Update issue 141
Status: FixedInGit
2015-01-23 21:29:29 +02:00
Petteri Aimonen
0286a0746a Update changelog 2015-01-15 19:34:49 +02:00
Petteri Aimonen
24a45b0a9c Fix clang compiler warning in intsizes unit test. 2015-01-15 19:18:48 +02:00
Petteri Aimonen
e1c50496d9 Release memory when overwriting oneof fields.
Update issue 131
Status: FixedInGit
2015-01-15 18:58:08 +02:00
Petteri Aimonen
f4b3a1c202 Add oneofs to AllTypes test case 2015-01-11 19:47:27 +02:00
Petteri Aimonen
d2e023e3e5 Bugfixes for oneof support.
Fixes crashes / memory leaks when using pointer type fields.
Also fixes initialization of which_oneof fields.
2015-01-11 19:46:15 +02:00
Petteri Aimonen
8d12fecc7e New generator options for oneofs: allow skipping or generating as normal 'optional' fields.
The behaviour with no_unions:true is the same as of nanopb 0.3.1 and earlier.
2015-01-11 19:45:16 +02:00
Petteri Aimonen
8ef0392231 Fix generator error with OneOfs 2015-01-07 18:59:44 +02:00
Petteri Aimonen
fa444be424 Allow using 8/16/32/64 as values in int_size setting 2015-01-05 22:32:34 +02:00
Petteri Aimonen
cc3c8732fd Actually make the protoc version check work 2015-01-04 20:20:40 +02:00
Petteri Aimonen
7135e2797a Only run oneof test when protoc >= 2.6 is available 2015-01-04 20:00:37 +02:00
Petteri Aimonen
77a71ceb6d Fix build failure 2015-01-04 19:48:09 +02:00
Petteri Aimonen
7713d43bc3 Implement support for oneofs (C unions).
Basic test included, should probably add an oneof to the AllTypes test also.

Update issue 131
Status: Started
2015-01-04 19:39:37 +02:00
Petteri Aimonen
a0f0440394 Detect too large varint values when decoding.
Because Issue #139 now allows limiting integer fields, it is good
to check the values received from other protobuf libraries against
the lower limits.
2015-01-04 12:17:24 +02:00
Petteri Aimonen
50c67ecec4 Add int_size option for generator.
This allows overriding the integer field types to e.g. uint8_t for
saving RAM.

Update issue 139
Status: FixedInGit
2015-01-04 12:02:15 +02:00
Petteri Aimonen
b0d31468da Change PB_RETURN_ERROR() macro to avoid compiler warnings.
Update issue 140
Status: FixedInGit
2015-01-03 10:59:19 +02:00
Petteri Aimonen
7be7c7769f Fix build failure due to missing dependency in SConscript 2014-12-27 00:37:59 +02:00
Petteri Aimonen
88b2efe047 Fix memory leaks with PB_ENABLE_MALLOC and certain submessage type combinations.
There was a memory leak when:

1) A statically allocated submessage or
2) an extension field submessage

contained

A) a pointer-type field or
B) a submessage that further contained a pointer-type field.

This was because pb_release() didn't recurse into non-pointer fields.

Update issue 138
Status: FixedInGit
2014-12-26 23:27:35 +02:00
Petteri Aimonen
5008830488 Initialize also extension fields to defaults in pb_decode().
This makes the behaviour more consistent with non-extension fields,
and also makes sure that all 'found' fields of extensions are initially
false.
2014-12-26 23:27:35 +02:00
Petteri Aimonen
58643217b1 Fix bug in backwards_compatibility test case.
The memset() filled also the extensions field, which was just
waiting for a crash to happen.
2014-12-26 23:27:34 +02:00
Petteri Aimonen
1515cfb5c2 Add testcase for releasing memory in submessages/extensions 2014-12-26 23:27:29 +02:00
Petteri Aimonen
418f7d88b3 Add support for POINTER type in extensions 2014-12-26 18:23:36 +02:00
Petteri Aimonen
980f899dd5 Include the field type in a comment for extension fields 2014-12-26 17:43:38 +02:00
Petteri Aimonen
0f3c0f79bc Move malloc support to tests/common directory 2014-12-26 17:34:45 +02:00
Petteri Aimonen
8a28b70351 Move malloc_wrappers.c to tests/common 2014-12-26 17:08:17 +02:00
Petteri Aimonen
e5cbee84e1 Verify build with protobuf-3.0.0, fix problems.
Also updated descriptor.proto from protobuf-3.0.0.
2014-12-22 22:52:36 +02:00
Petteri Aimonen
cfc517f36b Add compilation option to disable struct packing.
Update issue 136
Status: FixedInGit
2014-12-22 20:52:40 +02:00
Petteri Aimonen
baf44b367f Set version to nanopb-0.3.2-dev 2014-09-16 20:41:45 +03:00
Petteri Aimonen
b947dc6e2c Publishing nanopb-0.3.1 nanopb-0.3.1 2014-09-11 19:36:14 +03:00
Petteri Aimonen
8d7deb4952 Update changelog 2014-09-11 19:26:32 +03:00
Petteri Aimonen
07e9ffb97b Add a fuzz testing stub for ability to use external generators also 2014-09-11 19:22:57 +03:00
Petteri Aimonen
d2099cc8f1 Protect against size_t overflows in pb_dec_bytes/pb_dec_string.
Possible consequences of bug:
1) Denial of service by causing a crash
   Possible when all of the following apply:
      - Untrusted data is passed to pb_decode()
      - The top-level message contains a static string field as the first field.
   Causes a single write of '0' byte to 1 byte before the message struct.

2) Remote code execution
   Possible when all of the following apply:
      - 64-bit platform
      - The message or a submessage contains a static/pointer string field.
      - Decoding directly from a custom pb_istream_t
      - bytes_left on the stream is set to larger than 4 GB
   Causes a write of up to 4 GB of data past the string field.

3) Possible heap corruption or remote code execution
   Possible when all of the following apply:
      - less than 64-bit platform
      - The message or a submessage contains a pointer-type bytes field.
   Causes a write of sizeof(pb_size_t) bytes of data past a 0-byte long
   malloc()ed buffer. On many malloc() implementations, this causes at
   most a crash. However, remote code execution through a controlled jump
   cannot be ruled out.

--

Detailed analysis follows

In the following consideration, I define "platform bitness" as equal to
number of bits in size_t datatype. Therefore most 8-bit platforms are
regarded as 16-bit for the purposes of this discussion.

1. The overflow in pb_dec_string

The overflow happens in this computation:

uint32_t size;
size_t alloc_size;
alloc_size = size + 1;

There are two ways in which the overflow can occur: In the uint32_t
addition, or in the cast to size_t. This depends on the platform
bitness.

On 32- and 64-bit platforms, the size has to be UINT32_MAX for the
overflow to occur. In that case alloc_size will be 0.

On 16-bit platforms, overflow will happen whenever size is more than
UINT16_MAX, and resulting alloc_size is attacker controlled.

For static fields, the alloc_size value is just checked against the
field data size. For pointer fields, the alloc_size value is passed to
malloc(). End result in both cases is the same, the storage is 0 or
just a few bytes in length.

On 16-bit platforms, another overflow occurs in the call to pb_read(),
when passing the original size. An attacker will want the passed value
to be larger than the alloc_size, therefore the only reasonable choice
is to have size = UINT16_MAX and alloc_size = 0. Any larger multiple
will truncate to the same values.

At this point we have read atleast the tag and the string length of the
message, i.e. atleast 3 bytes. The maximum initial value for stream
bytes_left is SIZE_MAX, thus at this point at most SIZE_MAX-3 bytes are
remaining.

On 32-bit and 16-bit platforms this means that the size passed to
pb_read() is always larger than the number of remaining bytes. This
causes pb_read() to fail immediately, before reading any bytes.

On 64-bit platforms, it is possible for the bytes_left value to be set
to a value larger than UINT32_MAX, which is the wraparound point in
size calculation. In this case pb_read() will succeed and write up to 4
GB of attacker controlled data over the RAM that comes after the string
field.

On all platforms, there is an unconditional write of a terminating null
byte. Because the size of size_t typically reflects the size of the
processor address space, a write at UINT16_MAX or UINT32_MAX bytes
after the string field actually wraps back to before the string field.
Consequently, on 32-bit and 16-bit platforms, the bug causes a single
write of '0' byte at one byte before the string field.

If the string field is in the middle of a message, this will just
corrupt other data in the message struct. Because the message contents
is attacker controlled anyway, this is a non-issue. However, if the
string field is the first field in the top-level message, it can
corrupt other data on the stack/heap before it. Typically a single '0'
write at a location not controlled by attacker is enough only for a
denial-of-service attack.

When using pointer fields and malloc(), the attacker controlled
alloc_size will cause a 0-size allocation to happen. By the same logic
as before, on 32-bit and 16-bit platforms this causes a '0' byte write
only. On 64-bit platforms, however, it will again allow up to 4 GB of
malicious data to be written over memory, if the stream length allows
the read.

2. The overflow in pb_dec_bytes

This overflow happens in the PB_BYTES_ARRAY_T_ALLOCSIZE macro:

The computation is done in size_t data type this time. This means that
an overflow is possible only when n is larger than SIZE_MAX -
offsetof(..). The offsetof value in this case is equal to
sizeof(pb_size_t) bytes.

Because the incoming size value is limited to 32 bits, no overflow can
happen here on 64-bit platforms.

The size will be passed to pb_read(). Like before, on 32-bit and 16-bit
platforms the read will always fail before writing anything.

This leaves only the write of bdest->size as exploitable. On statically
allocated fields, the size field will always be allocated, regardless
of alloc_size. In this case, no buffer overflow is possible here, but
user code could possibly use the attacker controlled size value and
read past a buffer.

If the field is allocated through malloc(), this will allow a write of
sizeof(pb_size_t) attacker controlled bytes to past a 0-byte long
buffer. In typical malloc implementations, this will either fit in
unused alignment padding area, or cause a heap corruption and a crash.
Under very exceptional situation it could allow attacker to influence
the behaviour of malloc(), possibly jumping into an attacker-controlled
location and thus leading to remote code execution.
2014-09-11 19:22:57 +03:00
Petteri Aimonen
d0466bdf43 Add just-to-be-sure check to allocate_field().
This check will help to detect bugs earlier, and is quite lightweight
compared to malloc() anyway.
2014-09-11 19:22:57 +03:00
Petteri Aimonen
5e3edb5415 Fix memory leak with duplicated fields and PB_ENABLE_MALLOC.
If a required or optional field appeared twice in the message data,
pb_decode will overwrite the old data with new one. That is fine, but
with submessage fields, it didn't release the allocated subfields before
overwriting.

This bug can manifest if all of the following conditions are true:

1. There is a message with a "optional" or "required" submessage field
   that has type:FT_POINTER.

2. The submessage contains atleast one field with type:FT_POINTER.

3. The message data to be decoded has the submessage field twice in it.
2014-09-11 19:22:57 +03:00
Petteri Aimonen
13a07e35b6 Fix crash in pb_release() if called twice on same message.
There was a double-free bug in pb_release() because it didn't set size fields
to zero after deallocation. Most commonly this happens if pb_decode() fails,
internally calls pb_release() and then application code also calls pb_release().
2014-09-11 19:22:57 +03:00
Petteri Aimonen
0dce9ef635 Add a better fuzz test.
Attempts to verify all the properties defined in the security model,
while also being portable and able to run on many platforms.
2014-09-11 19:22:57 +03:00
Petteri Aimonen
8189d538dd Add test case for simulated io errors.
Update issue 126
Status: FixedInGit
2014-09-07 20:31:36 +03:00
Petteri Aimonen
38613acdb4 Add a few missing unit tests 2014-09-07 20:30:17 +03:00