Skip to content

Commit 9ca5e7e

Browse files
Shigeru Yoshidadavem330
Shigeru Yoshida
authored andcommitted
l2tp: Avoid possible recursive deadlock in l2tp_tunnel_register()
When a file descriptor of pppol2tp socket is passed as file descriptor of UDP socket, a recursive deadlock occurs in l2tp_tunnel_register(). This situation is reproduced by the following program: int main(void) { int sock; struct sockaddr_pppol2tp addr; sock = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); if (sock < 0) { perror("socket"); return 1; } addr.sa_family = AF_PPPOX; addr.sa_protocol = PX_PROTO_OL2TP; addr.pppol2tp.pid = 0; addr.pppol2tp.fd = sock; addr.pppol2tp.addr.sin_family = PF_INET; addr.pppol2tp.addr.sin_port = htons(0); addr.pppol2tp.addr.sin_addr.s_addr = inet_addr("192.168.0.1"); addr.pppol2tp.s_tunnel = 1; addr.pppol2tp.s_session = 0; addr.pppol2tp.d_tunnel = 0; addr.pppol2tp.d_session = 0; if (connect(sock, (const struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("connect"); return 1; } return 0; } This program causes the following lockdep warning: ============================================ WARNING: possible recursive locking detected 6.2.0-rc5-00205-gc96618275234 #56 Not tainted -------------------------------------------- repro/8607 is trying to acquire lock: ffff8880213c8130 (sk_lock-AF_PPPOX){+.+.}-{0:0}, at: l2tp_tunnel_register+0x2b7/0x11c0 but task is already holding lock: ffff8880213c8130 (sk_lock-AF_PPPOX){+.+.}-{0:0}, at: pppol2tp_connect+0xa82/0x1a30 other info that might help us debug this: Possible unsafe locking scenario: CPU0 ---- lock(sk_lock-AF_PPPOX); lock(sk_lock-AF_PPPOX); *** DEADLOCK *** May be due to missing lock nesting notation 1 lock held by repro/8607: #0: ffff8880213c8130 (sk_lock-AF_PPPOX){+.+.}-{0:0}, at: pppol2tp_connect+0xa82/0x1a30 stack backtrace: CPU: 0 PID: 8607 Comm: repro Not tainted 6.2.0-rc5-00205-gc96618275234 #56 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.1-2.fc37 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0x100/0x178 __lock_acquire.cold+0x119/0x3b9 ? lockdep_hardirqs_on_prepare+0x410/0x410 lock_acquire+0x1e0/0x610 ? l2tp_tunnel_register+0x2b7/0x11c0 ? lock_downgrade+0x710/0x710 ? __fget_files+0x283/0x3e0 lock_sock_nested+0x3a/0xf0 ? l2tp_tunnel_register+0x2b7/0x11c0 l2tp_tunnel_register+0x2b7/0x11c0 ? sprintf+0xc4/0x100 ? l2tp_tunnel_del_work+0x6b0/0x6b0 ? debug_object_deactivate+0x320/0x320 ? lockdep_init_map_type+0x16d/0x7a0 ? lockdep_init_map_type+0x16d/0x7a0 ? l2tp_tunnel_create+0x2bf/0x4b0 ? l2tp_tunnel_create+0x3c6/0x4b0 pppol2tp_connect+0x14e1/0x1a30 ? pppol2tp_put_sk+0xd0/0xd0 ? aa_sk_perm+0x2b7/0xa80 ? aa_af_perm+0x260/0x260 ? bpf_lsm_socket_connect+0x9/0x10 ? pppol2tp_put_sk+0xd0/0xd0 __sys_connect_file+0x14f/0x190 __sys_connect+0x133/0x160 ? __sys_connect_file+0x190/0x190 ? lockdep_hardirqs_on+0x7d/0x100 ? ktime_get_coarse_real_ts64+0x1b7/0x200 ? ktime_get_coarse_real_ts64+0x147/0x200 ? __audit_syscall_entry+0x396/0x500 __x64_sys_connect+0x72/0xb0 do_syscall_64+0x38/0xb0 entry_SYSCALL_64_after_hwframe+0x63/0xcd This patch fixes the issue by getting/creating the tunnel before locking the pppol2tp socket. Fixes: 0b2c597 ("l2tp: close all race conditions in l2tp_tunnel_register()") Cc: Cong Wang <[email protected]> Signed-off-by: Shigeru Yoshida <[email protected]> Reviewed-by: Guillaume Nault <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 4368640 commit 9ca5e7e

File tree

1 file changed

+67
-58
lines changed

1 file changed

+67
-58
lines changed

net/l2tp/l2tp_ppp.c

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -650,54 +650,22 @@ static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
650650
return mtu - PPPOL2TP_HEADER_OVERHEAD;
651651
}
652652

