Skip to content

Commit 5da7685

Browse files
ELF: fix the layout of the segments added on 64KB-page architectures
Fix: #1366
1 parent fb73059 commit 5da7685

14 files changed

Lines changed: 594 additions & 58 deletions

‎doc/sphinx/changelog.rst‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@
2424
- |lief-assembly-x86-Instruction-lock|
2525
- |lief-assembly-x86-Instruction-unlock|
2626

27+
:ELF:
28+
29+
* Fix the layout of the segments added by LIEF for the architectures that
30+
can use pages larger than 4K (:issue:`1366`)
31+
* Fix the modification of binaries that have already been modified by LIEF.
32+
The segment table was relocated a second time which, for the non-PIE
33+
binaries, shifted the sections without shifting the segments nor the
34+
dynamic entries. It resulted in a ``DT_STRTAB`` that was no longer pointing
35+
to ``.dynstr`` (i.e. garbage ``DT_NEEDED``/``DT_RUNPATH`` names) and in a
36+
``PT_PHDR`` that was not wrapped by a ``PT_LOAD`` segment (:issue:`1366`).
37+
* |lief-elf-check_layout| now reports the ``PT_LOAD`` segments that can't be
38+
mapped independently because they share the same page. It also report
39+
mismatch between ``.dynstr`` virtual address and ``DT_STRTAB``.
40+
2741
:COFF:
2842

2943
* Add |lief-coff-binary-get_section| to look up a section by its name. Names

