Module Name:    src
Committed By:   maxv
Date:           Sat Sep 14 15:21:19 UTC 2019

Modified Files:
        src/sys/dev/usb: ustir.c

Log Message:
Fix error handling, to prevent kernel crashes at detach time. Found by
vHCI.


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/sys/dev/usb/ustir.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/usb/ustir.c
diff -u src/sys/dev/usb/ustir.c:1.40 src/sys/dev/usb/ustir.c:1.41
--- src/sys/dev/usb/ustir.c:1.40	Sun May  5 03:17:54 2019
+++ src/sys/dev/usb/ustir.c	Sat Sep 14 15:21:19 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: ustir.c,v 1.40 2019/05/05 03:17:54 mrg Exp $	*/
+/*	$NetBSD: ustir.c,v 1.41 2019/09/14 15:21:19 maxv Exp $	*/
 
 /*
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ustir.c,v 1.40 2019/05/05 03:17:54 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ustir.c,v 1.41 2019/09/14 15:21:19 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -96,6 +96,10 @@ struct ustir_softc {
 	device_t		sc_dev;
 	struct usbd_device	*sc_udev;
 	struct usbd_interface	*sc_iface;
+	enum {
+		USTIR_INIT_NONE,
+		USTIR_INIT_INITED
+	} sc_init_state;
 
 	uint8_t			*sc_ur_buf; /* Unencapsulated frame */
 	u_int			sc_ur_framelen;
@@ -250,6 +254,7 @@ ustir_attach(device_t parent, device_t s
 	DPRINTFN(10,("ustir_attach: sc=%p\n", sc));
 
 	sc->sc_dev = self;
+	sc->sc_init_state = USTIR_INIT_NONE;
 
 	aprint_naive("\n");
 	aprint_normal("\n");
@@ -302,6 +307,7 @@ ustir_attach(device_t parent, device_t s
 	sc->sc_child = config_found(self, &ia, ir_print);
 	selinit(&sc->sc_rd_sel);
 	selinit(&sc->sc_wr_sel);
+	sc->sc_init_state = USTIR_INIT_INITED;
 
 	return;
 }
@@ -369,10 +375,12 @@ ustir_detach(device_t self, int flags)
 	if (sc->sc_child != NULL)
 		rv = config_detach(sc->sc_child, flags);
 
-	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
-
-	seldestroy(&sc->sc_rd_sel);
-	seldestroy(&sc->sc_wr_sel);
+	if (sc->sc_init_state >= USTIR_INIT_INITED) {
+		usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
+		    sc->sc_dev);
+		seldestroy(&sc->sc_rd_sel);
+		seldestroy(&sc->sc_wr_sel);
+	}
 
 	return rv;
 }

Reply via email to