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 · 2 comments
Open

Layout check of __LINKEDIT segment fails for modified binary #1151

DzenIsRich opened this issue Jan 7, 2025 · 2 comments
Assignees
Labels

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");
}

@romainthomas
Copy link
Member

True. Actually the binary instance after a write operation can't be considered as consistent.
This is why most of the tests are done on a reloaded instance.

I agree it's not ideal but it simplifies some parts of the write process

@romainthomas
Copy link
Member

Sorry actually I misread the where the error occurs. I agree that check_layout should run correctly after the extend_section.

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

No branches or pull requests

2 participants