Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TLN_SetRenderTarget with a larger pitch causes heap buffer overflow (segfault) #126

Open
alvinhochun opened this issue Jan 13, 2025 · 4 comments · May be fixed by #127
Open

TLN_SetRenderTarget with a larger pitch causes heap buffer overflow (segfault) #126

alvinhochun opened this issue Jan 13, 2025 · 4 comments · May be fixed by #127

Comments

@alvinhochun
Copy link

For example, if we do the following:

TLN_Init(240, 160, 4, 128, 0);
TLN_SetRenderTarget(framebuffer, 4 * 256); // texture width must be power-of-2

Then when calling TLN_UpdateFrame we get a heap buffer overflow at:

memset(engine->priority, 0, engine->framebuffer.pitch);

The buffer is allocated at:

context->priority = (uint32_t*)malloc(context->framebuffer.pitch);

@megamarc
Copy link
Owner

  • The framebuffer must be big enough to store a full frame, at 4 bytes/pixel (32 bpp). That is: w*h*4.
  • The pitch is the number of bytes that each scanline takes. That is, w*4
uint8_t framebuffer[240 * 160 * 4];

TLN_Init(240, 160, 4, 128, 0);
TLN_SetRenderTarget(framebuffer, 240 * 4);

BTW, texture width is not required to be power of 2

@alvinhochun
Copy link
Author

texture width is not required to be power of 2

It could be required on certain GPU architectures. The point is, it sometimes makes sense to want to have Tilengine draw each row of pixels with a "pitch" (also called "stride") of some specific alignment for various reasons, otherwise the user may have to memmove/memcpy each row to fix that.

Is the pitch argument not intended for this purpose? I mean, if it is supposed to be w * 4 then I don't see the point of having it as an argument.

@megamarc
Copy link
Owner

There isn't any problem in setting a pitch bigger that the required by the framebuffer (for example, setting 320x240 inside a texture of 512x256). Just make sure you're allocating the framebuffer correctly with required memory. I've tested right now and it works as expected.

@alvinhochun
Copy link
Author

I've tested right now and it works as expected.

Please test with ASan (-sanitize=address). With the code I am testing it shows a heap-buffer-overflow at the memset call I mentioned above.

Looking at the code:

  • When the buffer context->priority = (uint32_t*)malloc(context->framebuffer.pitch); is allocated, framebuffer.pitch is set to width * 4.
  • If TLN_SetRenderTarget is called with a larger pitch afterwards, framebuffer.pitch is set to that larger value.
  • Finally memset(engine->priority, 0, engine->framebuffer.pitch); this causes a buffer overriun because framebuffer.pitch is now larger than the allocation (width * 4).

@alvinhochun alvinhochun linked a pull request Feb 22, 2025 that will close this issue
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 a pull request may close this issue.

2 participants