do_scale() obtains the reference frame with
ff_framesync_get_frame(fs, 1, &ref, 0), i.e. @ref aliases
fs->in[1].frame (get=0 does not copy or detach it).  When the reference
frame geometry changes, or when the main frame triggers a
reconfiguration, do_scale()/scale_frame() call config_props(), which
tears the framesync down and rebuilds it
(ff_framesync_uninit/ff_framesync_init/ff_framesync_configure).
ff_framesync_uninit() av_frame_free()s fs->in[1].frame, so @ref becomes
a dangling pointer.

Previously the VAR_REF_T expression read @ref->pts after config_props()
had already run on the frame_changed path (scale2ref with
eval_mode=frame), i.e. a use-after-free of the reference frame.

Read everything needed from @ref (VAR_REF_N/VAR_REF_T) before any
config_props() call, so the reconfiguration cannot free a frame that is
still being dereferenced.

Reported-by: FFmpeg UAF audit (libavfilter/vf_scale framesync
reconfiguration)

Signed-off-by: Hongyan Xu <[email protected]>
---
 libavfilter/vf_scale.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c
index f2d3021..cf91472 100644
--- a/libavfilter/vf_scale.c
+++ b/libavfilter/vf_scale.c
@@ -917,6 +917,20 @@ static int do_scale(FFFrameSync *fs)
         AVFilterLink *reflink = ctx->inputs[1];
         FilterLink      *rl   = ff_filter_link(reflink);
 
+        /*
+         * Read everything we need from @ref before any config_props()
+         * below may run: config_props() tears the framesync down and
+         * rebuilds it (ff_framesync_uninit/init/configure).  @ref was
+         * obtained with ff_framesync_get_frame(..., get=0), i.e. it is
+         * fs->in[1].frame itself, so once config_props() has run, @ref
+         * points at freed memory.  Keep all @ref dereferences ahead of
+         * the reconfiguration.
+         */
+        if (scale->eval_mode == EVAL_MODE_FRAME) {
+            scale->var_values[VAR_REF_N] = rl->frame_count_out;
+            scale->var_values[VAR_REF_T] = TS2T(ref->pts, reflink->time_base);
+        }
+
         frame_changed = ref->width  != reflink->w ||
                         ref->height != reflink->h ||
                         ref->format != reflink->format ||
@@ -938,11 +952,6 @@ static int do_scale(FFFrameSync *fs)
             if (ret < 0)
                 goto err;
         }
-
-        if (scale->eval_mode == EVAL_MODE_FRAME) {
-            scale->var_values[VAR_REF_N] = rl->frame_count_out;
-            scale->var_values[VAR_REF_T] = TS2T(ref->pts, reflink->time_base);
-        }
     }
 
     ret = scale_frame(ctx->inputs[0], &in, &out);
-- 
2.50.1.windows.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to