git: e58464800b48 - main - inpcb: treat the ephemeral port range as inclusive

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

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

commit e58464800b486e46615719d6e83ca61a0bc7dc97
Author:     Nick Price <nprice@FreeBSD.org>
AuthorDate: 2026-11-24 05:41:45 +0000
Commit:     Nick Price <nprice@FreeBSD.org>
CommitDate: 2026-11-24 05:44:38 +0000

    inpcb: treat the ephemeral port range as inclusive
    
    in_pcb_lport_dest() sizes [first, last] as last - first, so the random
    start never picks last and the search tries one port too few.
    
    With first == last the random start divides by zero: once root sets the
    range, any user's connect() panics the host.
    
    Reviewed by:    glebius
    Differential Revision:  https://reviews.freebsd.org/D61010
    Signed-off-by:  Nick Price <nprice@FreeBSD.org>
---
 sys/netinet/in_pcb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sys/netinet/in_pcb.c b/sys/netinet/in_pcb.c
index 5d2ea6ea3f4d..9a9ead358dcc 100644
--- a/sys/netinet/in_pcb.c
+++ b/sys/netinet/in_pcb.c
@@ -801,7 +801,7 @@ in_pcb_lport_dest(struct inpcbinfo_ctx *ipictx, struct inpcb *inp,
 #endif
 
 	if (V_ipport_randomized) {
-		port = first + (arc4random() % (last - first));
+		port = first + (arc4random() % (last - first + 1));
 	} else {
 		port = atomic_fetchadd_int(lastport, 1);
 		if (__predict_false(port < first || port > last)) {
@@ -810,7 +810,7 @@ in_pcb_lport_dest(struct inpcbinfo_ctx *ipictx, struct inpcb *inp,
 		}
 	}
 
-	for (count = last - first; count > 0; count--) {
+	for (count = last - first + 1; count > 0; count--) {
 		struct inpcbinfo_ctx tmpctx = {
 			.pcbinfo = ipictx->pcbinfo
 		};