git: b9296d4d85bb - stable/14 - g_dev_orphan(): Return early if the device is already gone

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 14 Mar 2025 17:52:27 UTC
The branch stable/14 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=b9296d4d85bbc4f84c21615e760c5e12d1c81406

commit b9296d4d85bbc4f84c21615e760c5e12d1c81406
Author:     Fabian Keil <fk@fabiankeil.de>
AuthorDate: 2025-03-13 23:13:46 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2025-03-14 17:51:03 +0000

    g_dev_orphan(): Return early if the device is already gone
    
    The following panic was the result of running "cdcontrol eject" after
    using the physical ejection key on the device before the tray was
    actually ejected. So we have hardware racing software.
    
    The device was loaded with a DVD.
    
    Resulted in a NULL pointer dereference
    
    g_dev_orphan() at g_dev_orphan+0x2e/frame 0xfffffe01eba0a9f0
    g_resize_provider_event() at g_resize_provider_event+0x71/frame 0xfffffe01eba0aa20
    g_run_events() at g_run_events+0x20e/frame 0xfffffe01eba0aa70
    fork_exit() at fork_exit+0x85/frame 0xfffffe01eba0aab0
    fork_trampoline() at fork_trampoline+0xe/frame 0xfffffe01eba0aab0
    
    Avoid this possibility and return early of dev is NULL already.
    
    PR:  215856
    Reviewed by: imp (I've triggered this once or twice over the years too)
    Sponsored by: Netflix
    
    (cherry picked from commit f7856fe81df2df3c4355e674d34a4c15a095a33c)
---
 sys/geom/geom_dev.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/sys/geom/geom_dev.c b/sys/geom/geom_dev.c
index 0b5cfdf77149..8d71cb584f80 100644
--- a/sys/geom/geom_dev.c
+++ b/sys/geom/geom_dev.c
@@ -854,6 +854,9 @@ g_dev_orphan(struct g_consumer *cp)
 	dev = sc->sc_dev;
 	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
 
+	if (dev == NULL)
+		return;
+
 	/* Reset any dump-area set on this device */
 	if (dev->si_flags & SI_DUMPDEV) {
 		struct diocskerneldump_arg kda;