* [PATCH net v2] sctp: Fix uninitialized new_value in custom sysctl handlers
@ 2026-12-01 17:44 Ian Rogers
0 siblings, 0 replies; only message in thread
From: Ian Rogers @ 2026-12-01 17:44 UTC (permalink / raw)
To: Marcelo Ricardo Leitner, Xin Long, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni
Cc: Willem de Bruijn, Simon Horman, linux-sctp, netdev, linux-kernel,
llvm, Elkin Cruz, Ian Rogers
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
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-01 17:46 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 17:44 [PATCH net v2] sctp: Fix uninitialized new_value in custom sysctl handlers Ian Rogers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox