-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[lld][ELF] Improve the vulnerability in Orphan Sections initialization #156354
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
base: main
Are you sure you want to change the base?
Conversation
Fix the error generated during the linking process when the relocation section is placed before the relocated section and the relocated section is not defined in the linker script.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-lld Author: None (mykouHW) ChangesFix the error generated during the linking process when the relocation section is placed before the relocated section and the relocated section is not defined in the linker script. Issue Cause: If the However, during the construction and registration of the Solution: Full diff: https://github.com/llvm/llvm-project/pull/156354.diff 2 Files Affected:
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 921128dae2bdb..067abbc42a13d 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -1037,10 +1037,14 @@ void LinkerScript::addOrphanSections() {
if (ctx.arg.relocatable && (isec->flags & SHF_LINK_ORDER))
continue;
- if (auto *sec = dyn_cast<InputSection>(isec))
- if (InputSectionBase *rel = sec->getRelocatedSection())
+ if (auto *sec = dyn_cast<InputSection>(isec)){
+ if (InputSectionBase *rel = sec->getRelocatedSection()){
+ if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel))
+ add(relIS);
if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
add(relIS);
+ }
+ }
add(isec);
if (ctx.arg.relocatable)
for (InputSectionBase *depSec : isec->dependentSections)
diff --git a/lld/test/ELF/linkerscript/orphan-sections-init.s b/lld/test/ELF/linkerscript/orphan-sections-init.s
new file mode 100644
index 0000000000000..1701336f098e2
--- /dev/null
+++ b/lld/test/ELF/linkerscript/orphan-sections-init.s
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+# RUN: rm -rf %t && mkdir -p %t
+# RUN: split-file %s %t && cd %t
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 foo.s -o foo.o
+
+# RUN: ld.lld -r foo.o -T script.ld -o foo_mc.o
+
+# RUN: llvm-objcopy --rename-section .text=.com.text foo_mc.o foo_mc.o
+# RUN: llvm-objcopy --rename-section .rela.text=.rela.com.text foo_mc.o foo_mc.o
+
+# RUN: ld.lld -r foo_mc.o -T script.ld -o foo_mc_after.o
+
+#--- foo.s
+ .text
+ .globl foo
+ .p2align 4
+ .type foo,@function
+foo:
+ mov $bar, %rax
+
+
+
+#--- script.ld
+SECTIONS
+{
+ .rela.text 0 : { *(.rela.text) }
+ .text 0 : { *(.text) }
+}
+
|
@llvm/pr-subscribers-lld-elf Author: None (mykouHW) ChangesFix the error generated during the linking process when the relocation section is placed before the relocated section and the relocated section is not defined in the linker script. Issue Cause: If the However, during the construction and registration of the Solution: Full diff: https://github.com/llvm/llvm-project/pull/156354.diff 2 Files Affected:
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 921128dae2bdb..067abbc42a13d 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -1037,10 +1037,14 @@ void LinkerScript::addOrphanSections() {
if (ctx.arg.relocatable && (isec->flags & SHF_LINK_ORDER))
continue;
- if (auto *sec = dyn_cast<InputSection>(isec))
- if (InputSectionBase *rel = sec->getRelocatedSection())
+ if (auto *sec = dyn_cast<InputSection>(isec)){
+ if (InputSectionBase *rel = sec->getRelocatedSection()){
+ if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel))
+ add(relIS);
if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
add(relIS);
+ }
+ }
add(isec);
if (ctx.arg.relocatable)
for (InputSectionBase *depSec : isec->dependentSections)
diff --git a/lld/test/ELF/linkerscript/orphan-sections-init.s b/lld/test/ELF/linkerscript/orphan-sections-init.s
new file mode 100644
index 0000000000000..1701336f098e2
--- /dev/null
+++ b/lld/test/ELF/linkerscript/orphan-sections-init.s
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+# RUN: rm -rf %t && mkdir -p %t
+# RUN: split-file %s %t && cd %t
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 foo.s -o foo.o
+
+# RUN: ld.lld -r foo.o -T script.ld -o foo_mc.o
+
+# RUN: llvm-objcopy --rename-section .text=.com.text foo_mc.o foo_mc.o
+# RUN: llvm-objcopy --rename-section .rela.text=.rela.com.text foo_mc.o foo_mc.o
+
+# RUN: ld.lld -r foo_mc.o -T script.ld -o foo_mc_after.o
+
+#--- foo.s
+ .text
+ .globl foo
+ .p2align 4
+ .type foo,@function
+foo:
+ mov $bar, %rax
+
+
+
+#--- script.ld
+SECTIONS
+{
+ .rela.text 0 : { *(.rela.text) }
+ .text 0 : { *(.text) }
+}
+
|
fixes #156417
Solution:
Before processing the
RelocationSection
, ensure that theOutputSection
for theRelocatedSection
is created and registered. The creation and registration logic is protected by theadd
function, which prevents duplicate creation. However, it may result in duplicate establishment of theparent
relationship, which does not affect correctness.