diff mbox series

[FFmpeg-devel,3/4] avcodec/h264_parser: Fix nalsize check

Message ID 20210905192418.25970-3-michael@niedermayer.cc
State New
Headers show
Series [FFmpeg-devel,1/4] avformat/mxfdec: check channel number in mxf_get_d10_aes3_packet() | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Michael Niedermayer Sept. 5, 2021, 7:24 p.m. UTC
Fixes: Assertion failure
Fixes: 37463/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_H264_fuzzer-4914693494931456

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/h264_parser.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Andreas Rheinhardt Sept. 5, 2021, 7:51 p.m. UTC | #1
Michael Niedermayer:
> Fixes: Assertion failure
> Fixes: 37463/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_H264_fuzzer-4914693494931456
> 
> Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavcodec/h264_parser.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
> index 01ea016409..47cc1d5c78 100644
> --- a/libavcodec/h264_parser.c
> +++ b/libavcodec/h264_parser.c
> @@ -87,7 +87,7 @@ static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
>              i = next_avc;
>              for (j = 0; j < p->nal_length_size; j++)
>                  nalsize = (nalsize << 8) | buf[i++];
> -            if (!nalsize || nalsize > buf_size - i) {
> +            if (!nalsize || i > buf_size || nalsize > buf_size - i) {
>                  av_log(logctx, AV_LOG_ERROR, "AVC-parser: nal size %"PRIu32" "
>                         "remaining %d\n", nalsize, buf_size - i);
>                  return buf_size;
> 
The cheapest fix for this is to make nalsize int64_t. But actually this
whole code looks highly weird: In contrast to how the parsing API is
supposed to be, it requires the whole buffer being available, despite
the PARSER_FLAG_COMPLETE_FRAMES being unset.

- Andreas
Michael Niedermayer Sept. 6, 2021, 8:36 p.m. UTC | #2
On Sun, Sep 05, 2021 at 09:51:32PM +0200, Andreas Rheinhardt wrote:
> Michael Niedermayer:
> > Fixes: Assertion failure
> > Fixes: 37463/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_H264_fuzzer-4914693494931456
> > 
> > Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> > Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> > ---
> >  libavcodec/h264_parser.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
> > index 01ea016409..47cc1d5c78 100644
> > --- a/libavcodec/h264_parser.c
> > +++ b/libavcodec/h264_parser.c
> > @@ -87,7 +87,7 @@ static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
> >              i = next_avc;
> >              for (j = 0; j < p->nal_length_size; j++)
> >                  nalsize = (nalsize << 8) | buf[i++];
> > -            if (!nalsize || nalsize > buf_size - i) {
> > +            if (!nalsize || i > buf_size || nalsize > buf_size - i) {
> >                  av_log(logctx, AV_LOG_ERROR, "AVC-parser: nal size %"PRIu32" "
> >                         "remaining %d\n", nalsize, buf_size - i);
> >                  return buf_size;
> > 
> The cheapest fix for this is to make nalsize int64_t. But actually this
> whole code looks highly weird: In contrast to how the parsing API is
> supposed to be, it requires the whole buffer being available, despite
> the PARSER_FLAG_COMPLETE_FRAMES being unset.

you are correct, this implementation assumes things beyond what the
API alone gurantees packetization wise.
the various specs gurantee that though i think for H.264
If you or someone wants to implement the generic case, i think its a good
idea. OTOH if not i dont think that will be a problem either.
as you prefer int64_t instead of the check, i will apply that change
instead

thx

[...]
diff mbox series

Patch

diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index 01ea016409..47cc1d5c78 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -87,7 +87,7 @@  static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
             i = next_avc;
             for (j = 0; j < p->nal_length_size; j++)
                 nalsize = (nalsize << 8) | buf[i++];
-            if (!nalsize || nalsize > buf_size - i) {
+            if (!nalsize || i > buf_size || nalsize > buf_size - i) {
                 av_log(logctx, AV_LOG_ERROR, "AVC-parser: nal size %"PRIu32" "
                        "remaining %d\n", nalsize, buf_size - i);
                 return buf_size;