NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: kern/60104: vnd use-after-free bug



The following reply was made to PR kern/60104; it has been noted by GNATS.

From: Showta Ishizaki <zakinko%snowrabbit.org@localhost>
To: gnats-bugs%NetBSD.org@localhost
Cc: zakinko%snowrabbit.org@localhost, imuwoto%gmail.com@localhost, riastradh%NetBSD.org@localhost, mlelstv%NetBSD.org@localhost
Subject: Re: kern/60104: vnd use-after-free bug
Date: Fri, 27 Nov 2026 00:28:54 +0900 (JST)

 cgd(4) has the same window.  cgdopen() drops cgd_lock before calling
 dk_open(), and cgdclose() can destroy an unconfigured unit in between,
 under the open that still holds its softc.  I came to it by reading,
 after the vnd change, and have not seen it on a stock kernel: the race
 program from my previous mail ran 9999984 opens of /dev/cgd0* on four
 CPUs without a panic.  With a kpause(1) between cgd_unlock() and
 dk_open() to widen the window, a trunk GENERIC kernel panicked within
 30 seconds both times, in mutex_vector_enter() from dk_open(), on the
 dk_openlock of the freed softc.
 
 The diff below, against cgd.c 1.146 (the same on trunk and netbsd-11),
 follows the vnd one.  The open takes a reference with
 device_lookup_acquire() or config_attach_pseudo_acquire(), the close
 leaves the unit in place, and units are destroyed on module unload or
 detach with config_detach_release().  As with vnd, a unit that has
 been opened stays in drvctl -l.  cgd is D_MPSAFE, so it cannot lean on
 the kernel lock as vnd does; two fields in cgdvar.h count the opens in
 dk_open() and mark a dying unit, and cgd_detach() returns EBUSY under
 cgd_lock while either says so.  The clear also drops DKF_VLABEL,
 DKF_KLABEL and DKF_WLABEL, which would otherwise outlive the
 configuration now that the softc does.
 
 With the same kpause and the diff, the program ran to the end, 1999968
 opens.  On a LOCKDEBUG and DIAGNOSTIC kernel with cgd as a module,
 cgdconfig, newfs, mount and twenty configure, verify and unconfigure
 cycles worked, and the race ran alongside 60 modunload/modload cycles
 and 200 drvctl -d, which were refused while the unit was busy and
 otherwise went through, with no panic or lockdebug report.
 
 Separately, the MODULE() line of cgd.c does not list adiantum, so
 cgd.kmod does not load on its own on a kernel without cgd built in.
 
 --- sys/dev/cgd.c
 +++ sys/dev/cgd.c
 @@ -389,12 +389,12 @@ static int cgd_detach(device_t, int);
  static int cgd_match(device_t, cfdata_t, void *);
  static void cgd_attach(device_t, device_t, void *);
  static int cgd_detach(device_t, int);
 -static struct cgd_softc	*cgd_spawn(int);
 +static device_t	cgd_spawn(int);
 +static device_t	cgd_acquire(int);
  static struct cgd_worker *cgd_create_one_worker(void);
  static void cgd_destroy_one_worker(struct cgd_worker *);
  static struct cgd_worker *cgd_create_worker(void);
  static void cgd_destroy_worker(struct cgd_worker *);
 -static int cgd_destroy(device_t);
  
  /* Internal Functions */
  
 @@ -532,9 +532,26 @@ cgd_detach(device_t self, int flags)
  	struct cgd_softc *sc = device_private(self);
  	struct dk_softc *dksc = &sc->sc_dksc;
  
 -	if (DK_BUSY(dksc, 0))
 +	/*
 +	 * An open between dropping the lock and dk_open() holds a
 +	 * reference, which keeps the softc but not what is destroyed
 +	 * below.
 +	 */
 +	(void)cgd_lock(false);
 +	if (DK_BUSY(dksc, 0) || sc->sc_opening != 0) {
 +		cgd_unlock();
  		return EBUSY;
 +	}
 +	sc->sc_dying = true;
 +	cgd_unlock();
  
 +	/*
 +	 * Deleting the wedges waits for their opens, and those may wait
 +	 * in device_lookup_acquire() for this unit until this says the
 +	 * detach will not back out.
 +	 */
 +	config_detach_commit(self);
 +
  	if (DK_ATTACHED(dksc) &&
  	    (ret = cgd_ioctl_clr(sc, curlwp)) != 0)
  		return ret;
 @@ -564,18 +581,20 @@ cgdattach(int num)
  	cgd_selftest();
  }
  
 -static struct cgd_softc *
 +static device_t
  cgd_spawn(int unit)
  {
  	cfdata_t cf;
  	struct cgd_worker *cw;
  	struct cgd_softc *sc;
 +	device_t dv;
  
  	cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
  	cf->cf_name = cgd_cd.cd_name;
  	cf->cf_atname = cgd_cd.cd_name;
  	cf->cf_unit = unit;
 -	cf->cf_fstate = FSTATE_STAR;
 +	/* Not STAR, which would take the next unit if this one is busy. */
 +	cf->cf_fstate = FSTATE_NOTFOUND;
  
  	cw = cgd_create_one_worker();
  	if (cw == NULL) {
 @@ -583,34 +602,43 @@ cgd_spawn(int unit)
  		return NULL;
  	}
  
 -	sc = device_private(config_attach_pseudo(cf));
 -	if (sc == NULL) {
 +	dv = config_attach_pseudo_acquire(cf, NULL);
 +	if (dv == NULL) {
  		cgd_destroy_one_worker(cw);
 +		kmem_free(cf, sizeof(*cf));
  		return NULL;
  	}
  
 +	sc = device_private(dv);
  	sc->sc_worker = cw;
  
 -	return sc;
 +	return dv;
  }
  
 -static int
 -cgd_destroy(device_t dev)
 -{
 -	struct cgd_softc *sc = device_private(dev);
 -	struct cgd_worker *cw = sc->sc_worker;
 -	cfdata_t cf;
 -	int error;
 +/*
 + * Find a unit, or make one.  A unit stays once made: cgdclose() used to
 + * destroy it as soon as it saw it unconfigured, with no regard for an
 + * open that had dropped the lock and was on its way to dk_open().
 + *
 + * The lookup is done without the lock, as cgd_detach() takes it while
 + * a lookup of its unit sleeps.  Should another open make the unit
 + * first, it is there to be found.
 + */
 +static device_t
 +cgd_acquire(int unit)
 +{
 +	device_t dv;
  
 -	cf = device_cfdata(dev);
 -	error = config_detach(dev, DETACH_QUIET);
 -	if (error)
 -		return error;
 -
 -	cgd_destroy_one_worker(cw);
 -
 -	kmem_free(cf, sizeof(*cf));
 -	return 0;
 +	if ((dv = device_lookup_acquire(&cgd_cd, unit)) != NULL)
 +		return dv;
 +	if (cgd_lock(true) != 0)
 +		return NULL;
 +	if (device_lookup(&cgd_cd, unit) == NULL)
 +		dv = cgd_spawn(unit);
 +	cgd_unlock();
 +	if (dv == NULL)
 +		dv = device_lookup_acquire(&cgd_cd, unit);
 +	return dv;
  }
  
  static void
 @@ -713,21 +741,39 @@ cgdopen(dev_t dev, int flags, int fmt, struct lwp *l)
  cgdopen(dev_t dev, int flags, int fmt, struct lwp *l)
  {
  	struct	cgd_softc *sc;
 +	device_t dv;
  	int error;
  
  	DPRINTF_FOLLOW(("cgdopen(0x%"PRIx64", %d)\n", dev, flags));
  
 -	error = cgd_lock(true);
 -	if (error)
 +	if ((dv = cgd_acquire(CGDUNIT(dev))) == NULL)
 +		return ENXIO;
 +	sc = device_private(dv);
 +
 +	if ((error = cgd_lock(true)) != 0) {
 +		device_release(dv);
  		return error;
 -	sc = getcgd_softc(dev);
 -	if (sc == NULL)
 -		sc = cgd_spawn(CGDUNIT(dev));
 -	cgd_unlock();
 -	if (sc == NULL)
 +	}
 +	if (sc->sc_dying) {
 +		cgd_unlock();
 +		device_release(dv);
  		return ENXIO;
 +	}
 +	/*
 +	 * dk_open() may read the label, and must not do that with every
 +	 * other unit waiting for the lock.  This keeps cgd_detach() away.
 +	 */
 +	sc->sc_opening++;
 +	cgd_unlock();
  
 -	return dk_open(&sc->sc_dksc, dev, flags, fmt, l);
 +	error = dk_open(&sc->sc_dksc, dev, flags, fmt, l);
 +
 +	(void)cgd_lock(false);
 +	sc->sc_opening--;
 +	cgd_unlock();
 +	device_release(dv);
 +
 +	return error;
  }
  
  static int
 @@ -748,17 +794,9 @@ cgdclose(dev_t dev, int flags, int fmt, struct lwp *l)
  		goto done;
  	}
  
 +	/* The unit stays, configured or not; see cgd_acquire(). */
  	dksc = &sc->sc_dksc;
 -	if ((error =  dk_close(dksc, dev, flags, fmt, l)) != 0)
 -		goto done;
 -
 -	if (!DK_ATTACHED(dksc)) {
 -		if ((error = cgd_destroy(sc->sc_dksc.sc_dev)) != 0) {
 -			device_printf(dksc->sc_dev,
 -			    "unable to detach instance\n");
 -			goto done;
 -		}
 -	}
 +	error = dk_close(dksc, dev, flags, fmt, l);
  
  done:
  	cgd_unlock();
 @@ -1378,6 +1416,15 @@ cgd_ioctl_clr(struct cgd_softc *sc, struct lwp *l)
  	kmem_free(sc->sc_data, MAXPHYS);
  	sc->sc_data_used = false;
  	dk_detach(dksc);
 +
 +	/*
 +	 * The softc outlives this configuration.  A label kept valid
 +	 * would have the next open look at dk_label, which disk_detach()
 +	 * frees, and the next configuration would start from it.
 +	 */
 +	mutex_enter(&dksc->sc_dkdev.dk_openlock);
 +	dksc->sc_flags &= ~(DKF_VLABEL | DKF_KLABEL | DKF_WLABEL);
 +	mutex_exit(&dksc->sc_dkdev.dk_openlock);
  	disk_detach(&dksc->sc_dkdev);
  
  	return 0;
 @@ -1728,6 +1775,49 @@ CFDRIVER_DECL(cgd, DV_DISK, NULL);
  #ifdef _MODULE
  CFDRIVER_DECL(cgd, DV_DISK, NULL);
  
 +static int
 +cgd_destroy(device_t dev)
 +{
 +	struct cgd_softc *sc = device_private(dev);
 +	struct cgd_worker *cw = sc->sc_worker;
 +	cfdata_t cf;
 +	int error;
 +
 +	cf = device_cfdata(dev);
 +	error = config_detach_release(dev, DETACH_QUIET);
 +	if (error)
 +		return error;
 +
 +	(void)cgd_lock(false);
 +	cgd_destroy_one_worker(cw);
 +	cgd_unlock();
 +
 +	kmem_free(cf, sizeof(*cf));
 +	return 0;
 +}
 +
 +/* Destroy the units left by cgd_acquire(), if none of them is in use. */
 +static int
 +cgd_destroy_idle(void)
 +{
 +	device_t dv;
 +	struct cgd_softc *sc;
 +	int error, unit;
 +
 +	for (unit = 0; unit < cgd_cd.cd_ndevs; unit++) {
 +		if ((dv = device_lookup_acquire(&cgd_cd, unit)) == NULL)
 +			continue;
 +		sc = device_private(dv);
 +		if (DK_ATTACHED(&sc->sc_dksc)) {
 +			device_release(dv);
 +			return EBUSY;
 +		}
 +		if ((error = cgd_destroy(dv)) != 0)
 +			return error;
 +	}
 +	return 0;
 +}
 +
  devmajor_t cgd_bmajor = -1, cgd_cmajor = -1;
  #endif
  
 @@ -1778,6 +1868,13 @@ cgd_modcmd(modcmd_t cmd, void *arg)
  	case MODULE_CMD_FINI:
  #ifdef _MODULE
  		/*
 +		 * A unit made by an open after this keeps the cfattach
 +		 * from being detached below.  The {b,c}devsw has to stay
 +		 * until then: nothing in use may lose it.
 +		 */
 +		if ((error = cgd_destroy_idle()) != 0)
 +			break;
 +		/*
  		 * Remove device from autoconf database
  		 */
  		error = config_cfattach_detach(cgd_cd.cd_name, &cgd_ca);
 --- sys/dev/cgdvar.h
 +++ sys/dev/cgdvar.h
 @@ -116,6 +116,8 @@ struct cgd_softc {
  	kcondvar_t		 sc_cv;
  	bool			 sc_busy;
  	struct cgd_worker	*sc_worker;	/* shared worker data */
 +	u_int			 sc_opening;	/* opens in dk_open() */
 +	bool			 sc_dying;	/* cgd_detach() has begun */
  };
  #endif
  
 



Home | Main Index | Thread Index | Old Index