| 1 | /* |
| 2 | * Regression harness for the hashtable size*node_size integer overflow |
| 3 | * (Leonid Bugaev May-2026 re-audit, KI-11/12). |
| 4 | * |
| 5 | * hashtable_create() once computed the slot-array byte count as |
| 6 | * new_array0(char, size * node_size) in 32-bit int arithmetic; for a large |
| 7 | * peer/data-driven size the product wrapped to a tiny value, under-allocating |
| 8 | * the table while tbl->size recorded the huge size -- a later node access then |
| 9 | * ran out of bounds (heap overflow / SEGV). The fix passes size and node_size |
| 10 | * as separate factors so my_alloc's --max-alloc guard rejects the oversized |
| 11 | * request, exiting RERR_MALLOC instead of under-allocating. |
| 12 | * |
| 13 | * This harness sets a realistic max_alloc (t_stub leaves it at SIZE_MAX) and |
| 14 | * asks for an absurd size: the fixed code exits RERR_MALLOC; a regressed build |
| 15 | * under-allocates and crashes on the node access below. Not linked into rsync. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify it under |
| 18 | * the terms of the GNU General Public License version 3 as published by the |
| 19 | * Free Software Foundation. |
| 20 | */ |
| 21 | |
| 22 | #include "rsync.h" |
| 23 | |
| 24 | extern size_t max_alloc; /* defined in util2.o/t_stub.o */ |
| 25 | short info_levels[COUNT_INFO], debug_levels[COUNT_DEBUG]; /* for DEBUG_GTE in hashtable.o */ |
| 26 | |
| 27 | int main(UNUSED(int argc), UNUSED(char *argv[])) |
| 28 | { |
| 29 | struct hashtable *tbl; |
| 30 | int i; |
| 31 | |
| 32 | /* A realistic --max-alloc cap (the default is 1 GiB) so my_alloc's guard |
| 33 | * can engage; t_stub.o leaves max_alloc == SIZE_MAX. */ |
| 34 | max_alloc = (size_t)1024 * 1024 * 1024; |
| 35 | |
| 36 | /* 2^28 buckets * 16-byte node = 2^32 bytes: the product wraps int to ~0 in |
| 37 | * the unfixed code. The fix must reject this (exit RERR_MALLOC) rather than |
| 38 | * under-allocate. */ |
| 39 | tbl = hashtable_create(1 << 28, 0); |
| 40 | |
| 41 | /* Unreachable with the fix (hashtable_create exits above). If a regression |
| 42 | * lets it return, touch a node near the claimed end -- an under-allocated |
| 43 | * table faults here -- and report the unexpected survival as a failure. */ |
| 44 | for (i = 0; i < tbl->size; i += tbl->size / 64 + 1) { |
| 45 | struct ht_int32_node *node = HT_NODE(tbl, tbl->nodes, i); |
| 46 | node->key = i; |
| 47 | } |
| 48 | fprintf(stderr, "FAIL: hashtable_create(1<<28) was not rejected\n"); |
| 49 | return 1; |
| 50 | } |