diff mbox

[FFmpeg-devel,1/5] avcodec/tiff: Enforce increasing offsets

Message ID 20190808232350.16076-1-michael@niedermayer.cc
State Accepted
Commit 1fedba3c350a9eb22a1748c9056206d63d4d2dd9
Headers show

Commit Message

Michael Niedermayer Aug. 8, 2019, 11:23 p.m. UTC
This may break some valid tiff files, it appears the specification does not require
the offsets to be increasing. They increase in the 2 test files i have though except
the last offset which is 0 (an end marker) and for which a special case is added to
avoid asking for a sample for that end marker.

See: [FFmpeg-devel] [PATCH 2/2] avcodec/tiff: Detect infinite retry loop
for an alternative implementation

Fixes: Timeout (Infinite -> Finite)
Fixes: 15706/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_TIFF_fuzzer-5114674904825856

This variant was requested by paul on IRC
Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/tiff.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Comments

Paul B Mahol Aug. 11, 2019, 9:32 a.m. UTC | #1
LGTM

On Fri, Aug 9, 2019 at 1:25 AM Michael Niedermayer <michael@niedermayer.cc>
wrote:

> This may break some valid tiff files, it appears the specification does
> not require
> the offsets to be increasing. They increase in the 2 test files i have
> though except
> the last offset which is 0 (an end marker) and for which a special case is
> added to
> avoid asking for a sample for that end marker.
>
> See: [FFmpeg-devel] [PATCH 2/2] avcodec/tiff: Detect infinite retry loop
> for an alternative implementation
>
> Fixes: Timeout (Infinite -> Finite)
> Fixes:
> 15706/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_TIFF_fuzzer-5114674904825856
>
> This variant was requested by paul on IRC
> Found-by: continuous fuzzing process
> https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> Signed-off-by
> <https://github.com/google/oss-fuzz/tree/master/projects/ffmpegSigned-off-by>:
> Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavcodec/tiff.c | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
> index c520d7df83..1f1a1a3698 100644
> --- a/libavcodec/tiff.c
> +++ b/libavcodec/tiff.c
> @@ -1399,7 +1399,7 @@ static int decode_frame(AVCodecContext *avctx,
>      TiffContext *const s = avctx->priv_data;
>      AVFrame *const p = data;
>      ThreadFrame frame = { .f = data };
> -    unsigned off;
> +    unsigned off, last_off;
>      int le, ret, plane, planes;
>      int i, j, entries, stride;
>      unsigned soff, ssize;
> @@ -1454,6 +1454,7 @@ again:
>      /** whether we should process this multi-page IFD's next page */
>      retry_for_page = s->get_page && s->cur_page + 1 < s->get_page;  //
> get_page is 1-indexed
>
> +    last_off = off;
>      if (retry_for_page) {
>          // set offset to the next IFD
>          off = ff_tget_long(&s->gb, le);
> @@ -1463,6 +1464,14 @@ again:
>      }
>
>      if (retry_for_subifd || retry_for_page) {
> +        if (!off) {
> +            av_log(avctx, AV_LOG_ERROR, "Requested entry not found\n");
> +            return AVERROR_INVALIDDATA;
> +        }
> +        if (off <= last_off) {
> +            avpriv_request_sample(s->avctx, "non increasing IFD
> offset\n");
> +            return AVERROR_INVALIDDATA;
> +        }
>          if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
>              av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image
> size\n");
>              return AVERROR_INVALIDDATA;
> --
> 2.22.0
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Michael Niedermayer Aug. 11, 2019, 3:17 p.m. UTC | #2
On Sun, Aug 11, 2019 at 11:32:36AM +0200, Paul B Mahol wrote:
> LGTM

will apply

thx

[...]
diff mbox

Patch

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..1f1a1a3698 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -1399,7 +1399,7 @@  static int decode_frame(AVCodecContext *avctx,
     TiffContext *const s = avctx->priv_data;
     AVFrame *const p = data;
     ThreadFrame frame = { .f = data };
-    unsigned off;
+    unsigned off, last_off;
     int le, ret, plane, planes;
     int i, j, entries, stride;
     unsigned soff, ssize;
@@ -1454,6 +1454,7 @@  again:
     /** whether we should process this multi-page IFD's next page */
     retry_for_page = s->get_page && s->cur_page + 1 < s->get_page;  // get_page is 1-indexed
 
+    last_off = off;
     if (retry_for_page) {
         // set offset to the next IFD
         off = ff_tget_long(&s->gb, le);
@@ -1463,6 +1464,14 @@  again:
     }
 
     if (retry_for_subifd || retry_for_page) {
+        if (!off) {
+            av_log(avctx, AV_LOG_ERROR, "Requested entry not found\n");
+            return AVERROR_INVALIDDATA;
+        }
+        if (off <= last_off) {
+            avpriv_request_sample(s->avctx, "non increasing IFD offset\n");
+            return AVERROR_INVALIDDATA;
+        }
         if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
             av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
             return AVERROR_INVALIDDATA;