git: 21acd0c6de7c - main - tcp/rack: range-check pace_max_seg sysctl and socket option

From: Maxim Konovalov <maxim_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 20:53:05 UTC
The branch main has been updated by maxim:

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

commit 21acd0c6de7c96bf220018dd9c420e45d7801ce7
Author:     Michael Adler <madler@tapil.com>
AuthorDate: 2026-11-25 18:56:43 +0000
Commit:     Maxim Konovalov <maxim@FreeBSD.org>
CommitDate: 2026-11-26 20:51:59 +0000

    tcp/rack: range-check pace_max_seg sysctl and socket option
    
    The net.inet.tcp.rack.pacing.pace_max_seg sysctl is a 32-bit integer,
    but rack_init() copies it into each new connection's 7-bit bitfield
    rc_user_set_max_segs. Values that do not fit are truncated, and a value
    of 128 becomes 0. A connection with rc_user_set_max_segs of 0 ends up
    with a pacing max segment size of 0, and rack_setup_fast_output() then
    executes rounddown(left_to_send, 0), causing an integer divide fault:
    
      Fatal trap 18: integer divide fault while in kernel mode
      #5 rack_do_segment_nounlock+0x29eb
      ... (fault in rack_setup_fast_output() inlined into rack_output())
    
    The TCP_RACK_PACE_MAX_SEG socket option clamps values above
    MAX_USER_SET_SEG (63), but it accepts 0, which leads to the same
    division by zero.
    
    Validate the sysctl with a handler that only accepts 1 to
    MAX_USER_SET_SEG, and reject a socket option value of 0 with EINVAL
    before any state is changed. Sysctl values from 64 to 127 used to be
    stored unchanged and are now rejected too.
    
    Reproduced on 15.1-RELEASE with
    "sysctl net.inet.tcp.rack.pacing.pace_max_seg=128" while RACK was the
    default stack: the host panicked on the next connection's data transfer.
    
    Reviewed by:    tuexen
    Signed-off-by:  Michael Adler
    Fixes:          030434acaf46 ("Update rack to the latest code used at NF.")
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D62071
---
 sys/netinet/tcp_stacks/rack.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/sys/netinet/tcp_stacks/rack.c b/sys/netinet/tcp_stacks/rack.c
index 4c000908a0f1..680da541c222 100644
--- a/sys/netinet/tcp_stacks/rack.c
+++ b/sys/netinet/tcp_stacks/rack.c
@@ -858,6 +858,28 @@ sysctl_rack_wma_divisor(SYSCTL_HANDLER_ARGS)
 	return (0);
 }
 
+static int
+sysctl_rack_pace_max_seg(SYSCTL_HANDLER_ARGS)
+{
+	int32_t val;
+	int error;
+
+	val = *(int32_t *)arg1;
+	error = sysctl_handle_int(oidp, &val, 0, req);
+	if (error != 0 || req->newptr == NULL)
+		return (error);
+	/*
+	 * This becomes rc_user_set_max_segs of each new connection, so
+	 * apply the same 1 to MAX_USER_SET_SEG limit as the
+	 * TCP_RACK_PACE_MAX_SEG socket option. A value of 0 leads to a
+	 * division by zero in rack_setup_fast_output().
+	 */
+	if (val < 1 || val > MAX_USER_SET_SEG)
+		return (EINVAL);
+	*(int32_t *)arg1 = val;
+	return (0);
+}
+
 static void
 rack_init_sysctls(void)
 {
@@ -1082,11 +1104,11 @@ rack_init_sysctls(void)
 	    OID_AUTO, "gp_per_rec", CTLFLAG_RW,
 	    &rack_per_of_gp_rec, 200,
 	    "If non zero, what percentage of goodput to pace at in recovery");
-	SYSCTL_ADD_S32(&rack_sysctl_ctx,
+	SYSCTL_ADD_PROC(&rack_sysctl_ctx,
 	    SYSCTL_CHILDREN(rack_pacing),
-	    OID_AUTO, "pace_max_seg", CTLFLAG_RW,
-	    &rack_hptsi_segments, 40,
-	    "What size is the max for TSO segments in pacing and burst mitigation");
+	    OID_AUTO, "pace_max_seg", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
+	    &rack_hptsi_segments, 0, sysctl_rack_pace_max_seg, "I",
+	    "What size is the max for TSO segments in pacing and burst mitigation (1-63)");
 	SYSCTL_ADD_S32(&rack_sysctl_ctx,
 	    SYSCTL_CHILDREN(rack_pacing),
 	    OID_AUTO, "burst_reduces", CTLFLAG_RW,
@@ -23244,6 +23266,15 @@ rack_process_option(struct tcpcb *tp, struct tcp_rack *rack, int sopt_name,
 	case TCP_RACK_PACE_MAX_SEG:
 		/* Max segments size in a pace in bytes */
 		RACK_OPTS_INC(tcp_rack_max_seg);
+		if (optval == 0) {
+			/*
+			 * Valid values are 1 to MAX_USER_SET_SEG (larger
+			 * values are clamped below). A value of 0 leads to
+			 * a division by zero in rack_setup_fast_output().
+			 */
+			error = EINVAL;
+			break;
+		}
 		if ((rack->dgp_on == 1) &&
 		    (rack->r_ctl.pacing_method & RACK_DGP_PACING)) {
 			/*