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

Improved allocation of .bss(-like) sections #505

Merged

Conversation

alchzh
Copy link
Collaborator

@alchzh alchzh commented Oct 24, 2024

The existing strategy for allocating .bss in RAM has a few problems

  • only one contiguous .bss region per FEM is supported.
  • alignment was not properly calculated for .bss, leading to linker errors

This commit generalizes our process for allocating .text, .data, .rodata, etc. to .bss and like as well. It also improves our support for different platforms by reducing the use of hardcoded section names for determining behavior in favor of attributes defined by the binary format like sh_type and sh_flags

With the new system, instead of passing unsafe_bss_segment to make_fem, .bss sections are allocated during the allocate_bom step. This is supported by free space regions that don't map to any data on the parent resource. The new code allows .bss regions to be allocated in existing RW regions that are mapped to data by zeroing out those areas.

Changes (incomplete):

  • Use SHT_NOBITS type to detect .bss(-like) sections on ELF toolchains

  • Create FreeSpaceWithoutData tag for allocating regions for .bss. These are created from MemoryRegion children of resources with data_range=None

  • Use SHF_ALLOC flag to determine whether section needs to be patched or not instead of hard coded list of names.

  • Pass flag to compiler to prevent .eh_frame generation

  • Backwards compatible with old bss allocation for now, emits deprecation warnings.

  • I have reviewed the OFRAK contributor guide and attest that this pull request is in accordance with it.

The existing strategy for allocating `.bss` in RAM has a few problems
 - only one contiguous `.bss` region per FEM is supported.
 - alignment was not properly calculated for `.bss`, leading to linker errors

This commit generalizes our process for allocating `.text`, `.data`,
`.rodata`, etc. to `.bss` and like as well. It also improves our support
for different platforms by reducing the use of hardcoded section names
for determining behavior in favor of attributes defined by the binary
format like `sh_type` and `sh_flags`

With the new system, instead of passing `unsafe_bss_segment` to
`make_fem`, `.bss` sections are allocated during the `allocate_bom`
step. This is supported by free space regions that don't map to any
data on the parent resource.

Changes (incomplete):
- Use SHT_NOBITS type to detect .bss(-like) sections on ELF toolchains
- Create `FreeSpaceWithoutData`` tag for allocating regions for .bss.
  These are created from `MemoryRegion` children of resources with
  `data_range=None`
- Use SHF_ALLOC flag to determine whether section needs to be patched
  or not instead of hard coded list of names.
- Pass flag to compiler to prevent `.eh_frame` generation
- Backwards compatible with old bss allocation for now, emits
  deprecation warnings.
@alchzh alchzh marked this pull request as draft November 18, 2024 20:32
@alchzh
Copy link
Collaborator Author

alchzh commented Nov 18, 2024

Marking as draft as I work on better test cases for this

@alchzh alchzh marked this pull request as ready for review November 19, 2024 21:14
@alchzh alchzh requested a review from rbs-jacob November 19, 2024 21:14
@alchzh
Copy link
Collaborator Author

alchzh commented Nov 19, 2024

test cases done

@dannyp303 dannyp303 self-requested a review November 22, 2024 20:10
@dannyp303
Copy link
Collaborator

This is good to merge IMO, after merge conflicts are done, sorry for the delayed response.

Copy link
Collaborator

@dannyp303 dannyp303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGT$

@alchzh alchzh merged commit 71348a6 into redballoonsecurity:master Jan 3, 2025
4 checks passed
alchzh added a commit that referenced this pull request Jan 13, 2025
whyitfor pushed a commit that referenced this pull request Jan 15, 2025
…#505 (#569)

* Fix typo in free_space.py introduced in #505

* Change free space tests to use a memoryregion with non-zero offset in parent to catch bugs

* Improve clarity of free space test

* Reference new PR in the changelog line
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.

3 participants