NetBSD Problem Report #38528

From martin@duskware.de  Sun Apr 27 11:36:26 2008
Return-Path: <martin@duskware.de>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by narn.NetBSD.org (Postfix) with ESMTP id 5AD4B63B8BC
	for <gnats-bugs@gnats.netbsd.org>; Sun, 27 Apr 2008 11:36:26 +0000 (UTC)
Message-Id: <20080427110922.2822763B8BC@narn.NetBSD.org>
Date: Sun, 27 Apr 2008 11:09:22 +0000 (UTC)
From: Christoph_Egger@gmx.de
Reply-To: Christoph_Egger@gmx.de
To: netbsd-bugs-owner@NetBSD.org
Subject: panic on unplugging Apple USB keyboard
X-Send-Pr-Version: www-1.0

>Number:         38528
>Category:       kern
>Synopsis:       panic on unplugging Apple USB keyboard
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kern-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Apr 27 11:40:00 +0000 2008
>Closed-Date:    Mon May 26 19:03:35 +0000 2008
>Last-Modified:  Mon May 26 19:05:02 +0000 2008
>Originator:     Christoph Egger
>Release:        4.99.61
>Organization:
>Environment:
NetBSD netbsdamd64 4.99.61 NetBSD 4.99.61 (GENERIC) #22: Sun Apr 27 12:17:06 CEST 2008  cegger@powermacg5.local:/Users/cegger/devel/bsd/netbsd/obj.kern.amd64/GENERIC amd64

>Description:

A DIAGNOSTIC + DEBUG  kernel panics when unplugging
an Apple USB Keyboard.

It calls KASSERT(false); in src/sys/dev/usb/uhub.c:uhub_childdet(),
line 641.

On the uhub attaches uhidev0 and uhidev1.
Detaching uhidev0, but on detaching uhidev1, dev->subdevs[0] is NULL
in line 634: "for (i = 0; dev->subdevs[i]; i++) {"

Therefore, the loop is not entered and KASSERT(false); is triggered.

A successful tested possible solution is this diff written
by mlelstv:

Index: uhub.c
===================================================================
RCS file: /cvsroot/src/sys/dev/usb/uhub.c,v
retrieving revision 1.96
diff -u -r1.96 uhub.c
--- uhub.c      21 Apr 2008 23:31:18 -0000      1.96
+++ uhub.c      27 Apr 2008 09:39:14 -0000
@@ -633,7 +633,9 @@
                        continue;
                for (i = 0; dev->subdevs[i]; i++) {
                        if (dev->subdevs[i] == child) {
-                               dev->subdevs[i] = NULL;
+                               do {
+                                       dev->subdevs[i] = dev->subdevs[i+1];
+                               } while (dev->subdevs[++i] != NULL);
                                return;
                        }
                }


However, this triggers another bug in src/sys/dev/usb/usb_subr.c:usb_disconnect_port :

config_detach: detached device uhub2 has children uhidev1
panic: config_detach

backtrace:
config_detach+0x388
usb_disconnect_port+0x7d
uhub_explore+0x12b
usb_discover+0x37
usb_event_thread+0x3e

The problem in usb_disconnect_port is 1506:

