Skip to content

fix mac UAF from async buffer read in update_with_buffer_stride - #401

Merged
emoon merged 3 commits into
emoon:masterfrom
ACK101101:fix/macos-async-buffer-uaf
Aug 6, 2026
Merged

emoon merged 3 commits into
emoon:masterfrom
ACK101101:fix/macos-async-buffer-uaf

Conversation

@ACK101101

@ACK101101 ACK101101 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Supplement to demonstrate the bug and the fix! https://github.com/ACK101101/minifb-mac-update-uaf

@ACK101101
ACK101101 marked this pull request as draft July 16, 2026 18:14
@emoon

emoon commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Hey, Thanks for the PR!

Can you describe more what this code fixes?

The synchronous [view draw] does fix the UAF, since replaceRegion:withBytes: copies the buffer before mfb_update_with_buffer returns.

Two suggestions:

  1. Clear the pointer after the draw, so any stray draw can never read freed memory (drawInMTKView already guards on a NULL buffer):
[view draw];
win->draw_parameters->buffer = NULL;
  1. view.paused = YES with the default enableSetNeedsDisplay = NO means the view only draws on the explicit [view draw]. During a live resize drag AppKit's tracking loop blocks the app's update loop, so the window shows a stale stretched frame until the drag ends. With the pointer cleared as above, it's safe to also set view.enableSetNeedsDisplay = YES so resize/expose repaints still work.

@ACK101101

ACK101101 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Makes sense, thank you for the suggestions! I am new to this. I ran into this bug doing my first project in rust.

When trying out the change there, I see the window being drawn while drag moved, but not while being drag resized. I am unsure if I have it misconfigured or if there is something else I am missing.

@ACK101101
ACK101101 marked this pull request as ready for review July 16, 2026 19:31
@emoon

emoon commented Jul 16, 2026

Copy link
Copy Markdown
Owner

hum... I don't have access to a mac right now, but I can try it out to tomorrow.

@emoon

emoon commented Jul 20, 2026

Copy link
Copy Markdown
Owner

Sorry, I haven't managed to get around to check this yet. Will try as soon as I can

@emoon

emoon commented Jul 26, 2026

Copy link
Copy Markdown
Owner

I had a look and what I think we need to do is to remove this line

view.enableSetNeedsDisplay = YES;

The issue is that to fix the resize issue with this enabled requires quite bit of more changes when this is enabled, which I think need it's own PR.

@ACK101101

Copy link
Copy Markdown
Contributor Author

Gotcha, just removed that line

@emoon

emoon commented Jul 28, 2026

Copy link
Copy Markdown
Owner

Cool. Does this solve the current resize issue?

@ACK101101

Copy link
Copy Markdown
Contributor Author

Sorry for the late response! I'm still seeing the resize issue on my project. Maybe that should be a separate issue still?

@emoon
emoon merged commit af5ca3a into emoon:master Aug 6, 2026
4 checks passed
@emoon

emoon commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Sounds good!

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.

2 participants