Repository navigation
[REBASE & FF] Unmap Free Pool Pages from Fixed Blocks - #1837
Conversation
✅ QEMU Validation PassedAll QEMU validation jobs completed successfully.
Workflow run: https://github.com/OpenDevicePartnership/patina/actions/runs/38303078234 Boot Time to EFI Shell
Dependencies
This comment was automatically generated by the Patina QEMU PR Validation Post workflow. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
patina-qemu validation is failing because I missed pushing a change. |
b09bf26 to
c931db6
Compare
Ended up splitting this into two more commits (the first two) to make it easier to review. |
7885614 to
3c41d0c
Compare
f63724f to
640f488
Compare
|
@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. |
|
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. |
640f488 to
623ed3c
Compare
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>
623ed3c to
464dd17
Compare
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.
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.