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

Fix Wrong Arithmetic When Computing Guest Code Size #99

Closed
wants to merge 1 commit into from

Conversation

ppenna
Copy link

@ppenna ppenna commented Dec 5, 2024

Description

This PR closes #98

The ElfInfo::get_va_size() function is used for determining the size of the gest_code slot during the construction of the sand memory layout.

This function calculates the size based on the loadable program headers, which represent the size of the ELF binary when loaded into memory.

Although the current implementation works, it does not account for guest binaries that have non-loadable segments which still occupy memory at runtime, such as the BSS segment. As a result, the size computation for these binaries will be incorrect.

To fix this, we should consider arithmetic on sections that are tagged as SHF_ALLOC. From the ELF Specification [1]:

The section occupies memory during process execution. Some control
sections do not reside in the memory image of an object file; this attribute
is off for those sections.

Previously, we were using program headers to compute total size of ELF
memory when loaded in memory. Unfortunately, this is not enough, as some
segments that are not loaded may also allocate memory, such as the BSS.

This commit fixes this problem by changing this logic to use section
headers instead.

Signed-off-by: Pedro Henrique Penna <[email protected]>
@ppenna ppenna marked this pull request as draft December 5, 2024 17:51
@syntactically syntactically self-assigned this Dec 5, 2024
@ppenna
Copy link
Author

ppenna commented Dec 6, 2024

Hello @syntactically,

Thank you for the details. Based on them I was able to identify that this undefined behavior was triggered to other changes that I introduced in my private fork.

Please, close this PR.

@ppenna ppenna deleted the bugfix-elf-size branch February 11, 2025 23:00
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

Successfully merging this pull request may close these issues.

Wrong Arithmetic When Computing Guest Code Size
2 participants