Repository navigation
Validate codebook size in read_ResidualQuantizer_old and bounds-check IndexBinaryIVF::reconstruct_from_offset - #5764
Conversation
… IndexBinaryIVF::reconstruct_from_offset
|
Hi @professor-moody! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
@trang-nm-nguyen has imported this pull request. If you are a Meta employee, you can view this in D126706046. |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Summary
This change extends existing deserialization and reconstruction bounds checks to two remaining sibling code paths:
read_ResidualQuantizer_oldand"IxRQ"/"IxRq"reader (faiss/impl/index_read.cpp):read_ResidualQuantizer,read_LocalSearchQuantizer, andread_ProductAdditiveQuantizercallvalidate_codebooks_size(const AdditiveQuantizer& aq, size_t effective_d)so that additive quantizers with a truncatedcodebooksbuffer are rejected duringread_index().read_ResidualQuantizer_old(used by the"IxRQ"fourcc branch) sits abovevalidate_codebooks_sizeinfaiss/impl/index_read.cppand did not call it afterrq.set_derived_values(). Forward-declaringvalidate_codebooks_sizeand callingvalidate_codebooks_size(rq, rq.d)at the end ofread_ResidualQuantizer_old(allowing an early return only when!aq.is_trained && aq.codebooks.empty()) ensures legacy residual quantizer streams enforcecodebooks.size() >= d * total_codebook_sizebeforeAdditiveQuantizer::decodeorcompute_codesindexescodebooks."IxRQ"/"IxRq"IndexResidualQuantizerreader block, enforceidxr->rq.is_trained == idxr->is_trainedandidxr->ntotal == 0 || idxr->rq.is_trainedso a stream cannot mark the outer index trained (or populatentotal > 0codes) while leaving the innerResidualQuantizeruntrained with empty codebooks.IndexBinaryIVF::reconstruct_from_offset(faiss/IndexBinaryIVF.cpp):IndexIVF::reconstructvalidateslist_noandoffsetat runtime becauseDirectMap::get(key)does not validate the decoded(list_no, offset)pair stored in a serializeddirect_mapand assertions insideArrayInvertedListsare compiled out underNDEBUG.FAISS_THROW_IF_NOT_FMTchecks onlist_no >= 0 && static_cast<uint64_t>(list_no) < static_cast<uint64_t>(nlist)andoffset >= 0 && static_cast<uint64_t>(offset) < static_cast<uint64_t>(invlists->list_size(list_no))inIndexBinaryIVF::reconstruct_from_offsetprotects bothIndexBinaryIVF::reconstruct(key, recons)andIndexBinaryIVF::search_and_reconstructwithout 32-bitsize_ttruncation.Test Plan
"IxRQ"deserialization with valid codebooks (control), truncated codebooks withrq.is_trained = true(mode_rq_old), and inconsistent outeridxr->is_trained = true, ntotal = 1with innerrq.is_trained = false(mode_rq_old_untrained_mismatch)."IBIv") reconstruction with validDirectMap::Arrayentries (control) and out-of-bounds(list_no, offset)entries (offset >= list_size(list_no)andlist_no >= nlist).-O3 -DNDEBUG) and ASan+UBSan (-fsanitize=address,undefined -g -O1).Note: This patch was prepared with AI assistance and verified by human review and local test execution.