Skip to content

Commit

Permalink
Fix BER decoder integer overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Nov 28, 2016
2 parents 7140635 + 06a9334 commit 987ad74
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
11 changes: 10 additions & 1 deletion doc/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ Advisories
2016
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* 2016-10-8871 (CVE-2016-8871) OAEP side channel
* 2016-11-27 (CVE-2016-xxxx) Integer overflow in BER decoder

While decoding BER length fields, an integer overflow could occur. This could
occur while parsing untrusted inputs such as X.509 certificates. The overflow
does not seem to lead to any obviously exploitable condition, but exploitation
cannot be positively ruled out. Only 32-bit platforms are likely affected; to
cause an overflow on 64-bit the parsed data would have to be many gigabytes.
Bug found by Falko Strenzke, cryptosource GmbH.

* 2016-10-26 (CVE-2016-8871) OAEP side channel

A side channel in OAEP decoding could be used to distinguish RSA ciphertexts
that did or did not have a leading 0 byte. For an attacker capable of
Expand Down
5 changes: 4 additions & 1 deletion src/lib/asn1/ber_dec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <botan/ber_dec.h>
#include <botan/bigint.h>
#include <botan/loadstor.h>
#include <botan/internal/safeint.h>

namespace Botan {

Expand Down Expand Up @@ -126,7 +127,9 @@ size_t find_eoc(DataSource* ber)
size_t item_size = decode_length(&source, length_size);
source.discard_next(item_size);

length += item_size + length_size + tag_size;
length = BOTAN_CHECKED_ADD(length, item_size);
length = BOTAN_CHECKED_ADD(length, tag_size);
length = BOTAN_CHECKED_ADD(length, length_size);

if(type_tag == EOC && class_tag == UNIVERSAL)
break;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/info.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define UTIL_FUNCTIONS 20150919
define UTIL_FUNCTIONS 20161127

load_on always

Expand Down Expand Up @@ -31,6 +31,7 @@ filesystem.h
os_utils.h
prefetch.h
rounding.h
safeint.h
semaphore.h
stl_util.h
</header:internal>
Expand Down
39 changes: 39 additions & 0 deletions src/lib/utils/safeint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Safe(r) Integer Handling
* (C) 2016 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_UTILS_SAFE_INT_H__
#define BOTAN_UTILS_SAFE_INT_H__

#include <botan/exceptn.h>
#include <string>

namespace Botan {

class Integer_Overflow_Detected : public Exception
{
public:
Integer_Overflow_Detected(const std::string& file, int line) :
Exception("Integer overflow detected at " + file + ":" + std::to_string(line))
{}
};

inline size_t checked_add(size_t x, size_t y, const char* file, int line)
{
// TODO: use __builtin_x_overflow on GCC and Clang
size_t z = x + y;
if(z < x)
{
throw Integer_Overflow_Detected(file, line);
}
return z;
}

#define BOTAN_CHECKED_ADD(x,y) checked_add(x,y,__FILE__,__LINE__)

}

#endif

0 comments on commit 987ad74

Please sign in to comment.