Commit 269ac43
Bound recursion depth in index deserialization (CWE-674) (#5468)
Summary:
Pull Request resolved: #5468
Agentic fuzzing (T278233426) found an uncontrolled-recursion stack
overflow in `faiss::read_index_binary_up`. The serialized index format is
self-describing and recursively nestable: wrapper types (`IBMp`/`IBM2`
IndexBinaryIDMap, `IBHf`/`IBHc` HNSW storage, `IBFf` IndexBinaryFromFloat)
each recurse back into the reader to deserialize their inner index, with
no depth bound. A stream of a few thousand nested wrapper markers — each
only a 4-byte fourcc plus a small header — drives recursion until the
thread stack is exhausted, killing the process (SIGSEGV on the guard
page). The float reader `read_index_up` has the identical structure
(`IxMp`, `IxPT`, `IxRF`, HNSW storage, ...).
Add a shared, thread-local nesting-depth guard (RAII) at the entry of both
`read_index_up` and `read_index_binary_up`, capped at 50 levels. The
check runs before incrementing so a thrown limit leaves the counter
consistent; the destructor decrements on unwind. Legitimate indexes nest
only a handful of levels, so the limit is generous while turning the
stack-exhaustion crash into a catchable `FaissException`.
Reviewed By: alibeklfc
Differential Revision: D113119945
fbshipit-source-id: 7c4015131ca6c0bc7de4a3b8ba764ecff0119dae1 parent 3e313e4 commit 269ac43
1 file changed
Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1608 | 1608 | | |
1609 | 1609 | | |
1610 | 1610 | | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
| 1631 | + | |
1611 | 1632 | | |
| 1633 | + | |
1612 | 1634 | | |
1613 | 1635 | | |
1614 | 1636 | | |
| |||
3275 | 3297 | | |
3276 | 3298 | | |
3277 | 3299 | | |
| 3300 | + | |
3278 | 3301 | | |
3279 | 3302 | | |
3280 | 3303 | | |
| |||
0 commit comments