Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MachO] Detect overflow in section offset. #98685

Merged
merged 4 commits into from
Jul 17, 2024

Conversation

efriedma-quic
Copy link
Collaborator

The section offset field is only 32 bits; if the computed section offset is larger, make sure we don't emit a corrupt object file.

The section offset field is only 32 bits; if the computed section offset
is larger, make sure we don't emit a corrupt object file.
@llvmbot llvmbot added the mc Machine (object) code label Jul 12, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 12, 2024

@llvm/pr-subscribers-mc

Author: Eli Friedman (efriedma-quic)

Changes

The section offset field is only 32 bits; if the computed section offset is larger, make sure we don't emit a corrupt object file.


Full diff: https://github.com/llvm/llvm-project/pull/98685.diff

2 Files Affected:

  • (modified) llvm/lib/MC/MachObjectWriter.cpp (+2)
  • (added) llvm/test/MC/MachO/section-offset-overflow.s (+9)
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 53eed0092a5b4..f890ecc1d20ee 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -277,6 +277,8 @@ void MachObjectWriter::writeSection(const MCAssembler &Asm,
     W.write<uint32_t>(VMAddr);      // address
     W.write<uint32_t>(SectionSize); // size
   }
+  if (!isUInt<32>(FileOffset))
+    report_fatal_error("Cannot encode offset of section");
   W.write<uint32_t>(FileOffset);
 
   W.write<uint32_t>(Log2(Section.getAlign()));
diff --git a/llvm/test/MC/MachO/section-offset-overflow.s b/llvm/test/MC/MachO/section-offset-overflow.s
new file mode 100644
index 0000000000000..51fc90c2e3479
--- /dev/null
+++ b/llvm/test/MC/MachO/section-offset-overflow.s
@@ -0,0 +1,9 @@
+// RUN: not --crash llvm-mc -triple x86_64-apple-macosx -filetype=obj -o /dev/null %s 2>&1 | FileCheck  %s
+
+// CHECK: Cannot encode offset of section
+
+        .data
+        .long 1
+        .zero 0x100000000
+        .const
+        .long 1

@@ -277,6 +277,8 @@ void MachObjectWriter::writeSection(const MCAssembler &Asm,
W.write<uint32_t>(VMAddr); // address
W.write<uint32_t>(SectionSize); // size
}
if (!isUInt<32>(FileOffset))
report_fatal_error("Cannot encode offset of section");
W.write<uint32_t>(FileOffset);

W.write<uint32_t>(Log2(Section.getAlign()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind adding another one for RelocationStart? Looks like it might have the same problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can call Asm.getContext().reportError instead to not trigger a crash.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps let the caller (MachObjectWriter::writeObject) check this and early return.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of the early return would be to avoid duplicate error messages? That makes sense, I guess.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried rearranging as suggested.

if (!cast<MCSectionMachO>(Sec).isVirtualSection() &&
!isUInt<32>(SectionStart)) {
Asm.getContext().reportError(
SMLoc(), "Cannot encode offset of section; object file too large");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reportError messages are generally not capitalized per https://llvm.org/docs/CodingStandards.html#error-and-warning-messages

@@ -0,0 +1,9 @@
// RUN: not llvm-mc -triple x86_64-apple-macosx -filetype=obj -o /dev/null %s 2>&1 | FileCheck %s

// CHECK: Cannot encode offset of section
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check the error: prefix of the diagnostic

@efriedma-quic efriedma-quic merged commit a10570b into llvm:main Jul 17, 2024
5 of 6 checks passed
sgundapa pushed a commit to sgundapa/upstream_effort that referenced this pull request Jul 23, 2024
The section offset field is only 32 bits; if the computed section offset
is larger, make sure we don't emit a corrupt object file.
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary:
The section offset field is only 32 bits; if the computed section offset
is larger, make sure we don't emit a corrupt object file.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants