Netdev List
 help / color / mirror / Atom feed
* [PATCH net] sctp: initialize new_value in custom proc_dointvec sysctl handlers
@ 2026-11-19 17:03 Ian Rogers
  2026-11-24 14:25 ` Xin Long
  0 siblings, 1 reply; 5+ messages in thread
From: Ian Rogers @ 2026-11-19 17:03 UTC (permalink / raw)
  To: Marcelo Ricardo Leitner, Xin Long, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni
  Cc: 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 on writes before
calling proc_dointvec().

proc_dointvec() can return 0 without writing to tbl.data, for example on
a zero-length write (*lenp == 0) or on a write at a non-zero file offset
ignored by proc_first_pos_non_zero_ignore() (such as pwrite(fd, "1\n",
2, 1) or a split write where the trailing newline is written separately).

When that happens:
- proc_sctp_do_auth() unconditionally stores the uninitialized stack int
  into net->sctp.auth_enable and sctp_sk(sk)->ep->auth_enable, both
  corrupting SCTP authentication state and leaking 4 bytes of kernel
  stack memory back on subsequent sysctl reads.
- proc_sctp_do_rto_min(), proc_sctp_do_rto_max(),
  proc_sctp_do_udp_port(), and proc_sctp_do_probe_interval() validate
  the uninitialized value and overwrite the netns setting whenever stack
  bytes happen to fall within range.
- proc_sctp_do_udp_port() also stops and restarts the UDP encapsulation
  socket even when the port number did not change.

Initialize new_value to the current netns setting before calling
proc_dointvec() (matching proc_dobool()), and skip tearing down and
restarting the UDP encapsulation socket in proc_sctp_do_udp_port() when
new_value equals net->sctp.udp_port.

Found by enabling Clang's -Wconditional-uninitialized on an x86_64
allyesconfig build, with AI coding assistant help to audit the warning
and prepare the fix. Build-tested with Clang; not runtime-tested.

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>
---
 net/sctp/sysctl.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/net/sctp/sysctl.c b/net/sctp/sysctl.c
index 2b94c211427d..0c33f05fa532 100644
--- a/net/sctp/sysctl.c
+++ b/net/sctp/sysctl.c
@@ -425,8 +425,9 @@ static int proc_sctp_do_rto_min(const struct ctl_table *ctl, int write,
 	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;
+	int new_value = net->sctp.rto_min;
 	struct ctl_table tbl;
-	int ret, new_value;
+	int ret;
 
 	memset(&tbl, 0, sizeof(struct ctl_table));
 	tbl.maxlen = sizeof(unsigned int);
@@ -453,8 +454,9 @@ static int proc_sctp_do_rto_max(const struct ctl_table *ctl, int write,
 	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;
+	int new_value = net->sctp.rto_max;
 	struct ctl_table tbl;
-	int ret, new_value;
+	int ret;
 
 	memset(&tbl, 0, sizeof(struct ctl_table));
 	tbl.maxlen = sizeof(unsigned int);
@@ -489,8 +491,9 @@ 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 new_value = net->sctp.auth_enable;
 	struct ctl_table tbl;
-	int new_value, ret;
+	int ret;
 
 	memset(&tbl, 0, sizeof(struct ctl_table));
 	tbl.maxlen = sizeof(unsigned int);
@@ -522,8 +525,9 @@ static int proc_sctp_do_udp_port(const struct ctl_table *ctl, int write,
 	struct net *net = container_of(ctl->data, struct net, sctp.udp_port);
 	unsigned int min = *(unsigned int *)ctl->extra1;
 	unsigned int max = *(unsigned int *)ctl->extra2;
+	int new_value = net->sctp.udp_port;
 	struct ctl_table tbl;
-	int ret, new_value;
+	int ret;
 
 	memset(&tbl, 0, sizeof(struct ctl_table));
 	tbl.maxlen = sizeof(unsigned int);
@@ -541,18 +545,20 @@ static int proc_sctp_do_udp_port(const struct ctl_table *ctl, int write,
 			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 (new_value != net->sctp.udp_port) {
+			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;
+			}
+
+			/* 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);
 	}
 
@@ -564,8 +570,9 @@ 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 new_value = net->sctp.probe_interval;
 	struct ctl_table tbl;
-	int ret, new_value;
+	int ret;
 
 	memset(&tbl, 0, sizeof(struct ctl_table));
 	tbl.maxlen = sizeof(unsigned int);
-- 
2.56.1.708.g77570a6a1d-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net] sctp: initialize new_value in custom proc_dointvec sysctl handlers
  2026-11-19 17:03 [PATCH net] sctp: initialize new_value in custom proc_dointvec sysctl handlers Ian Rogers
@ 2026-11-24 14:25 ` Xin Long
  2026-11-26  1:43   ` willemb
  0 siblings, 1 reply; 5+ messages in thread
From: Xin Long @ 2026-11-24 14:25 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Marcelo Ricardo Leitner, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, linux-sctp, netdev,
	linux-kernel, llvm, Elkin Cruz

On Thu, Nov 19, 2026 at 12:03 PM Ian Rogers <irogers@google.com> wrote:
>
> 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 on writes before
> calling proc_dointvec().
>
> proc_dointvec() can return 0 without writing to tbl.data, for example on
> a zero-length write (*lenp == 0) or on a write at a non-zero file offset
> ignored by proc_first_pos_non_zero_ignore() (such as pwrite(fd, "1\n",
> 2, 1) or a split write where the trailing newline is written separately).
>
> When that happens:
> - proc_sctp_do_auth() unconditionally stores the uninitialized stack int
>   into net->sctp.auth_enable and sctp_sk(sk)->ep->auth_enable, both
>   corrupting SCTP authentication state and leaking 4 bytes of kernel
>   stack memory back on subsequent sysctl reads.
> - proc_sctp_do_rto_min(), proc_sctp_do_rto_max(),
>   proc_sctp_do_udp_port(), and proc_sctp_do_probe_interval() validate
>   the uninitialized value and overwrite the netns setting whenever stack
>   bytes happen to fall within range.
> - proc_sctp_do_udp_port() also stops and restarts the UDP encapsulation
>   socket even when the port number did not change.
>
> Initialize new_value to the current netns setting before calling
> proc_dointvec() (matching proc_dobool()), and skip tearing down and
> restarting the UDP encapsulation socket in proc_sctp_do_udp_port() when
> new_value equals net->sctp.udp_port.
>
> Found by enabling Clang's -Wconditional-uninitialized on an x86_64
> allyesconfig build, with AI coding assistant help to audit the warning
> and prepare the fix. Build-tested with Clang; not runtime-tested.
>
> 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>
> ---
>  net/sctp/sysctl.c | 39 +++++++++++++++++++++++----------------
>  1 file changed, 23 insertions(+), 16 deletions(-)
>
> diff --git a/net/sctp/sysctl.c b/net/sctp/sysctl.c
> index 2b94c211427d..0c33f05fa532 100644
> --- a/net/sctp/sysctl.c
> +++ b/net/sctp/sysctl.c
> @@ -425,8 +425,9 @@ static int proc_sctp_do_rto_min(const struct ctl_table *ctl, int write,
>         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;
> +       int new_value = net->sctp.rto_min;
>         struct ctl_table tbl;
> -       int ret, new_value;
> +       int ret;
>
>         memset(&tbl, 0, sizeof(struct ctl_table));
>         tbl.maxlen = sizeof(unsigned int);
> @@ -453,8 +454,9 @@ static int proc_sctp_do_rto_max(const struct ctl_table *ctl, int write,
>         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;
> +       int new_value = net->sctp.rto_max;
>         struct ctl_table tbl;
> -       int ret, new_value;
> +       int ret;
>
>         memset(&tbl, 0, sizeof(struct ctl_table));
>         tbl.maxlen = sizeof(unsigned int);
> @@ -489,8 +491,9 @@ 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 new_value = net->sctp.auth_enable;
>         struct ctl_table tbl;
> -       int new_value, ret;
> +       int ret;
>
>         memset(&tbl, 0, sizeof(struct ctl_table));
>         tbl.maxlen = sizeof(unsigned int);
> @@ -522,8 +525,9 @@ static int proc_sctp_do_udp_port(const struct ctl_table *ctl, int write,
>         struct net *net = container_of(ctl->data, struct net, sctp.udp_port);
>         unsigned int min = *(unsigned int *)ctl->extra1;
>         unsigned int max = *(unsigned int *)ctl->extra2;
> +       int new_value = net->sctp.udp_port;
>         struct ctl_table tbl;
> -       int ret, new_value;
> +       int ret;
>
>         memset(&tbl, 0, sizeof(struct ctl_table));
>         tbl.maxlen = sizeof(unsigned int);
> @@ -541,18 +545,20 @@ static int proc_sctp_do_udp_port(const struct ctl_table *ctl, int write,
>                         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 (new_value != net->sctp.udp_port) {
> +                       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;
> +                       }
> +
> +                       /* 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);
>         }
>
> @@ -564,8 +570,9 @@ 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 new_value = net->sctp.probe_interval;
>         struct ctl_table tbl;
> -       int ret, new_value;
> +       int ret;
>
>         memset(&tbl, 0, sizeof(struct ctl_table));
>         tbl.maxlen = sizeof(unsigned int);
> --
> 2.56.1.708.g77570a6a1d-goog
>

Acked-by: Xin Long <lucien.xin@gmail.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] sctp: initialize new_value in custom proc_dointvec sysctl handlers
  2026-11-24 14:25 ` Xin Long
