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

Segmentation fault on freeing memory allocated by strdup #17

Closed
raju2b opened this issue Oct 4, 2019 · 1 comment
Closed

Segmentation fault on freeing memory allocated by strdup #17

raju2b opened this issue Oct 4, 2019 · 1 comment

Comments

@raju2b
Copy link

raju2b commented Oct 4, 2019

While checking memory leaks, buffer overflows using test_free, test_malloc, etc getting segmentation fault in the following case.

  • Mocked Dynamic Memory handling functions like below
#ifdef UNITTEST
    extern void* _test_malloc(const size_t size, const char* file, const int line);
    extern void* _test_calloc(size_t nmemb, const size_t size, const char* file, const int line);
    extern void* _test_realloc(void *ptr, const size_t size, const char* file, const int line);
    extern void _test_free(void *ptr, const char* file, const int line);

    #define malloc(size) _test_malloc(size, __FILE__, __LINE__)
    #define calloc(nmemb,size) _test_calloc(nmemb, size, __FILE__, __LINE__)
    #define realloc(ptr,size) _test_realloc(ptr, size, __FILE__, __LINE__)
    #define free(ptr) _test_free(ptr, __FILE__, __LINE__)
#endif
  • Segmentation fault is thrown in the code when test_free tried to deallocate the memory allocated by strdup.
  • Below is the Call stack when run through GDB
#0  strlen () at ../sysdeps/i386/strlen.S:35
#1  0xf7bc3166 in _IO_vfprintf_internal (s=0xffffc7d0, format=0xf7d2c21e "%s:%u: error: Guard block of %p size=%lu is corrupt\n%s:%u: note: allocated here at %p\n", ap=0xffffcd58 "e-4c") at vfprintf.c:1643
#2  0xf7be5baf in _IO_vsnprintf (string=0xffffc8fc "src/config.c:49: error: Guard block of 0x8063010 size=943077171 is corrupt\n", maxlen=<optimized out>,
    format=0xf7d2c21e "%s:%u: error: Guard block of %p size=%lu is corrupt\n%s:%u: note: allocated here at %p\n", args=0xffffcd44 "\377\204\005\b1") at vsnprintf.c:114
#3  0xf7d28421 in vcm_print_error (format=0xf7d2c21e "%s:%u: error: Guard block of %p size=%lu is corrupt\n%s:%u: note: allocated here at %p\n", args=0xffffcd44 "\377\204\005\b1")
    at /home/sowri/cmocka-1.0.1_32/src/cmocka.c:1535
#4  0xf7d28957 in cm_print_error (format=0xf7d2c21e "%s:%u: error: Guard block of %p size=%lu is corrupt\n%s:%u: note: allocated here at %p\n")
    at /home/sowri/cmocka-1.0.1_32/src/cmocka.c:1825
#5  0xf7d2a75b in _test_free (ptr=0x8063010, file=0x80584ff "src/config.c", line=49) at /home/sowri/cmocka-1.0.1_32/src/cmocka.c:1643
#6  0x0804a2b3 in config_reset (cfg=0x8060e20 <g_config>) at src/config.c:49
#7  0x0804cd0c in config_load (config_file=0x8058feb "/cfg/fwcm-updaterd.conf") at src/config.c:352
#8  0x0804f9e4 in test_config_load (state=0x80621a0) at unittest/config_ut.c:323
#9  0xf7d2aa9f in cmocka_run_one_test_or_fixture (function_name=0x805948b "test_config_load", test_func=0x804ee80 <test_config_load>, setup_func=setup_func@entry=0x0, teardown_func=0x0, state=<optimized out>, heap_check_point=0x0)
    at /home/sowri/omc/cmocka-1.0.1_32/src/cmocka.c:2305
#10 0xf7d2b2c3 in cmocka_run_one_tests (test_state=0x8062198) at /home/sowri/omc/cmocka-1.0.1_32/src/cmocka.c:2413
#11 _cmocka_run_group_tests (group_name=0x805949c "tests", tests=0xffffd040, num_tests=3, group_setup=0x0, group_teardown=0x0) at /home/sowri//cmocka-1.0.1_32/src/cmocka.c:2518
#12 0x08052818 in main () at unittest/config_ut.c:85

But when i have replaced strdup with custom strdup to do the same, its working perfectly, No SEG-FAULT been observed. It seems Cmocka is not tracking the memory block allocated by the strdup.

Work-Around:

  • I can mock strdup with Custom implementation while running the Test case but looking for straight Forward solution.

Is there any mocking i missed or is it not yet supported by Cmocka ?

@MarkSymsCtx
Copy link

Just hit this issue myself. I sort of understand why it happens (as the allocation is done inside a separate shared library) but it doesn't make it any less irritating.

coreboot-org-bot pushed a commit to flashrom/flashrom that referenced this issue Jul 19, 2021
This is a known issue in cmocka (see
clibs/cmocka#17) where cmocka does not
recognise memory allocation happening inside strdup, and then later
throws an error when the memory is freed. If the issue is fixed at
some point, this workaround can be removed.

Given that cmocka already overrides malloc, calloc, realloc, free,
adding strdup there seems fine.

Existing tests now can (and have to) free the memory they allocated
by strdup, and this is in the same patch.

BUG=b:193584590
TEST=ninja test

Change-Id: I56aef6b342752d80995c36ab075b12198fc101d9
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/56323
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
@jwerle jwerle closed this as completed in 6827e25 Sep 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants