Skip to content

fix texture handling UAF defect - #6047

Open
ab9rf wants to merge 1 commit into
DFHack:developfrom
ab9rf:textures-6045
Open

ab9rf wants to merge 1 commit into
DFHack:developfrom
ab9rf:textures-6045

Conversation

@ab9rf

@ab9rf ab9rf commented Nov 24, 2026

Copy link
Copy Markdown
Member

the problem: the prophylactic refcount++ was taken per handle, but DF frees per slot-occupancy. Anything that put the surface into raws an extra time (re-registration during the deferred-clear window, delayed-registration overlap) let DF's decrements outnumber our increments, allowing a surface to be freed while g_handle_to_surface still cached it, which lead to a UAF in deleteHandle

this code avoids this by ensuring refcounts are incremented before DF can possibly free them, validates that the texture in a slot to be freed is actually what it is supposed to be before freeing it or returning it to a caller, and avoids using operator[] on a map (which can insert undesired phantom keys)

FIxes #6045

As a note: this code has always had a slight risk of a race with DF's render thread - this code is called from the simulation thread and the render thread is consuming this data asynchronously so it's possible for this to cause the render thread to do something weird. However, this PR doesn't change this risk and no adverse effect has ever been observed. But we might need to revisit this if Bay12 ever makes significant changes to the renderer.

the problem: the prophylactic `refcount++` was taken per handle, but DF frees per slot-occupancy. Anything that put the surface into raws an extra time (re-registration during the deferred-clear window, delayed-registration overlap) let DF's decrements outnumber our increments, allowing a surface to be freed while `g_handle_to_surface` still cached it, which lead to a UAF in `deleteHandle`

this code avoids this by ensuring refcounts are incremented before DF can possibly free them, validates that the texture in a slot to be freed is actually what it is supposed to be before freeing it or returning it to a caller, and avoids using `operator[]` on a map (which can insert undesired phantom keys)

FIxes DFHack#6045
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.

Textures module can handle textures in unsafe ways

1 participant