Skip to content

Commit 7716682

Browse files
edumazetdavem330
authored andcommitted
tcp/dccp: fix another race at listener dismantle
Ilya reported following lockdep splat: kernel: ========================= kernel: [ BUG: held lock freed! ] kernel: 4.5.0-rc1-ceph-00026-g5e0a311 #1 Not tainted kernel: ------------------------- kernel: swapper/5/0 is freeing memory ffff880035c9d200-ffff880035c9dbff, with a lock still held there! kernel: (&(&queue->rskq_lock)->rlock){+.-...}, at: [<ffffffff816f6a88>] inet_csk_reqsk_queue_add+0x28/0xa0 kernel: 4 locks held by swapper/5/0: kernel: #0: (rcu_read_lock){......}, at: [<ffffffff8169ef6b>] netif_receive_skb_internal+0x4b/0x1f0 kernel: #1: (rcu_read_lock){......}, at: [<ffffffff816e977f>] ip_local_deliver_finish+0x3f/0x380 kernel: #2: (slock-AF_INET){+.-...}, at: [<ffffffff81685ffb>] sk_clone_lock+0x19b/0x440 kernel: #3: (&(&queue->rskq_lock)->rlock){+.-...}, at: [<ffffffff816f6a88>] inet_csk_reqsk_queue_add+0x28/0xa0 To properly fix this issue, inet_csk_reqsk_queue_add() needs to return to its callers if the child as been queued into accept queue. We also need to make sure listener is still there before calling sk->sk_data_ready(), by holding a reference on it, since the reference carried by the child can disappear as soon as the child is put on accept queue. Reported-by: Ilya Dryomov <[email protected]> Fixes: ebb516a ("tcp/dccp: fix race at listener dismantle phase") Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent deed49d commit 7716682

File tree

6 files changed

+38
-37
lines changed

6 files changed

+38
-37
lines changed

include/net/inet_connection_sock.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,9 @@ struct dst_entry *inet_csk_route_child_sock(const struct sock *sk,
270270
struct sock *newsk,
271271
const struct request_sock *req);
272272

273-
void inet_csk_reqsk_queue_add(struct sock *sk, struct request_sock *req,
274-
struct sock *child);
273+
struct sock *inet_csk_reqsk_queue_add(struct sock *sk,
274+
struct request_sock *req,
275+
struct sock *child);
275276
void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
276277
unsigned long timeout);
277278
struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,

net/dccp/ipv4.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -824,26 +824,26 @@ static int dccp_v4_rcv(struct sk_buff *skb)
824824

