PR #26350 opened by ndren URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26350 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26350.patch
Closing the demuxer unmaps the capture buffers and frees the context that zero-copy packets use to requeue their buffer. Dereferencing those packets after avformat_close_input() leads to use-after-free. Refcount the mappings and give each zero-copy packet a reference. On close, detach them from the context. Packets released afterwards only drop their reference, and the last one unmaps. Always copy with -use_libv4l2. When converting, libv4l2 hands out its own buffer and warns if it is still mapped in v4l2_close(): https://git.linuxtv.org/v4l-utils.git/tree/lib/libv4l2/libv4l2.c?id=d2e89246cefcb6371425361b12c4ca2a36a662d2#n840 Fixes #21621. >From 1642724bb06dfa7ac4a95871b76a43a44369a280 Mon Sep 17 00:00:00 2001 From: "Andrei E." <[email protected]> Date: Wed, 25 Nov 2026 10:56:54 +0000 Subject: [PATCH] avdevice/v4l2: keep zero-copy packets valid after close Closing the demuxer unmaps the capture buffers and frees the context that zero-copy packets use to requeue their buffer. Dereferencing those packets after avformat_close_input() leads to use-after-free. Refcount the mappings and give each zero-copy packet a reference. On close, detach them from the context. Packets released afterwards only drop their reference, and the last one unmaps. Always copy with -use_libv4l2. When converting, libv4l2 hands out its own buffer and warns if it is still mapped in v4l2_close(): https://git.linuxtv.org/v4l-utils.git/tree/lib/libv4l2/libv4l2.c?id=d2e89246cefcb6371425361b12c4ca2a36a662d2#n840 Fixes #21621. --- doc/indevs.texi | 3 +- libavdevice/v4l2.c | 99 ++++++++++++++++++++++++++++++---------------- 2 files changed, 68 insertions(+), 34 deletions(-) diff --git a/doc/indevs.texi b/doc/indevs.texi index dc4b92a0a09..6a36c6c029e 100644 --- a/doc/indevs.texi +++ b/doc/indevs.texi @@ -1459,7 +1459,8 @@ Force conversion from monotonic to absolute timestamps. Default value is @code{default}. @item use_libv4l2 -Use libv4l2 (v4l-utils) conversion functions. Default is 0. +Use libv4l2 (v4l-utils) conversion functions. Captured frames are copied +when this option is enabled. Default is 0. @end table diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c index 4d42a3f5de4..7fd04010932 100644 --- a/libavdevice/v4l2.c +++ b/libavdevice/v4l2.c @@ -38,6 +38,8 @@ #include "libavutil/mem.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" +#include "libavutil/refstruct.h" +#include "libavutil/thread.h" #include "libavutil/time.h" #include "libavcodec/avcodec.h" #include "libavcodec/codec_desc.h" @@ -85,6 +87,16 @@ struct buf_data { unsigned int len; }; +/* Refcounted by the demuxer and by zero-copy packets, so that the mappings + * outlive the demuxer if packets are still referenced on close. */ +struct buf_pool { + AVMutex lock; + struct video_data *s; ///< NULL once the demuxer has been closed + int (*munmap_f)(void *_start, size_t length); + int nb_mapped; + struct buf_data buf_data[]; +}; + struct video_data { AVClass *class; int fd; @@ -102,7 +114,7 @@ struct video_data { int buffers; atomic_int buffers_queued; - struct buf_data *buf_data; + struct buf_pool *pool; char *standard; v4l2_std_id std_id; int channel; @@ -126,7 +138,7 @@ struct video_data { }; struct buf_desc { - struct video_data *s; + struct buf_pool *pool; int index; }; @@ -360,11 +372,13 @@ static void list_standards(AVFormatContext *ctx) } } -static void mmap_free(struct video_data *s, int n) +static void buf_pool_free(AVRefStructOpaque unused, void *obj) { - for (int i = 0; i < n; i++) - v4l2_munmap(s->buf_data[i].start, s->buf_data[i].len); - av_freep(&s->buf_data); + struct buf_pool *pool = obj; + + for (int i = 0; i < pool->nb_mapped; i++) + pool->munmap_f(pool->buf_data[i].start, pool->buf_data[i].len); + ff_mutex_destroy(&pool->lock); } static int mmap_init(AVFormatContext *ctx) @@ -388,13 +402,18 @@ static int mmap_init(AVFormatContext *ctx) return AVERROR(ENOMEM); } s->buffers = req.count; - s->buf_data = av_malloc_array(s->buffers, sizeof(struct buf_data)); - if (!s->buf_data) { + s->pool = av_refstruct_alloc_ext(sizeof(*s->pool) + s->buffers * sizeof(*s->pool->buf_data), + 0, NULL, buf_pool_free); + if (!s->pool) { av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer data\n"); return AVERROR(ENOMEM); } + ff_mutex_init(&s->pool->lock, NULL); + s->pool->s = s; + s->pool->munmap_f = s->munmap_f; for (i = 0; i < req.count; i++) { + struct buf_data *bd = &s->pool->buf_data[i]; unsigned int buf_length, buf_offset; struct v4l2_plane planes[VIDEO_MAX_PLANES]; struct v4l2_buffer buf = { @@ -423,29 +442,30 @@ static int mmap_init(AVFormatContext *ctx) buf_offset = buf.m.offset; } - s->buf_data[i].len = buf_length; - if (s->frame_size > 0 && s->buf_data[i].len < s->frame_size) { + bd->len = buf_length; + if (s->frame_size > 0 && bd->len < s->frame_size) { av_log(ctx, AV_LOG_ERROR, "buf_data[%d].len = %d < expected frame size %d\n", i, buf_length, s->frame_size); res = AVERROR(ENOMEM); goto fail; } - s->buf_data[i].start = v4l2_mmap(NULL, buf_length, - PROT_READ | PROT_WRITE, MAP_SHARED, - s->fd, buf_offset); + bd->start = v4l2_mmap(NULL, buf_length, + PROT_READ | PROT_WRITE, MAP_SHARED, + s->fd, buf_offset); - if (s->buf_data[i].start == MAP_FAILED) { + if (bd->start == MAP_FAILED) { res = AVERROR(errno); av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", av_err2str(res)); goto fail; } + s->pool->nb_mapped++; } return 0; fail: - mmap_free(s, i); + av_refstruct_unref(&s->pool); return res; } @@ -468,16 +488,23 @@ static void mmap_release_buffer(void *opaque, uint8_t *data) struct v4l2_plane planes[VIDEO_MAX_PLANES]; struct v4l2_buffer buf = { 0 }; struct buf_desc *buf_descriptor = opaque; - struct video_data *s = buf_descriptor->s; + struct buf_pool *pool = buf_descriptor->pool; + struct video_data *s; + + ff_mutex_lock(&pool->lock); + s = pool->s; + if (s) { + buf.type = s->buf_type; + buf.memory = V4L2_MEMORY_MMAP; + buf.index = buf_descriptor->index; + buf.m.planes = s->multiplanar ? planes : NULL; + buf.length = s->multiplanar ? VIDEO_MAX_PLANES : 0; + enqueue_buffer(s, &buf); + } + ff_mutex_unlock(&pool->lock); - buf.type = s->buf_type; - buf.memory = V4L2_MEMORY_MMAP; - buf.index = buf_descriptor->index; - buf.m.planes = s->multiplanar ? planes : NULL; - buf.length = s->multiplanar ? VIDEO_MAX_PLANES : 0; av_free(buf_descriptor); - - enqueue_buffer(s, &buf); + av_refstruct_unref(&pool); } #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC) @@ -603,8 +630,11 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) } } - /* Image is at s->buf_data[buf.index].start */ - if (atomic_load(&s->buffers_queued) == FFMAX(s->buffers / 8, 1) || + /* Image is at s->pool->buf_data[buf.index].start. + * libv4l2 may own the mapping and does not allow it to outlive the fd, + * so always copy in that case. */ + if (s->use_libv4l2 || + atomic_load(&s->buffers_queued) == FFMAX(s->buffers / 8, 1) || ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) { /* when we start getting low on queued buffers, fall back on copying data */ res = av_new_packet(pkt, bytesused); @@ -613,7 +643,7 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) enqueue_buffer(s, &buf); return res; } - memcpy(pkt->data, s->buf_data[buf.index].start, bytesused); + memcpy(pkt->data, s->pool->buf_data[buf.index].start, bytesused); res = enqueue_buffer(s, &buf); if (res) { @@ -623,7 +653,7 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) } else { struct buf_desc *buf_descriptor; - pkt->data = s->buf_data[buf.index].start; + pkt->data = s->pool->buf_data[buf.index].start; pkt->size = bytesused; buf_descriptor = av_malloc(sizeof(struct buf_desc)); @@ -637,13 +667,14 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) return AVERROR(ENOMEM); } buf_descriptor->index = buf.index; - buf_descriptor->s = s; + buf_descriptor->pool = av_refstruct_ref(s->pool); pkt->buf = av_buffer_create(pkt->data, pkt->size, mmap_release_buffer, buf_descriptor, 0); if (!pkt->buf) { av_log(ctx, AV_LOG_ERROR, "Failed to create a buffer\n"); enqueue_buffer(s, &buf); + av_refstruct_unref(&buf_descriptor->pool); av_freep(&buf_descriptor); return AVERROR(ENOMEM); } @@ -695,11 +726,17 @@ static void mmap_close(struct video_data *s) enum v4l2_buf_type type; type = s->buf_type; + + /* Detach outstanding packets, they will only unmap from now on. */ + ff_mutex_lock(&s->pool->lock); + s->pool->s = NULL; + ff_mutex_unlock(&s->pool->lock); + /* We do not check for the result, because we could * not do anything about it anyway... */ v4l2_ioctl(s->fd, VIDIOC_STREAMOFF, &type); - mmap_free(s, s->buffers); + av_refstruct_unref(&s->pool); } static int v4l2_set_parameters(AVFormatContext *ctx) @@ -1051,10 +1088,6 @@ static int v4l2_read_close(AVFormatContext *ctx) { struct video_data *s = ctx->priv_data; - if (atomic_load(&s->buffers_queued) != s->buffers) - av_log(ctx, AV_LOG_WARNING, "Some buffers are still owned by the caller on " - "close.\n"); - mmap_close(s); ff_timefilter_destroy(s->timefilter); -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
