git: d0754fea495e - main - inpcb: reject a reversed ephemeral port range

From: Nick Price <nprice_at_FreeBSD.org>
Date: Tue, 24 Nov 2026 05:46:52 UTC
The branch main has been updated by nprice:

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

commit d0754fea495e3b83945890613571584ab6031be8
Author:     Nick Price <nprice@FreeBSD.org>
AuthorDate: 2026-11-24 05:44:53 +0000
Commit:     Nick Price <nprice@FreeBSD.org>
CommitDate: 2026-11-24 05:46:27 +0000

    inpcb: reject a reversed ephemeral port range
    
    vxlan, geneve and the RDMA CM take portrange.first and last as an
    ordered pair and divide by zero when first == last + 1.
    
    Refuse a write that would put first above last or hifirst above hilast.
    
    Reviewed by:    glebius
    Differential Revision:  https://reviews.freebsd.org/D61010
    Signed-off-by:  Nick Price <nprice@FreeBSD.org>
---
 sys/netinet/in_pcb.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/sys/netinet/in_pcb.c b/sys/netinet/in_pcb.c
index 9a9ead358dcc..df1cd54b5855 100644
--- a/sys/netinet/in_pcb.c
+++ b/sys/netinet/in_pcb.c
@@ -149,18 +149,26 @@ static struct inpcb	*in_pcblookup_wild_locked(struct inpcbinfo *pcbinfo,
 static int
 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
 {
+	u_int *var, port;
 	int error;
 
-	error = sysctl_handle_int(oidp, arg1, arg2, req);
-	if (error == 0) {
-		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
-		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
-		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
-		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
-		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
-		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
+	var = arg1;
+	port = *var;
+	error = sysctl_handle_int(oidp, &port, 0, req);
+	if (error != 0 || req->newptr == NULL)
+		return (error);
+	if (var == &V_ipport_lowfirstauto || var == &V_ipport_lowlastauto) {
+		RANGECHK(port, 1, IPPORT_RESERVED - 1);
+	} else {
+		RANGECHK(port, IPPORT_RESERVED, IPPORT_MAX);
 	}
-	return (error);
+	if ((var == &V_ipport_firstauto && port > V_ipport_lastauto) ||
+	    (var == &V_ipport_lastauto && port < V_ipport_firstauto) ||
+	    (var == &V_ipport_hifirstauto && port > V_ipport_hilastauto) ||
+	    (var == &V_ipport_hilastauto && port < V_ipport_hifirstauto))
+		return (EINVAL);
+	*var = port;
+	return (0);
 }
 
 #undef RANGECHK