The branch main has been updated by markj:

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

commit ac1750dd143e53225c518bc1ca0462c3fffe10f6
Author:     Mark Johnston <[email protected]>
AuthorDate: 2022-11-02 17:03:41 +0000
Commit:     Mark Johnston <[email protected]>
CommitDate: 2022-11-02 17:46:24 +0000

    inpcb: Remove NULL checks of credential references
    
    Some auditing of the code shows that "cred" is never non-NULL in these
    functions, either because all callers pass a non-NULL reference or
    because they unconditionally dereference "cred".  So, let's simplify the
    code a bit and remove NULL checks.  No functional change intended.
    
    Reviewed by:    glebius
    MFC after:      1 week
    Sponsored by:   Modirum MDPay
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D37025
---
 sys/netinet/in_pcb.c   | 27 ++++++++++++---------------
 sys/netinet6/in6_pcb.c | 10 ++++------
 2 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/sys/netinet/in_pcb.c b/sys/netinet/in_pcb.c
index 95db8f22f1fe..39d147464cfa 100644
--- a/sys/netinet/in_pcb.c
+++ b/sys/netinet/in_pcb.c
@@ -1130,11 +1130,12 @@ in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, 
struct in_addr *laddr,
 
        NET_EPOCH_ASSERT();
        KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
+
        /*
         * Bypass source address selection and use the primary jail IP
         * if requested.
         */
-       if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
+       if (!prison_saddrsel_ip4(cred, laddr))
                return (0);
 
        error = 0;
@@ -1179,7 +1180,7 @@ in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, 
struct in_addr *laddr,
                        goto done;
                }
 
-               if (cred == NULL || !prison_flag(cred, PR_IP4)) {
+               if (!prison_flag(cred, PR_IP4)) {
                        laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
                        goto done;
                }
@@ -1220,7 +1221,7 @@ in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, 
struct in_addr *laddr,
                struct ifnet *ifp;
 
                /* If not jailed, use the default returned. */
-               if (cred == NULL || !prison_flag(cred, PR_IP4)) {
+               if (!prison_flag(cred, PR_IP4)) {
                        ia = (struct in_ifaddr *)nh->nh_ifa;
                        laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
                        goto done;
@@ -1281,7 +1282,7 @@ in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, 
struct in_addr *laddr,
                if (ia == NULL)
                        ia = ifatoia(ifa_ifwithaddr(sintosa(&dst)));
 
-               if (cred == NULL || !prison_flag(cred, PR_IP4)) {
+               if (!prison_flag(cred, PR_IP4)) {
                        if (ia == NULL) {
                                error = ENETUNREACH;
                                goto done;
@@ -1392,8 +1393,7 @@ in_pcbconnect_setup(struct inpcb *inp, struct sockaddr 
*nam,
                if (faddr.s_addr == INADDR_ANY) {
                        faddr =
                            IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
-                       if (cred != NULL &&
-                           (error = prison_get_ip4(cred, &faddr)) != 0)
+                       if ((error = prison_get_ip4(cred, &faddr)) != 0)
                                return (error);
                } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
                        if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags 
&
@@ -1418,10 +1418,9 @@ in_pcbconnect_setup(struct inpcb *inp, struct sockaddr 
*nam,
                        if (imo->imo_multicast_ifp != NULL) {
                                ifp = imo->imo_multicast_ifp;
                                CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, 
ia_link) {
-                                       if ((ia->ia_ifp == ifp) &&
-                                           (cred == NULL ||
+                                       if (ia->ia_ifp == ifp &&
                                            prison_check_ip4(cred,
-                                           &ia->ia_addr.sin_addr) == 0))
+                                           &ia->ia_addr.sin_addr) == 0)
                                                break;
                                }
                                if (ia == NULL)
@@ -2067,9 +2066,8 @@ in_pcblookup_local(struct inpcbinfo *pcbinfo, struct 
in_addr laddr,
                                /*
                                 * Found?
                                 */
-                               if (cred == NULL ||
-                                   prison_equal_ip4(cred->cr_prison,
-                                       inp->inp_cred->cr_prison))
+                               if (prison_equal_ip4(cred->cr_prison,
+                                   inp->inp_cred->cr_prison))
                                        return (inp);
                        }
                }
@@ -2100,9 +2098,8 @@ in_pcblookup_local(struct inpcbinfo *pcbinfo, struct 
in_addr laddr,
                         */
                        CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
                                wildcard = 0;
-                               if (cred != NULL &&
-                                   !prison_equal_ip4(inp->inp_cred->cr_prison,
-                                       cred->cr_prison))
+                               if (!prison_equal_ip4(inp->inp_cred->cr_prison,
+                                   cred->cr_prison))
                                        continue;
 #ifdef INET6
                                /* XXX inp locking */
diff --git a/sys/netinet6/in6_pcb.c b/sys/netinet6/in6_pcb.c
index 176a5227c9a1..3a3c4043f749 100644
--- a/sys/netinet6/in6_pcb.c
+++ b/sys/netinet6/in6_pcb.c
@@ -749,9 +749,8 @@ in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct 
in6_addr *laddr,
                            IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
                            inp->inp_lport == lport) {
                                /* Found. */
-                               if (cred == NULL ||
-                                   prison_equal_ip6(cred->cr_prison,
-                                       inp->inp_cred->cr_prison))
+                               if (prison_equal_ip6(cred->cr_prison,
+                                   inp->inp_cred->cr_prison))
                                        return (inp);
                        }
                }
@@ -782,9 +781,8 @@ in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct 
in6_addr *laddr,
                         */
                        CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
                                wildcard = 0;
-                               if (cred != NULL &&
-                                   !prison_equal_ip6(cred->cr_prison,
-                                       inp->inp_cred->cr_prison))
+                               if (!prison_equal_ip6(cred->cr_prison,
+                                   inp->inp_cred->cr_prison))
                                        continue;
                                /* XXX inp locking */
                                if ((inp->inp_vflag & INP_IPV6) == 0)

Reply via email to