diff mbox series

[FFmpeg-devel,4/7] avformat/nistspheredec: Check bits_per_coded_sample and channels

Message ID 20210116230729.30613-4-michael@niedermayer.cc
State Accepted
Headers show
Series [FFmpeg-devel,1/7] avformat/mpsubdec: Use av_sat_add/sub64() in fracval handling
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Michael Niedermayer Jan. 16, 2021, 11:07 p.m. UTC
Fixes: signed integer overflow: 80 * 92233009 cannot be represented in type 'int'
Fixes: 26910/clusterfuzz-testcase-minimized-ffmpeg_dem_NISTSPHERE_fuzzer-6669100654919680

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

Comments

Anton Khirnov Jan. 18, 2021, 8:06 p.m. UTC | #1
Quoting Michael Niedermayer (2021-01-17 00:07:26)
> Fixes: signed integer overflow: 80 * 92233009 cannot be represented in type 'int'
> Fixes: 26910/clusterfuzz-testcase-minimized-ffmpeg_dem_NISTSPHERE_fuzzer-6669100654919680
> 
> Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavformat/nistspheredec.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/libavformat/nistspheredec.c b/libavformat/nistspheredec.c
> index 079369929f..fec5c88892 100644
> --- a/libavformat/nistspheredec.c
> +++ b/libavformat/nistspheredec.c
> @@ -80,7 +80,9 @@ static int nist_read_header(AVFormatContext *s)
>  
>              avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
>  
> -            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * st->codecpar->channels / 8;
> +            if (st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8 > INT_MAX)
> +                return AVERROR_INVALIDDATA;
> +            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8;
>  
>              if (avio_tell(s->pb) > header_size)
>                  return AVERROR_INVALIDDATA;
> -- 
> 2.17.1

Both bits_per_coded_sample and channels seem to be arbitrary ints, so
you are assuming sizeof(int) < sizeof(int64)
Michael Niedermayer Jan. 19, 2021, 6 p.m. UTC | #2
On Mon, Jan 18, 2021 at 09:06:10PM +0100, Anton Khirnov wrote:
> Quoting Michael Niedermayer (2021-01-17 00:07:26)
> > Fixes: signed integer overflow: 80 * 92233009 cannot be represented in type 'int'
> > Fixes: 26910/clusterfuzz-testcase-minimized-ffmpeg_dem_NISTSPHERE_fuzzer-6669100654919680
> > 
> > Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> > Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> > ---
> >  libavformat/nistspheredec.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> > 
> > diff --git a/libavformat/nistspheredec.c b/libavformat/nistspheredec.c
> > index 079369929f..fec5c88892 100644
> > --- a/libavformat/nistspheredec.c
> > +++ b/libavformat/nistspheredec.c
> > @@ -80,7 +80,9 @@ static int nist_read_header(AVFormatContext *s)
> >  
> >              avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
> >  
> > -            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * st->codecpar->channels / 8;
> > +            if (st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8 > INT_MAX)
> > +                return AVERROR_INVALIDDATA;
> > +            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8;
> >  
> >              if (avio_tell(s->pb) > header_size)
> >                  return AVERROR_INVALIDDATA;
> > -- 
> > 2.17.1
> 
> Both bits_per_coded_sample and channels seem to be arbitrary ints, so
> you are assuming sizeof(int) < sizeof(int64)

I wonder if we shouldnt limit these 2 fields in a platform independant way
But if we dont then the change below should do more correct checks

@@ -80,7 +80,11 @@ static int nist_read_header(AVFormatContext *s)
 
             avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
 
-            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * st->codecpar->channels / 8;
+            if (st->codecpar->bits_per_coded_sample < 0 || st->codecpar->channels <= 0 ||
+                st->codecpar->bits_per_coded_sample > UINT64_MAX / st->codecpar->channels ||
+                st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8 > INT_MAX)
+                return AVERROR_INVALIDDATA;
+            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8;
 
             if (avio_tell(s->pb) > header_size)
                 return AVERROR_INVALIDDATA;


[...]
Anton Khirnov Jan. 20, 2021, 8:41 a.m. UTC | #3
Quoting Michael Niedermayer (2021-01-19 19:00:47)
> On Mon, Jan 18, 2021 at 09:06:10PM +0100, Anton Khirnov wrote:
> > Quoting Michael Niedermayer (2021-01-17 00:07:26)
> > > Fixes: signed integer overflow: 80 * 92233009 cannot be represented in type 'int'
> > > Fixes: 26910/clusterfuzz-testcase-minimized-ffmpeg_dem_NISTSPHERE_fuzzer-6669100654919680
> > > 
> > > Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> > > Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> > > ---
> > >  libavformat/nistspheredec.c | 4 +++-
> > >  1 file changed, 3 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/libavformat/nistspheredec.c b/libavformat/nistspheredec.c
> > > index 079369929f..fec5c88892 100644
> > > --- a/libavformat/nistspheredec.c
> > > +++ b/libavformat/nistspheredec.c
> > > @@ -80,7 +80,9 @@ static int nist_read_header(AVFormatContext *s)
> > >  
> > >              avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
> > >  
> > > -            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * st->codecpar->channels / 8;
> > > +            if (st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8 > INT_MAX)
> > > +                return AVERROR_INVALIDDATA;
> > > +            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8;
> > >  
> > >              if (avio_tell(s->pb) > header_size)
> > >                  return AVERROR_INVALIDDATA;
> > > -- 
> > > 2.17.1
> > 
> > Both bits_per_coded_sample and channels seem to be arbitrary ints, so
> > you are assuming sizeof(int) < sizeof(int64)
> 
> I wonder if we shouldnt limit these 2 fields in a platform independant way
> But if we dont then the change below should do more correct checks

