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

[CHERRY-PICK] StandaloneMmPkg: Call PeCoffLoaderUnloadImage When Unloading Image #1252

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

os-d
Copy link
Contributor

@os-d os-d commented Jan 9, 2025

Description

Today, StandaloneMmCore calls PeCoffLoaderRelocateImage() when loading images, which calls PeCoffLoaderRelocateImageExtraAction(). On AARCH64, this sets the image memory protections accordingly, RO + E on code sections, RW + NX on data sections.

However, if an image fails to start (i.e. its entry point returns a failure) StandaloneMmCore does not call the corresponding PeCoffLoaderUnloadImage, which calls PeCoffLoaderUnloadImageExtraAction, which on AARCH64 undoes the memory protections on the image, setting the whole memory region back to RW + NX. The core then frees this memory and the next allocation attempts to use it, which results in a data abort if a read only memory region is attempted to be written to. Theoretically, other instances of the PeCoffExtraActionLib could take other actions and so regardless of architecture, the contract with the PeCoffLoader should be maintained.

This patch calls PeCoffLoaderUnloadImage when an image's entry point returns a failure, before freeing the image memory. This meets the contract and follows the DXE core behavior.

Cherry-picked (with a little massaging due to uncherry-picked changes in edk2) from: tianocore/edk2@21cbba1

  • Impacts functionality?
  • Impacts security?
  • Breaking change?
  • Includes tests?
  • Includes documentation?
  • Backport to release branch?

How This Was Tested

Tested on an AARCH64 platform where a Standalone MM driver was returning an error, causing the system to crash. With this patch, the system can boot.

Integration Instructions

N/A.

@os-d os-d requested review from cfernald and kuqin12 January 9, 2025 17:28
@github-actions github-actions bot added impact:non-functional Does not have a functional impact type:backport Backport changes in a dev branch PR to its release branch. labels Jan 9, 2025
@os-d os-d added type:bug Something isn't working and removed impact:non-functional Does not have a functional impact labels Jan 9, 2025
@os-d os-d linked an issue Jan 9, 2025 that may be closed by this pull request
1 task
…ading Image

Today, StandaloneMmCore calls PeCoffLoaderRelocateImage() when loading
images, which calls PeCoffLoaderRelocateImageExtraAction(). On AARCH64,
this sets the image memory protections accordingly, RO + E on code
sections, RW + NX on data sections.

However, if an image fails to start (i.e. its entry point returns a
failure) StandaloneMmCore does not call the corresponding
PeCoffLoaderUnloadImage, which calls PeCoffLoaderUnloadImageExtraAction,
which on AARCH64 undoes the memory protections on the image, setting the
whole memory region back to RW + NX. The core then frees this memory
and the next allocation attempts to use it, which results in a data
abort if a read only memory region is attempted to be written to.
Theoretically, other instances of the PeCoffExtraActionLib could take
other actions and so regardless of architecture, the contract with the
PeCoffLoader should be maintained.

This patch calls PeCoffLoaderUnloadImage when an image's entry point
returns a failure, before freeing the image memory. This meets the
contract and follows the DXE core behavior.

Signed-off-by: Oliver Smith-Denny <[email protected]>
@os-d os-d enabled auto-merge (rebase) January 9, 2025 21:30
@os-d os-d merged commit cd81b47 into microsoft:dev/202405 Jan 9, 2025
31 checks passed
@os-d os-d deleted the cp_9 branch January 9, 2025 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:backport Backport changes in a dev branch PR to its release branch. type:bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: StandaloneMmPkg does not clear memory protections on unloaded images
3 participants