Skip to content

[REBASE & FF] Unmap Free Pool Pages from Fixed Blocks - #1837

Merged
os-d merged 5 commits into
OpenDevicePartnership:mainfrom
os-d:free_pool_pages_rebase
Oct 12, 2026
Merged

os-d merged 5 commits into
OpenDevicePartnership:mainfrom
os-d:free_pool_pages_rebase

Conversation

@os-d

@os-d os-d commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR contains five commits that strive to catch use-after-free bugs.

gcd: Ensure Root Page is Identity Mapped

The current GCD logic relies on mapping the root page of the page table by accident; it uses the internal allocate_memory_space() API that does not remove the RP attribute. However, the init_paging code then maps all allocated memory, so it would get mapped.

This logic is brittle and does not work when allocator pages are partially unmapped.

This commit changes the logic to explicitly map the root page.

GCD: Ensure RP Descs are not Mapped

In preparation for the allocators unmapping the fixed block pool pages, the GCD initial mapping needs to ensure it is not mapping any descriptors with RP set.

This commit also moves the memory allocation before we add the page table reference to the GCD so that the initial allocator expansion and page mapping/unmapping has been recorded in the GCD so we can a consistent state before doing the initial mapping in the page table. Otherwise we can end up with a torn state initially.

allocator: Unmap Unused Pool Pages

Currently, the heap allocators allocate memory in blocks from the GCD and keep it forever, never unmapping it.

This commit updates their behavior to track pool usage in pages and unmap the pages when they are emptied. The pool pages also start out unmapped.

This allows for a wider window of use-after-free bugs to be caught because these access will become page faults. It cannot catch every use-after-free unless it is also coupled with Pool Guard (which is not yet implemented), which effectively forces all pool allocations into page allocations.

The first page of every allocator expansion is used to host metadata about the expansion region, so this must always remain mapped. Because the allocators expand at larger than page granularity, typically, the memory is never freed back to the GCD because this would require the entire region be freed back, which cannot be done, both because the metadata page is always in use and because we are unmapping pages piecemeal to catch use-after-free faster.

Performance testing on physical machines has shown that there is not a great change with this; test runs have seen this take anywhere from 100ms less than the mainline to 200ms more than the mainline.

allocator: Push Free Block to Tail of Free List

Currently, the allocators push free blocks onto the heads of the free block lists. This means that addresses are frequently reused; the next allocation of the same size will use that memory.

This commit pushes the free blocks to the end of the list to maximize the time before a free block is handed out again, to both make it more likely that a use-after-free bug is caught (because the whole page is unmapped when all the blocks are free and blocks spending longer on the free list makes it more likely they will all become free) and make it less likely that a use-after-free will corrupt other memory (instead it would continue to be valid for a longer period, until the page is unmapped and use-after-free is caught or the block is handed out again).

Allocator: Reclaim Pages in FIFO Order

This commit updates the reclaim semantics for the pages backing the fixed pool blocks to reclaim them in FIFO order, maximizing the time between address reuse.

  • Impacts functionality?
  • Impacts security?
  • Breaking change?
  • Includes tests?
  • Includes documentation?

How This Was Tested

Tested on Q35, ArmVirt, physical Intel, and physical ARM64. Perf testing was done on physical Intel and physical ARM64, where the range was 100ms faster than baseline to 200ms slower than baseline.

Integration Instructions

If page faults occur after this change, use-after-free has been caught and should be fixed.

@patina-automation

patina-automation Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

✅ QEMU Validation Passed

All QEMU validation jobs completed successfully.

Note: Q35 is only built on Windows hosts (QEMU boot is disabled due to a QEMU vfat issue).

Workflow run: https://github.com/OpenDevicePartnership/patina/actions/runs/38303078234

Boot Time to EFI Shell

Platform Elapsed
ArmVirt (Linux Host) 21.5s
Q35 (Linux Host) 28.8s

Dependencies

Repository Ref
patina 464dd17
patina-dxe-core-qemu 93ef089
patina-fw-patcher a450500
patina-qemu firmware v4.1.2
patina-qemu build script f212970

This comment was automatically generated by the Patina QEMU PR Validation Post workflow.

@github-actions github-actions Bot added the impact:security Has a security impact label Oct 5, 2026
@codecov

codecov Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.64569% with 53 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...e_core/src/allocator/fixed_size_block_allocator.rs 87.03% 52 Missing ⚠️
patina_dxe_core/src/gcd/spin_locked_gcd/paging.rs 95.65% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@os-d

os-d commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

patina-qemu validation is failing because I missed pushing a change.

