[SCTP]: Fix a race between module load and protosw access
There is a race is SCTP between the loading of the module
and the access by the socket layer to the protocol functions.
In particular, a list of addresss that SCTP maintains is
not initialized prior to the registration with the protosw.
Thus it is possible for a user application to gain access
to SCTP functions before everything has been initialized.
The problem shows up as odd crashes during connection
initializtion when we try to access the SCTP address list.
The solution is to refactor how we do registration and
initialize the lists prior to registering with the protosw.
Care must be taken since the address list initialization
depends on some other pieces of SCTP initialization. Also
the clean-up in case of failure now also needs to be refactored.
Signed-off-by: Vlad Yasevich <vladislav.yasevich@hp.com>
Acked-by: Sridhar Samudrala <sri@us.ibm.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c
index 7a7646a..f90091a 100644
--- a/net/sctp/protocol.c
+++ b/net/sctp/protocol.c
@@ -992,6 +992,58 @@
free_percpu(sctp_statistics[1]);
}
+static void sctp_v4_pf_init(void)
+{
+ /* Initialize the SCTP specific PF functions. */
+ sctp_register_pf(&sctp_pf_inet, PF_INET);
+ sctp_register_af(&sctp_af_inet);
+}
+
+static void sctp_v4_pf_exit(void)
+{
+ list_del(&sctp_af_inet.list);
+}
+
+static int sctp_v4_protosw_init(void)
+{
+ int rc;
+
+ rc = proto_register(&sctp_prot, 1);
+ if (rc)
+ return rc;
+
+ /* Register SCTP(UDP and TCP style) with socket layer. */
+ inet_register_protosw(&sctp_seqpacket_protosw);
+ inet_register_protosw(&sctp_stream_protosw);
+
+ return 0;
+}
+
+static void sctp_v4_protosw_exit(void)
+{
+ inet_unregister_protosw(&sctp_stream_protosw);
+ inet_unregister_protosw(&sctp_seqpacket_protosw);
+ proto_unregister(&sctp_prot);
+}
+
+static int sctp_v4_add_protocol(void)
+{
+ /* Register notifier for inet address additions/deletions. */
+ register_inetaddr_notifier(&sctp_inetaddr_notifier);
+
+ /* Register SCTP with inet layer. */
+ if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
+ return -EAGAIN;
+
+ return 0;
+}
+
+static void sctp_v4_del_protocol(void)
+{
+ inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
+ unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
+}
+
/* Initialize the universe into something sensible. */
SCTP_STATIC __init int sctp_init(void)
{
@@ -1035,8 +1087,6 @@
/* Initialize object count debugging. */
sctp_dbg_objcnt_init();
- /* Initialize the SCTP specific PF functions. */
- sctp_register_pf(&sctp_pf_inet, PF_INET);
/*
* 14. Suggested SCTP Protocol Parameter Values
*/
@@ -1194,19 +1244,22 @@
sctp_sysctl_register();
INIT_LIST_HEAD(&sctp_address_families);
- sctp_register_af(&sctp_af_inet);
+ sctp_v4_pf_init();
+ sctp_v6_pf_init();
- status = proto_register(&sctp_prot, 1);
+ /* Initialize the local address list. */
+ INIT_LIST_HEAD(&sctp_local_addr_list);
+ spin_lock_init(&sctp_local_addr_lock);
+ sctp_get_local_addr_list();
+
+ status = sctp_v4_protosw_init();
+
if (status)
- goto err_proto_register;
+ goto err_protosw_init;
- /* Register SCTP(UDP and TCP style) with socket layer. */
- inet_register_protosw(&sctp_seqpacket_protosw);
- inet_register_protosw(&sctp_stream_protosw);
-
- status = sctp_v6_init();
+ status = sctp_v6_protosw_init();
if (status)
- goto err_v6_init;
+ goto err_v6_protosw_init;
/* Initialize the control inode/socket for handling OOTB packets. */
if ((status = sctp_ctl_sock_init())) {
@@ -1215,19 +1268,9 @@
goto err_ctl_sock_init;
}
- /* Initialize the local address list. */
- INIT_LIST_HEAD(&sctp_local_addr_list);
- spin_lock_init(&sctp_local_addr_lock);
- sctp_get_local_addr_list();
-
- /* Register notifier for inet address additions/deletions. */
- register_inetaddr_notifier(&sctp_inetaddr_notifier);
-
- /* Register SCTP with inet layer. */
- if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) {
- status = -EAGAIN;
+ status = sctp_v4_add_protocol();
+ if (status)
goto err_add_protocol;
- }
/* Register SCTP with inet6 layer. */
status = sctp_v6_add_protocol();
@@ -1238,18 +1281,18 @@
out:
return status;
err_v6_add_protocol:
- inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
- unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
+ sctp_v6_del_protocol();
err_add_protocol:
- sctp_free_local_addr_list();
+ sctp_v4_del_protocol();
sock_release(sctp_ctl_socket);
err_ctl_sock_init:
- sctp_v6_exit();
-err_v6_init:
- inet_unregister_protosw(&sctp_stream_protosw);
- inet_unregister_protosw(&sctp_seqpacket_protosw);
- proto_unregister(&sctp_prot);
-err_proto_register:
+ sctp_v6_protosw_exit();
+err_v6_protosw_init:
+ sctp_v4_protosw_exit();
+err_protosw_init:
+ sctp_free_local_addr_list();
+ sctp_v4_pf_exit();
+ sctp_v6_pf_exit();
sctp_sysctl_unregister();
list_del(&sctp_af_inet.list);
free_pages((unsigned long)sctp_port_hashtable,
@@ -1282,23 +1325,21 @@
/* Unregister with inet6/inet layers. */
sctp_v6_del_protocol();
- inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
-
- /* Unregister notifier for inet address additions/deletions. */
- unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
-
- /* Free the local address list. */
- sctp_free_local_addr_list();
+ sctp_v4_del_protocol();
/* Free the control endpoint. */
sock_release(sctp_ctl_socket);
- /* Cleanup v6 initializations. */
- sctp_v6_exit();
+ /* Free protosw registrations */
+ sctp_v6_protosw_exit();
+ sctp_v4_protosw_exit();
+
+ /* Free the local address list. */
+ sctp_free_local_addr_list();
/* Unregister with socket layer. */
- inet_unregister_protosw(&sctp_stream_protosw);
- inet_unregister_protosw(&sctp_seqpacket_protosw);
+ sctp_v6_pf_exit();
+ sctp_v4_pf_exit();
sctp_sysctl_unregister();
list_del(&sctp_af_inet.list);
@@ -1317,8 +1358,6 @@
kmem_cache_destroy(sctp_chunk_cachep);
kmem_cache_destroy(sctp_bucket_cachep);
-
- proto_unregister(&sctp_prot);
}
module_init(sctp_init);