The branch stable/13 has been updated by tuexen:

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

commit 331db93815afb49b01f269aeff0fe899acd47455
Author:     Michael Tuexen <tue...@freebsd.org>
AuthorDate: 2024-11-03 09:20:08 +0000
Commit:     Michael Tuexen <tue...@freebsd.org>
CommitDate: 2024-11-06 12:33:58 +0000

    sctp: improve handling of address changes
    
    Identify interfaces consistenly by the pair of the ifn pointer
    and the index.
    This avoids a use after free when the ifn and or index was reused.
    
    Reported by:    bz, pho, and others
    
    (cherry picked from commit 523913c94371ab50a8129cbab820394d25f7a269)
---
 sys/netinet/sctp_bsd_addr.c |  1 +
 sys/netinet/sctp_pcb.c      | 23 +++++++++++++----------
 sys/netinet/sctp_pcb.h      |  2 +-
 3 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/sys/netinet/sctp_bsd_addr.c b/sys/netinet/sctp_bsd_addr.c
index d190be1948ba..5381898ab0ef 100644
--- a/sys/netinet/sctp_bsd_addr.c
+++ b/sys/netinet/sctp_bsd_addr.c
@@ -338,6 +338,7 @@ sctp_addr_change(struct ifaddr *ifa, int cmd)
                    (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
        } else {
                sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
+                   (void *)ifa->ifa_ifp,
                    ifa->ifa_ifp->if_index);
 
                /*
diff --git a/sys/netinet/sctp_pcb.c b/sys/netinet/sctp_pcb.c
index 469c401978ac..0ad898c6df69 100644
--- a/sys/netinet/sctp_pcb.c
+++ b/sys/netinet/sctp_pcb.c
@@ -193,12 +193,11 @@ sctp_find_ifn(void *ifn, uint32_t ifn_index)
        struct sctp_ifnlist *hash_ifn_head;
 
        SCTP_IPI_ADDR_LOCK_ASSERT();
+       KASSERT(ifn != NULL, ("sctp_find_ifn(NULL, %u) called", ifn_index));
        hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & 
SCTP_BASE_INFO(vrf_ifn_hashmark))];
        LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
-               if (sctp_ifnp->ifn_index == ifn_index) {
-                       break;
-               }
-               if (ifn != NULL && sctp_ifnp->ifn_p == ifn) {
+               if (sctp_ifnp->ifn_index == ifn_index &&
+                   sctp_ifnp->ifn_p == ifn) {
                        break;
                }
        }
@@ -439,7 +438,8 @@ sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t 
ifn_index,
        if (sctp_ifap != NULL) {
                /* The address being added is already or still known. */
                if (sctp_ifap->ifn_p != NULL) {
-                       if (sctp_ifap->ifn_p->ifn_index == ifn_index) {
+                       if (sctp_ifap->ifn_p->ifn_index == ifn_index &&
+                           sctp_ifap->ifn_p->ifn_p == ifn) {
                                SCTPDBG(SCTP_DEBUG_PCB4,
                                    "Using existing ifn %s (0x%x) for ifa %p\n",
                                    sctp_ifap->ifn_p->ifn_name, ifn_index,
@@ -578,7 +578,7 @@ sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t 
ifn_index,
                         */
                        SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
                        /* Opps, must decrement the count */
-                       sctp_del_addr_from_vrf(vrf_id, addr, ifn_index);
+                       sctp_del_addr_from_vrf(vrf_id, addr, ifn, ifn_index);
                        return (NULL);
                }
                SCTP_INCR_LADDR_COUNT();
@@ -603,7 +603,7 @@ sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t 
ifn_index,
 
 void
 sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
-    uint32_t ifn_index)
+    void *ifn, uint32_t ifn_index)
 {
        struct sctp_vrf *vrf;
        struct sctp_ifa *sctp_ifap;
@@ -624,9 +624,12 @@ sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr 
*addr,
        if (sctp_ifap != NULL) {
                /* Validate the delete */
                if (sctp_ifap->ifn_p) {
-                       if (ifn_index != sctp_ifap->ifn_p->ifn_index) {
-                               SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - 
ignoring delete\n",
-                                   sctp_ifap->ifn_p->ifn_index, 
sctp_ifap->ifn_p->ifn_name);
+                       if (ifn_index != sctp_ifap->ifn_p->ifn_index ||
+                           ifn != sctp_ifap->ifn_p->ifn_p) {
+                               SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d (p) ifname:%s 
- ignoring delete\n",
+                                   sctp_ifap->ifn_p->ifn_index,
+                                   sctp_ifap->ifn_p->ifn_p,
+                                   sctp_ifap->ifn_p->ifn_name);
                                SCTP_IPI_ADDR_WUNLOCK();
                                return;
                        }
diff --git a/sys/netinet/sctp_pcb.h b/sys/netinet/sctp_pcb.h
index 7ef6feee9c01..ce38e7cede2d 100644
--- a/sys/netinet/sctp_pcb.h
+++ b/sys/netinet/sctp_pcb.h
@@ -497,7 +497,7 @@ void sctp_free_ifa(struct sctp_ifa *sctp_ifap);
 
 void
 sctp_del_addr_from_vrf(uint32_t vrfid, struct sockaddr *addr,
-    uint32_t ifn_index);
+    void *ifn, uint32_t ifn_index);
 
 struct sctp_nets *sctp_findnet(struct sctp_tcb *, struct sockaddr *);
 

Reply via email to