diff mbox series

[FFmpeg-devel,v2,1/3] fftools/ffmpeg_filter: fix NULL pointer dereference

Message ID tencent_BC34B781C034113F1D56399DF8E0D05BCE09@qq.com
State New
Headers show
Series [FFmpeg-devel,v2,1/3] fftools/ffmpeg_filter: fix NULL pointer dereference | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Zhao Zhili Dec. 19, 2023, 3:37 p.m. UTC
From: Zhao Zhili <zhilizhao@tencent.com>

In close_output(), a dummy frame is created with format NONE passed
to enc_open(), which doesn't prepare for it. The NULL pointer
dereference happened at
av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth.

When fgt.graph is NULL, skip fg_output_frame() since there is
nothing to output.

frame #0: 0x0000005555bc34a4 ffmpeg_g`enc_open(opaque=0xb400007efe2db690, frame=0xb400007efe2d9f70) at ffmpeg_enc.c:235:44
frame #1: 0x0000005555bef250 ffmpeg_g`enc_open(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1462:11
frame #2: 0x0000005555bee094 ffmpeg_g`send_to_enc(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1571:19
frame #3: 0x0000005555bee01c ffmpeg_g`sch_filter_send(sch=0xb400007dde2d4090, fg_idx=0, out_idx=0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:2154:12
frame #4: 0x0000005555bcf124 ffmpeg_g`close_output(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08) at ffmpeg_filter.c:2225:15
frame #5: 0x0000005555bcb000 ffmpeg_g`fg_output_frame(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08, frame=0x0000000000000000) at ffmpeg_filter.c:2317:16
frame #6: 0x0000005555bc7e48 ffmpeg_g`filter_thread(arg=0xb400007eae2ce7a0) at ffmpeg_filter.c:2836:15
frame #7: 0x0000005555bee568 ffmpeg_g`task_wrapper(arg=0xb400007d8e2db478) at ffmpeg_sched.c:2200:21
---
 fftools/ffmpeg_filter.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

Comments

