Skip to content

Commit 626bda8

Browse files
rr00xxyygregkh
authored andcommitted
sctp: avoid auth_enable sysctl UAF during netns teardown
commit f8d5e78 upstream. proc_sctp_do_auth() updates the SCTP control socket after changing net.sctp.auth_enable. The handler gets the per-net SCTP state from ctl->data, so an already opened sysctl file can still target a network namespace while that namespace is being torn down. SCTP previously registered its per-net sysctls from sctp_defaults_init(), while the control socket is created later from sctp_ctrlsock_init(). This exposed a window during initialization where auth_enable was writable before net->sctp.ctl_sock existed, and a teardown window where auth_enable stayed writable after inet_ctl_sock_destroy() had released the control socket. Move the per-net SCTP sysctl registration into sctp_ctrlsock_init() after sctp_ctl_sock_init() succeeds, and unregister the sysctl table before destroying the control socket in sctp_ctrlsock_exit(). If sysctl registration fails after the control socket was created, destroy the control socket in the same init path. Make sctp_sysctl_net_unregister() tolerate a missing header and clear the saved pointer so init-error and exit paths can safely share the unregister helper. Fixes: 15649fd ("sctp: sysctl: auth_enable: avoid using current->nsproxy") Cc: stable@vger.kernel.org Reported-by: Yuan Tan <yuantan098@gmail.com> Reported-by: Yifan Wu <yifanwucs@gmail.com> Reported-by: Juefei Pu <tomapufckgml@gmail.com> Reported-by: Xin Liu <bird@lzu.edu.cn> Co-developed-by: Qi Tang <tpluszz77@gmail.com> Signed-off-by: Qi Tang <tpluszz77@gmail.com> Signed-off-by: Zhiling Zou <roxy520tt@gmail.com> Signed-off-by: Ren Wei <n05ec@lzu.edu.cn> Acked-by: Xin Long <lucien.xin@gmail.com> Link: https://patch.msgid.link/390cd5e91ed60eea27b0b64d0468301a9e73b808.1784033357.git.roxy520tt@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 74e8f3e commit 626bda8

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

net/sctp/protocol.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,10 +1397,6 @@ static int __net_init sctp_defaults_init(struct net *net)
13971397
net->sctp.l3mdev_accept = 1;
13981398
#endif
13991399

1400-
status = sctp_sysctl_net_register(net);
1401-
if (status)
1402-
goto err_sysctl_register;
1403-
14041400
/* Allocate and initialise sctp mibs. */
14051401
status = init_sctp_mibs(net);
14061402
if (status)
@@ -1434,8 +1430,6 @@ static int __net_init sctp_defaults_init(struct net *net)
14341430
cleanup_sctp_mibs(net);
14351431
#endif
14361432
err_init_mibs:
1437-
sctp_sysctl_net_unregister(net);
1438-
err_sysctl_register:
14391433
return status;
14401434
}
14411435

@@ -1450,7 +1444,6 @@ static void __net_exit sctp_defaults_exit(struct net *net)
14501444
net->sctp.proc_net_sctp = NULL;
14511445
#endif
14521446
cleanup_sctp_mibs(net);
1453-
sctp_sysctl_net_unregister(net);
14541447
}
14551448

14561449
static struct pernet_operations sctp_defaults_ops = {
@@ -1464,16 +1457,27 @@ static int __net_init sctp_ctrlsock_init(struct net *net)
14641457

14651458
/* Initialize the control inode/socket for handling OOTB packets. */
14661459
status = sctp_ctl_sock_init(net);
1467-
if (status)
1460+
if (status) {
14681461
pr_err("Failed to initialize the SCTP control sock\n");
1462+
return status;
1463+
}
1464+
1465+
status = sctp_sysctl_net_register(net);
1466+
if (status) {
1467+
inet_ctl_sock_destroy(net->sctp.ctl_sock);
1468+
net->sctp.ctl_sock = NULL;
1469+
}
14691470

14701471
return status;
14711472
}
14721473

14731474
static void __net_exit sctp_ctrlsock_exit(struct net *net)
14741475
{
1476+
sctp_sysctl_net_unregister(net);
1477+
14751478
/* Free the control endpoint. */
14761479
inet_ctl_sock_destroy(net->sctp.ctl_sock);
1480+
net->sctp.ctl_sock = NULL;
14771481
}
14781482

14791483
static struct pernet_operations sctp_ctrlsock_ops = {

net/sctp/sysctl.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,16 @@ int sctp_sysctl_net_register(struct net *net)
626626

627627
void sctp_sysctl_net_unregister(struct net *net)
628628
{
629+
struct ctl_table_header *header = net->sctp.sysctl_header;
629630
const struct ctl_table *table;
630631

631-
table = net->sctp.sysctl_header->ctl_table_arg;
632-
unregister_net_sysctl_table(net->sctp.sysctl_header);
632+
if (!header)
633+
return;
634+
635+
table = header->ctl_table_arg;
636+
unregister_net_sysctl_table(header);
633637
kfree(table);
638+
net->sctp.sysctl_header = NULL;
634639
}
635640

636641
static struct ctl_table_header *sctp_sysctl_header;

0 commit comments

Comments
 (0)