Skip to content

Commit 4c8941d

Browse files
Paolo Abenidavem330
authored andcommitted
mptcp: avoid flipping mp_capable field in syn_recv_sock()
If multiple CPUs races on the same req_sock in syn_recv_sock(), flipping such field can cause inconsistent child socket status. When racing, the CPU losing the req ownership may still change the mptcp request socket mp_capable flag while the CPU owning the request is cloning the socket, leaving the child socket with 'is_mptcp' set but no 'mp_capable' flag. Such socket will stay with 'conn' field cleared, heading to oops in later mptcp callback. Address the issue tracking the fallback status in a local variable. Fixes: 58b0991 ("mptcp: create msk early") Co-developed-by: Florian Westphal <[email protected]> Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Paolo Abeni <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 5e20087 commit 4c8941d

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

net/mptcp/subflow.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,17 @@ static void mptcp_force_close(struct sock *sk)
376376
sk_common_release(sk);
377377
}
378378

379+
static void subflow_ulp_fallback(struct sock *sk,
380+
struct mptcp_subflow_context *old_ctx)
381+
{
382+
struct inet_connection_sock *icsk = inet_csk(sk);
383+
384+
mptcp_subflow_tcp_fallback(sk, old_ctx);
385+
icsk->icsk_ulp_ops = NULL;
386+
rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
387+
tcp_sk(sk)->is_mptcp = 0;
388+
}
389+
379390
static struct sock *subflow_syn_recv_sock(const struct sock *sk,
380391
struct sk_buff *skb,
381392
struct request_sock *req,
@@ -388,6 +399,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
388399
struct tcp_options_received opt_rx;
389400
bool fallback_is_fatal = false;
390401
struct sock *new_msk = NULL;
402+
bool fallback = false;
391403
struct sock *child;
392404

393405
pr_debug("listener=%p, req=%p, conn=%p", listener, req, listener->conn);
@@ -412,14 +424,14 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
412424
subflow_req->remote_key = opt_rx.mptcp.sndr_key;
413425
subflow_req->remote_key_valid = 1;
414426
} else {
415-
subflow_req->mp_capable = 0;
427+
fallback = true;
416428
goto create_child;
417429
}
418430

419431
create_msk:
420432
new_msk = mptcp_sk_clone(listener->conn, req);
421433
if (!new_msk)
422-
subflow_req->mp_capable = 0;
434+
fallback = true;
423435
} else if (subflow_req->mp_join) {
424436
fallback_is_fatal = true;
425437
opt_rx.mptcp.mp_join = 0;
@@ -438,12 +450,18 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
438450
if (child && *own_req) {
439451
struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
440452

441-
/* we have null ctx on TCP fallback, which is fatal on
442-
* MPJ handshake
453+
/* we need to fallback on ctx allocation failure and on pre-reqs
454+
* checking above. In the latter scenario we additionally need
455+
* to reset the context to non MPTCP status.
443456
*/
444-
if (!ctx) {
457+
if (!ctx || fallback) {
445458
if (fallback_is_fatal)
446459
goto close_child;
460+
461+
if (ctx) {
462+
subflow_ulp_fallback(child, ctx);
463+
kfree_rcu(ctx, rcu);
464+
}
447465
goto out;
448466
}
449467

@@ -474,6 +492,13 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
474492
/* dispose of the left over mptcp master, if any */
475493
if (unlikely(new_msk))
476494
mptcp_force_close(new_msk);
495+
496+
/* check for expected invariant - should never trigger, just help
497+
* catching eariler subtle bugs
498+
*/
499+
WARN_ON_ONCE(*own_req && child && tcp_sk(child)->is_mptcp &&
500+
(!mptcp_subflow_ctx(child) ||
501+
!mptcp_subflow_ctx(child)->conn));
477502
return child;
478503

479504
close_child:
@@ -1076,17 +1101,6 @@ static void subflow_ulp_release(struct sock *sk)
10761101
kfree_rcu(ctx, rcu);
10771102
}
10781103

1079-
static void subflow_ulp_fallback(struct sock *sk,
1080-
struct mptcp_subflow_context *old_ctx)
1081-
{
1082-
struct inet_connection_sock *icsk = inet_csk(sk);
1083-
1084-
mptcp_subflow_tcp_fallback(sk, old_ctx);
1085-
icsk->icsk_ulp_ops = NULL;
1086-
rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
1087-
tcp_sk(sk)->is_mptcp = 0;
1088-
}
1089-
10901104
static void subflow_ulp_clone(const struct request_sock *req,
10911105
struct sock *newsk,
10921106
const gfp_t priority)

0 commit comments

Comments
 (0)