Skip to content

Fix -Z (low-memory) segfault: stack alignment + use-after-free race - #30

Open
doomfrawen wants to merge 2 commits into
nfc-tools:masterfrom
doomfrawen:fix/z-mode-avx2-crash
Open

doomfrawen wants to merge 2 commits into
nfc-tools:masterfrom
doomfrawen:fix/z-mode-avx2-crash

Conversation

@doomfrawen

Copy link
Copy Markdown

Summary

  • `-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 is a segfault.
  • `-Z` (hard_low_memory) mode still crashed after that fix, though, with a use-after-free: `check_for_BitFlipProperties()` spawns one worker 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 returned by `get_bitflip_data()` at once. The old code freed that shared table the instant one thread finished with it (`remove_bitflip_data()`), with nothing stopping another thread from still reading it. Confirmed via gdb backtrace landing in `count_bitarray_AND_AVX2` on a table that had just been freed out from under it.
  • Fixed with reference counting instead of free-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).

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

  • Reproduced the crash under gdb, confirmed use-after-free in `count_bitarray_AND_AVX2`
  • Rebuilt with the fix, confirmed `-Z -O .mfd` completes without segfaulting on real hardware
  • Clean build with no new warnings

…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.
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

Successfully merging this pull request may close these issues.

1 participant