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

svcenc: Integer overflow in irc_ba_get_cur_frm_est_texture_bits #75

Closed
wants to merge 1 commit into from

Conversation

AshwinNatesan-ittiam
Copy link
Contributor

RC was incorrectly handling cases where 'I_TO_P_BIT_RATIO' multiplied by 'i4_est_texture_bits_for_frm' (which stores the estimated texture bits for the current frame), exceeded the range of int32_t. This has been fixed by clipping the product of the two quantities above.

Bug = ossfuzz:63175
Test: svc_enc_fuzzer

encoder/irc_datatypes.h Outdated Show resolved Hide resolved
encoder/irc_datatypes.h Outdated Show resolved Hide resolved
encoder/irc_bit_allocation.c Outdated Show resolved Hide resolved
encoder/irc_bit_allocation.c Outdated Show resolved Hide resolved
@harishdm harishdm requested a review from ram-mohan October 16, 2023 14:00
@harishdm
Copy link
Member

@AshwinNatesan-ittiam the issue being addressed here may be related to oss-fuzz:63044. There is another CL in internal review that is being validated to that issue. Maybe it is better to wait for that fix to land and see if this change is really needed.

@AshwinNatesan-ittiam
Copy link
Contributor Author

@AshwinNatesan-ittiam the issue being addressed here may be related to oss-fuzz:63044. There is another CL in internal review that is being validated to that issue. Maybe it is better to wait for that fix to land and see if this change is really needed.

I will wait for the other bug to be fixed. I will also push the changes you asked for after the other bug is fixed, if needed.

@AshwinNatesan-ittiam
Copy link
Contributor Author

AshwinNatesan-ittiam commented Oct 20, 2023

@harishdm

I have rebased the branch atop main. The latest commit doesn't fix the overflow in svcenc.

I have also included all of the changes requested for in the review.

i4_est_texture_bits_for_frm;
if(I_PIC == e_pic_type)
{
WORD64 i4_i_pic_bits =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ashwin,
We would need to investigate a bit more on why i4_i_pic_bits going beyond INT32_MAX.
what were the values of bitrate, framerate that lead to this high value and if something can be checked in svc encoder's configure level that would be ideal.
Also note, this doesn't show up in avc encoder, so maybe svc encoder is indeed missing some checks during initial set up?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will gather some data and see how this can be detected during init.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked deeper and it seems like the issue was being caused by incorrect handling of constraints on the order of control commands, specifically set_dimensions and set_frame_rate. These are expected to occur in that order since the first one eventually triggers rc_init, which initialises frame_time struct used by a function eventually invoked by set_frame_rate.

Also, I noticed the CI fuzzer failures with this change. I am looking into them.

@ram-mohan
Copy link
Collaborator

Instead of rejecting initializations if they are not in the desired order, would it benefit if all the init commands are captured and applied at once?
main...ram-mohan:libavc:configupdate

@AshwinNatesan-ittiam
Copy link
Contributor Author

Instead of rejecting initializations if they are not in the desired order, would it benefit if all the init commands are captured and applied at once? main...ram-mohan:libavc:configupdate

Yes, I agree this is a better approach.

But, what happens if one of the control commands, for example the set_dimensions command, fails? In this PR's bug, this would cause the frame_rate used by RC to be set to some garbage value, since the fuzzer doesn't check errors for any of the control commands. Shouldn't we then still be checking for the ordering of at least the two control commands here?

@ram-mohan
Copy link
Collaborator

I assume the set dimensions sets resolution of current encode. If this fails, then codec will be in un init state. Any further encode calls should return with error. Any error that is fatal puts component in permanent error state and encode calls shall be rejected.
https://github.com/ram-mohan/libavc/blob/configupdate/encoder/ih264e_encode.c
Line 241, 260 checks for fatal errors, Init failure sets these line 295, 315, ...

@AshwinNatesan-ittiam
Copy link
Contributor Author

I assume the set dimensions sets resolution of current encode. If this fails, then codec will be in un init state. Any further encode calls should return with error. Any error that is fatal puts component in permanent error state and encode calls shall be rejected. https://github.com/ram-mohan/libavc/blob/configupdate/encoder/ih264e_encode.c Line 241, 260 checks for fatal errors, Init failure sets these line 295, 315, ...

Note that there was no init failure in the fuzzer test that resulted in this PR. Init failures will result in fatal errors in svcenc and svc_enc_fuzzer as well.

The 'ISVCE_CMD_CTL_SET_DIMENSIONS' was failing because the input dimensions were incorrect. But in 'codec_update_config', calls to rc_init is invoked only when call to 'ISVCE_CMD_CTL_SET_DIMENSIONS' returns successfully.

'ISVCE_CMD_CTL_SET_DIMENSIONS' is expected to occur before
'ISVCE_CMD_CTL_SET_FRAMERATE'. This is necesssary since
'isvce_rc_init' initialises the frame_time_t struct used
by 'ih264e_frame_time_update_src_frame_rate'. This state
was not being handled correctly, and consequently, many
of the computations in RC were using incorrectly initialised
values for frame rate. This was resulting in signed integer
overflow of 'i4_est_texture_bits_for_frm'.

Bug = ossfuzz:63175
Test: svc_enc_fuzzer
@harishdm harishdm closed this Dec 13, 2023
@harishdm harishdm deleted the ossFuzz-63175 branch December 13, 2023 05:02
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.

3 participants