[BitcodeReader] Validate Strtab before accessing.
authorFlorian Hahn <flo@fhahn.com>
Tue, 22 Jun 2021 13:48:45 +0000 (14:48 +0100)
committerFlorian Hahn <flo@fhahn.com>
Tue, 22 Jun 2021 13:52:16 +0000 (14:52 +0100)
This fixes a crash with invalid bitcode files that have records
referencing names in Strtab, but Strtab is not present or the index is
out-of-bounds.

This fixes the following clusterfuzz issue:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=29895

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D95554

llvm/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/test/Bitcode/invalid-record-strtab.ll [new file with mode: 0644]
llvm/test/Bitcode/invalid-record-strtab.ll.bc [new file with mode: 0644]

index e002019..1631dc3 100644 (file)
@@ -3407,9 +3407,12 @@ Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {
 
   // Record[16] is the address space number.
 
-  // Check whether we have enough values to read a partition name.
-  if (Record.size() > 18)
+  // Check whether we have enough values to read a partition name. Also make
+  // sure Strtab has enough values.
+  if (Record.size() > 18 && Strtab.data() &&
+      Record[17] + Record[18] <= Strtab.size()) {
     Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18]));
+  }
 
   ValueList.push_back(Func);
 
diff --git a/llvm/test/Bitcode/invalid-record-strtab.ll b/llvm/test/Bitcode/invalid-record-strtab.ll
new file mode 100644 (file)
index 0000000..4973090
--- /dev/null
@@ -0,0 +1,5 @@
+; Bitcode with an invalid record that indexes a name outside of strtab.
+
+; RUN: not llvm-dis %s.bc -o - 2>&1 | FileCheck %s
+
+; CHECK: error: Invalid record
diff --git a/llvm/test/Bitcode/invalid-record-strtab.ll.bc b/llvm/test/Bitcode/invalid-record-strtab.ll.bc
new file mode 100644 (file)
index 0000000..8ff7e39
Binary files /dev/null and b/llvm/test/Bitcode/invalid-record-strtab.ll.bc differ