PR #25863 opened by michaelni URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25863 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25863.patch
>From b962611ea16227b94458734a00498af9f5a175de Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Sun, 8 Nov 2026 14:06:55 +0100 Subject: [PATCH 1/6] avcodec/libxevd: do not free an access unit packet a decoded picture references Fixes: use after free Fixes: IFxu9nTKTcYM Found during the review of https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25416 Use after free Replicated through UnModified FFmpeg with ASAN --- libavcodec/libxevd.c | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/libavcodec/libxevd.c b/libavcodec/libxevd.c index b1bede5e1bf..8a070519ec7 100644 --- a/libavcodec/libxevd.c +++ b/libavcodec/libxevd.c @@ -245,7 +245,7 @@ static av_cold int libxevd_init(AVCodecContext *avctx) } static int libxevd_return_frame(AVCodecContext *avctx, AVFrame *frame, - XEVD_IMGB *imgb, AVPacket **pkt_au) + XEVD_IMGB *imgb) { AVPacket *pkt_au_imgb = (AVPacket*)imgb->pdata[0]; int ret; @@ -253,8 +253,6 @@ static int libxevd_return_frame(AVCodecContext *avctx, AVFrame *frame, if (!pkt_au_imgb) { av_log(avctx, AV_LOG_ERROR, "Invalid data needed to fill frame properties\n"); - if (pkt_au) - av_packet_free(pkt_au); av_frame_unref(frame); imgb->release(imgb); @@ -351,10 +349,8 @@ static int libxevd_receive_frame(AVCodecContext *avctx, AVFrame *frame) nalu_size = read_nal_unit_length(pkt_au->data + bs_read_pos, XEVD_NAL_UNIT_LENGTH_BYTE, avctx); if (nalu_size <= 0 || nalu_size > pkt_au->size - bs_read_pos - XEVD_NAL_UNIT_LENGTH_BYTE) { av_log(avctx, AV_LOG_ERROR, "Invalid bitstream\n"); - av_packet_free(&pkt_au); ret = AVERROR_INVALIDDATA; - - return ret; + goto fail; } bs_read_pos += XEVD_NAL_UNIT_LENGTH_BYTE; @@ -365,18 +361,17 @@ static int libxevd_receive_frame(AVCodecContext *avctx, AVFrame *frame) bitb.ts[XEVD_TS_DTS] = pkt_au->dts; if (!bitb.pdata[1]) { - av_packet_free(&pkt_au); - return AVERROR(ENOMEM); + ret = AVERROR(ENOMEM); + goto fail; } /* main decoding block */ xevd_ret = xevd_decode(xectx->id, &bitb, &stat); if (XEVD_FAILED(xevd_ret)) { av_log(avctx, AV_LOG_ERROR, "Failed to decode bitstream\n"); - av_packet_free(&pkt_au); av_freep(&bitb.pdata[1]); - - return AVERROR_EXTERNAL; + ret = AVERROR_EXTERNAL; + goto fail; } bs_read_pos += nalu_size; @@ -386,9 +381,7 @@ static int libxevd_receive_frame(AVCodecContext *avctx, AVFrame *frame) if (stat.nalu_type == XEVD_NUT_SPS) { // EVC stream parameters changed if ((ret = export_stream_params(xectx, avctx)) != 0) { av_log(avctx, AV_LOG_ERROR, "Failed to export stream params\n"); - av_packet_free(&pkt_au); - - return ret; + goto fail; } } @@ -405,10 +398,8 @@ static int libxevd_receive_frame(AVCodecContext *avctx, AVFrame *frame) if (XEVD_FAILED(xevd_ret)) { av_log(avctx, AV_LOG_ERROR, "Failed to pull the decoded image (xevd error code: %d, frame#=%d)\n", xevd_ret, stat.fnum); - - av_packet_free(&pkt_au); - - return AVERROR_EXTERNAL; + ret = AVERROR_EXTERNAL; + goto fail; } else if (xevd_ret == XEVD_OK_FRM_DELAYED) { if(bs_read_pos == pkt_au->size) { return AVERROR(EAGAIN); @@ -417,8 +408,6 @@ static int libxevd_receive_frame(AVCodecContext *avctx, AVFrame *frame) if (!imgb) { if(bs_read_pos == pkt_au->size) { av_log(avctx, AV_LOG_ERROR, "Invalid decoded image data\n"); - - av_packet_free(&pkt_au); return AVERROR(EAGAIN); } } else { @@ -426,13 +415,16 @@ static int libxevd_receive_frame(AVCodecContext *avctx, AVFrame *frame) frame->pict_type = AV_PICTURE_TYPE_I; frame->flags |= AV_FRAME_FLAG_KEY; } - return libxevd_return_frame(avctx, frame, imgb, &pkt_au); + return libxevd_return_frame(avctx, frame, imgb); } } } } + ret = AVERROR(EAGAIN); +fail: if (!pkt_au_retained) av_packet_free(&pkt_au); + return ret; } else { // decoder draining mode handling while (1) { @@ -457,14 +449,12 @@ static int libxevd_receive_frame(AVCodecContext *avctx, AVFrame *frame) frame->pict_type = AV_PICTURE_TYPE_I; frame->flags |= AV_FRAME_FLAG_KEY; } - return libxevd_return_frame(avctx, frame, imgb, NULL); + return libxevd_return_frame(avctx, frame, imgb); } av_log(avctx, AV_LOG_WARNING, "Unexpected return code while draining (xevd error code: %d)\n", xevd_ret); return AVERROR_EOF; } } - - return AVERROR(EAGAIN); } /** -- 2.54.0 >From 8660acf3695f8d3a280c2fefc7eb89de5f62bfbf Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Sun, 8 Nov 2026 14:07:03 +0100 Subject: [PATCH 2/6] avfilter/vf_lcevc: allocate the base picture after the enhancement data was accepted When LCEVC_SendDecoderEnhancementData() failed, the picture allocated for the base frame was not freed. Fixes: memleak Fixes: a4b506JEoHv4 Found during the review of https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25134 Memleak Replicated through Modified FFmpeg with the LCEVCdec unreleased picture log --- libavfilter/vf_lcevc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libavfilter/vf_lcevc.c b/libavfilter/vf_lcevc.c index 01e5ded9006..a47dcc987b3 100644 --- a/libavfilter/vf_lcevc.c +++ b/libavfilter/vf_lcevc.c @@ -150,10 +150,6 @@ static int send_frame(AVFilterLink *inlink, AVFrame *in) LCEVC_ReturnCode res; int ret; - ret = alloc_base_frame(inlink, in, &picture); - if (ret < 0) - return ret; - if (sd) { res = LCEVC_SendDecoderEnhancementData(lcevc->decoder, in->pts, sd->data, sd->size); if (res == LCEVC_Again) @@ -164,6 +160,10 @@ static int send_frame(AVFilterLink *inlink, AVFrame *in) } } + ret = alloc_base_frame(inlink, in, &picture); + if (ret < 0) + return ret; + res = LCEVC_SetPictureUserData(lcevc->decoder, picture, in); if (res != LCEVC_Success) { av_log(ctx, AV_LOG_ERROR, "LCEVC_SetPictureUserData failed\n"); -- 2.54.0 >From 8a922a39a8eeafe10d0367d6683f5194104d36f9 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Sun, 8 Nov 2026 14:09:38 +0100 Subject: [PATCH 3/6] avfilter/vf_lcevc: keep the output frame while LCEVCdec holds its picture Fixes: use after free Fixes: c6FrmJrjoJee Found during triage of the Fairy review of https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25134 Use after free Replicated through UnModified FFmpeg with ASAN --- libavfilter/vf_lcevc.c | 49 +++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/libavfilter/vf_lcevc.c b/libavfilter/vf_lcevc.c index a47dcc987b3..a22d47a20d2 100644 --- a/libavfilter/vf_lcevc.c +++ b/libavfilter/vf_lcevc.c @@ -29,6 +29,7 @@ typedef struct LCEVCContext { LCEVC_DecoderHandle decoder; + AVFrame *out; int w, h; } LCEVCContext; @@ -209,7 +210,7 @@ static int alloc_enhanced_frame(AVFilterLink *inlink, const AVFrame *out, return 0; } -static int generate_output(AVFilterLink *inlink, AVFrame *out) +static int generate_output(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; AVFilterLink *outlink = ctx->outputs[0]; @@ -218,23 +219,22 @@ static int generate_output(AVFilterLink *inlink, AVFrame *out) LCEVC_DecodeInformation info; LCEVC_PictureHandle picture; LCEVC_ReturnCode res; + AVFrame *out; res = LCEVC_ReceiveDecoderPicture(lcevc->decoder, &picture, &info); if (res == LCEVC_Again) { int64_t pts; int status; - if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { - av_frame_free(&out); + if (ff_inlink_acknowledge_status(inlink, &status, &pts)) ff_outlink_set_status(outlink, status, pts); - return 0; - } - // this shouldn't be reachable, but instead of asserting, just error out - return AVERROR_BUG; + return 0; } else if (res != LCEVC_Success) { av_log(ctx, AV_LOG_ERROR, "LCEVC_ReceiveDecoderPicture failed\n"); return AVERROR_EXTERNAL; } + out = lcevc->out; + lcevc->out = NULL; av_frame_copy_props(out, (AVFrame *)info.baseUserData); av_frame_remove_side_data(out, AV_FRAME_DATA_LCEVC); @@ -266,25 +266,33 @@ static int generate_output(AVFilterLink *inlink, AVFrame *out) return ff_filter_frame(outlink, out); } -static int receive_frame(AVFilterLink *inlink, AVFrame *out) +static int send_output_picture(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; LCEVCContext *lcevc = ctx->priv; LCEVC_PictureHandle picture; LCEVC_ReturnCode res; int ret; - ret = alloc_enhanced_frame(inlink, out, &picture); - if (ret < 0) + lcevc->out = ff_get_video_buffer(outlink, lcevc->w, lcevc->h); + if (!lcevc->out) + return AVERROR(ENOMEM); + + ret = alloc_enhanced_frame(inlink, lcevc->out, &picture); + if (ret < 0) { + av_frame_free(&lcevc->out); return ret; + } res = LCEVC_SendDecoderPicture(lcevc->decoder, picture); if (res != LCEVC_Success) { av_log(ctx, AV_LOG_ERROR, "LCEVC_SendDecoderPicture failed\n"); + av_frame_free(&lcevc->out); return AVERROR_EXTERNAL; } - return generate_output(inlink, out); + return 0; } static int config_props(AVFilterLink *outlink) @@ -318,7 +326,7 @@ static int activate(AVFilterContext *ctx) LCEVCContext *lcevc = ctx->priv; AVFilterLink *inlink = ctx->inputs[0]; AVFilterLink *outlink = ctx->outputs[0]; - AVFrame *in, *out; + AVFrame *in; int status, ret; FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); @@ -356,16 +364,16 @@ static int activate(AVFilterContext *ctx) } } - out = ff_get_video_buffer(outlink, lcevc->w, lcevc->h); - if (!out) - return AVERROR(ENOMEM); - - ret = receive_frame(inlink, out); - if (ret < 0) { - av_frame_free(&out); - return ret; + if (!lcevc->out) { + ret = send_output_picture(inlink); + if (ret < 0) + return ret; } + ret = generate_output(inlink); + if (ret < 0) + return ret; + flush_bases(ctx); return ret; @@ -429,6 +437,7 @@ static av_cold void uninit(AVFilterContext *ctx) LCEVC_FlushDecoder(lcevc->decoder); flush_bases(ctx); LCEVC_DestroyDecoder(lcevc->decoder); + av_frame_free(&lcevc->out); } static const AVFilterPad lcevc_outputs[] = { -- 2.54.0 >From dd5660a289ea3e3006f35e5aa574cf7c551631c9 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Sun, 8 Nov 2026 14:09:56 +0100 Subject: [PATCH 4/6] avfilter/vf_lcevc: free the output picture when LCEVC_SendDecoderPicture() fails Found during triage of the Fairy review of https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25134 --- libavfilter/vf_lcevc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libavfilter/vf_lcevc.c b/libavfilter/vf_lcevc.c index a22d47a20d2..fa1cb77e444 100644 --- a/libavfilter/vf_lcevc.c +++ b/libavfilter/vf_lcevc.c @@ -288,6 +288,7 @@ static int send_output_picture(AVFilterLink *inlink) res = LCEVC_SendDecoderPicture(lcevc->decoder, picture); if (res != LCEVC_Success) { av_log(ctx, AV_LOG_ERROR, "LCEVC_SendDecoderPicture failed\n"); + LCEVC_FreePicture(lcevc->decoder, picture); av_frame_free(&lcevc->out); return AVERROR_EXTERNAL; } -- 2.54.0 >From cc30494044808e7930b535bfbea39042934cd0d5 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Sun, 8 Nov 2026 14:11:02 +0100 Subject: [PATCH 5/6] avfilter/vf_lcevc: wait for the flushed frames before collecting their base pictures LCEVC_FlushDecoder() only marks the pending frames as flushed, their base pictures become available once their tasks have run. uninit() collected the base pictures right after the flush, so the input frames attached to frames still pending at that point were never freed. Fixes: memleak Found during triage of the Fairy review of https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25134 Memleak Replicated through UnModified FFmpeg with ASAN --- libavfilter/vf_lcevc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavfilter/vf_lcevc.c b/libavfilter/vf_lcevc.c index fa1cb77e444..a5a1b09de6d 100644 --- a/libavfilter/vf_lcevc.c +++ b/libavfilter/vf_lcevc.c @@ -435,7 +435,7 @@ static av_cold void uninit(AVFilterContext *ctx) { LCEVCContext *lcevc = ctx->priv; - LCEVC_FlushDecoder(lcevc->decoder); + LCEVC_SynchronizeDecoder(lcevc->decoder, 1); flush_bases(ctx); LCEVC_DestroyDecoder(lcevc->decoder); av_frame_free(&lcevc->out); -- 2.54.0 >From 762be5b94ecb5eddf585220fdd211b673bc403be Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Sun, 8 Nov 2026 14:17:51 +0100 Subject: [PATCH 6/6] avfilter/vf_lcevc: pass the LCEVCdec log messages through The size LCEVCdec reports for a log event includes the terminating NUL, so the check strlen(data) != size rejected every message and no library log, errors included, ever reached the user. Fixes: dropped library log messages Found during triage of the Fairy review of https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25134 Dropped library log messages Replicated through UnModified FFmpeg with the ffmpeg log output --- libavfilter/vf_lcevc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavfilter/vf_lcevc.c b/libavfilter/vf_lcevc.c index a5a1b09de6d..3142636d9b7 100644 --- a/libavfilter/vf_lcevc.c +++ b/libavfilter/vf_lcevc.c @@ -387,7 +387,7 @@ static void log_callback(LCEVC_DecoderHandle dec, LCEVC_Event event, if (event != LCEVC_Log) // shouldn't happen return; - if (strlen(data) != size) // sanitize input + if (!size || data[size - 1]) // sanitize input return; av_log(logctx, AV_LOG_INFO, "LCEVC Log: %s\n", data); -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
