[PATCH] D112744: [llvm-objcopy] Fix misaligned access to load command data.

Daniel Rodríguez Troitiño via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 28 11:51:07 PDT 2021


drodriguez created this revision.
drodriguez added reviewers: nuriamari, alexander-shaposhnikov, uabelho.
Herald added a reviewer: rupprecht.
Herald added a reviewer: jhenderson.
Herald added a subscriber: abrachet.
drodriguez requested review of this revision.
Herald added subscribers: llvm-commits, MaskRay.
Herald added a project: LLVM.

It seems that llvm-objcopy stores data temporarily misaligned with the
requirements of the underlaying struct from libBinaryFormat, and UBSan
generates a runtime error.

Instead of trying to reinterpret the memory as the struct itself, simply
access the `char *` pointer that we are interested in, and that do not
have alignment restrictions.

This problem was pointed out in a comment of D111164 <https://reviews.llvm.org/D111164>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112744

Files:
  llvm/tools/llvm-objcopy/MachO/MachOReader.cpp


Index: llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
===================================================================
--- llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
+++ llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
@@ -124,9 +124,12 @@
       O.CodeSignatureCommandIndex = O.LoadCommands.size();
       break;
     case MachO::LC_SEGMENT:
-      if (StringRef(
-              reinterpret_cast<MachO::segment_command const *>(LoadCmd.Ptr)
-                  ->segname) == TextSegmentName)
+      // LoadCmd.Ptr might not be aligned temporarily as
+      // MachO::segment_command requires, but the segname char pointer do not
+      // have alignment restrictions.
+      if (StringRef(reinterpret_cast<const char *>(
+              LoadCmd.Ptr + offsetof(MachO::segment_command, segname))) ==
+          TextSegmentName)
         O.TextSegmentCommandIndex = O.LoadCommands.size();
 
       if (Expected<std::vector<std::unique_ptr<Section>>> Sections =
@@ -137,9 +140,12 @@
         return Sections.takeError();
       break;
     case MachO::LC_SEGMENT_64:
-      if (StringRef(
-              reinterpret_cast<MachO::segment_command_64 const *>(LoadCmd.Ptr)
-                  ->segname) == TextSegmentName)
+      // LoadCmd.Ptr might not be aligned temporarily as
+      // MachO::segment_command_64 requires, but the segname char pointer do
+      // not have alignment restrictions.
+      if (StringRef(reinterpret_cast<const char *>(
+              LoadCmd.Ptr + offsetof(MachO::segment_command_64, segname))) ==
+          TextSegmentName)
         O.TextSegmentCommandIndex = O.LoadCommands.size();
 
       if (Expected<std::vector<std::unique_ptr<Section>>> Sections =


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112744.383106.patch
Type: text/x-patch
Size: 1687 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211028/ec2c24bb/attachment.bin>


More information about the llvm-commits mailing list