Skip to content

Commit 805c4bc

Browse files
edumazetdavem330
authored andcommitted
tcp: fix req->saved_syn race
For the reasons explained in commit ce10500 ("tcp/dccp: fix ireq->pktopts race"), we need to make sure we do not access req->saved_syn unless we own the request sock. This fixes races for listeners using TCP_SAVE_SYN option. Fixes: e994b2f ("tcp: do not lock listener to process SYN packets") Fixes: 079096f ("tcp/dccp: install syn_recv requests into ehash table") Signed-off-by: Eric Dumazet <[email protected]> Reported-by: Ying Cai <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 428ad1b commit 805c4bc

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

include/linux/tcp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,13 @@ static inline void fastopen_queue_tune(struct sock *sk, int backlog)
397397
queue->fastopenq.max_qlen = min_t(unsigned int, backlog, somaxconn);
398398
}
399399

400+
static inline void tcp_move_syn(struct tcp_sock *tp,
401+
struct request_sock *req)
402+
{
403+
tp->saved_syn = req->saved_syn;
404+
req->saved_syn = NULL;
405+
}
406+
400407
static inline void tcp_saved_syn_free(struct tcp_sock *tp)
401408
{
402409
kfree(tp->saved_syn);

net/ipv4/tcp_ipv4.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,8 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
13261326
if (__inet_inherit_port(sk, newsk) < 0)
13271327
goto put_and_exit;
13281328
*own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash));
1329+
if (*own_req)
1330+
tcp_move_syn(newtp, req_unhash);
13291331

13301332
return newsk;
13311333

net/ipv4/tcp_minisocks.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,6 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,
551551
newtp->rack.mstamp.v64 = 0;
552552
newtp->rack.advanced = 0;
553553

554-
newtp->saved_syn = req->saved_syn;
555-
req->saved_syn = NULL;
556-
557554
TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_PASSIVEOPENS);
558555
}
559556
return newsk;

net/ipv6/tcp_ipv6.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,14 +1140,18 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
11401140
goto out;
11411141
}
11421142
*own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash));
1143-
/* Clone pktoptions received with SYN, if we own the req */
1144-
if (*own_req && ireq->pktopts) {
1145-
newnp->pktoptions = skb_clone(ireq->pktopts,
1146-
sk_gfp_atomic(sk, GFP_ATOMIC));
1147-
consume_skb(ireq->pktopts);
1148-
ireq->pktopts = NULL;
1149-
if (newnp->pktoptions)
1150-
skb_set_owner_r(newnp->pktoptions, newsk);
1143+
if (*own_req) {
1144+
tcp_move_syn(newtp, req_unhash);
1145+
1146+
/* Clone pktoptions received with SYN, if we own the req */
1147+
if (ireq->pktopts) {
1148+
newnp->pktoptions = skb_clone(ireq->pktopts,
1149+
sk_gfp_atomic(sk, GFP_ATOMIC));
1150+
consume_skb(ireq->pktopts);
1151+
ireq->pktopts = NULL;
1152+
if (newnp->pktoptions)
1153+
skb_set_owner_r(newnp->pktoptions, newsk);
1154+
}
11511155
}
11521156

11531157
return newsk;

0 commit comments

Comments
 (0)