@ 2026-11-26  1:43   ` willemb
  2026-11-26 22:17     ` Xin Long
  0 siblings, 1 reply; 5+ messages in thread
From: willemb @ 2026-11-26  1:43 UTC (permalink / raw)
  To: Xin Long, Ian Rogers
  Cc: Marcelo Ricardo Leitner, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, linux-sctp, netdev,
	linux-kernel, llvm, Elkin Cruz

Xin Long wrote:
> On Thu, Nov 19, 2026 at 12:03 PM Ian Rogers <irogers@google.com> wrote:
> >
> > 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 on writes before
> > calling proc_dointvec().
> >
> > proc_dointvec() can return 0 without writing to tbl.data, for example on
> > a zero-length write (*lenp == 0) or on a write at a non-zero file offset
> > ignored by proc_first_pos_non_zero_ignore() (such as pwrite(fd, "1\n",
> > 2, 1) or a split write where the trailing newline is written  
> separately).
> >
> > When that happens:
> > - proc_sctp_do_auth() unconditionally stores the uninitialized stack int
> >   into net->sctp.auth_enable and sctp_sk(sk)->ep->auth_enable, both
> >   corrupting SCTP authentication state and leaking 4 bytes of kernel
> >   stack memory back on subsequent sysctl reads.
> > - proc_sctp_do_rto_min(), proc_sctp_do_rto_max(),
> >   proc_sctp_do_udp_port(), and proc_sctp_do_probe_interval() validate
> >   the uninitialized value and overwrite the netns setting whenever stack
> >   bytes happen to fall within range.
> > - proc_sctp_do_udp_port() also stops and restarts the UDP encapsulation
> >   socket even when the port number did not change.
> >
> > Initialize new_value to the current netns setting before calling
> > proc_dointvec() (matching proc_dobool()), and skip tearing down and
> > restarting the UDP encapsulation socket in proc_sctp_do_udp_port() when
> > new_value equals net->sctp.udp_port.
> >
> > Found by enabling Clang's -Wconditional-uninitialized on an x86_64
> > allyesconfig build, with AI coding assistant help to audit the warning
> > and prepare the fix. Build-tested with Clang; not runtime-tested.
> >
> > 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>
> > ---
> >  net/sctp/sysctl.c | 39 +++++++++++++++++++++++----------------
> >  1 file changed, 23 insertions(+), 16 deletions(-)
> >
> > diff --git a/net/sctp/sysctl.c b/net/sctp/sysctl.c
> > index 2b94c211427d..0c33f05fa532 100644
> > --- a/net/sctp/sysctl.c
> > +++ b/net/sctp/sysctl.c
> > @@ -425,8 +425,9 @@ static int proc_sctp_do_rto_min(const struct  
> ctl_table *ctl, int write,
> >         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;
> > +       int new_value = net->sctp.rto_min;
> >         struct ctl_table tbl;
> > -       int ret, new_value;
> > +       int ret;

     Initializing new_value with a plain unlocked load here (and in
     proc_sctp_do_rto_max(), proc_sctp_do_auth(), and  
proc_sctp_do_probe_interval())
     does not actually match proc_dobool(), which uses READ_ONCE() /  
WRITE_ONCE(),
     and introduces a new unmarked-read vs. unmarked-write data race against
     concurrent writers (even on read-only !write calls where new_value is  
unused).

     In addition, on an ignored write (*lenp == 0 or *ppos > 0), these four
     handlers still execute the post-proc_dointvec() commit block  
unconditionally:
     if another writer updates the setting between the initial load and the  
store,
     the no-op write overwrites it with the stale pre-call value (and in
     proc_sctp_do_auth(), needlessly acquires lock_sock(net->sctp.ctl_sock)  
and
     reverts ep->auth_enable as well).

     Returning early on reads (`if (!write) return proc_dointvec(ctl, write,
     buffer, lenp, ppos);`), initializing `old_value = READ_ONCE(...)` /
     `new_value = old_value` unconditionally on the write path (keeping  
Clang's
     -Wconditional-uninitialized happy), and gating the commit block on
     `new_value != old_value` with WRITE_ONCE() avoids both the data race and
     stale writebacks on ignored writes.
> >
> >         memset(&tbl, 0, sizeof(struct ctl_table));
> >         tbl.maxlen = sizeof(unsigned int);
> > @@ -453,8 +454,9 @@ static int proc_sctp_do_rto_max(const struct  
> ctl_table *ctl, int write,
> >         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;
> > +       int new_value = net->sctp.rto_max;
> >         struct ctl_table tbl;
> > -       int ret, new_value;
> > +       int ret;
> >
> >         memset(&tbl, 0, sizeof(struct ctl_table));
> >         tbl.maxlen = sizeof(unsigned int);
> > @@ -489,8 +491,9 @@ 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 new_value = net->sctp.auth_enable;
> >         struct ctl_table tbl;
> > -       int new_value, ret;
> > +       int ret;
> >
> >         memset(&tbl, 0, sizeof(struct ctl_table));
> >         tbl.maxlen = sizeof(unsigned int);
> > @@ -522,8 +525,9 @@ static int proc_sctp_do_udp_port(const struct  
> ctl_table *ctl, int write,
> >         struct net *net = container_of(ctl->data, struct net,  
> sctp.udp_port);
> >         unsigned int min = *(unsigned int *)ctl->extra1;
> >         unsigned int max = *(unsigned int *)ctl->extra2;
> > +       int new_value = net->sctp.udp_port;
> >         struct ctl_table tbl;
> > -       int ret, new_value;
> > +       int ret;
> >
> >         memset(&tbl, 0, sizeof(struct ctl_table));
> >         tbl.maxlen = sizeof(unsigned int);
> > @@ -541,18 +545,20 @@ static int proc_sctp_do_udp_port(const struct  
> ctl_table *ctl, int write,
> >                         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 (new_value != net->sctp.udp_port) {
> > +                       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;
> > +                       }

     net->sctp.udp_port is mutated under sctp_sysctl_mutex, including a  
transient
     store of new_value before sctp_udp_sock_start() can fail and reset it  
to 0.
     Reading net->sctp.udp_port outside sctp_sysctl_mutex (and without  
READ_ONCE())
     both introduces a new KCSAN data race on every read/write of udp_port  
and
     creates a TOCTOU race for the ignored-write case:

       1. Initial net->sctp.udp_port == 0.
       2. Thread A starts a no-op write (e.g. *lenp == 0 or trailing "\n" at
          *ppos > 0) and reads new_value = 0 before locking  
sctp_sysctl_mutex.
       3. Thread B writes "9899" at *ppos == 0, takes sctp_sysctl_mutex, sets
          net->sctp.udp_port = 9899, starts the UDP tunnel sockets, and  
unlocks.
       4. Thread A's proc_dointvec() ignores the write and returns 0 with
          new_value still 0. Thread A takes sctp_sysctl_mutex, sees
          new_value (0) != net->sctp.udp_port (9899), tears down Thread B's  
UDP
          sockets, and resets net->sctp.udp_port = 0.

     Conversely, if Thread A reads net->sctp.udp_port while Thread B is  
inside a
     failing sctp_udp_sock_start(), Thread A's ignored write captures the  
transient
     port and can attempt to bind it after Thread B rolls net->sctp.udp_port  
back
     to 0.

     Since proc_sys_call_handler() already copies the write buffer into  
kernel
     memory before invoking ->proc_handler(), handle !write with an early
     `return proc_dointvec(ctl, write, buffer, lenp, ppos);` and acquire
     sctp_sysctl_mutex before initializing `new_value = net->sctp.udp_port`  
and
     calling proc_dointvec() on writes (using WRITE_ONCE() for the stores  
under
     the mutex so lockless readers stay KCSAN-clean).
> > +
> > +                       /* 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);
> >         }
> >
> > @@ -564,8 +570,9 @@ 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 new_value = net->sctp.probe_interval;
> >         struct ctl_table tbl;
> > -       int ret, new_value;
> > +       int ret;
> >
> >         memset(&tbl, 0, sizeof(struct ctl_table));
> >         tbl.maxlen = sizeof(unsigned int);
> > --
> > 2.56.1.708.g77570a6a1d-goog
> >

> Acked-by: Xin Long <lucien.xin@gmail.com>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] sctp: initialize new_value in custom proc_dointvec sysctl handlers
  2026-11-26  1:43   ` willemb
@ 2026-11-26 22:17     ` Xin Long
  2026-12-01 17:38       ` Ian Rogers
  0 siblings, 1 reply; 5+ messages in thread
From: Xin Long @ 2026-11-26 22:17 UTC (permalink / raw)
  To: willemb
  Cc: Ian Rogers, Marcelo Ricardo Leitner, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	linux-sctp, netdev, linux-kernel, llvm, Elkin Cruz

On Wed, Nov 25, 2026 at 8:43 PM <willemb@google.com> wrote:
>
> Xin Long wrote:
> > On Thu, Nov 19, 2026 at 12:03 PM Ian Rogers <irogers@google.com> wrote:
> > >
> > > 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 on writes before
> > > calling proc_dointvec().
> > >
> > > proc_dointvec() can return 0 without writing to tbl.data, for example on
> > > a zero-length write (*lenp == 0) or on a write at a non-zero file offset
> > > ignored by proc_first_pos_non_zero_ignore() (such as pwrite(fd, "1\n",
> > > 2, 1) or a split write where the trailing newline is written
> > separately).
> > >
> > > When that happens:
> > > - proc_sctp_do_auth() unconditionally stores the uninitialized stack int
> > >   into net->sctp.auth_enable and sctp_sk(sk)->ep->auth_enable, both
> > >   corrupting SCTP authentication state and leaking 4 bytes of kernel
> > >   stack memory back on subsequent sysctl reads.
> > > - proc_sctp_do_rto_min(), proc_sctp_do_rto_max(),
> > >   proc_sctp_do_udp_port(), and proc_sctp_do_probe_interval() validate
> > >   the uninitialized value and overwrite the netns setting whenever stack
> > >   bytes happen to fall within range.
> > > - proc_sctp_do_udp_port() also stops and restarts the UDP encapsulation
> > >   socket even when the port number did not change.
> > >
> > > Initialize new_value to the current netns setting before calling
> > > proc_dointvec() (matching proc_dobool()), and skip tearing down and
> > > restarting the UDP encapsulation socket in proc_sctp_do_udp_port() when
> > > new_value equals net->sctp.udp_port.
> > >
> > > Found by enabling Clang's -Wconditional-uninitialized on an x86_64
> > > allyesconfig build, with AI coding assistant help to audit the warning
> > > and prepare the fix. Build-tested with Clang; not runtime-tested.
> > >
> > > 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>
> > > ---
> > >  net/sctp/sysctl.c | 39 +++++++++++++++++++++++----------------
> > >  1 file changed, 23 insertions(+), 16 deletions(-)
> > >
> > > diff --git a/net/sctp/sysctl.c b/net/sctp/sysctl.c
> > > index 2b94c211427d..0c33f05fa532 100644
> > > --- a/net/sctp/sysctl.c
> > > +++ b/net/sctp/sysctl.c
> > > @@ -425,8 +425,9 @@ static int proc_sctp_do_rto_min(const struct
> > ctl_table *ctl, int write,
> > >         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;
> > > +       int new_value = net->sctp.rto_min;
> > >         struct ctl_table tbl;
> > > -       int ret, new_value;
> > > +       int ret;
>
>      Initializing new_value with a plain unlocked load here (and in
>      proc_sctp_do_rto_max(), proc_sctp_do_auth(), and
> proc_sctp_do_probe_interval())
>      does not actually match proc_dobool(), which uses READ_ONCE() /
> WRITE_ONCE(),
>      and introduces a new unmarked-read vs. unmarked-write data race against
>      concurrent writers (even on read-only !write calls where new_value is
> unused).
>
>      In addition, on an ignored write (*lenp == 0 or *ppos > 0), these four
>      handlers still execute the post-proc_dointvec() commit block
> unconditionally:
>      if another writer updates the setting between the initial load and the
> store,
>      the no-op write overwrites it with the stale pre-call value (and in
>      proc_sctp_do_auth(), needlessly acquires lock_sock(net->sctp.ctl_sock)
> and
>      reverts ep->auth_enable as well).
>
>      Returning early on reads (`if (!write) return proc_dointvec(ctl, write,
>      buffer, lenp, ppos);`), initializing `old_value = READ_ONCE(...)` /
>      `new_value = old_value` unconditionally on the write path (keeping
> Clang's
>      -Wconditional-uninitialized happy), and gating the commit block on
>      `new_value != old_value` with WRITE_ONCE() avoids both the data race and
>      stale writebacks on ignored writes.
Right.

I think you can replace proc_sctp_do_rto_min() and proc_sctp_do_rto_max()
with proc_dointvec_minmax().

Then apply Willem's suggestion only to proc_sctp_do_auth() and
proc_sctp_do_probe_interval().

We may also need to use READ_ONCE() when accessing these net->sctp.xxx
fields elsewhere in the SCTP stack, but that can be addressed in a separate
patch.

> > >
> > >         memset(&tbl, 0, sizeof(struct ctl_table));
> > >         tbl.maxlen = sizeof(unsigned int);
> > > @@ -453,8 +454,9 @@ static int proc_sctp_do_rto_max(const struct
> > ctl_table *ctl, int write,
> > >         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;
> > > +       int new_value = net->sctp.rto_max;
> > >         struct ctl_table tbl;
> > > -       int ret, new_value;
> > > +       int ret;
> > >
> > >         memset(&tbl, 0, sizeof(struct ctl_table));
> > >         tbl.maxlen = sizeof(unsigned int);
> > > @@ -489,8 +491,9 @@ 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 new_value = net->sctp.auth_enable;
> > >         struct ctl_table tbl;
> > > -       int new_value, ret;
> > > +       int ret;
> > >
> > >         memset(&tbl, 0, sizeof(struct ctl_table));
> > >         tbl.maxlen = sizeof(unsigned int);
> > > @@ -522,8 +525,9 @@ static int proc_sctp_do_udp_port(const struct
> > ctl_table *ctl, int write,
> > >         struct net *net = container_of(ctl->data, struct net,
> > sctp.udp_port);
> > >         unsigned int min = *(unsigned int *)ctl->extra1;
> > >         unsigned int max = *(unsigned int *)ctl->extra2;
> > > +       int new_value = net->sctp.udp_port;
> > >         struct ctl_table tbl;
> > > -       int ret, new_value;
> > > +       int ret;
> > >
> > >         memset(&tbl, 0, sizeof(struct ctl_table));
> > >         tbl.maxlen = sizeof(unsigned int);
> > > @@ -541,18 +545,20 @@ static int proc_sctp_do_udp_port(const struct
> > ctl_table *ctl, int write,
> > >                         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 (new_value != net->sctp.udp_port) {
> > > +                       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;
> > > +                       }
>
>      net->sctp.udp_port is mutated under sctp_sysctl_mutex, including a
> transient
>      store of new_value before sctp_udp_sock_start() can fail and reset it
> to 0.
>      Reading net->sctp.udp_port outside sctp_sysctl_mutex (and without
> READ_ONCE())
>      both introduces a new KCSAN data race on every read/write of udp_port
> and
>      creates a TOCTOU race for the ignored-write case:
>
>        1. Initial net->sctp.udp_port == 0.
>        2. Thread A starts a no-op write (e.g. *lenp == 0 or trailing "\n" at
>           *ppos > 0) and reads new_value = 0 before locking
> sctp_sysctl_mutex.
>        3. Thread B writes "9899" at *ppos == 0, takes sctp_sysctl_mutex, sets
>           net->sctp.udp_port = 9899, starts the UDP tunnel sockets, and
> unlocks.
>        4. Thread A's proc_dointvec() ignores the write and returns 0 with
>           new_value still 0. Thread A takes sctp_sysctl_mutex, sees
>           new_value (0) != net->sctp.udp_port (9899), tears down Thread B's
> UDP
>           sockets, and resets net->sctp.udp_port = 0.
>
>      Conversely, if Thread A reads net->sctp.udp_port while Thread B is
> inside a
>      failing sctp_udp_sock_start(), Thread A's ignored write captures the
> transient
>      port and can attempt to bind it after Thread B rolls net->sctp.udp_port
> back
>      to 0.
>
>      Since proc_sys_call_handler() already copies the write buffer into
> kernel
>      memory before invoking ->proc_handler(), handle !write with an early
>      `return proc_dointvec(ctl, write, buffer, lenp, ppos);` and acquire
>      sctp_sysctl_mutex before initializing `new_value = net->sctp.udp_port`
> and
>      calling proc_dointvec() on writes (using WRITE_ONCE() for the stores
> under
>      the mutex so lockless readers stay KCSAN-clean).
Makes sense.

With sctp_sysctl_mutex, we don't need the extra old_value variable, unlike
in proc_sctp_do_auth() and proc_sctp_do_probe_interval() above.

Thanks for the comment.

> > > +
> > > +                       /* 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);
> > >         }
> > >
> > > @@ -564,8 +570,9 @@ 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 new_value = net->sctp.probe_interval;
> > >         struct ctl_table tbl;
> > > -       int ret, new_value;
> > > +       int ret;
> > >
> > >         memset(&tbl, 0, sizeof(struct ctl_table));
> > >         tbl.maxlen = sizeof(unsigned int);
> > > --
> > > 2.56.1.708.g77570a6a1d-goog
> > >
>
> > Acked-by: Xin Long <lucien.xin@gmail.com>
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] sctp: initialize new_value in custom proc_dointvec sysctl handlers
  2026-11-26 22:17     ` Xin Long
@ 2026-12-01 17:38       ` Ian Rogers
  0 siblings, 0 replies; 5+ messages in thread
From: Ian Rogers @ 2026-12-01 17:38 UTC (permalink / raw)
  To: Xin Long
  Cc: willemb, Marcelo Ricardo Leitner, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, linux-sctp, netdev,
	linux-kernel, llvm, Elkin Cruz, Ian Rogers

On Thu, Nov 26, 2026 at 2:17 PM Xin Long <lucien.xin@gmail.com> wrote:
>
> On Wed, Nov 25, 2026 at 8:43 PM <willemb@google.com> wrote:
> >
> > Returning early on reads (`if (!write) return proc_dointvec(ctl, write,
> > buffer, lenp, ppos);`), initializing `old_value = READ_ONCE(...)` /
> > `new_value = old_value` unconditionally on the write path (keeping Clang's
> > -Wconditional-uninitialized happy), and gating the commit block on
> > `new_value != old_value` with WRITE_ONCE() avoids both the data race and
> > stale writebacks on ignored writes.
> Right.
>
> I think you can replace proc_sctp_do_rto_min() and proc_sctp_do_rto_max()
> with proc_dointvec_minmax().
>
> Then apply Willem's suggestion only to proc_sctp_do_auth() and
> proc_sctp_do_probe_interval().
[...]
> With sctp_sysctl_mutex, we don't need the extra old_value variable, unlike
> in proc_sctp_do_auth() and proc_sctp_do_probe_interval() above.

Thanks both for the thorough review! I've updated the patch to replace
proc_sctp_do_rto_min() and proc_sctp_do_rto_max() with
proc_dointvec_minmax(), apply the early !write return + READ_ONCE() /
WRITE_ONCE() + new_value != old_value check to 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().

I also reproduced the unpatched stack corruption/-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. I'll send v2 shortly.

Thanks,
Ian

pw-bot: cr

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-01 17:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 17:03 [PATCH net] sctp: initialize new_value in custom proc_dointvec sysctl handlers Ian Rogers
2026-11-24 14:25 ` Xin Long
2026-11-26  1:43   ` willemb
2026-11-26 22:17     ` Xin Long
2026-12-01 17:38       ` Ian Rogers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox