async_buffer_task() (background thread) fills the ring buffer and updates ring->read_pos / the underlying fifo while holding c->mutex. async_read_internal() does the same on the read side.
async_seek()'s short-seek path reads ring_size() and ring_size_of_read_back() and, for a backward seek, calls ring_drain() to rewind the read position. These helpers touch ring->read_pos and the av_fifo state which the background thread modifies concurrently, so reading the fifo size and then draining in a later, separate critical section is racy: the fifo may shrink in between, letting ring_drain() rewind past the available data and corrupt/overread the fifo. Take c->mutex once around the range check and, when the seek is a backward short seek, the drain itself, so the fifo state cannot change between the two. A forward short seek still runs outside the lock because async_read_internal() takes c->mutex itself (taking it again here would deadlock). Reported-by: FFmpeg UAF audit (libavformat/async.c lockless ring access) Signed-off-by: Hongyan Xu <[email protected]> --- libavformat/async.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/libavformat/async.c b/libavformat/async.c index 5831b12..514925a 100644 --- a/libavformat/async.c +++ b/libavformat/async.c @@ -415,10 +415,13 @@ static int64_t async_seek(URLContext *h, int64_t pos, int whence) if (new_logical_pos < 0) return AVERROR(EINVAL); + pthread_mutex_lock(&c->mutex); fifo_size = ring_size(ring); fifo_size_of_read_back = ring_size_of_read_back(ring); + if (new_logical_pos == c->logical_pos) { /* current position */ + pthread_mutex_unlock(&c->mutex); return c->logical_pos; } else if ((new_logical_pos >= (c->logical_pos - fifo_size_of_read_back)) && (new_logical_pos < (c->logical_pos + fifo_size + SHORT_SEEK_THRESHOLD))) { @@ -429,21 +432,38 @@ static int64_t async_seek(URLContext *h, int64_t pos, int whence) (int)(new_logical_pos - c->logical_pos), fifo_size); if (pos_delta > 0) { - // fast seek forwards + /* + * fast seek forwards: done outside the lock because + * async_read_internal() takes c->mutex itself. + */ + pthread_mutex_unlock(&c->mutex); async_read_internal(h, NULL, pos_delta); + return c->logical_pos; } else { - // fast seek backwards + /* + * fast seek backwards: ring_drain() and ring_size() touch + * ring->read_pos and the underlying fifo, which the + * background fill thread also modifies while holding + * c->mutex. Hold the lock across both the range check above + * and the drain so the fifo state cannot change in between + * (otherwise a stale fifo_size could let ring_drain() walk + * past the drained amount and corrupt/overread the fifo). + */ ring_drain(ring, pos_delta); c->logical_pos = new_logical_pos; + pthread_mutex_unlock(&c->mutex); } return c->logical_pos; - } else if (c->logical_size <= 0) { - /* can not seek */ - return AVERROR(EINVAL); - } else if (new_logical_pos > c->logical_size) { - /* beyond end */ - return AVERROR(EINVAL); + } else { + pthread_mutex_unlock(&c->mutex); + if (c->logical_size <= 0) { + /* can not seek */ + return AVERROR(EINVAL); + } else if (new_logical_pos > c->logical_size) { + /* beyond end */ + return AVERROR(EINVAL); + } } pthread_mutex_lock(&c->mutex); -- 2.50.1.windows.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
