From 1fa073ab896e65c55ff63487be0b41d7cea9aa77 Mon Sep 17 00:00:00 2001 From: Zixu Wang <9819235+zixu-w@users.noreply.github.com> Date: Thu, 30 May 2024 23:08:01 -0700 Subject: [MachO] Stop parsing past end of rebase/bind table (#93897) `MachORebaseEntry::moveNext()` and `MachOBindEntry::moveNext()` assume that the rebase/bind table ends with `{REBASE|BIND}_OPCODE_DONE` or an actual rebase/bind. However a valid rebase/bind table might also end with other effectively no-op opcodes, which caused the parser to move past the end and go into the next table, resulting in corrupted entries or infinite loops. --- llvm/lib/Object/MachOObjectFile.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'llvm/lib/Object/MachOObjectFile.cpp') diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index 863bc12..61d880b 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -3501,15 +3501,17 @@ void MachORebaseEntry::moveNext() { --RemainingLoopCount; return; } - // REBASE_OPCODE_DONE is only used for padding if we are not aligned to - // pointer size. Therefore it is possible to reach the end without ever having - // seen REBASE_OPCODE_DONE. - if (Ptr == Opcodes.end()) { - Done = true; - return; - } + bool More = true; while (More) { + // REBASE_OPCODE_DONE is only used for padding if we are not aligned to + // pointer size. Therefore it is possible to reach the end without ever + // having seen REBASE_OPCODE_DONE. + if (Ptr == Opcodes.end()) { + Done = true; + return; + } + // Parse next opcode and set up next loop. const uint8_t *OpcodeStart = Ptr; uint8_t Byte = *Ptr++; @@ -3838,15 +3840,17 @@ void MachOBindEntry::moveNext() { --RemainingLoopCount; return; } - // BIND_OPCODE_DONE is only used for padding if we are not aligned to - // pointer size. Therefore it is possible to reach the end without ever having - // seen BIND_OPCODE_DONE. - if (Ptr == Opcodes.end()) { - Done = true; - return; - } + bool More = true; while (More) { + // BIND_OPCODE_DONE is only used for padding if we are not aligned to + // pointer size. Therefore it is possible to reach the end without ever + // having seen BIND_OPCODE_DONE. + if (Ptr == Opcodes.end()) { + Done = true; + return; + } + // Parse next opcode and set up next loop. const uint8_t *OpcodeStart = Ptr; uint8_t Byte = *Ptr++; -- cgit v1.1