From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796147201; b=Q2WYMW8n/2bAQ6yd1i4ghleDOcwqeWNkDmpEywYJOpJUpcQbTAoVpAViOGY02s6wNYkD BjZjIAGYVwSFLy/vI177A/adGSs5e9ko/bK+z14UoDASU1erFeg5okaMXGrgqI36sTEGd 2E1n44WYyC7oMBPTBIN9lepz6Ex7+Rf1DUJ4SKnU/ZDbN6UJ3HXDaQPZFqd1alfSJ0jy2 DPFOd76rz5tj7ZZIlqQGnRgf8UAbLD2xP6QYJnfYJzOB7/rFZP1OSlYJCaeUWQQHcJLLz S+oBmjv5dcXdr3cbhYCcdKgh2FV2a9C5btPUziRbi/QTemmFI1i2XtfaJZSBnjWy7bg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796147201; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=isiABbeCiFStd7zfxykBOdMJAViGeLMuwPwc2oA1hlg=; b=CLlrc2RHUrsYBOKvL/04a0oEik3ThXchlvxVl8GQY37cL3rzxqB4+dBgg/8iMY9MjPwm FROWHa9x3/mzHzVN+lkvjEbKE8+7q+UMtngddEIjr6vWY7ctgI9FwO7F4HG7T1Sjwfx0J y5+SA4y2oLudQoNOp0eJ8QHYR9d9I7/OeDcuP30cSwlxehl/H2ltvW+bl3K7coG5jrwSJ IZH8sVOEckkWXH6x68dQzW5fiyH7HQIfhcuHOH0xC59emSME1m7hUC2Jx1EeAlOcSzh25 OHCPM6zU8HgYrECEHY7sYZlEdlGg8bhbOdzTA8j+EOSrCi1Sx3C+2s1BtE3wACo/v5A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=JXZXaBuG; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FFE2606A332 for ; Tue, 1 Dec 2026 17:46:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7FFE2606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=JXZXaBuG Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-386a0f79047so13355186eec.1 for ; Tue, 01 Dec 2026 09:46:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796147201; x=1796752001; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=isiABbeCiFStd7zfxykBOdMJAViGeLMuwPwc2oA1hlg=; b=JXZXaBuGD0wZg/dCw2xOxvPx8EhN13axZQxovuMfNnYpYytR5lnCZYDEBZj1dkVz5c hZzwTg2/U0S6nv7vsMoJWcUQR6+WHzwbXqbfzdyDZ494HOK1KAwpXX85BG26E1HiHKTn KfrhM1Fqv48Ga7A/M6QEOyAlFEtOZyhRrmoflGMFuW3B7ul9GCyaZ+a8qBTgdyHrYjPe 7o4R09dp6vkslHd2bm27HMC7RvflqwERVzBU10H+GXgnKuiOIwAv+3JsYPIKGv9pbP/P 8KqX65fyc5Gww+i4AwP2WrZx7CQc88++AavxfvifZ7k0SSLdk58z5h5mPiTLW0PRIIc7 y3EA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796147201; x=1796752001; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=isiABbeCiFStd7zfxykBOdMJAViGeLMuwPwc2oA1hlg=; b=DVQqHoYkumyuD6xvtF3C0RUyr7xLGFRNBv7RV7SZcNEanZ0/4q5snXfkIONwddefZx HxcdLNqHPzG78KRYbUZE+L4aVMAwwrrVZhCSK6wtDCus906wKJb6QpYDaa3UFTHEz0Ex 5PHWFdKU1D8hGp90gHHUd2JcyvoEzscH+OpZKz3/pkpHlaLfP9vmuG0CmrLwcUDDonvU XNTg3NtIwhbtvJZowmWwBqIUSKMn32VzTdbf22DfEm7dCKdeYVbRni6kDsF/KBHC8FGc Too+2wksp/Y+GWUYnkAYK4YgYHcoR8copfIEV+8ZMGSAAXsfPZw0DVSV3D6UOMozkeJh N2Ww== X-Forwarded-Encrypted: i=1; AL+hErHrt+D3qHB8tSmVGNMzJgzHtlO5Uz0GaEz7pW2o1H2XNNPKtRuQ3RQjxKexBoGeFPIzpX9mCdU=@vger.kernel.org X-Gm-Message-State: APuU4Z+kBCyHvgdkBtIz8pMcNb/jH/M546FoILEs2S3g3XeeK99b0wC/ XE1n+5JLOukMSbo2xEU9FxebiJClwCdWt4RE8RIZ8bHV8eshyvjfia0lZSYKPXQu54B/HAlmfK/ UrDFJs4q82w== X-Received: from dycc21.prod.google.com ([2002:a05:693c:60d5:b0:36d:8068:9180]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:bf11:b0:368:7c2d:a612 with SMTP id 5a478bee46e88-3856fe59b16mr36601485eec.10.1796147200114; Tue, 01 Dec 2026 09:46:40 -0800 (PST) Date: Tue, 1 Dec 2026 09:44:40 -0800 X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201174440.1437470-1-irogers@google.com> Subject: [PATCH net v2] sctp: Fix uninitialized new_value in custom sysctl handlers From: Ian Rogers To: Marcelo Ricardo Leitner , Xin Long , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Willem de Bruijn , Simon Horman , linux-sctp@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Elkin Cruz , Ian Rogers Content-Type: text/plain; charset="UTF-8" proc_sctp_do_rto_min(), proc_sctp_do_rto_max(), proc_sctp_do_auth(), proc_sctp_do_udp_port(), and proc_sctp_do_probe_interval() point tbl.data at an uninitialized stack variable new_value before calling proc_dointvec() on writes. proc_dointvec() can return 0 without writing to tbl.data, for example on a zero-length write (*lenp == 0) or a write at a non-zero file offset ignored by proc_first_pos_non_zero_ignore(). When that happens, proc_sctp_do_auth() stores uninitialized stack bytes into net->sctp.auth_enable and sctp_sk(sk)->ep->auth_enable (leaking them on the next read), while the remaining handlers validate and apply uninitialized stack bytes if they happen to fall in range. Address all five handlers without introducing data races or stale writebacks on ignored writes: - Replace proc_sctp_do_rto_min() and proc_sctp_do_rto_max() with proc_dointvec_minmax(), which validates against extra1/extra2 and leaves the target unchanged on ignored writes. - In proc_sctp_do_auth() and proc_sctp_do_probe_interval(), return early on reads (!write), initialize old_value and new_value via READ_ONCE(), and only update the sysctl with WRITE_ONCE() when new_value != old_value. - In proc_sctp_do_udp_port(), return early on reads (!write) and hold sctp_sysctl_mutex across new_value = net->sctp.udp_port and proc_dointvec(), writing back with WRITE_ONCE() only when new_value changed. Converting other readers of these net->sctp fields outside sysctl.c to READ_ONCE() is left for a separate change. Discovered via Clang's -Wconditional-uninitialized on an x86_64 allyesconfig build; Gemini assisted with triaging the warning, drafting the fix and commit message, and writing the QEMU test scripts. Without this patch on an unmodified kernel booted in QEMU (built with CONFIG_INIT_STACK_NONE=y, no kernel modifications or error injection), a zero-length write: fd = os.open("/proc/sys/net/sctp/auth_enable", os.O_WRONLY) os.write(fd, b"") corrupted auth_enable from 0 to -29894, and the same zero-length write or os.pwrite(fd, b"1\n", 1) returned -EINVAL on rto_min, rto_max, udp_port, and plpmtud_probe_interval. Build-tested with LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized. Runtime-tested in QEMU with CONFIG_INIT_STACK_NONE=y, CONFIG_KCSAN_STRICT=y, and CONFIG_PROVE_LOCKING=y: all five sysctls preserve their values across zero-length and non-zero-offset writes, reject out-of-range and negative inputs, and pass concurrent read/write/ignored-write stress with no KCSAN or lockdep reports in SCTP. Also ran tools/testing/selftests/net/sctp_vrf.sh (all 24 IPv4 and IPv6 tests passed). Fixes: 4f3fdf3bc59c ("sctp: add check rto_min and rto_max in sysctl") Fixes: b14878ccb7fa ("net: sctp: cache auth_enable per endpoint") Fixes: 046c052b475e ("sctp: enable udp tunneling socks") Fixes: d1e462a7a5f3 ("sctp: add probe_interval in sysctl and sock/asoc/transport") Signed-off-by: Ian Rogers --- Notes: v2: - Replace proc_sctp_do_rto_min() and proc_sctp_do_rto_max() with proc_dointvec_minmax() (Xin Long). - Return early on reads (!write), use READ_ONCE()/WRITE_ONCE() with new_value != old_value gating in proc_sctp_do_auth() and proc_sctp_do_probe_interval(), and hold sctp_sysctl_mutex across new_value initialization and proc_dointvec() in proc_sctp_do_udp_port() (Willem de Bruijn, Xin Long). - Reproduced the unpatched stack leak/-EINVAL in QEMU with CONFIG_INIT_STACK_NONE=y and verified the fix under CONFIG_KCSAN_STRICT=y, CONFIG_PROVE_LOCKING=y, and tools/testing/selftests/net/sctp_vrf.sh. v1: https://lore.kernel.org/r/20261119170342.1508789-1-irogers@google.com net/sctp/sysctl.c | 150 +++++++++++++++------------------------------- 1 file changed, 48 insertions(+), 102 deletions(-) diff --git a/net/sctp/sysctl.c b/net/sctp/sysctl.c index 2b94c211427d..51ab1df526d9 100644 --- a/net/sctp/sysctl.c +++ b/net/sctp/sysctl.c @@ -45,10 +45,6 @@ static unsigned long max_autoclose_max = static int proc_sctp_do_hmac_alg(const struct ctl_table *ctl, int write, void *buffer, size_t *lenp, loff_t *ppos); -static int proc_sctp_do_rto_min(const struct ctl_table *ctl, int write, - void *buffer, size_t *lenp, loff_t *ppos); -static int proc_sctp_do_rto_max(const struct ctl_table *ctl, int write, void *buffer, - size_t *lenp, loff_t *ppos); static int proc_sctp_do_udp_port(const struct ctl_table *ctl, int write, void *buffer, size_t *lenp, loff_t *ppos); static int proc_sctp_do_alpha_beta(const struct ctl_table *ctl, int write, @@ -98,7 +94,7 @@ static const struct ctl_table sctp_net_table[] = { .data = &init_net.sctp.rto_min, .maxlen = sizeof(unsigned int), .mode = 0644, - .proc_handler = proc_sctp_do_rto_min, + .proc_handler = proc_dointvec_minmax, .extra1 = SYSCTL_ONE, .extra2 = &init_net.sctp.rto_max }, @@ -107,7 +103,7 @@ static const struct ctl_table sctp_net_table[] = { .data = &init_net.sctp.rto_max, .maxlen = sizeof(unsigned int), .mode = 0644, - .proc_handler = proc_sctp_do_rto_max, + .proc_handler = proc_dointvec_minmax, .extra1 = &init_net.sctp.rto_min, .extra2 = &timer_max }, @@ -419,62 +415,6 @@ static int proc_sctp_do_hmac_alg(const struct ctl_table *ctl, int write, return proc_dostring(&tbl, 0, buffer, lenp, ppos); } -static int proc_sctp_do_rto_min(const struct ctl_table *ctl, int write, - void *buffer, size_t *lenp, loff_t *ppos) -{ - struct net *net = container_of(ctl->data, struct net, sctp.rto_min); - unsigned int min = *(unsigned int *) ctl->extra1; - unsigned int max = *(unsigned int *) ctl->extra2; - struct ctl_table tbl; - int ret, new_value; - - memset(&tbl, 0, sizeof(struct ctl_table)); - tbl.maxlen = sizeof(unsigned int); - - if (write) - tbl.data = &new_value; - else - tbl.data = &net->sctp.rto_min; - - ret = proc_dointvec(&tbl, write, buffer, lenp, ppos); - if (write && ret == 0) { - if (new_value > max || new_value < min) - return -EINVAL; - - net->sctp.rto_min = new_value; - } - - return ret; -} - -static int proc_sctp_do_rto_max(const struct ctl_table *ctl, int write, - void *buffer, size_t *lenp, loff_t *ppos) -{ - struct net *net = container_of(ctl->data, struct net, sctp.rto_max); - unsigned int min = *(unsigned int *) ctl->extra1; - unsigned int max = *(unsigned int *) ctl->extra2; - struct ctl_table tbl; - int ret, new_value; - - memset(&tbl, 0, sizeof(struct ctl_table)); - tbl.maxlen = sizeof(unsigned int); - - if (write) - tbl.data = &new_value; - else - tbl.data = &net->sctp.rto_max; - - ret = proc_dointvec(&tbl, write, buffer, lenp, ppos); - if (write && ret == 0) { - if (new_value > max || new_value < min) - return -EINVAL; - - net->sctp.rto_max = new_value; - } - - return ret; -} - static int proc_sctp_do_alpha_beta(const struct ctl_table *ctl, int write, void *buffer, size_t *lenp, loff_t *ppos) { @@ -489,22 +429,24 @@ static int proc_sctp_do_auth(const struct ctl_table *ctl, int write, void *buffer, size_t *lenp, loff_t *ppos) { struct net *net = container_of(ctl->data, struct net, sctp.auth_enable); + int old_value, new_value, ret; struct ctl_table tbl; - int new_value, ret; + + if (!write) + return proc_dointvec(ctl, write, buffer, lenp, ppos); + + old_value = READ_ONCE(net->sctp.auth_enable); + new_value = old_value; memset(&tbl, 0, sizeof(struct ctl_table)); tbl.maxlen = sizeof(unsigned int); - - if (write) - tbl.data = &new_value; - else - tbl.data = &net->sctp.auth_enable; + tbl.data = &new_value; ret = proc_dointvec(&tbl, write, buffer, lenp, ppos); - if (write && ret == 0) { + if (ret == 0 && new_value != old_value) { struct sock *sk = net->sctp.ctl_sock; - net->sctp.auth_enable = new_value; + WRITE_ONCE(net->sctp.auth_enable, new_value); /* Update the value in the control socket */ lock_sock(sk); sctp_sk(sk)->ep->auth_enable = new_value; @@ -525,36 +467,37 @@ static int proc_sctp_do_udp_port(const struct ctl_table *ctl, int write, struct ctl_table tbl; int ret, new_value; + if (!write) + return proc_dointvec(ctl, write, buffer, lenp, ppos); + memset(&tbl, 0, sizeof(struct ctl_table)); tbl.maxlen = sizeof(unsigned int); + tbl.data = &new_value; - if (write) - tbl.data = &new_value; - else - tbl.data = &net->sctp.udp_port; - + mutex_lock(&sctp_sysctl_mutex); + new_value = net->sctp.udp_port; ret = proc_dointvec(&tbl, write, buffer, lenp, ppos); - if (write && ret == 0) { - struct sock *sk = net->sctp.ctl_sock; - - if (new_value > max || new_value < min) - return -EINVAL; - - mutex_lock(&sctp_sysctl_mutex); - net->sctp.udp_port = new_value; - sctp_udp_sock_stop(net); - if (new_value) { - ret = sctp_udp_sock_start(net); - if (ret) - net->sctp.udp_port = 0; + if (ret == 0) { + if (new_value > max || new_value < min) { + ret = -EINVAL; + } else if (new_value != net->sctp.udp_port) { + struct sock *sk = net->sctp.ctl_sock; + + WRITE_ONCE(net->sctp.udp_port, new_value); + sctp_udp_sock_stop(net); + if (new_value) { + ret = sctp_udp_sock_start(net); + if (ret) + WRITE_ONCE(net->sctp.udp_port, 0); + } + + /* Update the value in the control socket */ + lock_sock(sk); + sctp_sk(sk)->udp_port = htons(net->sctp.udp_port); + release_sock(sk); } - - /* Update the value in the control socket */ - lock_sock(sk); - sctp_sk(sk)->udp_port = htons(net->sctp.udp_port); - release_sock(sk); - mutex_unlock(&sctp_sysctl_mutex); } + mutex_unlock(&sctp_sysctl_mutex); return ret; } @@ -564,23 +507,26 @@ static int proc_sctp_do_probe_interval(const struct ctl_table *ctl, int write, { struct net *net = container_of(ctl->data, struct net, sctp.probe_interval); + int old_value, new_value, ret; struct ctl_table tbl; - int ret, new_value; + + if (!write) + return proc_dointvec(ctl, write, buffer, lenp, ppos); + + old_value = READ_ONCE(net->sctp.probe_interval); + new_value = old_value; memset(&tbl, 0, sizeof(struct ctl_table)); tbl.maxlen = sizeof(unsigned int); - - if (write) - tbl.data = &new_value; - else - tbl.data = &net->sctp.probe_interval; + tbl.data = &new_value; ret = proc_dointvec(&tbl, write, buffer, lenp, ppos); - if (write && ret == 0) { + if (ret == 0) { if (new_value && new_value < SCTP_PROBE_TIMER_MIN) return -EINVAL; - net->sctp.probe_interval = new_value; + if (new_value != old_value) + WRITE_ONCE(net->sctp.probe_interval, new_value); } return ret; -- 2.56.1.839.g22a8290db9-goog