-
Notifications
You must be signed in to change notification settings - Fork 221
Fix excessive stack usage when calling vorbis_analysis_wrote
with lots of samples
#104
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
Conversation
352b150
to
c9694a0
Compare
…ots of samples `vorbis_analysis_wrote` increments `v->pcm_current` by `vals`, and this incremented value can be used by `_preextrapolate_helper` right after to allocate a float array in the stack `v->pcm_current` positions large. Clearly, since `alloca` does not check that there is enough stack space available to satisfy the allocation request, this can lead to a stack overflow and memory corruption, which at best have no effect, more likely cause segmentation faults, and at worst introduce security risks. The documentation for `vorbis_analysis_buffer` and `vorbis_analysis_wrote` does not specify a maximum value for `vals`. It states that "1024 is a reasonable choice", but callers are free to use larger or smaller counts as they wish. Therefore, `libvorbis` not handling this case is undesirable behavior. To better handle this case without throwing the performance benefits of `alloca` out the window, let's check whether the allocation would exceed 256 KiB (an estimate for the minimum stack space available is 1 MiB, which is [the default on Windows platforms](https://learn.microsoft.com/en-us/windows/win32/procthread/thread-stack-size)), and if so fall back to a heap allocated array. The heap array that may be allocated for this purpose is freed when `vorbis_dsp_clear` is called. `_preextrapolate_helper` takes neglible execution time compared to the encoding process for usual sample block sizes, though. Signed-off-by: Alejandro González <[email protected]>
c9694a0
to
f3c156f
Compare
Thanks for this contribution and for pinging us on IRC, just a few comments inline. Usually development happens on gitlab.xiph.org (which you'll need to have an account approved for, as you mentioned in #vorbis) but since this has been waiting a long time you can just apply your changes here and I'll ensure the MR gets created there. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the review and comments, @tmatth! 🎉
I've just pushed a commit that should address your review comments. Please let me know if there is anything else I can help with 🙌
Merged in https://gitlab.xiph.org/xiph/vorbis/-/merge_requests/38 (with the limit lowered to 8kB as suggested by @tterribe). Thanks for the contribution. |
vorbis_analysis_wrote
incrementsv->pcm_current
byvals
, and this incremented value can be used by_preextrapolate_helper
right after to allocate a float array in the stackv->pcm_current
positions large. Clearly, sincealloca
does not check that there is enough stack space available to satisfy the allocation request, this can lead to a stack overflow and memory corruption, which at best have no effect, more likely cause segmentation faults, and at worst introduce security risks.The documentation for
vorbis_analysis_buffer
andvorbis_analysis_wrote
does not specify a maximum value forvals
. It states that "1024 is a reasonable choice", but callers are free to use larger or smaller counts as they wish. Therefore,libvorbis
not handling this case is undesirable behavior.To better handle this case without throwing the performance benefits of
alloca
out the window, let's check whether the allocation would exceed 256 KiB (an estimate for the minimum stack space available is 1 MiB, which is the default on Windows platforms), and if so fall back to a heap allocated array. The heap array that may be allocated for this purpose is freed whenvorbis_dsp_clear
is called._preextrapolate_helper
takes neglible execution time compared to the encoding process for usual sample block sizes, though.