I would be in favor of limiting them both to 16 bits, that should be way
beyond anything needed by any reasonable use case.

> 
> @@ -80,7 +80,11 @@ static int nist_read_header(AVFormatContext *s)
>  
>              avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
>  
> -            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * st->codecpar->channels / 8;
> +            if (st->codecpar->bits_per_coded_sample < 0 || st->codecpar->channels <= 0 ||
> +                st->codecpar->bits_per_coded_sample > UINT64_MAX / st->codecpar->channels ||
> +                st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8 > INT_MAX)
> +                return AVERROR_INVALIDDATA;
> +            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8;
>  
>              if (avio_tell(s->pb) > header_size)
>                  return AVERROR_INVALIDDATA;

This looks ok.

Though it would be even better to sanity-check each value for
zero/negative right after the sscanf() that reads it (not insisting you
do it).
Michael Niedermayer Jan. 21, 2021, 6:35 p.m. UTC | #4
On Wed, Jan 20, 2021 at 09:41:49AM +0100, Anton Khirnov wrote:
> Quoting Michael Niedermayer (2021-01-19 19:00:47)
> > On Mon, Jan 18, 2021 at 09:06:10PM +0100, Anton Khirnov wrote:
> > > Quoting Michael Niedermayer (2021-01-17 00:07:26)
> > > > Fixes: signed integer overflow: 80 * 92233009 cannot be represented in type 'int'
> > > > Fixes: 26910/clusterfuzz-testcase-minimized-ffmpeg_dem_NISTSPHERE_fuzzer-6669100654919680
> > > > 
> > > > Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> > > > Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> > > > ---
> > > >  libavformat/nistspheredec.c | 4 +++-
> > > >  1 file changed, 3 insertions(+), 1 deletion(-)
> > > > 
> > > > diff --git a/libavformat/nistspheredec.c b/libavformat/nistspheredec.c
> > > > index 079369929f..fec5c88892 100644
> > > > --- a/libavformat/nistspheredec.c
> > > > +++ b/libavformat/nistspheredec.c
> > > > @@ -80,7 +80,9 @@ static int nist_read_header(AVFormatContext *s)
> > > >  
> > > >              avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
> > > >  
> > > > -            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * st->codecpar->channels / 8;
> > > > +            if (st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8 > INT_MAX)
> > > > +                return AVERROR_INVALIDDATA;
> > > > +            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8;
> > > >  
> > > >              if (avio_tell(s->pb) > header_size)
> > > >                  return AVERROR_INVALIDDATA;
> > > > -- 
> > > > 2.17.1
> > > 
> > > Both bits_per_coded_sample and channels seem to be arbitrary ints, so
> > > you are assuming sizeof(int) < sizeof(int64)
> > 
> > I wonder if we shouldnt limit these 2 fields in a platform independant way
> > But if we dont then the change below should do more correct checks
> 
> I would be in favor of limiting them both to 16 bits, that should be way
> beyond anything needed by any reasonable use case.
> 
> > 
> > @@ -80,7 +80,11 @@ static int nist_read_header(AVFormatContext *s)
> >  
> >              avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
> >  
> > -            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * st->codecpar->channels / 8;
> > +            if (st->codecpar->bits_per_coded_sample < 0 || st->codecpar->channels <= 0 ||
> > +                st->codecpar->bits_per_coded_sample > UINT64_MAX / st->codecpar->channels ||
> > +                st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8 > INT_MAX)
> > +                return AVERROR_INVALIDDATA;
> > +            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8;
> >  
> >              if (avio_tell(s->pb) > header_size)
> >                  return AVERROR_INVALIDDATA;
> 
> This looks ok.
> 
> Though it would be even better to sanity-check each value for
> zero/negative right after the sscanf() that reads it (not insisting you
> do it).

will apply with these changes

thx

[...]
diff mbox series

Patch

diff --git a/libavformat/nistspheredec.c b/libavformat/nistspheredec.c
index 079369929f..fec5c88892 100644
--- a/libavformat/nistspheredec.c
+++ b/libavformat/nistspheredec.c
@@ -80,7 +80,9 @@  static int nist_read_header(AVFormatContext *s)
 
             avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
 
-            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * st->codecpar->channels / 8;
+            if (st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8 > INT_MAX)
+                return AVERROR_INVALIDDATA;
+            st->codecpar->block_align = st->codecpar->bits_per_coded_sample * (uint64_t)st->codecpar->channels / 8;
 
             if (avio_tell(s->pb) > header_size)
                 return AVERROR_INVALIDDATA;