Skip to contentSkip to conversation

media: dvb-core: publish device minors after registration succeeds

PATCH New Yousef Alhouseen 1 participant 1 message Jun 28

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

PATCH 1/1 media: dvb-core: publish device minors after registration succeeds

dvb_register_device() publishes dvbdev in the minor table before media graph and class-device creation, both of which can still fail. Their error paths then free dvbdev directly without removing the table entry or dropping its minor reference. Opening a manually created device node can race with or follow that failure, causing refcount corruption and use-after-free; without an open, the minor reference is leaked.

Keep the chosen minor private until all fallible registration steps have succeeded. On failure, release the initial reference through dvb_device_put() rather than bypassing the kref.

---
 drivers/media/dvb-core/dvbdev.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/media/dvb-core/dvbdev.c +6 -3
index d753d329502a..ca4d61a94270 100644
@@ -545,7 +545,6 @@ int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
 	}
 
 	dvbdev->minor = minor;
-	dvb_minors[minor] = dvb_device_get(dvbdev);
 	up_write(&minor_rwsem);
 	ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
 	if (ret) {
@@ -558,7 +557,7 @@ int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
 		}
 		dvb_media_device_free(dvbdev);
 		list_del(&dvbdev->list_head);
-		kfree(dvbdev);
+		dvb_device_put(dvbdev);
 		*pdvbdev = NULL;
 		mutex_unlock(&dvbdev_register_lock);
 		return ret;
@@ -577,12 +576,16 @@ int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
 		}
 		dvb_media_device_free(dvbdev);
 		list_del(&dvbdev->list_head);
-		kfree(dvbdev);
+		dvb_device_put(dvbdev);
 		*pdvbdev = NULL;
 		mutex_unlock(&dvbdev_register_lock);
 		return PTR_ERR(clsdev);
 	}
 
+	down_write(&minor_rwsem);
+	dvb_minors[minor] = dvb_device_get(dvbdev);
+	up_write(&minor_rwsem);
+
 	dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
 		adap->num, dnames[type], id, minor, minor);
 
Signature · 1 line
2.54.0