Anton Khirnov Dec. 19, 2023, 10:32 a.m. UTC | #1
Quoting Zhao Zhili (2023-12-19 16:37:01)
> From: Zhao Zhili <zhilizhao@tencent.com>
> 
> In close_output(), a dummy frame is created with format NONE passed
> to enc_open(), which doesn't prepare for it. The NULL pointer
> dereference happened at
> av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth.
> 
> When fgt.graph is NULL, skip fg_output_frame() since there is
> nothing to output.
> 
> frame #0: 0x0000005555bc34a4 ffmpeg_g`enc_open(opaque=0xb400007efe2db690, frame=0xb400007efe2d9f70) at ffmpeg_enc.c:235:44
> frame #1: 0x0000005555bef250 ffmpeg_g`enc_open(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1462:11
> frame #2: 0x0000005555bee094 ffmpeg_g`send_to_enc(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1571:19
> frame #3: 0x0000005555bee01c ffmpeg_g`sch_filter_send(sch=0xb400007dde2d4090, fg_idx=0, out_idx=0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:2154:12
> frame #4: 0x0000005555bcf124 ffmpeg_g`close_output(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08) at ffmpeg_filter.c:2225:15
> frame #5: 0x0000005555bcb000 ffmpeg_g`fg_output_frame(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08, frame=0x0000000000000000) at ffmpeg_filter.c:2317:16
> frame #6: 0x0000005555bc7e48 ffmpeg_g`filter_thread(arg=0xb400007eae2ce7a0) at ffmpeg_filter.c:2836:15
> frame #7: 0x0000005555bee568 ffmpeg_g`task_wrapper(arg=0xb400007d8e2db478) at ffmpeg_sched.c:2200:21
> ---
>  fftools/ffmpeg_filter.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
> index 9fc877b437..69a49a071e 100644
> --- a/fftools/ffmpeg_filter.c
> +++ b/fftools/ffmpeg_filter.c
> @@ -2335,10 +2335,7 @@ static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt,
>  
>      ret = av_buffersink_get_frame_flags(filter, frame,
>                                          AV_BUFFERSINK_FLAG_NO_REQUEST);
> -    if (ret == AVERROR_EOF && !fgt->eof_out[ofp->index]) {
> -        ret = fg_output_frame(ofp, fgt, NULL);
> -        return (ret < 0) ? ret : 1;

Why are you removing this block?
Zhao Zhili Dec. 19, 2023, 11:31 a.m. UTC | #2
> On Dec 19, 2023, at 18:32, Anton Khirnov <anton@khirnov.net> wrote:
> 
> Quoting Zhao Zhili (2023-12-19 16:37:01)
>> From: Zhao Zhili <zhilizhao@tencent.com>
>> 
>> In close_output(), a dummy frame is created with format NONE passed
>> to enc_open(), which doesn't prepare for it. The NULL pointer
>> dereference happened at
>> av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth.
>> 
>> When fgt.graph is NULL, skip fg_output_frame() since there is
>> nothing to output.
>> 
>> frame #0: 0x0000005555bc34a4 ffmpeg_g`enc_open(opaque=0xb400007efe2db690, frame=0xb400007efe2d9f70) at ffmpeg_enc.c:235:44
>> frame #1: 0x0000005555bef250 ffmpeg_g`enc_open(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1462:11
>> frame #2: 0x0000005555bee094 ffmpeg_g`send_to_enc(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1571:19
>> frame #3: 0x0000005555bee01c ffmpeg_g`sch_filter_send(sch=0xb400007dde2d4090, fg_idx=0, out_idx=0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:2154:12
>> frame #4: 0x0000005555bcf124 ffmpeg_g`close_output(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08) at ffmpeg_filter.c:2225:15
>> frame #5: 0x0000005555bcb000 ffmpeg_g`fg_output_frame(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08, frame=0x0000000000000000) at ffmpeg_filter.c:2317:16
>> frame #6: 0x0000005555bc7e48 ffmpeg_g`filter_thread(arg=0xb400007eae2ce7a0) at ffmpeg_filter.c:2836:15
>> frame #7: 0x0000005555bee568 ffmpeg_g`task_wrapper(arg=0xb400007d8e2db478) at ffmpeg_sched.c:2200:21
>> ---
>> fftools/ffmpeg_filter.c | 7 ++-----
>> 1 file changed, 2 insertions(+), 5 deletions(-)
>> 
>> diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
>> index 9fc877b437..69a49a071e 100644
>> --- a/fftools/ffmpeg_filter.c
>> +++ b/fftools/ffmpeg_filter.c
>> @@ -2335,10 +2335,7 @@ static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt,
>> 
>>     ret = av_buffersink_get_frame_flags(filter, frame,
>>                                         AV_BUFFERSINK_FLAG_NO_REQUEST);
>> -    if (ret == AVERROR_EOF && !fgt->eof_out[ofp->index]) {
>> -        ret = fg_output_frame(ofp, fgt, NULL);
>> -        return (ret < 0) ? ret : 1;
> 
> Why are you removing this block?

I misread what you mean
> I see, then seems to me it'd be better to run the final loop in
> filter_thread() (the one calling fg_output_frame(, NULL)) only when
> fgt->graph is non-NULL. That should fix this issue as well.
These two flush operations can be combined into a single one, no?

> 
> -- 
> Anton Khirnov
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org <mailto:ffmpeg-devel@ffmpeg.org>
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org <mailto:ffmpeg-devel-request@ffmpeg.org> with subject "unsubscribe".
Anton Khirnov Dec. 19, 2023, 11:40 a.m. UTC | #3
Quoting Zhao Zhili (2023-12-19 12:31:53)
> 
> 
> > On Dec 19, 2023, at 18:32, Anton Khirnov <anton@khirnov.net> wrote:
> > 
> > Quoting Zhao Zhili (2023-12-19 16:37:01)
> >> From: Zhao Zhili <zhilizhao@tencent.com>
> >> 
> >> In close_output(), a dummy frame is created with format NONE passed
> >> to enc_open(), which doesn't prepare for it. The NULL pointer
> >> dereference happened at
> >> av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth.
> >> 
> >> When fgt.graph is NULL, skip fg_output_frame() since there is
> >> nothing to output.
> >> 
> >> frame #0: 0x0000005555bc34a4 ffmpeg_g`enc_open(opaque=0xb400007efe2db690, frame=0xb400007efe2d9f70) at ffmpeg_enc.c:235:44
> >> frame #1: 0x0000005555bef250 ffmpeg_g`enc_open(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1462:11
> >> frame #2: 0x0000005555bee094 ffmpeg_g`send_to_enc(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1571:19
> >> frame #3: 0x0000005555bee01c ffmpeg_g`sch_filter_send(sch=0xb400007dde2d4090, fg_idx=0, out_idx=0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:2154:12
> >> frame #4: 0x0000005555bcf124 ffmpeg_g`close_output(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08) at ffmpeg_filter.c:2225:15
> >> frame #5: 0x0000005555bcb000 ffmpeg_g`fg_output_frame(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08, frame=0x0000000000000000) at ffmpeg_filter.c:2317:16
> >> frame #6: 0x0000005555bc7e48 ffmpeg_g`filter_thread(arg=0xb400007eae2ce7a0) at ffmpeg_filter.c:2836:15
> >> frame #7: 0x0000005555bee568 ffmpeg_g`task_wrapper(arg=0xb400007d8e2db478) at ffmpeg_sched.c:2200:21
> >> ---
> >> fftools/ffmpeg_filter.c | 7 ++-----
> >> 1 file changed, 2 insertions(+), 5 deletions(-)
> >> 
> >> diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
> >> index 9fc877b437..69a49a071e 100644
> >> --- a/fftools/ffmpeg_filter.c
> >> +++ b/fftools/ffmpeg_filter.c
> >> @@ -2335,10 +2335,7 @@ static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt,
> >> 
> >>     ret = av_buffersink_get_frame_flags(filter, frame,
> >>                                         AV_BUFFERSINK_FLAG_NO_REQUEST);
> >> -    if (ret == AVERROR_EOF && !fgt->eof_out[ofp->index]) {
> >> -        ret = fg_output_frame(ofp, fgt, NULL);
> >> -        return (ret < 0) ? ret : 1;
> > 
> > Why are you removing this block?
> 
> I misread what you mean
> > I see, then seems to me it'd be better to run the final loop in
> > filter_thread() (the one calling fg_output_frame(, NULL)) only when
> > fgt->graph is non-NULL. That should fix this issue as well.
> These two flush operations can be combined into a single one, no?

If you do this, then no encoder will get an EOF until the entire graph
is done.
Zhao Zhili Dec. 19, 2023, 11:50 a.m. UTC | #4
> 在 2023年12月19日,下午7:40,Anton Khirnov <anton@khirnov.net> 写道:
> 
> Quoting Zhao Zhili (2023-12-19 12:31:53)
>> 
>> 
>>>> On Dec 19, 2023, at 18:32, Anton Khirnov <anton@khirnov.net> wrote:
>>> 
>>> Quoting Zhao Zhili (2023-12-19 16:37:01)
>>>> From: Zhao Zhili <zhilizhao@tencent.com>
>>>> 
>>>> In close_output(), a dummy frame is created with format NONE passed
>>>> to enc_open(), which doesn't prepare for it. The NULL pointer
>>>> dereference happened at
>>>> av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth.
>>>> 
>>>> When fgt.graph is NULL, skip fg_output_frame() since there is
>>>> nothing to output.
>>>> 
>>>> frame #0: 0x0000005555bc34a4 ffmpeg_g`enc_open(opaque=0xb400007efe2db690, frame=0xb400007efe2d9f70) at ffmpeg_enc.c:235:44
>>>> frame #1: 0x0000005555bef250 ffmpeg_g`enc_open(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1462:11
>>>> frame #2: 0x0000005555bee094 ffmpeg_g`send_to_enc(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1571:19
>>>> frame #3: 0x0000005555bee01c ffmpeg_g`sch_filter_send(sch=0xb400007dde2d4090, fg_idx=0, out_idx=0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:2154:12
>>>> frame #4: 0x0000005555bcf124 ffmpeg_g`close_output(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08) at ffmpeg_filter.c:2225:15
>>>> frame #5: 0x0000005555bcb000 ffmpeg_g`fg_output_frame(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08, frame=0x0000000000000000) at ffmpeg_filter.c:2317:16
>>>> frame #6: 0x0000005555bc7e48 ffmpeg_g`filter_thread(arg=0xb400007eae2ce7a0) at ffmpeg_filter.c:2836:15
>>>> frame #7: 0x0000005555bee568 ffmpeg_g`task_wrapper(arg=0xb400007d8e2db478) at ffmpeg_sched.c:2200:21
>>>> ---
>>>> fftools/ffmpeg_filter.c | 7 ++-----
>>>> 1 file changed, 2 insertions(+), 5 deletions(-)
>>>> 
>>>> diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
>>>> index 9fc877b437..69a49a071e 100644
>>>> --- a/fftools/ffmpeg_filter.c
>>>> +++ b/fftools/ffmpeg_filter.c
>>>> @@ -2335,10 +2335,7 @@ static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt,
>>>> 
>>>>    ret = av_buffersink_get_frame_flags(filter, frame,
>>>>                                        AV_BUFFERSINK_FLAG_NO_REQUEST);
>>>> -    if (ret == AVERROR_EOF && !fgt->eof_out[ofp->index]) {
>>>> -        ret = fg_output_frame(ofp, fgt, NULL);
>>>> -        return (ret < 0) ? ret : 1;
>>> 
>>> Why are you removing this block?
>> 
>> I misread what you mean
>>> I see, then seems to me it'd be better to run the final loop in
>>> filter_thread() (the one calling fg_output_frame(, NULL)) only when
>>> fgt->graph is non-NULL. That should fix this issue as well.
>> These two flush operations can be combined into a single one, no?
> 
> If you do this, then no encoder will get an EOF until the entire graph
> is done.

Now I get it, will keep it in version 3.

> 
> --
> Anton Khirnov
> _______________________________________________
> 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".
diff mbox series

Patch

diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 9fc877b437..69a49a071e 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -2335,10 +2335,7 @@  static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt,
 
     ret = av_buffersink_get_frame_flags(filter, frame,
                                         AV_BUFFERSINK_FLAG_NO_REQUEST);
-    if (ret == AVERROR_EOF && !fgt->eof_out[ofp->index]) {
-        ret = fg_output_frame(ofp, fgt, NULL);
-        return (ret < 0) ? ret : 1;
-    } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
+    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
         return 1;
     } else if (ret < 0) {
         av_log(fgp, AV_LOG_WARNING,
@@ -2835,7 +2832,7 @@  read_frames:
     for (unsigned i = 0; i < fg->nb_outputs; i++) {
         OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
 
-        if (fgt.eof_out[i])
+        if (fgt.eof_out[i] || !fgt.graph)
             continue;
 
         ret = fg_output_frame(ofp, &fgt, NULL);