Comment thread docs/src/dxe_core/memory_management.md Resolved
@os-d
os-d force-pushed the free_pool_pages_rebase branch from b09bf26 to c931db6 Compare October 6, 2026 18:28
@os-d

os-d commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

patina-qemu validation is failing because I missed pushing a change.

Ended up splitting this into two more commits (the first two) to make it easier to review.

@os-d os-d changed the title [REBASE & FF] Unmap Free Pool Pages [REBASE & FF] Unmap Free Pool Pages from Fixed Blocks Oct 6, 2026
@os-d
os-d force-pushed the free_pool_pages_rebase branch 3 times, most recently from 7885614 to 3c41d0c Compare October 6, 2026 22:36
Comment thread docs/src/dxe_core/memory_management.md Resolved
Comment thread docs/src/dxe_core/memory_management.md Resolved
@os-d
os-d force-pushed the free_pool_pages_rebase branch 3 times, most recently from f63724f to 640f488 Compare October 8, 2026 16:12
@os-d

os-d commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

@cfernald @makubacki @joschock @Javagedes I have addressed all feedback (except for dropping the fallback allocator node caching which I am doing perf testing on now). Please re-review.

@os-d

os-d commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Pushing the last requested change, also fixed where I had accidentally applied more changes to the last commit that should have gone to an earlier commit.

@os-d
os-d force-pushed the free_pool_pages_rebase branch from 640f488 to 623ed3c Compare October 8, 2026 22:49
os-d added 5 commits October 12, 2026 13:03
The current GCD logic relies on mapping
the root page of the page table by accident;
it uses the internal allocate_memory_space()
API that does not remove the RP attribute.
However, the init_paging code then maps all
allocated memory, so it would get mapped.

This logic is brittle and does not work when
allocator pages are partially unmapped.

This commit changes the logic to explicitly map
the root page.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
In preparation for the allocators unmapping the
fixed block pool pages, the GCD initial mapping
needs to ensure it is not mapping any descriptors with
RP set.

This commit also moves the memory allocation before
we add the page table reference to the GCD so that
the initial allocator expansion and page mapping/unmapping
has been recorded in the GCD so we can a consistent
state before doing the initial mapping in the page table.
Otherwise we can end up with a torn state initially.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
Currently, the heap allocators allocate memory in blocks
from the GCD and keep it forever, never unmapping it.

This commit updates their behavior to track pool usage in
pages and unmap the pages when they are emptied. The pool
pages also start out unmapped. This only targets the fixed
block lists that we explicitly maintain, not the fallback
allocator. That will be targeted in a later change.

This allows for a wider window of use-after-free bugs to
be caught because these access will become page faults. It
cannot catch every use-after-free unless it is also coupled
with Pool Guard (which is not yet implemented), which effectively
forces all pool allocations into page allocations.

The first page of every allocator expansion is used to host metadata
about the expansion region, so this must always remain mapped.
Because the allocators expand at larger than page granularity,
typically, the memory is never freed back to the GCD because this
would require the entire region be freed back, which cannot be done,
both because the metadata page is always in use and because we are
unmapping pages piecemeal to catch use-after-free faster.

Performance testing on physical machines has shown that there is not
a great change with this; test runs have seen this take anywhere from
100ms less than the mainline to 200ms more than the mainline.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
Currently, the allocators push free blocks onto the
heads of the free block lists. This means that
addresses are frequently reused; the next allocation of
the same size will use that memory.

This commit pushes the free blocks to the end of the list
to maximize the time before a free block is handed out again,
to both make it more likely that a use-after-free bug is caught
(because the whole page is unmapped when all the blocks are
free and blocks spending longer on the free list makes it more
likely they will all become free) and make it less likely that
a use-after-free will corrupt other memory (instead it would
continue to be valid for a longer period, until the page is
unmapped and use-after-free is caught or the block is handed out
again).

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
This commit updates the reclaim semantics
for the pages backing the fixed pool blocks to
reclaim them in FIFO order, maximizing the time
between address reuse.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
@os-d
os-d force-pushed the free_pool_pages_rebase branch from 623ed3c to 464dd17 Compare October 12, 2026 20:03
@os-d
os-d enabled auto-merge (rebase) October 12, 2026 20:08
@os-d
os-d merged commit 6ce0b54 into OpenDevicePartnership:main Oct 12, 2026
12 checks passed
@os-d os-d linked an issue Oct 14, 2026 that may be closed by this pull request
@os-d os-d mentioned this pull request Oct 23, 2026
2 of 5 tasks
@os-d
os-d deleted the free_pool_pages_rebase branch October 30, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

impact:security Has a security impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Unmap Unused Allocator Pages

6 participants