Skip to content

Buffer overflow on xstrdup()

While running tests, we came across the following error when compiling with the -fsanitize=address flags:

=================================================================
==8744==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x607000004583 at pc 0x7f6718295d4f bp 0x7ffec58dee90 sp 0x7ffec58dee80
WRITE of size 1 at 0x607000004583 thread T0
    #0 0x7f6718295d4e in xstrdup (/builds/project-0/build/lib/.libs/libnorns_debug.so.0+0x13d4e)
    #1 0x7f6718291f34 in parse_config_file (/builds/project-0/build/lib/.libs/libnorns_debug.so.0+0xff34)
    #2 0x7f67182906af in load_config_file (/builds/project-0/build/lib/.libs/libnorns_debug.so.0+0xe6af)
    #3 0x7f6718290a37 in libnorns_reload_config_file (/builds/project-0/build/lib/.libs/libnorns_debug.so.0+0xea37)
    #4 0x7f671ac8064e in (anonymous namespace)::patch_libraries(boost::filesystem::path const&) (/builds/project-0/build/tests/.libs/api+0x3b664e)
    #5 0x7f671ac820d1 in test_env::test_env(fake_daemon_cfg const&) (/builds/project-0/build/tests/.libs/api+0x3b80d1)
    #6 0x7f671ac33bbc in ____C_A_T_C_H____T_E_S_T____0() (/builds/project-0/build/tests/.libs/api+0x369bbc)
    #7 0x7f671a9c883c in Catch::TestInvokerAsFunction::invoke() const (/builds/project-0/build/tests/.libs/api+0xfe83c)
    #8 0x7f671a9c7215 in Catch::TestCase::invoke() const (/builds/project-0/build/tests/.libs/api+0xfd215)
    #9 0x7f671a9ba642 in Catch::RunContext::invokeActiveTestCase() (/builds/project-0/build/tests/.libs/api+0xf0642)
    #10 0x7f671a9b9e8a in Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (/builds/project-0/build/tests/.libs/api+0xefe8a)
    #11 0x7f671a9b6945 in Catch::RunContext::runTest(Catch::TestCase const&) (/builds/project-0/build/tests/.libs/api+0xec945)
    #12 0x7f671a9be1df in Catch::(anonymous namespace)::runTests(std::shared_ptr<Catch::Config> const&) (/builds/project-0/build/tests/.libs/api+0xf41df)
    #13 0x7f671a9c0a07 in Catch::Session::runInternal() (/builds/project-0/build/tests/.libs/api+0xf6a07)
    #14 0x7f671a9c03cd in Catch::Session::run() (/builds/project-0/build/tests/.libs/api+0xf63cd)
    #15 0x7f671a9c0331 in Catch::Session::run(int, char**) (/builds/project-0/build/tests/.libs/api+0xf6331)
    #16 0x7f671a9f2573 in main (/builds/project-0/build/tests/.libs/api+0x128573)
    #17 0x7f6717348b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #18 0x7f671a996e79 in _start (/builds/project-0/build/tests/.libs/api+0xcce79)

0x607000004583 is located 0 bytes to the right of 67-byte region [0x607000004540,0x607000004583)
allocated by thread T0 here:
    #0 0x7f67197c3d38 in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xded38)
    #1 0x7f6718295bef in norns_xmalloc (/builds/project-0/build/lib/.libs/libnorns_debug.so.0+0x13bef)
    #2 0x7f6718295cf3 in xstrdup (/builds/project-0/build/lib/.libs/libnorns_debug.so.0+0x13cf3)
    #3 0x7f6718291f34 in parse_config_file (/builds/project-0/build/lib/.libs/libnorns_debug.so.0+0xff34)
    #4 0x7f67182906af in load_config_file (/builds/project-0/build/lib/.libs/libnorns_debug.so.0+0xe6af)
    #5 0x7f6718290a37 in libnorns_reload_config_file (/builds/project-0/build/lib/.libs/libnorns_debug.so.0+0xea37)
    #6 0x7f671ac8064e in (anonymous namespace)::patch_libraries(boost::filesystem::path const&) (/builds/project-0/build/tests/.libs/api+0x3b664e)
    #7 0x7f671ac820d1 in test_env::test_env(fake_daemon_cfg const&) (/builds/project-0/build/tests/.libs/api+0x3b80d1)
    #8 0x7f671ac33bbc in ____C_A_T_C_H____T_E_S_T____0() (/builds/project-0/build/tests/.libs/api+0x369bbc)
    #9 0x7f671a9c883c in Catch::TestInvokerAsFunction::invoke() const (/builds/project-0/build/tests/.libs/api+0xfe83c)
    #10 0x7f671a9c7215 in Catch::TestCase::invoke() const (/builds/project-0/build/tests/.libs/api+0xfd215)
    #11 0x7f671a9ba642 in Catch::RunContext::invokeActiveTestCase() (/builds/project-0/build/tests/.libs/api+0xf0642)
    #12 0x7f671a9b9e8a in Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (/builds/project-0/build/tests/.libs/api+0xefe8a)
    #13 0x7f671a9b6945 in Catch::RunContext::runTest(Catch::TestCase const&) (/builds/project-0/build/tests/.libs/api+0xec945)
    #14 0x7f671a9be1df in Catch::(anonymous namespace)::runTests(std::shared_ptr<Catch::Config> const&) (/builds/project-0/build/tests/.libs/api+0xf41df)
    #15 0x7f671a9c0a07 in Catch::Session::runInternal() (/builds/project-0/build/tests/.libs/api+0xf6a07)
    #16 0x7f671a9c03cd in Catch::Session::run() (/builds/project-0/build/tests/.libs/api+0xf63cd)
    #17 0x7f671a9c0331 in Catch::Session::run(int, char**) (/builds/project-0/build/tests/.libs/api+0xf6331)
    #18 0x7f671a9f2573 in main (/builds/project-0/build/tests/.libs/api+0x128573)
    #19 0x7f6717348b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)

SUMMARY: AddressSanitizer: heap-buffer-overflow (/builds/project-0/build/lib/.libs/libnorns_debug.so.0+0x13d4e) in xstrdup
Shadow bytes around the buggy address:
  0x0c0e7fff8860: fa fa fd fd fd fd fd fd fd fd fd fd fa fa fa fa
  0x0c0e7fff8870: fd fd fd fd fd fd fd fd fd fd fa fa fa fa 00 00
  0x0c0e7fff8880: 00 00 00 00 00 00 00 fa fa fa fa fa fd fd fd fd
  0x0c0e7fff8890: fd fd fd fd fd fa fa fa fa fa 00 00 00 00 00 00
  0x0c0e7fff88a0: 00 00 00 07 fa fa fa fa 00 00 00 00 00 00 00 00
=>0x0c0e7fff88b0:[03]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff88c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff88d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff88e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff88f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff8900: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==8744==ABORTING

The reason is the access to res[sz] in the following code (lib/xstring.c):

    sz = XMIN(strlen(str) + 1, XMAX_STRING_LENGTH);
    res = (char*) xmalloc(sz);
    strncpy(res, str, sz);
    res[sz] = '\0'; // ERROR: this writes out of the allocated buffer

Changing it to res[sz-1] fixes the problem.