git: b1dc0bc2cc86 - stable/14 - ifnet: Add some sanity checks
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 04 May 2026 16:51:14 UTC
The branch stable/14 has been updated by zlei:
URL: https://cgit.FreeBSD.org/src/commit/?id=b1dc0bc2cc8658992bed0c0a3148f6a9c67eef8b
commit b1dc0bc2cc8658992bed0c0a3148f6a9c67eef8b
Author: Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2026-04-07 04:33:05 +0000
Commit: Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2026-05-04 16:49:40 +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)
(cherry picked from commit 877fa4f2a2d37dcd9e828d12dc9736c5e83ffd8c)
---
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 55c81b7c931b..1d6b2f7b9461 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -440,8 +440,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++;
@@ -458,6 +462,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--;
@@ -1302,8 +1310,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);