Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Add unsigned int sanitization #2139

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -709,16 +709,17 @@ if(UBSAN)
if(NOT CLANG)
message(FATAL_ERROR "Cannot enable UBSAN unless using Clang")
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")

# https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#id6
# -fsanitize=undefined includes various other checks but not unsigned-integer-overflow.
# fsanitize-undefined-ignore-overflow-pattern ignores common overflow patterns that are intentional.
set(SANITIZER_FLAGS "-fsanitize=undefined,unsigned-integer-overflow")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add a comment about why unsigned-integer-overflow is listed (and not other options)? We might also reference the clang documentation.

I was going to ask why signed-integer-overflow was not included then saw that it's covered by -fsanitize=undefined:

-fsanitize=undefined: All of the checks listed above other than float-divide-by-zero, unsigned-integer-overflow, implicit-conversion, local-bounds and the nullability-* group of checks.

if(NOT UBSAN_RECOVER)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=undefined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=undefined")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-sanitize-recover=undefined")
set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fno-sanitize-recover=undefined,unsigned-integer-overflow")
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZER_FLAGS}")
endif()

if(GCOV)
Expand Down
1 change: 1 addition & 0 deletions crypto/base64/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

// constant_time_lt_args_8 behaves like |constant_time_lt_8| but takes |uint8_t|
// arguments for a slightly simpler implementation.
SUPPRESS_UNSIGNED_OVERFLOW
static inline uint8_t constant_time_lt_args_8(uint8_t a, uint8_t b) {
crypto_word_t aw = a;
crypto_word_t bw = b;
Expand Down
1 change: 1 addition & 0 deletions crypto/bytestring/cbb.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ int CBB_did_write(CBB *cbb, size_t len) {
return 1;
}

SUPPRESS_UNSIGNED_OVERFLOW
static int cbb_add_u(CBB *cbb, uint64_t v, size_t len_len) {
uint8_t *buf;
if (!CBB_add_space(cbb, &buf, len_len)) {
Expand Down
6 changes: 6 additions & 0 deletions crypto/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ typedef __uint128_t uint128_t;
#define OPENSSL_HAS_BUILTIN(x) 0
#endif

#if defined(__clang__)
#define SUPPRESS_UNSIGNED_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
#else
#define SUPPRESS_UNSIGNED_OVERFLOW
#endif

// Pointer utility functions.

Expand Down Expand Up @@ -339,6 +344,7 @@ static inline uint64_t value_barrier_u64(uint64_t a) {

// constant_time_msb_w returns the given value with the MSB copied to all the
// other bits.
SUPPRESS_UNSIGNED_OVERFLOW
static inline crypto_word_t constant_time_msb_w(crypto_word_t a) {
return 0u - (a >> (sizeof(a) * 8 - 1));
}
Expand Down
6 changes: 6 additions & 0 deletions ssl/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3720,6 +3720,12 @@ struct ssl_method_st {
const bssl::SSL_X509_METHOD *x509_method;
};

#if defined(__clang__)
#define SUPPRESS_UNSIGNED_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
#else
#define SUPPRESS_UNSIGNED_OVERFLOW
#endif

#define MIN_SAFE_FRAGMENT_SIZE 512
struct ssl_ctx_st : public bssl::RefCounted<ssl_ctx_st> {
explicit ssl_ctx_st(const SSL_METHOD *ssl_method);
Expand Down
1 change: 1 addition & 0 deletions ssl/ssl_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void SSLBuffer::Clear() {
buf_size_ = 0;
}

SUPPRESS_UNSIGNED_OVERFLOW
bool SSLBuffer::EnsureCap(size_t header_len, size_t new_cap) {
if (new_cap > SSLBUFFER_MAX_CAPACITY) {
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Expand Down
Loading