]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ipv4: igmp: Fix potential UAF in igmp_gq_start_timer()
authorEric Dumazet <edumazet@google.com>
Sun, 5 Jul 2026 18:17:54 +0000 (18:17 +0000)
committerPaolo Abeni <pabeni@redhat.com>
Wed, 8 Jul 2026 12:41:01 +0000 (14:41 +0200)
A race condition exists between device teardown (inetdev_destroy) and
incoming IGMP query processing (igmp_rcv), leading to a Use-After-Free
in the IGMP timer callback.

During device destruction, inetdev_destroy() drops the primary reference
to in_device, which can drop its refcount to 0. The actual freeing of
in_device memory is deferred via RCU (using call_rcu()).

Concurrently, igmp_rcv() runs under RCU read lock and obtains the
in_device pointer. Because the memory is RCU-protected, CPU-0 can safely
dereference in_device even if its refcount has hit 0.

However, if CPU-0 calls igmp_gq_start_timer() and re-arms the timer, it
attempts to acquire a reference using in_dev_hold(). This increments the
refcount from 0 to 1, triggering a "refcount_t: addition on 0" warning.
Since the in_device memory is still scheduled to be freed after the RCU
grace period (as the free callback does not check the refcount again),
the device is freed while the timer is still armed. When the timer
expires, it accesses the freed memory, causing a kernel panic.

Fix this by using refcount_inc_not_zero() (via a new helper
in_dev_hold_safe()) to prevent acquiring a reference if the device is
already being destroyed. If the refcount is 0, we do not arm the timer.

A similar issue in IPv6 MLD is fixed in a subsequent patch.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: Zero Day Initiative <zdi-disclosures@trendmicro.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
Link: https://patch.msgid.link/20260705181756.963063-2-edumazet@google.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
include/linux/inetdevice.h
net/ipv4/igmp.c

index dccbeb25f70141982160c776f3cc727296def2b7..6032eea2539a60d0476d85667bda3bfcad8f1425 100644 (file)
@@ -293,6 +293,11 @@ static inline void in_dev_put(struct in_device *idev)
 #define __in_dev_put(idev)  refcount_dec(&(idev)->refcnt)
 #define in_dev_hold(idev)   refcount_inc(&(idev)->refcnt)
 
+static inline bool in_dev_hold_safe(struct in_device *idev)
+{
+       return refcount_inc_not_zero(&idev->refcnt);
+}
+
 #endif /* __KERNEL__ */
 
 static __inline__ __be32 inet_make_mask(int logmask)
index d520ea4f6d142dbeb7df1d9c88f6d8155533bb00..3a1cb2a827f39667e7ccc1b949a0e3f9a4b759ea 100644 (file)
@@ -248,16 +248,20 @@ static void igmp_gq_start_timer(struct in_device *in_dev)
                return;
 
        in_dev->mr_gq_running = 1;
-       if (!mod_timer(&in_dev->mr_gq_timer, exp))
-               in_dev_hold(in_dev);
+       if (in_dev_hold_safe(in_dev)) {
+               if (mod_timer(&in_dev->mr_gq_timer, exp))
+                       in_dev_put(in_dev);
+       }
 }
 
 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
 {
-       int tv = get_random_u32_below(delay);
+       if (in_dev_hold_safe(in_dev)) {
+               int tv = get_random_u32_below(delay);
 
-       if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
-               in_dev_hold(in_dev);
+               if (mod_timer(&in_dev->mr_ifc_timer, jiffies + tv + 2))
+                       in_dev_put(in_dev);
+       }
 }
 
 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)