From 0c553bff8e76ebfbf9cd4e94ff565018ed1ff0c1 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 7 Feb 2022 11:51:19 +0100 Subject: [PATCH] [Bitcode] Guard against out of bounds value reference We should make sure that the value ID is in bounds, otherwise we will assert / read out of bounds. --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 8 ++++++-- .../Bitcode/Inputs/invalid-value-symbol-table.bc | Bin 0 -> 1192 bytes llvm/test/Bitcode/invalid.test | 5 +++++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Bitcode/Inputs/invalid-value-symbol-table.bc diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 308986a588f4..c24dcf030deb 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -2107,11 +2107,15 @@ Error BitcodeReader::parseGlobalValueSymbolTable() { if (!MaybeRecord) return MaybeRecord.takeError(); switch (MaybeRecord.get()) { - case bitc::VST_CODE_FNENTRY: // [valueid, offset] + case bitc::VST_CODE_FNENTRY: { // [valueid, offset] + unsigned ValueID = Record[0]; + if (ValueID >= ValueList.size() || !ValueList[ValueID]) + return error("Invalid value reference in symbol table"); setDeferredFunctionInfo(FuncBitcodeOffsetDelta, - cast(ValueList[Record[0]]), Record); + cast(ValueList[ValueID]), Record); break; } + } } } diff --git a/llvm/test/Bitcode/Inputs/invalid-value-symbol-table.bc b/llvm/test/Bitcode/Inputs/invalid-value-symbol-table.bc new file mode 100644 index 0000000000000000000000000000000000000000..509133ef46cdaccab734b030964a8eed6b68c835 GIT binary patch literal 1192 zcmXw3e@q*76#uqYdS~grcbkN@a#!zwX)_gW#UHKL8rws}IAa(02TLGp85Gch*U~^? zy0mMTW3hj9gv|bdIuiczznYMZKZw#EVkH%EF@{7!sFWX?j*tzPB`)f>pf9=i?tSy_ zz4!UN_dS<5zy4Ac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A0hb{ D@{*#x literal 0 HcmV?d00001 diff --git a/llvm/test/Bitcode/invalid.test b/llvm/test/Bitcode/invalid.test index 7dabafdb6d0c..32f93ab160e9 100644 --- a/llvm/test/Bitcode/invalid.test +++ b/llvm/test/Bitcode/invalid.test @@ -266,3 +266,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/unterminated-blob.bc 2>&1 | \ RUN: FileCheck --check-prefix=UNTERMINATED-BLOB %s UNTERMINATED-BLOB: Blob ends too soon + +RUN: not llvm-dis -disable-output %p/Inputs/invalid-value-symbol-table.bc 2>&1 | \ +RUN: FileCheck --check-prefix=INVALID-VALUE-SYMBOL-TABLE %s + +INVALID-VALUE-SYMBOL-TABLE: Invalid value reference in symbol table