825825
if (sk->sk_state == DCCP_NEW_SYN_RECV) {
826826
struct request_sock *req = inet_reqsk(sk);
827-
struct sock *nsk = NULL;
827+
struct sock *nsk;
828828

829829
sk = req->rsk_listener;
830-
if (likely(sk->sk_state == DCCP_LISTEN)) {
831-
nsk = dccp_check_req(sk, skb, req);
832-
} else {
830+
if (unlikely(sk->sk_state != DCCP_LISTEN)) {
833831
inet_csk_reqsk_queue_drop_and_put(sk, req);
834832
goto lookup;
835833
}
834+
sock_hold(sk);
835+
nsk = dccp_check_req(sk, skb, req);
836836
if (!nsk) {
837837
reqsk_put(req);
838-
goto discard_it;
838+
goto discard_and_relse;
839839
}
840840
if (nsk == sk) {
841-
sock_hold(sk);
842841
reqsk_put(req);
843842
} else if (dccp_child_process(sk, nsk, skb)) {
844843
dccp_v4_ctl_send_reset(sk, skb);
845-
goto discard_it;
844+
goto discard_and_relse;
846845
} else {
846+
sock_put(sk);
847847
return 0;
848848
}
849849
}

net/dccp/ipv6.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -691,26 +691,26 @@ static int dccp_v6_rcv(struct sk_buff *skb)
691691

692692
if (sk->sk_state == DCCP_NEW_SYN_RECV) {
693693
struct request_sock *req = inet_reqsk(sk);
694-
struct sock *nsk = NULL;
694+
struct sock *nsk;
695695

696696
sk = req->rsk_listener;
697-
if (likely(sk->sk_state == DCCP_LISTEN)) {
698-
nsk = dccp_check_req(sk, skb, req);
699-
} else {
697+
if (unlikely(sk->sk_state != DCCP_LISTEN)) {
700698
inet_csk_reqsk_queue_drop_and_put(sk, req);
701699
goto lookup;
702700
}
701+
sock_hold(sk);
702+
nsk = dccp_check_req(sk, skb, req);
703703
if (!nsk) {
704704
reqsk_put(req);
705-
goto discard_it;
705+
goto discard_and_relse;
706706
}
707707
if (nsk == sk) {
708-
sock_hold(sk);
709708
reqsk_put(req);
710709
} else if (dccp_child_process(sk, nsk, skb)) {
711710
dccp_v6_ctl_send_reset(sk, skb);
712-
goto discard_it;
711+
goto discard_and_relse;
713712
} else {
713+
sock_put(sk);
714714
return 0;
715715
}
716716
}

net/ipv4/inet_connection_sock.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -789,14 +789,16 @@ static void inet_child_forget(struct sock *sk, struct request_sock *req,
789789
reqsk_put(req);
790790
}
791791

792-
void inet_csk_reqsk_queue_add(struct sock *sk, struct request_sock *req,
793-
struct sock *child)
792+
struct sock *inet_csk_reqsk_queue_add(struct sock *sk,
793+
struct request_sock *req,
794+
struct sock *child)
794795
{
795796
struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
796797

797798
spin_lock(&queue->rskq_lock);
798799
if (unlikely(sk->sk_state != TCP_LISTEN)) {
799800
inet_child_forget(sk, req, child);
801+
child = NULL;
800802
} else {
801803
req->sk = child;
802804
req->dl_next = NULL;
@@ -808,6 +810,7 @@ void inet_csk_reqsk_queue_add(struct sock *sk, struct request_sock *req,
808810
sk_acceptq_added(sk);
809811
}
810812
spin_unlock(&queue->rskq_lock);
813+
return child;
811814
}
812815
EXPORT_SYMBOL(inet_csk_reqsk_queue_add);
813816

@@ -817,11 +820,8 @@ struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,
817820
if (own_req) {
818821
inet_csk_reqsk_queue_drop(sk, req);
819822
reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req);
820-
inet_csk_reqsk_queue_add(sk, req, child);
821-
/* Warning: caller must not call reqsk_put(req);
822-
* child stole last reference on it.
823-
*/
824-
return child;
823+
if (inet_csk_reqsk_queue_add(sk, req, child))
824+
return child;
825825
}
826826
/* Too bad, another child took ownership of the request, undo. */
827827
bh_unlock_sock(child);

net/ipv4/tcp_ipv4.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,30 +1597,30 @@ int tcp_v4_rcv(struct sk_buff *skb)
15971597

15981598
if (sk->sk_state == TCP_NEW_SYN_RECV) {
15991599
struct request_sock *req = inet_reqsk(sk);
1600-
struct sock *nsk = NULL;
1600+
struct sock *nsk;
16011601

16021602
sk = req->rsk_listener;
16031603
if (unlikely(tcp_v4_inbound_md5_hash(sk, skb))) {
16041604
reqsk_put(req);
16051605
goto discard_it;
16061606
}
1607-
if (likely(sk->sk_state == TCP_LISTEN)) {
1608-
nsk = tcp_check_req(sk, skb, req, false);
1609-
} else {
1607+
if (unlikely(sk->sk_state != TCP_LISTEN)) {
16101608
inet_csk_reqsk_queue_drop_and_put(sk, req);
16111609
goto lookup;
16121610
}
1611+
sock_hold(sk);
1612+
nsk = tcp_check_req(sk, skb, req, false);
16131613
if (!nsk) {
16141614
reqsk_put(req);
1615-
goto discard_it;
1615+
goto discard_and_relse;
16161616
}
16171617
if (nsk == sk) {
1618-
sock_hold(sk);
16191618
reqsk_put(req);
16201619
} else if (tcp_child_process(sk, nsk, skb)) {
16211620
tcp_v4_send_reset(nsk, skb);
1622-
goto discard_it;
1621+
goto discard_and_relse;
16231622
} else {
1623+
sock_put(sk);
16241624
return 0;
16251625
}
16261626
}

net/ipv6/tcp_ipv6.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,32 +1387,32 @@ static int tcp_v6_rcv(struct sk_buff *skb)
13871387

13881388
if (sk->sk_state == TCP_NEW_SYN_RECV) {
13891389
struct request_sock *req = inet_reqsk(sk);
1390-
struct sock *nsk = NULL;
1390+
struct sock *nsk;
13911391

13921392
sk = req->rsk_listener;
13931393
tcp_v6_fill_cb(skb, hdr, th);
13941394
if (tcp_v6_inbound_md5_hash(sk, skb)) {
13951395
reqsk_put(req);
13961396
goto discard_it;
13971397
}
1398-
if (likely(sk->sk_state == TCP_LISTEN)) {
1399-
nsk = tcp_check_req(sk, skb, req, false);
1400-
} else {
1398+
if (unlikely(sk->sk_state != TCP_LISTEN)) {
14011399
inet_csk_reqsk_queue_drop_and_put(sk, req);
14021400
goto lookup;
14031401
}
1402+
sock_hold(sk);
1403+
nsk = tcp_check_req(sk, skb, req, false);
14041404
if (!nsk) {
14051405
reqsk_put(req);
1406-
goto discard_it;
1406+
goto discard_and_relse;
14071407
}
14081408
if (nsk == sk) {
1409-
sock_hold(sk);
14101409
reqsk_put(req);
14111410
tcp_v6_restore_cb(skb);
14121411
} else if (tcp_child_process(sk, nsk, skb)) {
14131412
tcp_v6_send_reset(nsk, skb);
1414-
goto discard_it;
1413+
goto discard_and_relse;
14151414
} else {
1415+
sock_put(sk);
14161416
return 0;
14171417
}
14181418
}

0 commit comments

Comments
 (0)