Skip to contentSkip to conversation

sctp: initialize new_value in custom proc_dointvec sysctl handlers

PATCH New Ian Rogers 1 participant 1 message Nov 19

Subsystems (3)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 sctp: initialize new_value in custom proc_dointvec sysctl handlers

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")
---
 net/sctp/sysctl.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)
net/sctp/sysctl.c +23 -16
index 2b94c211427d..0c33f05fa532 100644
@@ -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);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog