diff --git a/faiss/impl/index_read.cpp b/faiss/impl/index_read.cpp index ba9be297b..79cf22c95 100644 --- a/faiss/impl/index_read.cpp +++ b/faiss/impl/index_read.cpp @@ -1608,7 +1608,29 @@ static std::unique_ptr read_ivfpq( int read_old_fmt_hack = 0; +namespace { + +constexpr int kMaxIndexNestingDepth = 50; +thread_local int index_read_nesting_depth = 0; + +struct IndexNestingGuard { + IndexNestingGuard() { + FAISS_THROW_IF_NOT_FMT( + index_read_nesting_depth < kMaxIndexNestingDepth, + "faiss index nesting depth exceeds limit of %d; " + "input may be corrupt or malicious", + kMaxIndexNestingDepth); + ++index_read_nesting_depth; + } + ~IndexNestingGuard() { + --index_read_nesting_depth; + } +}; + +} // namespace + std::unique_ptr read_index_up(IOReader* f, int io_flags) { + IndexNestingGuard nesting_guard; std::unique_ptr idx; uint32_t h; READ1(h); @@ -3275,6 +3297,7 @@ static void read_binary_multi_hash_map( } std::unique_ptr read_index_binary_up(IOReader* f, int io_flags) { + IndexNestingGuard nesting_guard; std::unique_ptr idx; uint32_t h; READ1(h);