Skip to content

Commit

Permalink
alt_saver: some bugfixes for user-reported issues (#46)
Browse files Browse the repository at this point in the history
* alt_saver: port fix for segment shifting breaking some systems

In certain circumstances, in particular with older kernels, some executables could fail to run after shifting. This was never seen on modern Ubuntu but was seen on RHEL.

NixOS/patchelf@8d2cb4f

* alt_saver: fix crash on invalid symbol section

This was an error in porting and thus a patchelf.rb specific issue.
  • Loading branch information
Bo98 authored Apr 21, 2024
1 parent 3e51189 commit e060c6c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/patchelf/alt_saver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,10 @@ def rewrite_headers(phdr_address)

begin
new_index = new_section_idx(old_shndx)
next unless new_index
rescue ArgumentError
Logger.warn "entry #{entry} in symbol table refers to a non existing section, skipping"
end
next unless new_index

sym[pack[:st_shndx]] = new_index

Expand Down Expand Up @@ -561,11 +561,12 @@ def rewrite_sections_executable
if needed_space > start_offset
needed_space += seg_num_bytes # new load segment is required

needed_pages = Helper.alignup(needed_space - start_offset, page_size) / page_size
extra_bytes = needed_space - start_offset
needed_pages = Helper.alignup(extra_bytes, page_size) / page_size
Logger.debug "needed pages is #{needed_pages}"
raise PatchError, 'virtual address space underrun' if needed_pages * page_size > first_page

shift_file(needed_pages, start_offset)
shift_file(needed_pages, start_offset, extra_bytes)

first_page -= needed_pages * page_size
start_offset += needed_pages * page_size
Expand Down Expand Up @@ -776,7 +777,7 @@ def shift_segments(shift, start_offset)
end
# rubocop:enable Metrics/PerceivedComplexity

def shift_file(extra_pages, start_offset)
def shift_file(extra_pages, start_offset, extra_bytes)
raise PatchError, "start_offset(#{start_offset}) < ehdr.num_bytes" if start_offset < ehdr.num_bytes

oldsz = @buffer.size
Expand All @@ -799,8 +800,8 @@ def shift_file(extra_pages, start_offset)
p_offset: split_phdr.p_offset - split_shift - shift,
p_vaddr: split_phdr.p_vaddr - split_shift - shift,
p_paddr: split_phdr.p_paddr - split_shift - shift,
p_filesz: split_shift + shift,
p_memsz: split_shift + shift,
p_filesz: split_shift + extra_bytes,
p_memsz: split_shift + extra_bytes,
p_flags: ELFTools::Constants::PF_R | ELFTools::Constants::PF_W,
p_align: page_size
)
Expand Down

0 comments on commit e060c6c

Please sign in to comment.