653-
/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
654-
*/
655-
static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
656-
int sockaddr_len, int flags)
653+
static struct l2tp_tunnel *pppol2tp_tunnel_get(struct net *net,
654+
const struct l2tp_connect_info *info,
655+
bool *new_tunnel)
657656
{
658-
struct sock *sk = sock->sk;
659-
struct pppox_sock *po = pppox_sk(sk);
660-
struct l2tp_session *session = NULL;
661-
struct l2tp_connect_info info;
662657
struct l2tp_tunnel *tunnel;
663-
struct pppol2tp_session *ps;
664-
struct l2tp_session_cfg cfg = { 0, };
665-
bool drop_refcnt = false;
666-
bool drop_tunnel = false;
667-
bool new_session = false;
668-
bool new_tunnel = false;
669658
int error;
670659

671-
error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
672-
if (error < 0)
673-
return error;
660+
*new_tunnel = false;
674661

675-
lock_sock(sk);
676-
677-
/* Check for already bound sockets */
678-
error = -EBUSY;
679-
if (sk->sk_state & PPPOX_CONNECTED)
680-
goto end;
681-
682-
/* We don't supporting rebinding anyway */
683-
error = -EALREADY;
684-
if (sk->sk_user_data)
685-
goto end; /* socket is already attached */
686-
687-
/* Don't bind if tunnel_id is 0 */
688-
error = -EINVAL;
689-
if (!info.tunnel_id)
690-
goto end;
691-
692-
tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
693-
if (tunnel)
694-
drop_tunnel = true;
662+
tunnel = l2tp_tunnel_get(net, info->tunnel_id);
695663

696664
/* Special case: create tunnel context if session_id and
697665
* peer_session_id is 0. Otherwise look up tunnel using supplied
698666
* tunnel id.
699667
*/
700-
if (!info.session_id && !info.peer_session_id) {
668+
if (!info->session_id && !info->peer_session_id) {
701669
if (!tunnel) {
702670
struct l2tp_tunnel_cfg tcfg = {
703671
.encap = L2TP_ENCAPTYPE_UDP,
@@ -706,40 +674,82 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
706674
/* Prevent l2tp_tunnel_register() from trying to set up
707675
* a kernel socket.
708676
*/
709-
if (info.fd < 0) {
710-
error = -EBADF;
711-
goto end;
712-
}
677+
if (info->fd < 0)
678+
return ERR_PTR(-EBADF);
713679

714-
error = l2tp_tunnel_create(info.fd,
715-
info.version,
716-
info.tunnel_id,
717-
info.peer_tunnel_id, &tcfg,
680+
error = l2tp_tunnel_create(info->fd,
681+
info->version,
682+
info->tunnel_id,
683+
info->peer_tunnel_id, &tcfg,
718684
&tunnel);
719685
if (error < 0)
720-
goto end;
686+
return ERR_PTR(error);
721687

722688
l2tp_tunnel_inc_refcount(tunnel);
723-
error = l2tp_tunnel_register(tunnel, sock_net(sk),
724-
&tcfg);
689+
error = l2tp_tunnel_register(tunnel, net, &tcfg);
725690
if (error < 0) {
726691
kfree(tunnel);
727-
goto end;
692+
return ERR_PTR(error);
728693
}
729-
drop_tunnel = true;
730-
new_tunnel = true;
694+
695+
*new_tunnel = true;
731696
}
732697
} else {
733698
/* Error if we can't find the tunnel */
734-
error = -ENOENT;
735699
if (!tunnel)
736-
goto end;
700+
return ERR_PTR(-ENOENT);
737701

738702
/* Error if socket is not prepped */
739-
if (!tunnel->sock)
740-
goto end;
703+
if (!tunnel->sock) {
704+
l2tp_tunnel_dec_refcount(tunnel);
705+
return ERR_PTR(-ENOENT);
706+
}
741707
}
742708

709+
return tunnel;
710+
}
711+
712+
/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
713+
*/
714+
static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
715+
int sockaddr_len, int flags)
716+
{
717+
struct sock *sk = sock->sk;
718+
struct pppox_sock *po = pppox_sk(sk);
719+
struct l2tp_session *session = NULL;
720+
struct l2tp_connect_info info;
721+
struct l2tp_tunnel *tunnel;
722+
struct pppol2tp_session *ps;
723+
struct l2tp_session_cfg cfg = { 0, };
724+
bool drop_refcnt = false;
725+
bool new_session = false;
726+
bool new_tunnel = false;
727+
int error;
728+
729+
error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
730+
if (error < 0)
731+
return error;
732+
733+
/* Don't bind if tunnel_id is 0 */
734+
if (!info.tunnel_id)
735+
return -EINVAL;
736+
737+
tunnel = pppol2tp_tunnel_get(sock_net(sk), &info, &new_tunnel);
738+
if (IS_ERR(tunnel))
739+
return PTR_ERR(tunnel);
740+
741+
lock_sock(sk);
742+
743+
/* Check for already bound sockets */
744+
error = -EBUSY;
745+
if (sk->sk_state & PPPOX_CONNECTED)
746+
goto end;
747+
748+
/* We don't supporting rebinding anyway */
749+
error = -EALREADY;
750+
if (sk->sk_user_data)
751+
goto end; /* socket is already attached */
752+
743753
if (tunnel->peer_tunnel_id == 0)
744754
tunnel->peer_tunnel_id = info.peer_tunnel_id;
745755

@@ -840,8 +850,7 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
840850
}
841851
if (drop_refcnt)
842852
l2tp_session_dec_refcount(session);
843-
if (drop_tunnel)
844-
l2tp_tunnel_dec_refcount(tunnel);
853+
l2tp_tunnel_dec_refcount(tunnel);
845854
release_sock(sk);
846855

847856
return error;

0 commit comments

Comments
 (0)