From 9f98b3ea746f97b61d37f1c11d0e7abb3cd81305 Mon Sep 17 00:00:00 2001 From: Hans Petter Selasky Date: Tue, 12 Jan 2021 18:51:09 +0100 Subject: Fix for use-after-free by if_ioctl() calls from user-space in USB drivers by detaching the ifnet before the miibus. PR: 252608 Suggested by: jhb@ Sponsored by: Mellanox Technologies // NVIDIA Networking (cherry picked from commit 4eac63af23ddafc2b1dfb2aad2896f4513c37cdd) --- sys/dev/usb/net/uhso.c | 2 +- sys/dev/usb/net/usb_ethernet.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/sys/dev/usb/net/uhso.c b/sys/dev/usb/net/uhso.c index c72fa8d7a36b..4ff5fcc075d3 100644 --- a/sys/dev/usb/net/uhso.c +++ b/sys/dev/usb/net/uhso.c @@ -691,10 +691,10 @@ uhso_detach(device_t self) free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit); mtx_lock(&sc->sc_mtx); uhso_if_stop(sc); + mtx_unlock(&sc->sc_mtx); bpfdetach(sc->sc_ifp); if_detach(sc->sc_ifp); if_free(sc->sc_ifp); - mtx_unlock(&sc->sc_mtx); usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX); } diff --git a/sys/dev/usb/net/usb_ethernet.c b/sys/dev/usb/net/usb_ethernet.c index e1eb2c247b5a..33659049f970 100644 --- a/sys/dev/usb/net/usb_ethernet.c +++ b/sys/dev/usb/net/usb_ethernet.c @@ -319,6 +319,12 @@ uether_ifdetach(struct usb_ether *ue) /* drain any callouts */ usb_callout_drain(&ue->ue_watchdog); + /* + * Detach ethernet first to stop miibus calls from + * user-space: + */ + ether_ifdetach(ifp); + /* detach miibus */ if (ue->ue_miibus != NULL) { mtx_lock(&Giant); /* device_xxx() depends on this */ @@ -326,9 +332,6 @@ uether_ifdetach(struct usb_ether *ue) mtx_unlock(&Giant); } - /* detach ethernet */ - ether_ifdetach(ifp); - /* free interface instance */ if_free(ifp); -- cgit v1.2.3