From 600c54135b1015d56070f702d878772dd9f0d51e Mon Sep 17 00:00:00 2001
From: Bart Van Assche <bvanassche@acm.org>
Date: Mon, 2 Aug 2021 19:04:31 -0700
Subject: [PATCH] IF-MIB, IP-FORWARD-MIB: Improve robustness

It can happen that a network interface disappears after scanning has started
and before netsnmp_arch_interface_index_find() or another query function is
called. If that happens, ignore the network interface silently.
---
 agent/mibgroup/if-mib/data_access/interface_linux.c   |  7 ++++++-
 .../mibgroup/ip-forward-mib/data_access/route_linux.c | 11 ++++++-----
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/agent/mibgroup/if-mib/data_access/interface_linux.c b/agent/mibgroup/if-mib/data_access/interface_linux.c
index 215b30e80..7e4a35130 100644
--- a/agent/mibgroup/if-mib/data_access/interface_linux.c
+++ b/agent/mibgroup/if-mib/data_access/interface_linux.c
@@ -734,7 +734,11 @@ netsnmp_arch_interface_container_load(netsnmp_container* container,
          * knows a better way, put it here!
          */
         if_index = netsnmp_arch_interface_index_find(ifstart);
-        netsnmp_assert(if_index != 0);
+        if (if_index == 0) {
+            DEBUGMSGTL(("access:interface", "network interface %s is gone",
+                        ifstart));
+            goto free_entry;
+        }
 #ifdef NETSNMP_ENABLE_IPV6
         _arch_interface_has_ipv6(if_index, &flags, addr_container);
 #endif
@@ -928,6 +932,7 @@ netsnmp_arch_interface_container_load(netsnmp_container* container,
                              "Encountered interface with index %" NETSNMP_PRIz "u twice: %s <> %s",
                              entry->index, existing ? existing->name : "(?)",
                              entry->name));
+free_entry:
             netsnmp_access_interface_entry_free(entry);
         }
     }
diff --git a/agent/mibgroup/ip-forward-mib/data_access/route_linux.c b/agent/mibgroup/ip-forward-mib/data_access/route_linux.c
index 956e127c9..9b6a63ead 100644
--- a/agent/mibgroup/ip-forward-mib/data_access/route_linux.c
+++ b/agent/mibgroup/ip-forward-mib/data_access/route_linux.c
@@ -97,9 +97,7 @@ _load_ipv4(netsnmp_container* container, u_long *index )
             snmp_log(LOG_ERR,
                      "/proc/net/route data format error (%d!=8), line ==|%s|",
                      rc, line);
-            
-            netsnmp_access_route_entry_free(entry);        
-            continue;
+            goto free_entry;
         }
 
         /*
@@ -114,9 +112,12 @@ _load_ipv4(netsnmp_container* container, u_long *index )
          * but since that will open/close a socket, and we might
          * have a lot of routes, call the ioctl routine directly.
          */
-        if ('*' != name[0])
+        if ('*' != name[0]) {
             entry->if_index =
                 netsnmp_access_interface_ioctl_ifindex_get(fd,name);
+            if (entry->if_index == 0)
+                goto free_entry;
+        }
 
         /*
          * arbitrary index
@@ -186,8 +187,8 @@ _load_ipv4(netsnmp_container* container, u_long *index )
         if (CONTAINER_INSERT(container, entry) < 0)
         {
             DEBUGMSGTL(("access:route:container", "error with route_entry: insert into container failed.\n"));
+free_entry:
             netsnmp_access_route_entry_free(entry);
-            continue;
         }
     }
 
-- 
2.33.0