for (i = 0; dev->subdevs[i]; i++) {

It assumes an 0 terminated list whereby the config_detach()
modifies it. So the assumption is wrong.

A successful tested possible solution is this diff written by
mlelstv:

Index: usb_subr.c
===================================================================
RCS file: /cvsroot/src/sys/dev/usb/usb_subr.c,v
retrieving revision 1.152
diff -u -r1.152 usb_subr.c
--- usb_subr.c  5 Apr 2008 16:35:35 -0000       1.152
+++ usb_subr.c  27 Apr 2008 10:15:26 -0000
@@ -1503,7 +1503,9 @@

        if (dev->subdevs != NULL) {
                DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
-               for (i = 0; dev->subdevs[i]; i++) {
+               for (i = 0; dev->subdevs[i]; i++)
+                       continue;
+               while (--i >= 0) {
                        printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
                               hubname);
                        if (up->portno != 0)




>How-To-Repeat:

>Fix:

Use an DIAGNOSTIC+DEBUG kernel and unplug an Apple USB keyboard.

>Release-Note:

>Audit-Trail:

State-Changed-From-To: open->feedback
State-Changed-By: drochner@NetBSD.org
State-Changed-When: Sun, 25 May 2008 21:43:14 +0000
State-Changed-Why:
I've reorganized the code somewhat, could you try with -current?


From: Matthias Drochner <drochner@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/38528 CVS commit: src/sys/dev/usb
Date: Sun, 25 May 2008 21:41:35 +0000 (UTC)

 Module Name:	src
 Committed By:	drochner
 Date:		Sun May 25 21:41:35 UTC 2008

 Modified Files:
 	src/sys/dev/usb: files.usb uhub.c usb.c usb_subr.c usbdivar.h

 Log Message:
 -make the list of USB child devices a (possibly sparse) array rather
  than a zero-terminated list; this makes the code simpler and also
  hopefully fixes the recent "childdet" botch, see PR kern/38528
 -handle the root hub specially a bit earlier, this allows to kick out
  the "submatch" functions completely which needed to second-guess
  from the port number (where "0" meant root hub")
  (we could handle the root hub specially even earlier, but as done
  now big parts of the hub emulation code are exercised regularely,
  this would bitrot otherwise)


 To generate a diff of this commit:
 cvs rdiff -r1.82 -r1.83 src/sys/dev/usb/files.usb
 cvs rdiff -r1.98 -r1.99 src/sys/dev/usb/uhub.c
 cvs rdiff -r1.113 -r1.114 src/sys/dev/usb/usb.c
 cvs rdiff -r1.154 -r1.155 src/sys/dev/usb/usb_subr.c
 cvs rdiff -r1.84 -r1.85 src/sys/dev/usb/usbdivar.h

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

From: Christoph Egger <Christoph_Egger@gmx.de>
To: gnats-bugs@NetBSD.org
Cc: kern-bug-people@netbsd.org, netbsd-bugs@netbsd.org, 
 gnats-admin@netbsd.org, drochner@NetBSD.org
Subject: Re: kern/38528 (panic on unplugging Apple USB keyboard)
Date: Mon, 26 May 2008 00:34:14 +0200

 drochner@NetBSD.org wrote:
 > I've reorganized the code somewhat, could you try with -current?

 A -current kernel still panics. The backtrace is different now:

 uhub2: uhub0 port 6 (addr 3) disconnected
 uhidev0: at uhub2 port 3 (addr 4) disconnected
 wskbd1: detached
 ukbd0: detached
 uhidev0: detached
 uhidev1: at uhub2 port 3 (addr 4) disconnected
 uvm_fault(0xffffffff80cac800, 0x0, 1) -> e
 kernel: page fault trap, code=0
 Stopped in pid 0.40 (system) at netbsd:uhidev_childdet+0x32: cmpq 
 0(%rax),%r12
 db{1}> bt
 uhidev_childdet() at netbsd:uhidev_childdet+0x32
 config_detach() at netbsd:config_detach+0x1de
 uhidev_detach() at netbsd:uhidev_detach+0x80
 config_detach() at netbsd:config_detach+0x192
 usb_disconnect_port() at netbsd:usb_disconnect_port+0x69
 uhub_detach() at netbsd:uhub_detach+0x82
 config_detach() at netbsd:uhub_detach+0x192
 usb_disconnect_port() at netbsd:usb_disconnect_port+0x69
 uhub_explore() at netbsd:uhub_explore+0x11e
 usb_discover() at netbsd:usb_discover+0x37
 usb_event_thread() at netbsd:usb_event_thread+0x3e

 The %rax is 0  in the cmpq 0(%rax),%r12 instruction.
 So it's a NULL dereference bug.

 Christoph

From: Matthias Drochner <M.Drochner@fz-juelich.de>
To: Christoph Egger <Christoph_Egger@gmx.de>
Cc: gnats-bugs@NetBSD.org, kern-bug-people@NetBSD.org,
	netbsd-bugs@NetBSD.org, gnats-admin@NetBSD.org, drochner@NetBSD.org
Subject: Re: kern/38528 (panic on unplugging Apple USB keyboard) 
Date: Mon, 26 May 2008 12:39:33 +0200

 This is a multipart MIME message.

 --==_Exmh_10470162210690
 Content-Type: text/plain; charset=us-ascii


 Christoph_Egger@gmx.de said:
 > db{1}> bt
 > uhidev_childdet() at netbsd:uhidev_childdet+0x32
 > config_detach() at netbsd:config_detach+0x1de 

 This is a different bug, a new one. Can you try the appended patch?

 best regards
 Matthias





 -------------------------------------------------------------------
 -------------------------------------------------------------------
 Forschungszentrum Juelich GmbH
 52425 Juelich

 Sitz der Gesellschaft: Juelich
 Eingetragen im Handelsregister des Amtsgerichts Dueren Nr. HR B 3498
 Vorsitzende des Aufsichtsrats: MinDir'in Baerbel Brumme-Bothe
 Geschaeftsfuehrung: Prof. Dr. Achim Bachem (Vorsitzender),
 Dr. Ulrich Krafft (stellv. Vorsitzender), Prof. Dr. Harald Bolt,
 Dr. Sebastian M. Schmidt
 -------------------------------------------------------------------
 -------------------------------------------------------------------

 --==_Exmh_10470162210690
 Content-Type: text/plain ; name="uhid.txt"; charset=us-ascii
 Content-Description: uhid.txt
 Content-Disposition: attachment; filename="uhid.txt"

 #
 # old_revision [bdb4e55c6bf540d7f3dea8012f0ea2b929c2e69b]
 #
 # patch "sys/dev/usb/uhidev.c"
 #  from [f79d1c3609a5e0b85227db74ee82e45cab36bf8a]
 #    to [6cb6f4302877f3032082964a88c8881fb7ab138e]
 # 
 # patch "sys/dev/usb/uhidev.h"
 #  from [f34395cbd85e729f736a8288b1d9622082443c55]
 #    to [8badca5b78c7b292fc1923c22ede3e6d17dd95dc]
 #
 ============================================================
 --- sys/dev/usb/uhidev.c	f79d1c3609a5e0b85227db74ee82e45cab36bf8a
 +++ sys/dev/usb/uhidev.c	6cb6f4302877f3032082964a88c8881fb7ab138e
 @@ -103,7 +103,8 @@ USB_ATTACH(uhidev)
  	usb_interface_descriptor_t *id;
  	usb_endpoint_descriptor_t *ed;
  	struct uhidev_attach_arg uha;
 -	struct uhidev *dev;
 +	device_t dev;
 +	struct uhidev *csc;
  	int size, nrepid, repid, repsz;
  	int *repsizes;
  	int i;
 @@ -290,16 +291,17 @@ nomem:
  			uha.reportid = repid;
  			locs[UHIDBUSCF_REPORTID] = repid;

 -			dev = device_private(config_found_sm_loc(self,
 +			dev = config_found_sm_loc(self,
  				"uhidbus", locs, &uha,
 -				uhidevprint, config_stdsubmatch));
 +				uhidevprint, config_stdsubmatch);
  			sc->sc_subdevs[repid] = dev;
  			if (dev != NULL) {
 -				dev->sc_in_rep_size = repsizes[repid];
 +				csc = device_private(dev);
 +				csc->sc_in_rep_size = repsizes[repid];
  #ifdef DIAGNOSTIC
  				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
  					 repid, dev));
 -				if (dev->sc_intr == NULL) {
 +				if (csc->sc_intr == NULL) {
  					free(repsizes, M_TEMP);
  					aprint_error_dev(self,
  					    "sc_intr == NULL\n");
 @@ -307,8 +309,8 @@ nomem:
  				}
  #endif
  #if NRND > 0
 -				rnd_attach_source(&dev->rnd_source,
 -						  USBDEVNAME(dev->sc_dev),
 +				rnd_attach_source(&csc->rnd_source,
 +						  USBDEVNAME(dev),
  						  RND_TYPE_TTY, 0);
  #endif
  			}
 @@ -362,7 +364,7 @@ uhidev_activate(device_t self, enum deva
  		for (i = 0; i < sc->sc_nrepid; i++)
  			if (sc->sc_subdevs[i] != NULL)
  				rv |= config_deactivate(
 -					sc->sc_subdevs[i]->sc_dev);
 +					sc->sc_subdevs[i]);
  		sc->sc_dying = 1;
  		break;
  	default:
 @@ -379,7 +381,7 @@ uhidev_childdet(device_t self, device_t 
  	struct uhidev_softc *sc = device_private(self);

  	for (i = 0; i < sc->sc_nrepid; i++) {
 -		if (sc->sc_subdevs[i]->sc_dev == child)
 +		if (sc->sc_subdevs[i] == child)
  			break;
  	}
  	KASSERT(i < sc->sc_nrepid);
 @@ -390,6 +392,9 @@ USB_DETACH(uhidev)
  {
  	USB_DETACH_START(uhidev, sc);
  	int i, rv;
 +#if NRND > 0
 +	struct uhidev *csc;
 +#endif

  	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));

 @@ -404,9 +409,10 @@ USB_DETACH(uhidev)
  	for (i = 0; i < sc->sc_nrepid; i++) {
  		if (sc->sc_subdevs[i] != NULL) {
  #if NRND > 0
 -			rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
 +			csc = device_private(sc->sc_subdevs[i]);
 +			rnd_detach_source(&csc->rnd_source);
  #endif
 -			rv |= config_detach(sc->sc_subdevs[i]->sc_dev, flags);
 +			rv |= config_detach(sc->sc_subdevs[i], flags);
  		}
  	}

 @@ -422,6 +428,7 @@ uhidev_intr(usbd_xfer_handle xfer, usbd_
  uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
  {
  	struct uhidev_softc *sc = addr;
 +	device_t cdev;
  	struct uhidev *scd;
  	u_char *p;
  	u_int rep;
 @@ -460,10 +467,13 @@ uhidev_intr(usbd_xfer_handle xfer, usbd_
  		printf("uhidev_intr: bad repid %d\n", rep);
  		return;
  	}
 -	scd = sc->sc_subdevs[rep];
 +	cdev = sc->sc_subdevs[rep];
 +	if (!cdev)
 +		return;
 +	scd = device_private(cdev);
  	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
  		    rep, scd, scd ? scd->sc_state : 0));
 -	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
 +	if (!(scd->sc_state & UHIDEV_OPEN))
  		return;
  	if (scd->sc_in_rep_size != cc) {
  		printf("%s: bad input length %d != %d\n",
 ============================================================
 --- sys/dev/usb/uhidev.h	f34395cbd85e729f736a8288b1d9622082443c55
 +++ sys/dev/usb/uhidev.h	8badca5b78c7b292fc1923c22ede3e6d17dd95dc
 @@ -53,7 +53,7 @@ struct uhidev_softc {
  	int sc_repdesc_size;

  	u_int sc_nrepid;
 -	struct uhidev **sc_subdevs;
 +	device_t *sc_subdevs;

  	int sc_refcnt;
  	u_char sc_dying;

 --==_Exmh_10470162210690--

From: Christoph Egger <Christoph_Egger@gmx.de>
To: M.Drochner@fz-juelich.de
Cc: gnats-bugs@NetBSD.org, kern-bug-people@NetBSD.org, 
 netbsd-bugs@NetBSD.org, gnats-admin@NetBSD.org, drochner@NetBSD.org
Subject: Re: kern/38528 (panic on unplugging Apple USB keyboard)
Date: Mon, 26 May 2008 20:37:01 +0200

 Matthias Drochner wrote:
 > Christoph_Egger@gmx.de said:
 >> db{1}> bt
 >> uhidev_childdet() at netbsd:uhidev_childdet+0x32
 >> config_detach() at netbsd:config_detach+0x1de 
 > 
 > This is a different bug, a new one.

 But still matches the topic :)

 > Can you try the appended patch?

 The patch works. Please commit and close this PR.

 Christoph

State-Changed-From-To: feedback->closed
State-Changed-By: drochner@NetBSD.org
State-Changed-When: Mon, 26 May 2008 19:03:35 +0000
State-Changed-Why:
the patch helps, everyone is happy


From: Matthias Drochner <drochner@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/38528 CVS commit: src/sys/dev/usb
Date: Mon, 26 May 2008 19:01:51 +0000 (UTC)

 Module Name:	src
 Committed By:	drochner
 Date:		Mon May 26 19:01:51 UTC 2008

 Modified Files:
 	src/sys/dev/usb: uhidev.c uhidev.h

 Log Message:
 Fix a dereference if a softc pointer which can legally be zero, leading
 to a crash reported by Christoph Egger in a followup to PR kern/38528.
 For consistency, keep track of the device_t pointer to child devices
 rather than the softc. We really shouldn't mess with child's softc data.


 To generate a diff of this commit:
 cvs rdiff -r1.41 -r1.42 src/sys/dev/usb/uhidev.c
 cvs rdiff -r1.8 -r1.9 src/sys/dev/usb/uhidev.h

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

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.