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=1796147202; b=jJJsIZZKeBGWuy6mfCD6RCM9D/00+erw19xS6GmTRyUHnQWLO+gXOhjFWlN3ZrE4cept pUwEfDKypiMlqcuDF97QCl2a9XugY7aAkLQ2XgUpMxHMwDAGo73uPq2b/7psQcmYaRHmf VB6m+awnxWxO5v0nqVGzIq5s15lmTONT8TWFKro0B7uRyHNUikPhalwujYUqTaAbRpFTn /g0BCSF2ttc7KMlt0RkjIn0ep9L5jIvnXuZCNJUkRpRa08UrtXlV6b8OXcdHdqYW8ALg0 rtXp0X/A8tx2H68RmVTSIgZyFmPlKiwxliR8VAk1PM2MHC/wIDq3FPX08Sm4duWrpeA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796147202; 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=xNRIPXhkPtRsXaNlqWDe8RakgettN/ie+7DJniRzKwRt/vQhJk7NryPXxubSomzfVS05 qBp7Cky+FDHj/+Sbzjn0fGZmnn7E3ReKsxbMmtzNaL7iZlEEFz4Il1KVI72I6j5JH8Fte 6q9MKQeAdOM6rSew33PGGp5NnRjqgwVfWUVuSi8E6iGcNgVucx/TqF45BNs7LFmchlKy8 IhgVt6dPggqYXvhcbIKXY1oJznUqbNcMcK8VlNgM7njhXbI/lHDcTuS+eukSiy7R2yJzp /NkUviOaQVuxLz/kltioW64c0hKeHAQlJIbrLTiUfpSkX3Yb0nb+QcxfqBoHQYCIOWw== 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 7FFB1606A32F for ; Tue, 1 Dec 2026 17:46:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7FFB1606A32F 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-386a0f79047so13355177eec.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=k46DsTj45Qv44M5BN/r9f8myQ2Z9gAqU0818MYnLOHqCpu0q/NkCmZE9Uhbda1fgNj FidtsO6IYhOeax0SrnabPi2AUVapwIDpHes/F1iasF3OD3L5b+vE/Ohzb3tw9MlTcIJL 4FCXxGHgIbFFj5d4w38g6hkc2OdLcRyOLTCzny2TIMWN8wVZrqZS2YRT1+VGwT4whHa2 yNMjKZtm9I86maWIwvoNjsdxrrXlGpmI07ilAFJGtZE99+r0VYvqAC2+EPtyiGFMFjf8 VDxjXhPhgu2VrXPUKxWOntLKP0QjahmELJIjlR4l59ItJcvrKA2qdEGQDXe+EVVWnB5W jsyg== X-Forwarded-Encrypted: i=1; AL+hErET+r0zHZDk6o974pMGBUKqZwuuMUzrvQa6/WbqYXmSNVpEi92pQVdXJvsOp1yxouDJilcIz7gXHWsgNZM=@vger.kernel.org X-Gm-Message-State: APuU4Z/rozpZAD1RWcVFlHhCkV+Y025hjJa6cL6IXLNYtljOOwfXJuID MYvxaiBtw5g5r9kF+oqvJ8SKqILfzSf3xZtBQCfCb3EuModV+D26yWzqH6qhVmgBVXHRBM5CaLh uDuNUCGscHA== 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: linux-kernel@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