‎include/LIEF/ELF/Binary.hpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,8 @@ class LIEF_API Binary : public LIEF::Binary {
10941094

10951095
uint64_t page_size() const override;
10961096

1097+
uint64_t layout_pagesize() const;
1098+
10971099
static bool classof(const LIEF::Binary* bin) {
10981100
return bin->format() == Binary::FORMATS::ELF ||
10991101
bin->format() == Binary::FORMATS::OAT;
@@ -1237,7 +1239,8 @@ class LIEF_API Binary : public LIEF::Binary {
12371239
LIEF_LOCAL Segment* add_segment(const Segment& segment,
12381240
uint64_t base) LIEF_LIFETIMEBOUND;
12391241

1240-
LIEF_LOCAL uint64_t relocate_phdr_table_auto();
1242+
LIEF_LOCAL uint64_t relocate_phdr_table_auto(size_t required_segments = 1);
1243+
LIEF_LOCAL uint64_t reuse_phdr_table();
12411244
LIEF_LOCAL uint64_t relocate_phdr_table_pie();
12421245
LIEF_LOCAL uint64_t relocate_phdr_table_v1();
12431246
LIEF_LOCAL uint64_t relocate_phdr_table_v2();

‎src/ELF/Binary.cpp‎

Lines changed: 125 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "Binary.tcc"
5555
#include "Object.tcc"
5656
#include "internal_utils.hpp"
57+
#include "paging.hpp"
5758

5859

5960
namespace LIEF::ELF {
@@ -2767,12 +2768,72 @@ uint64_t Binary::relocate_phdr_table(PHDR_RELOC type) {
27672768
}
27682769
}
27692770

2770-
uint64_t Binary::relocate_phdr_table_auto() {
2771-
if (phdr_reloc_info_.new_offset > 0) {
2772-
// Already relocated
2771+
/* If the segment table has already been relocated (which is the case for the
2772+
* binaries already processed by LIEF), this area can still have room for the
2773+
* segments we need to add.
2774+
*/
2775+
uint64_t Binary::reuse_phdr_table() {
2776+
const Segment* pt_phdr = get(Segment::TYPE::PHDR);
2777+
if (pt_phdr == nullptr) {
2778+
return 0;
2779+
}
2780+
2781+
const uint64_t phdr_offset = header_.program_headers_offset();
2782+
2783+
// PT_PHDR must describe the current location of the segment table
2784+
if (pt_phdr->file_offset() != phdr_offset || phdr_offset == 0) {
2785+
return 0;
2786+
}
2787+
2788+
const uint64_t phdr_size = type() == Header::CLASS::ELF32 ?
2789+
sizeof(details::ELF32::Elf_Phdr) :
2790+
sizeof(details::ELF64::Elf_Phdr);
2791+
2792+
const uint64_t used = (uint64_t)header_.numberof_segments() * phdr_size;
2793+
const uint64_t reserved = pt_phdr->physical_size();
2794+
2795+
if (reserved <= used) {
2796+
return 0;
2797+
}
2798+
2799+
const size_t available = (reserved - used) / phdr_size;
2800+
2801+
// The reserved area must be loaded, otherwise the new segments would not be
2802+
// mapped by the loader
2803+
const uint64_t phdr_end = phdr_offset + reserved;
2804+
const bool wrapped =
2805+
std::any_of(segments_.begin(), segments_.end(),
2806+
[phdr_offset, phdr_end](const std::unique_ptr<Segment>& s) {
2807+
return s->is_load() && s->file_offset() <= phdr_offset &&
2808+
phdr_end <= (s->file_offset() + s->physical_size());
2809+
});
2810+
2811+
if (!wrapped) {
2812+
return 0;
2813+
}
2814+
2815+
LIEF_DEBUG("Reusing the current segment table location ({:#x}) which can "
2816+
"still hold {} segments",
2817+
phdr_offset, available);
2818+
2819+
phdr_reloc_info_.new_offset = phdr_offset;
2820+
phdr_reloc_info_.nb_segments = available;
2821+
return phdr_offset;
2822+
}
2823+
2824+
uint64_t Binary::relocate_phdr_table_auto(size_t required_segments) {
2825+
if (phdr_reloc_info_.new_offset > 0 &&
2826+
phdr_reloc_info_.nb_segments >= required_segments)
2827+
{
27732828
return phdr_reloc_info_.new_offset;
27742829
}
27752830

2831+
if (uint64_t offset = reuse_phdr_table();
2832+
offset > 0 && phdr_reloc_info_.nb_segments >= required_segments)
2833+
{
2834+
return offset;
2835+
}
2836+
27762837
const bool has_phdr_s = has(Segment::TYPE::PHDR);
27772838
const bool has_interp_s = has(Segment::TYPE::INTERP);
27782839
const bool has_soname = has(DynamicEntry::TAG::SONAME);
@@ -2782,15 +2843,19 @@ uint64_t Binary::relocate_phdr_table_auto() {
27822843

27832844
uint64_t offset = 0;
27842845
if (is_dyn && (has_phdr_s || has_interp_s || has_soname)) {
2785-
if (offset = relocate_phdr_table_pie(); offset > 0) {
2846+
if (offset = relocate_phdr_table_pie();
2847+
offset > 0 && phdr_reloc_info_.nb_segments >= required_segments)
2848+
{
27862849
return offset;
27872850
}
27882851
LIEF_ERR("Failed to relocate PHDR table for this PIE binary");
27892852
}
27902853

27912854
if (is_dyn && !(has_phdr_s || has_interp_s) && !has_ep) {
27922855
// See libm-ubuntu24.so
2793-
if (offset = relocate_phdr_table_pie(); offset > 0) {
2856+
if (offset = relocate_phdr_table_pie();
2857+
offset > 0 && phdr_reloc_info_.nb_segments >= required_segments)
2858+
{
27942859
return offset;
27952860
}
27962861
}
@@ -2801,7 +2866,7 @@ uint64_t Binary::relocate_phdr_table_auto() {
28012866
if (is_valid_for_v3) {
28022867
LIEF_DEBUG("Try v3 relocator");
28032868
offset = relocate_phdr_table_v3();
2804-
if (offset > 0) {
2869+
if (offset > 0 && phdr_reloc_info_.nb_segments >= required_segments) {
28052870
return offset;
28062871
}
28072872
}
@@ -2815,10 +2880,14 @@ uint64_t Binary::relocate_phdr_table_auto() {
28152880
return 0;
28162881
}
28172882
}
2818-
return offset;
2883+
if (phdr_reloc_info_.new_offset == 0 ||
2884+
phdr_reloc_info_.nb_segments < required_segments)
2885+
{
2886+
return 0;
2887+
}
2888+
return phdr_reloc_info_.new_offset;
28192889
}
28202890

2821-
28222891
uint64_t Binary::relocate_phdr_table_pie() {
28232892

28242893
if (phdr_reloc_info_.new_offset > 0) {
@@ -2861,7 +2930,12 @@ uint64_t Binary::relocate_phdr_table_pie() {
28612930
* header_.numberof_segments();
28622931
* }
28632932
*/
2864-
static constexpr size_t shift = 0x1000;
2933+
static constexpr uint64_t DEFAULT_SHIFT = 0x1000;
2934+
2935+
const uint64_t pagesize = layout_pagesize();
2936+
const uint64_t shift = align(DEFAULT_SHIFT, pagesize);
2937+
2938+
LIEF_DEBUG("Shift: {:#x} (default: {:#x})", shift, DEFAULT_SHIFT);
28652939

28662940
phdr_reloc_info_.new_offset = from;
28672941
phdr_reloc_info_.nb_segments = shift / phdr_size - header_.numberof_segments();
@@ -2872,8 +2946,6 @@ uint64_t Binary::relocate_phdr_table_pie() {
28722946
return 0;
28732947
}
28742948

2875-
LIEF_DEBUG("Header shift: {:#x}", shift);
2876-
28772949
header().section_headers_offset(header().section_headers_offset() + shift);
28782950

28792951
shift_sections(from, shift);
@@ -2922,7 +2994,8 @@ uint64_t Binary::relocate_phdr_table_v3() {
29222994
std::max(last_off, segment->physical_size() + segment->file_offset());
29232995
}
29242996
}
2925-
uint64_t last_off_aligned = align(last_off, page_size());
2997+
const uint64_t pagesize = layout_pagesize();
2998+
uint64_t last_off_aligned = align(last_off, pagesize);
29262999

29273000
if (phdr_reloc_info_.new_offset > 0) {
29283001
return phdr_reloc_info_.new_offset;
@@ -2962,9 +3035,12 @@ uint64_t Binary::relocate_phdr_table_v3() {
29623035
phdr_load_segment->file_offset(phdr_reloc_info_.new_offset);
29633036
phdr_load_segment->physical_size(new_segtbl_sz);
29643037
phdr_load_segment->virtual_size(new_segtbl_sz);
2965-
phdr_load_segment->virtual_address(imagebase() + virtual_size());
3038+
// This segment must not share a page with the last PT_LOAD segment, otherwise
3039+
// the loader would map both at the same address (cf. issue #1366)
3040+
phdr_load_segment->virtual_address(align(imagebase() + virtual_size(),
3041+
pagesize));
29663042
phdr_load_segment->physical_address(phdr_load_segment->virtual_address());
2967-
phdr_load_segment->alignment(0x1000);
3043+
phdr_load_segment->alignment(pagesize);
29683044
phdr_load_segment->add(Segment::FLAGS::R);
29693045
phdr_load_segment->datahandler_ = datahandler_.get();
29703046

@@ -3073,10 +3149,15 @@ uint64_t Binary::relocate_phdr_table_v2() {
30733149
* LOAD_2.virtual_size
30743150
* \ /
30753151
* ------------ Offset --------------------/
3076-
* which should be aligned on a page size to avoid error.
3152+
* which should be aligned on the page size for which the binary has been
3153+
* laid out to avoid error. Aligning on a smaller value would let this new
3154+
* segment share a page with the bss-like segment, which the loader can't map
3155+
* independently (cf. issue #1366).
30773156
*
3078-
* The issue https://github.com/lief-project/LIEF/issues/671
3079-
* is a good example of what could go wrong.
3157+
* The issues
3158+
* - https://github.com/lief-project/LIEF/issues/671
3159+
* - https://github.com/lief-project/LIEF/issues/1366
3160+
* are a good example of what could go wrong.
30803161
*
30813162
* |WARNING|
30823163
* This modification can increase the binary size drastically
@@ -3094,7 +3175,7 @@ uint64_t Binary::relocate_phdr_table_v2() {
30943175
const uint64_t new_phdr_offset =
30953176
align(bss_segment->virtual_address() - imagebase() +
30963177
bss_segment->virtual_size(),
3097-
0x1000);
3178+
layout_pagesize());
30983179

30993180
const size_t nb_segments = header.numberof_segments() +
31003181
/* custom PT_LOAD */ 1 + USER_SEGMENTS;
@@ -3158,6 +3239,11 @@ uint64_t Binary::relocate_phdr_table_v2() {
31583239
phdr_segment->file_offset(nsegment_addr->file_offset());
31593240
phdr_segment->virtual_address(nsegment_addr->virtual_address());
31603241
phdr_segment->physical_address(nsegment_addr->physical_address());
3242+
// PT_PHDR must describe the area we just reserved. Otherwise, it would
3243+
// keep the size of its previous location which is likely to no longer be
3244+
// wrapped by a PT_LOAD segment.
3245+
phdr_segment->physical_size(nsegment_addr->physical_size());
3246+
phdr_segment->virtual_size(nsegment_addr->virtual_size());
31613247
phdr_segment->clear();
31623248
}
31633249

@@ -3439,6 +3525,27 @@ uint64_t Binary::page_size() const {
34393525
return LIEF::Binary::page_size();
34403526
}
34413527

3528+
uint64_t Binary::layout_pagesize() const {
3529+
uint64_t alignment = 0;
3530+
for (const Segment& segment : segments()) {
3531+
if (segment.is_load()) {
3532+
alignment = std::max(alignment, segment.alignment());
3533+
}
3534+
}
3535+
3536+
const uint64_t page_size = this->page_size();
3537+
3538+
// Check that the alignment used by the PT_LOAD segment is valid.
3539+
// Otherwise, default to the page_size
3540+
if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
3541+
alignment = page_size;
3542+
}
3543+
3544+
return std::max<uint64_t>(
3545+
page_size, std::min<uint64_t>(alignment, get_max_pagesize(*this))
3546+
);
3547+
}
3548+
34423549
const SymbolVersionRequirement*
34433550
Binary::find_version_requirement(const std::string& libname) const {
34443551
auto it = std::find_if(

‎src/ELF/Binary.tcc‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,19 @@ Segment* Binary::add_segment<Header::FILE_TYPE::EXEC>(const Segment& segment,
516516
uint64_t last_offset =
517517
std::max<uint64_t>(last_offset_sections, last_offset_segments);
518518

519-
const auto psize = page_size();
519+
const auto psize = layout_pagesize();
520520
const uint64_t last_offset_aligned = align(last_offset, psize);
521521
new_segment->file_offset(last_offset_aligned);
522522

523523
init_alignment(*this, *new_segment, this->ptr_size());
524524

525525
if (base == 0) {
526-
base = align(next_virtual_address(), new_segment->alignment());
526+
uint64_t address_alignment = new_segment->alignment();
527+
if (new_segment->is_load()) {
528+
address_alignment = std::max<uint64_t>(psize, new_segment->alignment());
529+
}
530+
531+
base = align(next_virtual_address(), address_alignment);
527532
}
528533

529534
if (segment.virtual_address() == 0) {
@@ -587,9 +592,19 @@ Segment* Binary::add_segment<Header::FILE_TYPE::EXEC>(const Segment& segment,
587592
template<>
588593
Segment* Binary::add_segment<Header::FILE_TYPE::DYN>(const Segment& segment,
589594
uint64_t base) {
590-
const auto psize = page_size();
595+
const auto psize = layout_pagesize();
591596
const auto ptr_size = this->ptr_size();
592-
/*const uint64_t new_phdr_offset = */ relocate_phdr_table_auto();
597+
const uint64_t new_phdr_offset = relocate_phdr_table_auto();
598+
599+
if (new_phdr_offset == 0) {
600+
LIEF_ERR("Failed to relocate the PHDR table for this binary");
601+
return nullptr;
602+
}
603+
604+
if (phdr_reloc_info_.nb_segments == 0) {
605+
LIEF_ERR("The segment table is full. We can't add segment");
606+
return nullptr;
607+
}
593608

594609
std::vector<uint8_t> content = as_vector(segment.content());
595610

@@ -622,8 +637,14 @@ Segment* Binary::add_segment<Header::FILE_TYPE::DYN>(const Segment& segment,
622637

623638
new_segment->file_offset(last_offset_aligned);
624639
if (base == 0) {
625-
const uint64_t alignment = new_segment->alignment();
626-
const uint64_t next_va = next_virtual_address();
640+
uint64_t alignment = new_segment->alignment();
641+
uint64_t next_va = next_virtual_address();
642+
643+
if (new_segment->is_load()) {
644+
alignment = std::max<uint64_t>(psize, new_segment->alignment());
645+
next_va = align(next_virtual_address(), psize);
646+
}
647+
627648
uint64_t virtual_address =
628649
align_down(next_va, alignment) + new_segment->file_offset() % alignment;
629650
if (virtual_address < next_va) {
@@ -673,6 +694,7 @@ Segment* Binary::add_segment<Header::FILE_TYPE::DYN>(const Segment& segment,
673694
seg_ptr =
674695
segments_.insert(segments_.begin() + idx, std::move(new_segment))->get();
675696
}
697+
phdr_reloc_info_.nb_segments--;
676698
assert(seg_ptr != nullptr);
677699
return seg_ptr;
678700
}

0 commit comments

Comments
 (0)