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

Layout check of __LINKEDIT segment fails for modified binary #1151

Open
DzenIsRich opened this issue Jan 7, 2025 · 0 comments
Open

Layout check of __LINKEDIT segment fails for modified binary #1151

DzenIsRich opened this issue Jan 7, 2025 · 0 comments
Assignees

Comments

@DzenIsRich
Copy link
Contributor

Consider the following tests/macho/test_builder.py test:

def test_extend_section_2(tmp_path):
    """ This test makes multiple calls to add_section,
        and then it makes calls to extend_section.
    """
    bin_path = pathlib.Path(get_sample("MachO/MachO64_x86-64_binary_id.bin"))
    original = lief.MachO.parse(bin_path.as_posix()).at(0)
    output = f"{tmp_path}/test_extend_section.bin"

    text_segment = original.get_segment("__TEXT")

    sections = []
    for i in range(3):
        section = lief.MachO.Section(f"__lief_{i}")
        section.alignment = 2 # 2^2 == 4 bytes
        sections.append(original.add_section(text_segment, section))

    for section in sections:
        assert original.extend_section(section, 1000)

    checked, err = lief.MachO.check_layout(original)
    assert checked, err # <------------------------ THIS CHECK FAILS

    original.write(output)
    new = lief.MachO.parse(output).at(0)

    checked, err = lief.MachO.check_layout(new)
    assert checked, err # <------------------------ THIS CHECK SUCCEEDS

I think that the problem is caused by not updated original_size field of a Binary:

if (linkedit->file_offset() + linkedit->file_size() != binary.original_size()) {
return error("__LINKEDIT segment does not wrap the end of the binary");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants