Skip to contentSkip to conversation

media: dvb-usb-v2: set active_fe before the demux goes live

PATCH In review Nguyen Ngoc Thang 1 participant 2 messages Sep 27 – Oct 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 media: dvb-usb-v2: set active_fe before the demux goes live

dvb_usbv2_adapter_dvb_init() registers the demux devices before dvb_usbv2_adapter_frontend_init() runs. Only the latter set adap->active_fe to -1, so until then it holds the kzalloc'ed 0 while adap->fe[0] is still NULL.

A process that opens the demux and starts a filter in that window, which lasts as long as frontend_attach() takes, gets past the active_fe == -1 check in dvb_usb_start_feed() and passes the NULL adap->fe[0] to streaming_ctrl() (or get_stream_config()). mxl111sf_ep6_streaming_ctrl() then dereferences it:

  Oops: general protection fault, probably for non-canonical address
  0xdffffc0000000061
  KASAN: null-ptr-deref in range [0x0000000000000308-0x000000000000030f]
  RIP: 0010:mxl111sf_ep6_streaming_ctrl+0x2e/0x2e0
   dvb_usb_start_feed+0x57a/0xc70
   dmx_ts_feed_start_filtering+0x129/0x220
   dvb_dmxdev_filter_start+0x434/0x10d0
   dvb_demux_do_ioctl+0x48b/0x540

Initialise active_fe to -1 before the demux is registered so early feeds fail with -EINVAL.

Reported-by [email protected]
Fixes ad6b9e517b39 ("[media] dvb_usb_v2: move from dvb-usb to dvb-usb-v2")
---
 drivers/media/usb/dvb-usb-v2/dvb_usb_core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/media/usb/dvb-usb-v2/dvb_usb_core.c +3 -1
index bd86d250433d..557451b541e6 100644
@@ -637,7 +637,6 @@ static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
 	dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
 
 	memset(adap->fe, 0, sizeof(adap->fe));
-	adap->active_fe = -1;
 
 	if (d->props->frontend_attach) {
 		ret = d->props->frontend_attach(adap);
@@ -795,6 +794,9 @@ static int dvb_usbv2_adapter_init(struct dvb_usb_device *d)
 			adap->max_feed_count = adap->props->pid_filter_count;
 		}
 
+		/* demux is live before frontends attach: no feed until then */
+		adap->active_fe = -1;
+
 		ret = dvb_usbv2_adapter_stream_init(adap);
 		if (ret)
 			goto err;
Signature · 1 line
2.43.0
#1 lore raw

syzbot has filed a second report for the same race, this time through the gl861 (Friio White) driver:

https://syzkaller.appspot.com/bug?extid=ac020fbb657e00356ca2

  Oops: general protection fault, probably for non-canonical address 0xdffffc0000000061
  KASAN: null-ptr-deref in range [0x0000000000000308-0x000000000000030f]
  RIP: 0010:friio_streaming_ctrl+0x43/0xd0 drivers/media/usb/dvb-usb-v2/gl861.c:521
   dvb_usb_start_feed+0x57a/0xc70 drivers/media/usb/dvb-usb-v2/dvb_usb_core.c:308
   dmx_section_feed_start_filtering+0x519/0x6b0
   dvb_dmxdev_filter_start+0xcdb/0x10d0
   dvb_demux_do_ioctl+0x470/0x540

The root cause is the same. The demux is registered while adap->active_fe still holds the kzalloc'ed 0 and adap->fe[0] is NULL. A DMX_SET_FILTER issued in that window passes the NULL frontend to ->streaming_ctrl(). In this report that is friio_streaming_ctrl(), which dereferences fe->dvb (offset 0x308). The fault is in the core, not in either driver, so this patch fixes both reports.

I tested this with syzbot's C reproducer, which binds a configfs SourceSink gadget with the Friio VID/PID (0x7a69:0x0001) over dummy_hcd while 8 threads keep opening the demux and starting section filters. Both kernels were built from the same tree and syzbot's config, with and without this patch:

- without the patch: the oops above on the first Friio probe, 3/3 runs
- with the patch: no oops over 20 Friio probe cycles in 300s

Mauro, if you pick up v2, please add the following tags for the second report:

Thanks,
Thang