[llvm] [readobj][ELF][AArch64] Handle misformed AArch64 build attribute section (PR #134888)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 8 10:29:23 PDT 2025


https://github.com/sivan-shani created https://github.com/llvm/llvm-project/pull/134888

Report an error when the .ARM.attributes section for AArch64 is malformed or violates expected format.

>From d6d379eecc63d9a073d2e7b4087d100d9913cdb4 Mon Sep 17 00:00:00 2001
From: Sivan Shani <sivan.shani at arm.com>
Date: Tue, 8 Apr 2025 18:28:24 +0100
Subject: [PATCH] [readobj][ELF][AArch64] Handle misformed AArch64 build
 attribute section

Report an error when the .ARM.attributes section for AArch64 is malformed or violates expected format.
---
 llvm/lib/Support/ELFAttrParserExtended.cpp    | 26 ++++++++++++
 .../AArch64/build-attributes-melformed-long.s | 40 ++++++++++++++++++
 .../build-attributes-melformed-short.s        | 41 ++++++++++++++++++
 .../AArch64/build-attributes-melformed-type.s | 25 +++++++++++
 .../build-attributes-melformed-values.s       | 42 +++++++++++++++++++
 .../AArch64/build-attributes-melformed-ver.s  | 24 +++++++++++
 6 files changed, 198 insertions(+)
 create mode 100644 llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-long.s
 create mode 100644 llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-short.s
 create mode 100644 llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-type.s
 create mode 100644 llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-values.s
 create mode 100644 llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-ver.s

diff --git a/llvm/lib/Support/ELFAttrParserExtended.cpp b/llvm/lib/Support/ELFAttrParserExtended.cpp
index c08ed5e6fe86e..001006b784ebf 100644
--- a/llvm/lib/Support/ELFAttrParserExtended.cpp
+++ b/llvm/lib/Support/ELFAttrParserExtended.cpp
@@ -103,6 +103,8 @@ Error ELFExtendedAttrParser::parse(ArrayRef<uint8_t> Section,
 
   // Get format-version
   uint8_t FormatVersion = De.getU8(Cursor);
+  if (!Cursor)
+    return Cursor.takeError();
   if (ELFAttrs::Format_Version != FormatVersion)
     return createStringError(errc::invalid_argument,
                              "unrecognized format-version: 0x" +
@@ -110,6 +112,8 @@ Error ELFExtendedAttrParser::parse(ArrayRef<uint8_t> Section,
 
   while (!De.eof(Cursor)) {
     uint32_t ExtBASubsectionLength = De.getU32(Cursor);
+    if (!Cursor)
+      return Cursor.takeError();
     // Minimal valid Extended Build Attributes subsection header size is at
     // least 8: length(4) name(at least a single char + null) optionality(1) and
     // type(1)
@@ -120,9 +124,25 @@ Error ELFExtendedAttrParser::parse(ArrayRef<uint8_t> Section,
               utohexstr(Cursor.tell() - 4));
 
     StringRef VendorName = De.getCStrRef(Cursor);
+    if (!Cursor)
+      return Cursor.takeError();
     uint8_t IsOptional = De.getU8(Cursor);
+    if (!Cursor)
+      return Cursor.takeError();
+    if (!(0 == IsOptional || 1 == IsOptional))
+      return createStringError(
+          errc::invalid_argument,
+          "\ninvalid Optionality at offset " + utohexstr(Cursor.tell() - 4) +
+              ": " + utohexstr(IsOptional) + " (Options are 1|0)");
     StringRef IsOptionalStr = IsOptional ? "optional" : "required";
     uint8_t Type = De.getU8(Cursor);
+    if (!Cursor)
+      return Cursor.takeError();
+    if (!(0 == Type || 1 == Type))
+      return createStringError(errc::invalid_argument,
+                               "\ninvalid Type at offset " +
+                                   utohexstr(Cursor.tell() - 4) + ": " +
+                                   utohexstr(Type) + " (Options are 1|0)");
     StringRef TypeStr = Type ? "ntbs" : "uleb128";
 
     BuildAttributeSubSection BASubSection;
@@ -150,6 +170,8 @@ Error ELFExtendedAttrParser::parse(ArrayRef<uint8_t> Section,
            (OffsetInSection + ExtBASubsectionLength - BytesAllButAttributes)) {
 
       uint64_t Tag = De.getULEB128(Cursor);
+      if (!Cursor)
+        return Cursor.takeError();
 
       StringRef TagName = getTagName(VendorName, Tag);
 
@@ -157,10 +179,14 @@ Error ELFExtendedAttrParser::parse(ArrayRef<uint8_t> Section,
       std::string ValueStr = "";
       if (Type) { // type==1 --> ntbs
         ValueStr = De.getCStrRef(Cursor);
+        if (!Cursor)
+          return Cursor.takeError();
         if (Sw)
           Sw->printString("" != TagName ? TagName : utostr(Tag), ValueStr);
       } else { // type==0 --> uleb128
         ValueInt = De.getULEB128(Cursor);
+        if (!Cursor)
+          return Cursor.takeError();
         if (Sw)
           Sw->printNumber("" != TagName ? TagName : utostr(Tag), ValueInt);
       }
diff --git a/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-long.s b/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-long.s
new file mode 100644
index 0000000000000..469a424e577c2
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-long.s
@@ -0,0 +1,40 @@
+# RUN: yaml2obj %s -o %t.o
+# RUN: not llvm-readobj --arch-specific %t.o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: BuildAttributes {
+# CHECK-NEXT: FormatVersion: 0x41
+# CHECK-NEXT: Section 1 {
+# CHECK-NEXT:   SectionLength: 25
+# CHECK-NEXT:   VendorName: aeabi_pauthabi Optionality: required Type: uleb128
+# CHECK-NEXT:   Attributes {
+# CHECK-NEXT:     Tag_PAuth_Platform: 1
+# CHECK-NEXT:     Tag_PAuth_Schema: 1
+# CHECK-NEXT:   }
+# CHECK-NEXT: }
+# CHECK-NEXT: Section 2 {
+# CHECK-NEXT:   SectionLength: 153
+# CHECK-NEXT:   VendorName: aeabi_feature_and_bits Optionality: optional Type: uleb128
+# CHECK-NEXT:   Attributes {
+# CHECK-NEXT:     Tag_Feature_BTI: 1
+# CHECK-NEXT:     Tag_Feature_PAC: 1
+# CHECK-NEXT:     Tag_Feature_GCS: 1
+# CHECK-NEXT: unable to dump attributes from the Unknown section with index 1: unable to decode LEB128 at offset 0x0000003d: malformed uleb128, extends past end
+
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data: ELFDATA2LSB
+  OSABI: ELFOSABI_NONE
+  Type: ET_REL
+  Machine: EM_AARCH64
+  Entry: 0x0
+
+Sections:
+  - Name: .ARM.attributes
+    Type: 0x70000003  # SHT_LOPROC + 3
+    AddressAlign: 1
+    Offset: 0x40
+    Size: 0x3d
+    Content: "411900000061656162695f7061757468616269000000010102019900000061656162695f666561747572655f616e645f62697473000100000101010201"
+...
diff --git a/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-short.s b/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-short.s
new file mode 100644
index 0000000000000..4ea930c9f6cd7
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-short.s
@@ -0,0 +1,41 @@
+# RUN: yaml2obj %s -o %t.o
+# RUN: not llvm-readobj --arch-specific %t.o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: BuildAttributes {
+# CHECK-NEXT: FormatVersion: 0x41
+# CHECK-NEXT: Section 1 {
+# CHECK-NEXT: SectionLength: 25
+# CHECK-NEXT: VendorName: aeabi_pauthabi Optionality: required Type: uleb128
+# CHECK-NEXT: Attributes {
+# CHECK-NEXT: Tag_PAuth_Platform: 1
+# CHECK-NEXT: Tag_PAuth_Schema: 1
+# CHECK-NEXT: }
+# CHECK-NEXT: }
+# CHECK-NEXT: Section 2 {
+# CHECK-NEXT: SectionLength: 32
+# CHECK-NEXT: VendorName: aeabi_feature_and_bits Optionality: optional Type: uleb128
+# CHECK-NEXT: Attributes {
+# CHECK-NEXT: Tag_Feature_BTI: 1
+# CHECK-NEXT: Tag_Feature_PAC: 1
+# CHECK-NEXT: }
+# CHECK-NEXT: }
+# CHECK-NEXT: unable to dump attributes from the Unknown section with index 1: invalid Extended Build Attributes subsection size at offset: 3B
+
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data: ELFDATA2LSB
+  OSABI: ELFOSABI_NONE
+  Type: ET_REL
+  Machine: EM_AARCH64
+  Entry: 0x0
+
+Sections:
+  - Name: .ARM.attributes
+    Type: 0x70000003  # SHT_LOPROC + 3
+    AddressAlign: 1
+    Offset: 0x40
+    Size: 0x41
+    Content: "411900000061656162695f7061757468616269000000010102012000000061656162695f666561747572655f616e645f6269747300010000010101020000"
+...
diff --git a/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-type.s b/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-type.s
new file mode 100644
index 0000000000000..31eb5642b24db
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-type.s
@@ -0,0 +1,25 @@
+# RUN: yaml2obj %s -o %t.o
+# RUN: not llvm-readobj --arch-specific %t.o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: BuildAttributes {
+# CHECK-NEXT: FormatVersion: 0x41
+# CHECK-NEXT: unable to dump attributes from the Unknown section with index 1: 
+# CHECK-NEXT: invalid Type at offset 12: 9 (Options are 1|0)
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data: ELFDATA2LSB
+  OSABI: ELFOSABI_NONE
+  Type: ET_REL
+  Machine: EM_AARCH64
+  Entry: 0x0
+
+Sections:
+  - Name: .ARM.attributes
+    Type: 0x70000003  # SHT_LOPROC + 3
+    AddressAlign: 1
+    Offset: 0x40
+    Size: 0x3d
+    Content: "411900000061656162695f7061757468616269000109010102012300000061656162695f666561747572655f616e645f62697473000100000101010201"
+...
diff --git a/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-values.s b/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-values.s
new file mode 100644
index 0000000000000..5653036af6880
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-values.s
@@ -0,0 +1,42 @@
+# RUN: yaml2obj %s -o %t.o
+# RUN: not llvm-readobj --arch-specific %t.o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: BuildAttributes {
+# CHECK-NEXT:  FormatVersion: 0x41
+# CHECK-NEXT:  Section 1 {
+# CHECK-NEXT:    SectionLength: 25
+# CHECK-NEXT:    VendorName: aeabi_pauthabi Optionality: required Type: uleb128
+# CHECK-NEXT:    Attributes {
+# CHECK-NEXT:      Tag_PAuth_Platform: 1
+# CHECK-NEXT:      Tag_PAuth_Schema: 1
+# CHECK-NEXT:    }
+# CHECK-NEXT:  }
+# CHECK-NEXT:  Section 2 {
+# CHECK-NEXT:    SectionLength: 35
+# CHECK-NEXT:    VendorName: aeabi_feature_and_bits Optionality: optional Type: uleb128
+# CHECK-NEXT:    Attributes {
+# CHECK-NEXT:      Tag_Feature_BTI: 1
+# CHECK-NEXT:      Tag_Feature_PAC: 1
+# CHECK-NEXT:      Tag_Feature_GCS: 0
+# CHECK-NEXT:    }
+# CHECK-NEXT:  }
+# CHECK-NEXT: unable to dump attributes from the Unknown section with index 1: invalid Extended Build Attributes subsection size at offset: 3D
+
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data: ELFDATA2LSB
+  OSABI: ELFOSABI_NONE
+  Type: ET_REL
+  Machine: EM_AARCH64
+  Entry: 0x0
+
+Sections:
+  - Name: .ARM.attributes
+    Type: 0x70000003  # SHT_LOPROC + 3
+    AddressAlign: 1
+    Offset: 0x40
+    Size: 0x41
+    Content: "411900000061656162695f7061757468616269000000010102012300000061656162695f666561747572655f616e645f6269747300010000010101020000"
+...
diff --git a/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-ver.s b/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-ver.s
new file mode 100644
index 0000000000000..c556be8a4acf6
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/AArch64/build-attributes-melformed-ver.s
@@ -0,0 +1,24 @@
+# RUN: yaml2obj %s -o %t.o
+# RUN: not llvm-readobj --arch-specific %t.o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: BuildAttributes {
+# CHECK-NEXT:  FormatVersion: 0x37
+# CHECK-NEXT:  unable to dump attributes from the Unknown section with index 1: unrecognized format-version: 0x37
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data: ELFDATA2LSB
+  OSABI: ELFOSABI_NONE
+  Type: ET_REL
+  Machine: EM_AARCH64
+  Entry: 0x0
+
+Sections:
+  - Name: .ARM.attributes
+    Type: 0x70000003  # SHT_LOPROC + 3
+    AddressAlign: 1
+    Offset: 0x40
+    Size: 0x3d
+    Content: "371900000061656162695f7061757468616269000000010102012300000061656162695f666561747572655f616e645f62697473000100000101010201"
+...



More information about the llvm-commits mailing list