process_callback() runs on the JACK realtime thread and reads self->new_pkts and writes self->filled_pkts. The demux thread writes new_pkts (supply_new_packets) and reads filled_pkts (audio_read_packet). AVFifo is not thread-safe, so these concurrent, lockless accesses are data races on the fifo internal state (offset_r/offset_w etc.) which can corrupt the fifo or read stale state.
A mutex cannot be used inside process_callback() because it must not block (it is realtime). Protect the short fifo critical sections with a spinlock instead; the critical sections only perform fifo read/write/can_read/can_write, which is bounded work. The lock is initialized once the fifos exist (start_jack) and destroyed in stop_jack() after jack_client_close() has guaranteed the callback is no longer running. Reported-by: FFmpeg UAF audit (libavdevice/jack.c lockless AVFifo access) Signed-off-by: Hongyan Xu <[email protected]> --- libavdevice/jack.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/libavdevice/jack.c b/libavdevice/jack.c index bb06971..f0a2b1a 100644 --- a/libavdevice/jack.c +++ b/libavdevice/jack.c @@ -21,6 +21,7 @@ */ #include "config.h" +#include <pthread.h> #include <semaphore.h> #include <jack/jack.h> @@ -53,6 +54,14 @@ typedef struct JackData { TimeFilter * timefilter; AVFifo * new_pkts; AVFifo * filled_pkts; + /* + * process_callback() runs on the JACK realtime thread while + * supply_new_packets()/audio_read_packet() run on the demux thread; + * both sides touch the (non thread-safe) AVFifos above. A spinlock + * is used instead of a mutex because the callback must not block, + * and the critical sections only guard short FIFO operations. + */ + pthread_spinlock_t fifo_lock; int pkt_xrun; int jack_xrun; } JackData; @@ -82,14 +91,17 @@ static int process_callback(jack_nframes_t nframes, void *arg) self->buffer_size); /* Check if an empty packet is available, and if there's enough space to send it back once filled */ + pthread_spin_lock(&self->fifo_lock); if (!av_fifo_can_read(self->new_pkts) || !av_fifo_can_write(self->filled_pkts)) { self->pkt_xrun = 1; + pthread_spin_unlock(&self->fifo_lock); return 0; } /* Retrieve empty (but allocated) packet */ av_fifo_read(self->new_pkts, &pkt, 1); + pthread_spin_unlock(&self->fifo_lock); pkt_data = (float *) pkt.data; latency = 0; @@ -108,7 +120,9 @@ static int process_callback(jack_nframes_t nframes, void *arg) pkt.pts = (cycle_time - (double) latency / (self->nports * self->sample_rate)) * 1000000.0; /* Send the now filled packet back, and increase packet counter */ + pthread_spin_lock(&self->fifo_lock); av_fifo_write(self->filled_pkts, &pkt, 1); + pthread_spin_unlock(&self->fifo_lock); sem_post(&self->packet_count); return 0; @@ -136,13 +150,16 @@ static int supply_new_packets(JackData *self, AVFormatContext *context) /* Supply the process callback with new empty packets, by filling the new * packets FIFO buffer with as many packets as possible. process_callback() * can't do this by itself, because it can't allocate memory in realtime. */ + pthread_spin_lock(&self->fifo_lock); while (av_fifo_can_write(self->new_pkts)) { if ((test = av_new_packet(&pkt, pkt_size)) < 0) { av_log(context, AV_LOG_ERROR, "Could not create packet of size %d\n", pkt_size); + pthread_spin_unlock(&self->fifo_lock); return test; } av_fifo_write(self->new_pkts, &pkt, 1); } + pthread_spin_unlock(&self->fifo_lock); return 0; } @@ -202,6 +219,7 @@ static int start_jack(AVFormatContext *context) jack_client_close(self->client); return AVERROR(ENOMEM); } + pthread_spin_init(&self->fifo_lock, PTHREAD_PROCESS_PRIVATE); if ((test = supply_new_packets(self, context))) { jack_client_close(self->client); return test; @@ -228,6 +246,12 @@ static void stop_jack(JackData *self) jack_client_close(self->client); } sem_destroy(&self->packet_count); + /* + * jack_client_close() above guarantees process_callback() is no + * longer running, so it is safe to destroy the lock and free the + * fifos. + */ + pthread_spin_destroy(&self->fifo_lock); free_pkt_fifo(&self->new_pkts); free_pkt_fifo(&self->filled_pkts); av_freep(&self->ports); @@ -312,7 +336,9 @@ static int audio_read_packet(AVFormatContext *context, AVPacket *pkt) } /* Retrieve the packet filled with audio data by process_callback() */ + pthread_spin_lock(&self->fifo_lock); av_fifo_read(self->filled_pkts, pkt, 1); + pthread_spin_unlock(&self->fifo_lock); if ((test = supply_new_packets(self, context))) return test; -- 2.50.1.windows.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
