]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
l2tp: fix race in duplicate tunnel detection
authorGuillaume Nault <g.nault@alphalink.fr>
Tue, 10 Apr 2018 19:01:13 +0000 (21:01 +0200)
committerStefan Bader <stefan.bader@canonical.com>
Tue, 22 May 2018 14:18:14 +0000 (16:18 +0200)
BugLink: http://bugs.launchpad.net/bugs/1769723
[ Upstream commit f6cd651b056ffd3b4e8496afd44d4ed44bf69136 ]

We can't use l2tp_tunnel_find() to prevent l2tp_nl_cmd_tunnel_create()
from creating a duplicate tunnel. A tunnel can be concurrently
registered after l2tp_tunnel_find() returns. Therefore, searching for
duplicates must be done at registration time.

Finally, remove l2tp_tunnel_find() entirely as it isn't use anywhere
anymore.

Fixes: 309795f4bec2 ("l2tp: Add netlink control API for L2TP")
Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
net/l2tp/l2tp_core.c
net/l2tp/l2tp_core.h
net/l2tp/l2tp_netlink.c

index a28c9c451cbad413d02f1e6b6b24881e9a57dbef..bf525f7e1e7215b0b6e4da4c53d20efbcb8c2516 100644 (file)
@@ -335,26 +335,6 @@ err_tlock:
 }
 EXPORT_SYMBOL_GPL(l2tp_session_register);
 
-/* Lookup a tunnel by id
- */
-struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id)
-{
-       struct l2tp_tunnel *tunnel;
-       struct l2tp_net *pn = l2tp_pernet(net);
-
-       rcu_read_lock_bh();
-       list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
-               if (tunnel->tunnel_id == tunnel_id) {
-                       rcu_read_unlock_bh();
-                       return tunnel;
-               }
-       }
-       rcu_read_unlock_bh();
-
-       return NULL;
-}
-EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
-
 struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth)
 {
        struct l2tp_net *pn = l2tp_pernet(net);
@@ -1510,6 +1490,7 @@ static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
                         struct l2tp_tunnel_cfg *cfg)
 {
+       struct l2tp_tunnel *tunnel_walk;
        struct l2tp_net *pn;
        struct socket *sock;
        struct sock *sk;
@@ -1538,7 +1519,16 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
        tunnel->l2tp_net = net;
 
        pn = l2tp_pernet(net);
+
        spin_lock_bh(&pn->l2tp_tunnel_list_lock);
+       list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) {
+               if (tunnel_walk->tunnel_id == tunnel->tunnel_id) {
+                       spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
+
+                       ret = -EEXIST;
+                       goto err_sock;
+               }
+       }
        list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
        spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
 
@@ -1567,7 +1557,10 @@ int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
        return 0;
 
 err_sock:
-       sockfd_put(sock);
+       if (tunnel->fd < 0)
+               sock_release(sock);
+       else
+               sockfd_put(sock);
 err:
        return ret;
 }
index c2b64ef6aaa75537de27c9b6b92f2b5d2eee8662..13e50ac774db9e370a8338d34ec6cfb46cb98bbf 100644 (file)
@@ -225,7 +225,6 @@ struct l2tp_session *l2tp_session_get(const struct net *net,
 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
                                                const char *ifname);
-struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id);
 struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth);
 
 int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
index 27c98921037f8aa6d89581daf1c76da7875b24e7..c56cb2c17d88f57833d0df9b472ea94e7628beaf 100644 (file)
@@ -236,12 +236,6 @@ static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info
        if (info->attrs[L2TP_ATTR_DEBUG])
                cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
 
-       tunnel = l2tp_tunnel_find(net, tunnel_id);
-       if (tunnel != NULL) {
-               ret = -EEXIST;
-               goto out;
-       }
-
        ret = -EINVAL;
        switch (cfg.encap) {
        case L2TP_ENCAPTYPE_UDP: