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

Havege does arithmetic on signed int #2598

Closed
gilles-peskine-arm opened this issue Apr 24, 2019 · 1 comment · Fixed by ARMmbed/mbed-crypto#149 or #2699
Closed

Havege does arithmetic on signed int #2598

gilles-peskine-arm opened this issue Apr 24, 2019 · 1 comment · Fixed by ARMmbed/mbed-crypto#149 or #2699
Labels
bug component-crypto Crypto primitives and low-level interfaces component-platform Portability layer and build scripts

Comments

@gilles-peskine-arm
Copy link
Contributor

gilles-peskine-arm commented Apr 24, 2019

Description

  • Type: Bug
  • Priority: Major

Runtime failure with Asan

  1. Check out https://github.com/ARMmbed/mbedtls at f790a6c or https://github.com/ARMmbed/mbedtls/tree/mbedtls-2.16 at 20d707d or https://github.com/ARMmbed/mbed-crypto at 521dbc6.
  2. Set the following configuration:
    scripts/config.pl full; scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE
    or
    scripts/config.pl full; scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE; scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C; scripts/config.pl unset MBEDTLS_MEMORY_DEBUG
  3. Build with clang and ASan (I used clang 3.8.0 Ubuntu 16.04). I've reproduced it with both in-tree and out-of-tree builds, both with Asan and AsanDbg.
    mkdir build-asan
    cd build-asan
    CC=clang cmake -D CMAKE_BUILD_TYPE:String=AsanDbg .
    make -j3
    cd tests
    ./test_suite_entropy
    
  4. Expected: the tests pass. Actual: ASan runtime errors like the following (the exact values change at every run):
    /tmp/mbedtls$ ./test_suite_entropy
    Create NV seed_file ............................................... PASS
    Entropy write/update seed file .................................... /tmp/mbedtls/library/havege.c:179:9: runtime error: left shift of 1813511924 by 23 places cannot be represented in type 'int'
    SUMMARY: AddressSanitizer: undefined-behavior /tmp/mbedtls/library/havege.c:179:9 in 
    /tmp/mbedtls$ ./test_suite_entropy
    Create NV seed_file ............................................... PASS
    Entropy write/update seed file .................................... /tmp/mbedtls/library/havege.c:179:9: runtime error: left shift of negative value -1080004887
    SUMMARY: AddressSanitizer: undefined-behavior /tmp/mbedtls/library/havege.c:179:9 in 
    

Note that I'm getting Asan complaints only with Clang, not with GCC. We run this test (in the configuration full minus MBEDTLS_MEMORY_BACKTRACE) on CI, but only with GCC, not with Clang, which is why we didn't notice sooner. I'm still surprised we never noticed in developer builds, though. I often test with clang+asan! So there may still be some extra condition needed to reproduce the complaints.

Analysis of havege.c

havege.c does indeed perform bit shifts on int that may cause an overflow. A shift that overflows a signed integer has undefined behavior in C. Asan's complaints are legitimate.

@gilles-peskine-arm gilles-peskine-arm added bug component-crypto Crypto primitives and low-level interfaces component-platform Portability layer and build scripts labels Apr 24, 2019
@gilles-peskine-arm
Copy link
Contributor Author

gilles-peskine-arm commented Apr 24, 2019

Regardless of this potential bug in havege.c, I would recommend keeping MBEDTLS_HAVEGE_C disabled in most if not all environments. (It is disabled by default.) HAVEGE as a library is not very useful. But that doesn't excuse the undefined behavior, nor the strange way semi-reproducibility of ASan's warnings.

gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jun 7, 2019
Update havege.h to the new version in the crypto module.

This is technically an API break, since the type mbedtls_havege_state
is exposed in a public header. However normal applications should not
be affected.

Fix Mbed-TLS#2598
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jun 7, 2019
Update havege.h to the new version in the crypto module.

This is technically an API break, since the type mbedtls_havege_state
is exposed in a public header. However normal applications should not
be affected.

Fix Mbed-TLS#2598
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jun 7, 2019
Update havege.h to the new version in the crypto module.

This is technically an API break, since the type mbedtls_havege_state
is exposed in a public header. However normal applications should not
be affected.

Fix Mbed-TLS#2598
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jun 14, 2019
Update havege.h to the new version in the crypto module.

This is technically an API break, since the type mbedtls_havege_state
is exposed in a public header. However normal applications should not
be affected.

There is no ABI break on platforms where uint32_t and int are treated
identically, which is virtually all of them.

Fix Mbed-TLS#2598
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug component-crypto Crypto primitives and low-level interfaces component-platform Portability layer and build scripts
Projects
None yet
1 participant