Skip to content
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

Split segment size fix #447

Merged
merged 3 commits into from
Jan 10, 2023
Merged

Split segment size fix #447

merged 3 commits into from
Jan 10, 2023

Commits on Dec 2, 2022

  1. shiftFile: when splitting a segment into two pieces, preserve the ori…

    …ginal flags in both
    Jason committed Dec 2, 2022
    Configuration menu
    Copy the full SHA
    f4f1848 View commit details
    Browse the repository at this point in the history
  2. Fix bug in file shifting that could cause conflicting PT_LOAD segments

    When a section in the file needs to be enlarged (e.g. to accommodate
    setting a larger RPATH), shiftFile() is used to shift all content
    following the growing section to a later position in the file.
    
    Commit 109b771 introduced logic to
    ensure that, after the segment split, no sections span multiple
    segments. This is done by sliding the portion of the segment after the
    split point later in the file, then adding a new PT_LOAD segment that
    contains the preceding data plus the extra room that is being added. The
    existing implementation does this by simply adding
    `extraPages*getPageSize()` bytes to the number of bytes ahead of the
    split point in the segment.
    
    However, this approach can result in two PT_LOAD segments that overlap
    when page boundaries are taken into account. As an example, this PT_LOAD
    section (taken from a Python 3.10 binary):
    
    LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                   0x0000000000000948 0x0000000000000948  R E    0x200000
    
    is split into the following two sections:
    
    LOAD           0x0000000000000000 0x00000000003ff000 0x00000000003ff000
                   0x0000000000001594 0x0000000000001594  R E    0x1000
    LOAD           0x0000000000001594 0x0000000000400594 0x0000000000400594
                   0x00000000000003b4 0x00000000000003b4  R E    0x1000
    
    Note that the two PT_LOAD sections both contain the memory page at
    address 0x400000. The Linux kernel's ELF loader (at least as of v4.18)
    does not accept this as a valid ELF executable, triggering a segfault
    with si_code=SI_KERNEL immediately when the binary is executed.
    
    The fix here is to set the length of the segment that comes before the
    split point more carefully; instead of adding `extraPages*getPageSize()`
    bytes to the portion of the segment that came before the split, the
    actual number of padding bytes that were needed (before rounding up to
    the next multiple of the page size) are used. This avoids the overlap
    in the PT_LOAD segments and makes the output files executable again.
    Jason committed Dec 2, 2022
    Configuration menu
    Copy the full SHA
    8d2cb4f View commit details
    Browse the repository at this point in the history

Commits on Dec 29, 2022

  1. Revert "shiftFile: when splitting a segment into two pieces, preserve…

    … the original flags in both"
    
    This reverts commit f4f1848.
    Jason committed Dec 29, 2022
    Configuration menu
    Copy the full SHA
    3a6d771 View commit details
    Browse the repository at this point in the history