Netdev List
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>,
	Xin Long <lucien.xin@gmail.com>,
	 "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	 Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>
Cc: Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	linux-sctp@vger.kernel.org,  netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
	 Elkin Cruz <elkin@google.com>, Ian Rogers <irogers@google.com>
Subject: [PATCH net v2] sctp: Fix uninitialized new_value in custom sysctl handlers
Date: Tue,  1 Dec 2026 09:44:40 -0800	[thread overview]
Message-ID: <20261201174440.1437470-1-irogers@google.com> (raw)

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 <irogers@google.com>
---

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


                 reply	other threads:[~2026-12-01 17:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201174440.1437470-1-irogers@google.com \
    --to=irogers@google.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=elkin@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sctp@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=lucien.xin@gmail.com \
    --cc=marcelo.leitner@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox