Freeing a guest address region lets go of the lock before it discards the pages, so a new region can be handed the same range and lose its first contents #1610
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot#1610
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type: correctness · Area: host, guest address regions · Part of: #1493
TL;DR
hos_region_freeremoves a region from the list, releases the lock, and only then discards the region's pages. A thread that allocates a region in between can be given the same address range and start writing to it, and the discard then wipes what it wrote. We found this by reading the code; no run has shown it. We propose to keep the range unavailable to allocations until the discard has finished, and a fixture that checks the order.Breakdown
Evidence
Code on
forge/mainat430eef4f2:host/hos_mem.c:190-205,hos_region_free: underLOCK()it finds the region, marks its pages used (pages_mark_used_locked), removes the entry withmemmoveand decrementsnregions.UNLOCK()is at:203. Only then, at:204,if (freed_size) discard_pages(freed, freed_size).:97-141,hos_region_alloc: first fit from the top over the sorted list underLOCK(), inserts the new entry,UNLOCK()at:139, thenhos_commit(found, size)at:140. Between:203and:204the freed range is in no list, so this function can return it.:152-177,discard_pages: on WindowsDiscardVirtualMemory(or aMEM_RESET), on Linux a hole punched in the guest window (hos_posix_guest_discard), and a memset of zeros when a Vulkan import holds the pages. Any of these removes data another thread has just written.hos_region_freeorhos_region_alloc. The two checks underteststhat mentionhost/hos_mem.care abouthos_mark_written.The fork's claim, not ours: an outside audit of a fork says the fork fixed the same race by marking the region as freeing, discarding while it was still listed, then removing it, with the lock dropped for the discard. We have not read that code and do not rely on it. The rule is simple enough to write from the problem.
Proposed change
hos_region_freesets it under the lock, releases the lock, discards the pages, takes the lock again and removes the entry.hos_region_alloctreats a marked entry as occupied.hos_regionsandhos_mem_region(the control bridge's range checks) keep listing it as mapped until it is removed.hos_commitreturns 0 when it is refused (host/hos.h:75), and its result is ignored athos_mem.c:140and inMapPhysicalMemory(host/hos_svc.c:471-475).How we would test it
host, that builds the region functions with hooks for the lock and for the discard. It frees a region and, when the discard hook fires, asks whether an allocation of the same size could return that range. Today it can, because the list no longer holds the entry. After the change it cannot.Related
New on this forge; not opened on GitHub.
Possibly related, for context: