git: 877fa4f2a2d3 - stable/15 - ifnet: Add some sanity checks
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 30 Apr 2026 09:53:20 UTC
The branch stable/15 has been updated by zlei:
URL: https://cgit.FreeBSD.org/src/commit/?id=877fa4f2a2d37dcd9e828d12dc9736c5e83ffd8c
commit 877fa4f2a2d37dcd9e828d12dc9736c5e83ffd8c
Author: Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2026-04-07 04:33:05 +0000
Commit: Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2026-04-30 09:51:59 +0000
ifnet: Add some sanity checks
To be more robust since the checking is now performed where the
interface is referenced.
While here, remove a redundant check from if_vmove_loan().
Reviewed by: kp, glebius, pouria
MFC after: 2 weeks
Differential Revision: https://reviews.freebsd.org/D55875
(cherry picked from commit 00d96da231d007673a1672452748d8ea4f6788ae)
---
sys/net/if.c | 29 +++++++++++++++++++----------
1 file changed, 19 insertions(+), 10 deletions(-)
diff --git a/sys/net/if.c b/sys/net/if.c
index aeb264f2ac3f..75ef79738010 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -438,8 +438,12 @@ VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
static void
if_link_ifnet(struct ifnet *ifp)
{
-
IFNET_WLOCK();
+
+ MPASS(refcount_load(&ifp->if_refcount) > 0);
+ MPASS(ifp->if_vnet == curvnet);
+ MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
+
CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
#ifdef VIMAGE
curvnet->vnet_ifcnt++;
@@ -456,6 +460,10 @@ if_unlink_ifnet(struct ifnet *ifp, bool vmove)
IFNET_WLOCK();
CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
if (iter == ifp) {
+ MPASS(refcount_load(&ifp->if_refcount) > 0);
+ MPASS(ifp->if_vnet == curvnet);
+ MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
+
CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
#ifdef VIMAGE
curvnet->vnet_ifcnt--;
@@ -1298,8 +1306,6 @@ if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
struct ifnet *difp;
bool found;
- MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
-
/* Try to find the prison within our visibility. */
sx_slock(&allprison_lock);
pr = prison_find_child(td->td_ucred->cr_prison, jid);
@@ -2222,14 +2228,13 @@ ifunit_ref(const char *name)
NET_EPOCH_ENTER(et);
CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
- !(ifp->if_flags & IFF_DYING))
+ !(ifp->if_flags & IFF_DYING)) {
+ MPASS(ifp->if_vnet == curvnet);
+ MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
+ if_ref(ifp);
break;
+ }
}
- if (ifp != NULL) {
- if_ref(ifp);
- MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
- }
-
NET_EPOCH_EXIT(et);
return (ifp);
}
@@ -2242,8 +2247,12 @@ ifunit(const char *name)
NET_EPOCH_ENTER(et);
CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
- if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
+ if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) {
+ MPASS(refcount_load(&ifp->if_refcount) > 0);
+ MPASS(ifp->if_vnet == curvnet);
+ MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
break;
+ }
}
NET_EPOCH_EXIT(et);
return (ifp);