Repository navigation
Fix -Z (low-memory) segfault: stack alignment + use-after-free race - #30
Open
doomfrawen wants to merge 2 commits into
Open
doomfrawen wants to merge 2 commits into
doomfrawen wants to merge 2 commits into
Conversation
…ib.c - src/nfc-utils.h/.c and src/windows.h include <err.h>, a BSD/glibc-only header not shipped by MinGW-w64, so the GNU-toolchain build (autoreconf + gcc, as opposed to the VS2019/clang-cl path this repo also supports) fails with 'err.h: No such file or directory'. windows.h doesn't call anything from it, so that include is simply dropped. nfc-utils.h's DBG/WARN/ERR macros do use warnx(), so on _WIN32 this adds a small static inline warnx() shim instead of the header, and drops the redundant second #include <err.h> in nfc-utils.c. - src/stdlib.c's setenv()/unsetenv() declare 'char *str[32]' (an array of 32 uninitialized pointers) and then do strcpy(*str, name) etc, which dereferences the first (uninitialized/garbage) pointer in that array and writes through it - undefined behavior, not just a compile-time type mismatch. Changed to 'char str[32]' (an actual 32-byte buffer) and dropped the erroneous dereferences.
-mstackrealign on the AVX/AVX2/AVX512 hardnested cores: MinGW-w64 only guarantees a 16-byte aligned stack, but code built for these targets can spill 32-byte YMM values to it. Without forced realignment that's a segfault. That alone didn't fix -Z (hard_low_memory) mode, though: it still crashed with a use-after-free, confirmed via gdb backtrace landing in count_bitarray_AND_AVX2 on a table check_for_BitFlipProperties_thread had just fetched via get_bitflip_data(). Root cause: -Z decompresses each bitflip property table on demand and frees it right after use via remove_bitflip_data(), but check_for_BitFlipProperties() spawns one thread per CPU that all walk the same bitflip index sequence over disjoint nonce ranges - so multiple threads can legitimately hold the same (odd_even, bitflip) table at once. One thread finishing and freeing it out from under another thread still reading it is a straight use-after-free. Fixed by reference-counting instead of freeing on every use: get_bitflip_data() takes a reference under the same lock that guards allocation, remove_bitflip_data() only frees once the count drops to zero. Also fixed both call sites in cmdhfmfhard.c to acquire the pointer once and reuse it, instead of calling get_bitflip_data() twice per use against one remove_bitflip_data() release, which would have left the refcount permanently inflated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Built and reproduced/fixed on a real MinGW-w64 + libnfc + PC/SC reader setup against a MIFARE Classic 1K card, both before (confirmed segfault via gdb backtrace) and after (confirmed clean `-Z` run) the fix.
Test plan