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

Potential segmentation fault due to incorrect realloc in CABAC_encoder_bitstream::check_size_and_resize #419

Closed
litios opened this issue Aug 30, 2023 · 1 comment

Comments

@litios
Copy link

litios commented Aug 30, 2023

Summary

There is a segmentation fault in CABAC_encoder_bitstream::append_byte and CABAC_encoder_bitstream::write_startcode if the call to realloc fails in check_size_and_resize

Tested with:

  • Commit: 6fc550f (master)
  • PoC: any YUV input file.
  • cmd: ./enc265/enc265 -i <yuv file>

Analysis

When executing CABAC_encoder_bitstream::check_size_and_resize, there is no check to ensure the call to realloc is successful.

In case it is not, it will return NULL that will be set as the value of data_mem. Later on, the variable is accessed in CABAC_encoder_bitstream::append_byte:

  if (byte<=3) {
    /**/ if (state< 2 && byte==0) { state++; }
    else if (state==2 && byte<=3) {
      data_mem[ data_size++ ] = 3;

      if (byte==0) state=1;
      else         state=0;
    }
    else { state=0; }
  }
  else { state=0; }


  // write actual data byte

  data_mem[ data_size++ ] = byte;

and CABAC_encoder_bitstream::write_startcode:

  data_mem[ data_size+0 ] = 0;
  data_mem[ data_size+1 ] = 0;
  data_mem[ data_size+2 ] = 1;
  data_size+=3;

resulting in a segmentation fault due to trying to access a NULL pointer.

Impact

  • Crash

Patch

In order to prevent a crash, a check for data_mem not being NULL should be added that can handle the case appropriately.

farindk added a commit that referenced this issue Sep 1, 2023
@farindk
Copy link
Contributor

farindk commented Sep 1, 2023

Thanks. I have fixed the potential crash.
Note that this code is used only in the encoder part and that is not used anywhere.

@farindk farindk closed this as completed Sep 1, 2023
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

No branches or pull requests

2 participants