MT#55283 jitter_buffer: lock the timer queue when discarding burst packets

remove_oldest_packets() took entries out of jb->ttq.entries and freed
them without holding jb->ttq.lock. It even dropped jb->lock first, but
that lock never protected the tree: the jitter buffer thread pops,
runs and re-inserts entries in timerthread_queue_run() under
ttq.lock only.

So when a burst of packets pushed the buffer over its target capacity,
the media thread could modify the GTree at the same time as the jitter
buffer thread. Both threads can also pick the same first entry, which
is then freed by the discard and sent and freed again by the timer
thread. Either way this corrupts the heap and crashes the daemon.

Take ttq.lock around the removal, as timerthread_queue_flush() does,
and keep jb->lock held. The lock order jb->lock -> ttq.lock is the one
timerthread_queue_push() already uses from queue_packet(), and
jb_packet_free() takes neither lock.

Closes #2225

Change-Id: Ibab99b0258c6a573018f76c1c58ee5518c04dac0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 4aba72b51f)
mr26.3
Dragos Oancea 3 weeks ago committed by Richard Fuchs
parent 1fbf6abcb5
commit 95f87ab677

@ -186,26 +186,29 @@ static struct jb_packet* get_jb_packet(struct media_packet *mp, const str *s) {
return p;
}
// jb is locked (temporarily unlocked during operation, then relocked)
// jb is locked
static int remove_oldest_packets(struct jitter_buffer *jb, int num_to_remove) {
if (num_to_remove <= 0)
return 0;
int removed = 0;
mutex_unlock(&jb->lock);
// The jitter buffer thread pops and re-inserts entries under the queue's
// own lock, so the tree must only be touched while holding it. Lock order
// jb->lock -> ttq.lock is the same as in timerthread_queue_push().
LOCK(&jb->ttq.lock);
for (int i = 0; i < num_to_remove; i++) {
struct timerthread_queue_entry *ttqe = rtpe_g_tree_first(jb->ttq.entries);
if (!ttqe)
break;
g_tree_remove(jb->ttq.entries, ttqe);
if (jb->ttq.entry_free_func)
jb->ttq.entry_free_func(ttqe);
removed++;
}
mutex_lock(&jb->lock);
return removed;
}

Loading…
Cancel
Save