media: dvb_demux: fix potential TOCTOU race conditions
Subsystems (2)
PATCH RESEND v2 1/1 media: dvb_demux: fix potential TOCTOU race conditions
The dvb_demux functions handle frontend connectivity without holding dvbdemux->mutex during checks, leading to TOCTOU race conditions. In dvbdmx_write(), a concurrent dvbdmx_disconnect_frontend() can set demux->frontend to NULL after the check, causing a potential NULL pointer dereference. In dvbdmx_connect_frontend(), a concurrent connection could set the frontend between the check and the lock. This allows the second caller to overwrite the existing frontend, leading to resource leaks. The dvb_demux module should use its own mutex to ensure thread safety for these internal state checks.
Fix this by extending the lock scope. Move the frontend state checks inside the dvbdemux->mutex critical section to ensure the state remains stable during the operation.
This possible bug was found by our experimental static analysis tool, which analyzes lock usage to detect TOCTOU issues.
---
v2:
* Remove unnecessary parentheses to fix checkpatch --strict warning, as reported by Media CI robot.
--- drivers/media/dvb-core/dvb_demux.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/media/dvb-core/dvb_demux.c +13 -8
@@ -1141,15 +1141,18 @@ static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_tstruct dvb_demux *dvbdemux = (struct dvb_demux *)demux;void *p; -if ((!demux->frontend) || (demux->frontend->source !=DMX_MEMORY_FE )) +if (mutex_lock_interruptible (&dvbdemux->mutex)) +return -ERESTARTSYS ; + +if (!demux->frontend || demux->frontend->source !=DMX_MEMORY_FE ) { +mutex_unlock (&dvbdemux->mutex);return -EINVAL ; + } p =memdup_user (buf, count); -if (IS_ERR (p)) +if (IS_ERR (p)) { +mutex_unlock (&dvbdemux->mutex);return PTR_ERR (p); -if (mutex_lock_interruptible (&dvbdemux->mutex)) { -kfree (p); -return -ERESTARTSYS ; }dvb_dmx_swfilter (dvbdemux, p, count);kfree (p);
@@ -1202,11 +1205,13 @@ static int dvbdmx_connect_frontend(struct dmx_demux *demux, {struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; -if (demux->frontend) -return -EINVAL ; -mutex_lock (&dvbdemux->mutex); +if (demux->frontend) { +mutex_unlock (&dvbdemux->mutex); +return -EINVAL ; + } + demux->frontend = frontend;mutex_unlock (&dvbdemux->mutex);return 0 ;
Signature · 1 line
2.43.0