Reduce the differences between TCP and MPTCP for TLS usage
Subsystems (2)
Geliang Tang
reviewerv17 cover Reduce the differences between TCP and MPTCP for TLS usage
From: Geliang Tang <[email protected]>
v17:
- Patch 2: add TFO synchronization: subflow_set_remote_key() preserves partial read progress by checking copied_seq against the initial TFO value before updating. Add WRITE_ONCE/READ_ONCE for ordering between BH (3rd ACK processing) and user-space readers. Add underflow guard (copied_seq >= map_seq) before offset subtraction in __mptcp_recvmsg_mskq() to protect MSG_PEEK traversals. Skip skbs entirely before copied_seq. Update subflow.c to set both map_seq (u32) and map_seq64 (u64) for TFO.
- Patch 3: adapt to the new map_seq (u32) + map_seq64 (u64) layout introduced in patch 2.
v16:
- Simplified the series to retain only the core changes required by both
kTLS and sockmap: drop the CB offset field, add copied_seq tracking,
and sync the MPTCP skb CB layout with the TCP one.
- Dropped patch 2, leaving the fastopen code unchanged.
- Moved patches 6-11 into a separate series "prepare interfaces for kTLS
and sockmap support".
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v15:
- Patch 2: drop mptcp_data_lock() in mptcp_inq_hint(), use READ_ONCE()
and __test_and_clear_bit() instead. This issue is unrelated to this
series and will be addressed in a separate patch later.
- Patch 5: Guard mptcp_cleanup_rbuf() with read_copied > 0. Initialize/
reset msk->read_copied. Drop redundant mptcp_recv_skb() in __mptcp_read_sock().
- Patch 6: simplify memory ordering, drop smp_mb()/smp_rmb(), keep
existing smp_mb__before_atomic(). Check SOCK_DONE inline.
- Patch 9: drop tcp_sock_rate_check_app_limited() split, call tcp_rate_check_app_limited() directly as Paolo suggested. Add
write_seq != snd_nxt early bail-out.
- Patch 10: wrap assertion behind expect_all_features().
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v14:
- v13 can no longer be applied to the export branch, so it has been
rebased here with the following corrections:
- Patch 2: move locking in mptcp_inq_hint() to avoid races with ack_seq
change from patch 3 to here. Update commit log.
- Patch 3: add mptcp_data_lock() in __mptcp_move_skb() around copied_seq update. Remove local delta variable in __mptcp_ofo_queue(). Initialize msk->copied_seq to 0 in mptcp_propagate_state() fallback path.
- Patch 4: add map_seq64 comparison in __mptcp_try_coalesce() for 64-bit sequence consistency.
- New patch 11: rename mptcp_check_readable to mptcp_stream_is_readable,
and align with TCP by calling sk_is_readable() for KTLS support.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v13:
- use smp_mb instead of smp_mb__before_atomic in mptcp_check_data_fin.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v12:
- Move sock_set_flag(SOCK_DONE) to after smp_mb__before_atomic() in mptcp_check_data_fin() and reuse the existing barrier rather than adding
a new smp_wmb().
- Sample SOCK_DONE before smp_rmb() in mptcp_inq_hint() and mptcp_inq(),
so the smp_rmb() only orders SOCK_DONE read relative to the subsequent ack_seq/copied_seq reads.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v11:
- __mptcp_move_skb(): gate __mptcp_sync_rcv_sequence() on test_and_clear_bit(MPTCP_SYNC_SEQ) so the dummy mapping is only re-based after ack_seq = iasn.
- a new patch to set and test SOCK_DONE.
- use SOCK_DONE in mptcp_inq_hint() and mptcp_inq().
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v10:
- Drop "mptcp: use atomic64_t for locklessly accessed u64 fields"
(v9 patch 1) and the squash to bpf_burst test (v9 patch 11).
Series reduced from 11 to 9 patches.
- Revert all atomic64_read()/atomic64_set() on msk->ack_seq back to
plain reads and WRITE_ONCE() in patches 1-4. Remove the local u64
ack_seq variables that only existed to hold atomic64_read() results.
- mptcp_inq_hint: move test_bit(MPTCP_SYNC_SEQ) inside mptcp_data_lock()
to close the race between the flag check and the ack_seq read. Revert atomic64_read() to plain msk->ack_seq under the lock.
- mptcp_inq (patch 6): use READ_ONCE() for both ack_seq and copied_seq
instead of atomic64_read(). Add comment for the FIN subtraction,
mirroring tcp_inq(). Update commit log.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v9:
- Extend atomic64_t conversion from only msk->ack_seq to all locklessly accessed u64 fields: write_seq, snd_nxt, snd_una, wnd_end,
bytes_received, bytes_consumed.
- Add mptcp_data_lock() in mptcp_inq_hint() to read ack_seq and
copied_seq atomically.
- Add read_copied field and drain it under mptcp_data_lock() in mptcp_read_complete() to fix the BH vs worker data race.
- New patches: mptcp_inq/peek_len and sendmsg_locked proto_ops.
- Squash to "selftests/bpf: Add bpf_burst scheduler & test".
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v8:
- check MPTCP_SYNC_SEQ in mptcp_inq_hint:
if (hint_val >= INT_MAX) {
if (test_bit(MPTCP_SYNC_SEQ, &msk->cb_flags))
return 0;
return INT_MAX;
} - rename mptcp_sock_rate_check_app_limited to
mptcp_rate_check_app_limited.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v7:
- Patch 1 is a new one to fix the existing issue Sashiko mentioned -
using atomic64_t for msk->ack_seq.
- Fix the map_subflow_seq setting in fallback mode in patch 4.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v6:
- Memory ordering for MPTCP_SYNC_SEQ: Add smp_wmb() before set_bit() in subflow_set_remote_key() and smp_rmb() after test_and_clear_bit() in __mptcp_move_skb() and mptcp_release_cb() to prevent data races on weakly-ordered architectures, ensuring ack_seq updates are visible
before the flag is set and readers see the updated sequence after
observing the flag.
- Lockdep nested locking: Use lock_sock_fast_nested(ssk) instead of lock_sock_fast(ssk) in mptcp_sock_rate_check_app_limited() to suppress
false positive recursive locking warnings when acquiring subflow socket
locks while holding the parent MPTCP socket lock.
- Receive window advertisement order: Swap mptcp_rcv_space_adjust() and mptcp_cleanup_rbuf() in mptcp_read_complete() to expand the receive
buffer before evaluating window updates, ensuring ACKs advertise the
new (larger) window size instead of delaying the advertisement until
the next cycle.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v5:
- only patch 3 changed.
- Fallback offset underflow: Initialize MPTCP sequence space to 0 in mptcp_propagate_state() when mp_opt == NULL, ensuring SKB's map_seq
starts from 0 to match msk->copied_seq and prevent offset calculation underflow in fallback mode.
- Stale peek_seq: Move peek_seq recomputation into the mptcp_move_skbs() continue path and add another recomputation after sk_wait_data(),
ensuring peek_seq stays synchronized with msk->copied_seq updates from MPTCP_SYNC_SEQ processing in both paths.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v4:
- mptcp_recvmsg: move the peek_seq recompute from after sk_wait_data() to before the mptcp_move_skbs() continue check. mptcp_move_skbs() -> __mptcp_move_skb() may shift the head skb's map_seq to the IASN frame via __mptcp_sync_rcv_sequence(); if 'continue' then exits the loop, the recompute is skipped, and the next __mptcp_recvmsg_mskq() computes offset with peek_seq in the old frame minus map_seq in the new frame. The underflow makes the offset test fail and the skb is skipped (and mptcp_recv_skb() actually frees the TFO skb via mptcp_eat_recv_skb()).
- Make all three MPTCP_SYNC_SEQ bit accesses atomic:
- subflow_set_remote_key: __set_bit -> set_bit - __mptcp_move_skb: __test_and_clear_bit -> test_and_clear_bit - mptcp_release_cb: __test_and_clear_bit -> test_and_clear_bit
The flag is set in BH under mptcp_data_lock() (msk slock held) and
cleared in user context under lock_sock() slow path (only owned=1,
slock not held). The non-atomic RMW on the two sides races, which can
lose the IASN sync.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v3:
- Patch 2, handle "offset" in __mptcp_sync_rcv_sequence().
- Patch 3, update __mptcp_move_skb() in response to Sashiko comments:
if (__test_and_clear_bit(MPTCP_SYNC_SEQ, &msk->cb_flags))
msk->copied_seq += mptcp_iasn(msk);
- Patch 4, replace after64() to after() in mptcp_prune_ofo_queue(). The pre-existing issue raised by Sashiko regarding changing the type of
ack_seq to atomic64_t is not addressed in this series.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v2:
- Patch 3, updated in response to Sashiko comments:
if (unlikely(msk->rcvd_dummy_seq)) {
msk->copied_seq += mptcp_iasn(msk);
__mptcp_sync_rcv_sequence(sk);
/* Release cb() would otherwise re-base copied_seq
* again.
*/
test_and_clear_bit(MPTCP_SYNC_SEQ, &msk->cb_flags);
}
/* Skip the already peeked data. */
if (offset >= skb->len) {
*last = skb;
continue;
} - Patch 5, replaced with Paolo's patch.
- Patch 6, 7, new patches addressing app-limited conditions.
- The patch "trim the duplicated skb head at receive enqueue" has been
dropped, as it is no longer needed after Paolo updated the "mptcp:
out-of-order queue pruning" series.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v1:
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
The goal of this series is to reduce the differences between TCP and MPTCP for TLS usage, in preparation for adding TLS over MPTCP support in the future.
In previous versions [1], a struct tls_prot_ops was defined to represent the interface differences between TCP and MPTCP, which contained the following callbacks:
struct sk_buff *(*recv_skb)(struct sock *sk, u32 *off); bool (*lock_is_held)(struct sock *sk); void (*read_done)(struct sock *sk, size_t len); u32 (*get_skb_seq)(struct sk_buff *skb); int (*skb_get_header)(const struct sk_buff *skb, int offset, void *to, int len); bool (*epollin_ready)(const struct sock *sk); void (*check_app_limited)(struct sock *sk);
In reality, some of these callbacks are unnecessary. This series aims to eliminate the get_skb_seq(), skb_get_header(), and lock_is_held() callbacks.
The first four patches come from Paolo's "mptcp: address stall under memory pressure" series v5 [2], with only minor cleanup from my side.
They remove the CB offset field and sync the MPTCP skb CB layout with the TCP one, so that we can obtain the TCP or MPTCP sequence number in a unified way, e.g.:
struct tls_skb_cb {
u32 seq;
};
#define TLS_SKB_CB(__skb) ((struct tls_skb_cb *)&((__skb)->cb[0]))This eliminates the need for a separate get_skb_seq() callback.
Building on the removal of the CB offset field, I also added patch 5 that trims the duplicated skb head at receive enqueue. With that in place, KTLS can retrieve the record header via skb_copy_bits() directly, so there is no longer any need for a dedicated MPTCP helper like mptcp_skb_get_header(). The skb_get_header() callback can thus be removed.
Patch 6 defers sk_data_ready to the worker, which avoids recursive locking when TLS calls back into MPTCP under mptcp_data_lock(). With this change, the lock_is_held() callback is no longer needed and can be removed.
[1]
https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
[2]
https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
Paolo Abeni (4):
mptcp: drop the mptcp_ooo_try_coalesce() helper
mptcp: remove CB offset field
mptcp: sync mptcp skb cb layout with tcp one
mptcp: defer read_sock cleanup to mptcp_worker
net/mptcp/fastopen.c | 14 +++- net/mptcp/protocol.c | 193 +++++++++++++++++++++++++------------------ net/mptcp/protocol.h | 11 ++- net/mptcp/subflow.c | 23 +++++- 4 files changed, 151 insertions(+), 90 deletions(-)
Signature · 1 line
2.55.0
Geliang Tang
reviewerPATCH v17 1/4 mptcp: drop the mptcp_ooo_try_coalesce() helper
From: Paolo Abeni <[email protected]>
It's used to save an additional comparison for in-order skbs, but is also a barrier to remove CB offset. Remove the helper, let __mptcp_try_coalesce() always perform the sequence check and remove duplicate checks from the callers.
--- net/mptcp/protocol.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-)
net/mptcp/protocol.c +6 -15
@@ -167,7 +167,8 @@ static bool __mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, {int limit =READ_ONCE (sk->sk_rcvbuf); -if (unlikely (MPTCP_SKB_CB (to)->cant_coalesce) || +if (MPTCP_SKB_CB (from)->map_seq !=MPTCP_SKB_CB (to)->end_seq || +unlikely (MPTCP_SKB_CB (to)->cant_coalesce) ||MPTCP_SKB_CB (from)->offset || ((to->len + from->len) > (limit >>3 )) || !skb_try_coalesce (to, from, fragstolen, delta))
@@ -200,15 +201,6 @@ static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,return true; } -static bool mptcp_ooo_try_coalesce (struct mptcp_sock *msk,struct sk_buff *to, -struct sk_buff *from) -{ -if (MPTCP_SKB_CB (from)->map_seq !=MPTCP_SKB_CB (to)->end_seq) -return false; - -return mptcp_try_coalesce ((struct sock *)msk, to, from); -} -/* "inspired" by tcp_rcvbuf_grow(), main difference: * - mptcp does not maintain a msk-level window clamp * - returns true when the receive buffer is actually updated
@@ -348,7 +340,7 @@ static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)/* with 2 subflows, adding at end of ooo queue is quite likely * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup. */ -if (mptcp_ooo_try_coalesce (msk, msk->ooo_last_skb, skb)) { +if (mptcp_try_coalesce (sk, msk->ooo_last_skb, skb)) {MPTCP_INC_STATS (sock_net (sk),MPTCP_MIB_OFOMERGE );MPTCP_INC_STATS (sock_net (sk),MPTCP_MIB_OFOQUEUETAIL );return ;
@@ -394,7 +386,7 @@ static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)MPTCP_INC_STATS (sock_net (sk),MPTCP_MIB_DUPDATA ); goto merge_right; } - }else if (mptcp_ooo_try_coalesce (msk, skb1, skb)) { + }else if (mptcp_try_coalesce (sk, skb1, skb)) {MPTCP_INC_STATS (sock_net (sk),MPTCP_MIB_OFOMERGE );return ; }
@@ -777,8 +769,7 @@ static void __mptcp_add_backlog(struct sock *sk,if (!list_empty (&msk->backlog_list)) tail = list_last_entry(&msk->backlog_list, struct sk_buff, list); -if (tail &&MPTCP_SKB_CB (skb)->map_seq ==MPTCP_SKB_CB (tail)->end_seq && - ssk == tail->sk && +if (tail && ssk == tail->sk &&__mptcp_try_coalesce (sk, tail, skb, &fragstolen, &delta)) { skb->truesize -= delta;kfree_skb_partial (skb, fragstolen);
@@ -902,7 +893,7 @@ static bool __mptcp_ofo_queue(struct mptcp_sock *msk) end_seq =MPTCP_SKB_CB (skb)->end_seq; tail =skb_peek_tail (&sk->sk_receive_queue); -if (!tail || !mptcp_ooo_try_coalesce (msk, tail, skb)) { +if (!tail || !mptcp_try_coalesce (sk, tail, skb)) {int delta = msk->ack_seq -MPTCP_SKB_CB (skb)->map_seq;/* skip overlapping data, if any */
Signature · 1 line
2.55.0
Geliang Tang
reviewerPATCH v17 2/4 mptcp: remove CB offset field
In reply to Geliang TangFrom: Paolo Abeni <[email protected]>
Instead, use a new msk-level copied_seq field to track the bytes already consumed from the MPTCP receive queue, carrying the amount of bytes already copied to user-space, alike what TCP is already doing.
The newly introduced copied_seq field (u64) is always accessed under the msk socket lock. For TFO synchronization, subflow_set_remote_key() checks if copied_seq still equals the initial TFO value (-(u64)skb->len) before updating it. If user-space has already partially read the TFO data, the progress is preserved. WRITE_ONCE/READ_ONCE ensure proper memory ordering between BH context (3rd ACK processing) and user-space readers.
Since both map_seq and copied_seq are u64, the offset calculation (copied_seq - map_seq) is straightforward. We check copied_seq >= map_seq before the subtraction to prevent underflow, which is critical for MSG_PEEK operations that traverse multiple skbs without advancing copied_seq.
Prior to this patch, the TFO skb dummy mapping (map_seq = -(u32)skb->len, zero-extended to u64) caused mptcp_inq_hint() to compute a wrapped ack_seq - map_seq, so SIOCINQ and TCP_CM_INQ returned INT_MAX before the MPC ACK. This patch fixes that behaviour through both the -(u64)skb->len sign fix and the new ack_seq - copied_seq hint.
Overall this simplifies __mptcp_recvmsg_mskq(), __mptcp_move_skb(), and __mptcp_read_sock() by removing per-skb offset tracking and will also make possible the next patch.
--- net/mptcp/fastopen.c | 12 ++++++---- net/mptcp/protocol.c | 53 ++++++++++++++++++++++---------------------- net/mptcp/protocol.h | 2 +- net/mptcp/subflow.c | 22 +++++++++++++++++- 4 files changed, 56 insertions(+), 33 deletions(-)
net/mptcp/fastopen.c +8 -4
@@ -9,6 +9,7 @@void mptcp_fastopen_subflow_synack_set_params (struct mptcp_subflow_context *subflow,struct request_sock *req) { +struct mptcp_sock *msk;struct sock *sk, *ssk;struct sk_buff *skb;struct tcp_sock *tp;
@@ -44,20 +45,23 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf subflow->ssn_offset += skb->len; has_rxtstamp =TCP_SKB_CB (skb)->has_rxtstamp; -/* Only the sequence delta is relevant */ -MPTCP_SKB_CB (skb)->map_seq = -skb->len; +/* The TFO segment data sits before the IASN; before receiving +* the remote key, IASN is assumed being 0. +*/ +MPTCP_SKB_CB (skb)->map_seq = -(u64 )skb->len;MPTCP_SKB_CB (skb)->end_seq =0 ; -MPTCP_SKB_CB (skb)->offset =0 ;MPTCP_SKB_CB (skb)->has_rxtstamp = has_rxtstamp;MPTCP_SKB_CB (skb)->cant_coalesce =1 ;mptcp_data_lock (sk);DEBUG_NET_WARN_ON_ONCE (sock_owned_by_user_nocheck (sk)); + msk =mptcp_sk (sk); + msk->copied_seq =MPTCP_SKB_CB (skb)->map_seq;mptcp_borrow_fwdmem (sk, skb);skb_set_owner_r (skb, sk);__skb_queue_tail (&sk->sk_receive_queue, skb); -mptcp_sk (sk)->bytes_received += skb->len; + msk->bytes_received += skb->len; sk->sk_data_ready (sk);
net/mptcp/protocol.c +26 -27
136 lines not on this page · raw message
net/mptcp/protocol.h +1 -1
@@ -130,7 +130,6 @@struct mptcp_skb_cb {u64 map_seq;u64 end_seq; -u32 offset;u8 has_rxtstamp;u8 cant_coalesce; };
@@ -311,6 +310,7 @@ struct mptcp_sock {u64 rcv_data_fin_seq;u64 bytes_retrans;u64 bytes_consumed; +u64 copied_seq;int snd_burst;int old_wspace;u64 recovery_snd_nxt;/* in recovery mode accept up to this seq;
net/mptcp/subflow.c +21 -1
@@ -533,6 +533,26 @@ static void subflow_set_remote_key(struct mptcp_sock *msk,WRITE_ONCE (msk->ack_seq, subflow->iasn);WRITE_ONCE (msk->can_ack, true);atomic64_set (&msk->rcv_wnd_sent, subflow->iasn); + +if (subflow->is_mptfo) { +struct sock *sk = (struct sock *)msk; +struct sk_buff *skb; + + skb =skb_peek (&sk->sk_receive_queue); +if (skb) { +u64 tfo_map_seq = subflow->iasn - skb->len; + +WRITE_ONCE (MPTCP_SKB_CB (skb)->map_seq, tfo_map_seq); +WRITE_ONCE (MPTCP_SKB_CB (skb)->end_seq, subflow->iasn); + +if (READ_ONCE (msk->copied_seq) != -(u64 )skb->len) +return ; + +WRITE_ONCE (msk->copied_seq, tfo_map_seq); +return ; + } + } +WRITE_ONCE (msk->copied_seq, subflow->iasn); } static void mptcp_propagate_state(struct sock *sk, struct sock *ssk,
@@ -1485,7 +1505,7 @@ static bool subflow_check_data_avail(struct sock *ssk) skb =skb_peek (&ssk->sk_receive_queue); subflow->map_valid =1 ; subflow->map_data_len = skb->len; - subflow->map_subflow_seq =tcp_sk (ssk)->copied_seq - subflow->ssn_offset; + subflow->map_subflow_seq =TCP_SKB_CB (skb)->seq - subflow->ssn_offset; subflow->map_seq = __mptcp_expand_seq(subflow->map_seq, subflow->iasn + TCP_SKB_CB(skb)->seq -
Signature · 1 line
2.55.0
Geliang Tang
reviewerPATCH v17 3/4 mptcp: sync mptcp skb cb layout with tcp one
In reply to Geliang TangFrom: Paolo Abeni <[email protected]>
The MPTCP protocol uses a significantly different CB layout WRT TCP, as it includes different information and use 64 bits for the sequence numbers.
As the msk-level rcvbuf buffer size is limited by the core socket code the INT_MAX; after validating the incoming skb vs the current receive window, we can safely use 32 bits for MPTCP-level sequence number. This allow updating the MPTCP CB layout so that fields with a corresponding TCP-level data use the same area inside the CB itself.
The 32-bit sequence numbers are safe because all SKBs in the receive queue and OFO queue have been validated against the receive window (max INT_MAX), so their sequence numbers are guaranteed to be within 2^31 of the current ack_seq or copied_seq. This ensures that 32-bit arithmetic (comparisons and subtractions) produces correct results even when 64-bit values cross the 2^32 boundary, as the relative distance remains small.
Add build time check to ensure the latter invariant.
--- net/mptcp/fastopen.c | 6 ++- net/mptcp/protocol.c | 90 +++++++++++++++++++++++++++----------------- net/mptcp/protocol.h | 7 +++- net/mptcp/subflow.c | 3 +- 4 files changed, 66 insertions(+), 40 deletions(-)
net/mptcp/fastopen.c +4 -2
@@ -48,8 +48,10 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf/* The TFO segment data sits before the IASN; before receiving * the remote key, IASN is assumed being 0. */ -MPTCP_SKB_CB (skb)->map_seq = -(u64 )skb->len; +MPTCP_SKB_CB (skb)->map_seq64 = -(u64 )skb->len; +MPTCP_SKB_CB (skb)->map_seq =MPTCP_SKB_CB (skb)->map_seq64;MPTCP_SKB_CB (skb)->end_seq =0 ; +MPTCP_SKB_CB (skb)->flags =0 ;MPTCP_SKB_CB (skb)->has_rxtstamp = has_rxtstamp;MPTCP_SKB_CB (skb)->cant_coalesce =1 ;
@@ -57,7 +59,7 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subfDEBUG_NET_WARN_ON_ONCE (sock_owned_by_user_nocheck (sk)); msk =mptcp_sk (sk); - msk->copied_seq =MPTCP_SKB_CB (skb)->map_seq; + msk->copied_seq =MPTCP_SKB_CB (skb)->map_seq64;mptcp_borrow_fwdmem (sk, skb);skb_set_owner_r (skb, sk);__skb_queue_tail (&sk->sk_receive_queue, skb);
net/mptcp/protocol.c +55 -35
225 lines not on this page · raw message
net/mptcp/protocol.h +5 -2
@@ -128,10 +128,13 @@#define MPTCP_SYNC_SNDBUF 7struct mptcp_skb_cb { -u64 map_seq; -u64 end_seq; +u32 map_seq; +u32 end_seq; +u32 unused; +u16 flags;u8 has_rxtstamp;u8 cant_coalesce; +u64 map_seq64; };#define MPTCP_SKB_CB (__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0]))
net/mptcp/subflow.c +2 -1
@@ -542,7 +542,8 @@ static void subflow_set_remote_key(struct mptcp_sock *msk,if (skb) {u64 tfo_map_seq = subflow->iasn - skb->len; -WRITE_ONCE (MPTCP_SKB_CB (skb)->map_seq, tfo_map_seq); +WRITE_ONCE (MPTCP_SKB_CB (skb)->map_seq, (u32 )tfo_map_seq); +WRITE_ONCE (MPTCP_SKB_CB (skb)->map_seq64, tfo_map_seq);WRITE_ONCE (MPTCP_SKB_CB (skb)->end_seq, subflow->iasn);if (READ_ONCE (msk->copied_seq) != -(u64 )skb->len)
Signature · 1 line
2.55.0
Geliang Tang
reviewerPATCH v17 4/4 mptcp: defer read_sock cleanup to mptcp_worker
In reply to Geliang TangFrom: Paolo Abeni <[email protected]>
When MPTCP carries TLS, __mptcp_read_sock() is reached from BH context (via tls_strp_read_copyin() -> sk->sk_read_sock()), where calling mptcp_rcv_space_adjust() and mptcp_cleanup_rbuf() is not safe as both expect process context.
Move both calls to mptcp_worker(): __mptcp_read_sock() accumulates the consumed bytes in msk->read_copied and signals the worker through a new MPTCP_WORK_READ_COMPLETE bit. The worker drains the accumulator under mptcp_data_lock() and runs rcvbuf autotuning and RBU update in process context.
Also remove the backlog flush from mptcp_recv_skb() (covered by mptcp_release_cb()), drop the msk_owned_by_me() assertion (BH caller does not own the msk lock), and drop the redundant mptcp_recv_skb() call after the main loop.
Initialize msk->read_copied in __mptcp_init_sock() and reset it in mptcp_disconnect().
--- net/mptcp/protocol.c | 37 ++++++++++++++++++++++++++++--------- net/mptcp/protocol.h | 2 ++ 2 files changed, 30 insertions(+), 9 deletions(-)
net/mptcp/protocol.c +28 -9
@@ -3125,6 +3125,21 @@ static void mptcp_backlog_purge(struct sock *sk)sk_mem_reclaim (sk); } +static void mptcp_read_complete (struct sock *sk) +{ +struct mptcp_sock *msk =mptcp_sk (sk); +int read_copied; + +mptcp_data_lock (sk); + read_copied = msk->read_copied; + msk->read_copied =0 ; +mptcp_data_unlock (sk); + +mptcp_rcv_space_adjust (msk, read_copied); +if (read_copied >0 ) +mptcp_cleanup_rbuf (msk, read_copied); +} +static void mptcp_do_fastclose (struct sock *sk) {struct mptcp_subflow_context *subflow, *tmp;
@@ -3200,6 +3215,9 @@ static void mptcp_worker(struct work_struct *work)if (test_and_clear_bit (MPTCP_WORK_RTX , &msk->flags))__mptcp_retrans (sk); +if (test_and_clear_bit (MPTCP_WORK_READ_COMPLETE , &msk->flags)) +mptcp_read_complete (sk); + fail_tout = msk->first ?READ_ONCE (mptcp_subflow_ctx (msk->first)->fail_tout) :0 ;if (fail_tout &&time_after (jiffies, fail_tout))mptcp_mp_fail_no_response (msk);
@@ -3243,6 +3261,7 @@ static void __mptcp_init_sock(struct sock *sk) msk->last_data_sent = tcp_jiffies32; msk->last_data_recv = tcp_jiffies32; msk->last_ack_recv = tcp_jiffies32; + msk->read_copied =0 ;mptcp_pm_data_init (msk);spin_lock_init (&msk->fallback_lock);
@@ -3688,6 +3707,7 @@ static int mptcp_disconnect(struct sock *sk, int flags) msk->bytes_retrans =0 ; msk->rcvspace_init =0 ; msk->fastclosing =0 ; + msk->read_copied =0 ;mptcp_init_rtt_est (msk);/* for fallback's sake */
@@ -4605,9 +4625,6 @@ static struct sk_buff *mptcp_recv_skb(struct sock *sk, u32 *off)struct sk_buff *skb;u32 offset; -if (!list_empty (&msk->backlog_list)) -mptcp_move_skbs (sk); -while ((skb =skb_peek (&sk->sk_receive_queue)) !=NULL ) { offset = (u32 )msk->copied_seq -MPTCP_SKB_CB (skb)->map_seq;
@@ -4623,6 +4640,7 @@ static struct sk_buff *mptcp_recv_skb(struct sock *sk, u32 *off)/* * Note: * - It is assumed that the socket was locked by the caller. +* - Can be invoked in BH scope. */ static int __mptcp_read_sock (struct sock *sk,read_descriptor_t *desc,sk_read_actor_t recv_actor,bool noack)
@@ -4632,8 +4650,6 @@ static int __mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,int copied =0 ;u32 offset; -msk_owned_by_me (msk); -if (sk->sk_state ==TCP_LISTEN )return -ENOTCONN ;while ((skb =mptcp_recv_skb (sk, &offset)) !=NULL ) {
@@ -4664,11 +4680,14 @@ static int __mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,if (noack) goto out; -mptcp_rcv_space_adjust (msk, copied); - +/* The backlog flushing is only needed when some data is actually +* moved and will take place in mptcp_release_cb() via +* release_sock() in the worker. +*/ if (copied >0 ) { -mptcp_recv_skb (sk, &offset); -mptcp_cleanup_rbuf (msk, copied); + msk->read_copied += copied; +set_bit (MPTCP_WORK_READ_COMPLETE , &msk->flags); +mptcp_schedule_work (sk); } out:return copied;
net/mptcp/protocol.h +2 -0
@@ -117,6 +117,7 @@#define MPTCP_FALLBACK_DONE 2#define MPTCP_WORK_CLOSE_SUBFLOW 3#define MPTCP_RTX_ENABLED 4 +#define MPTCP_WORK_READ_COMPLETE 5/* MPTCP socket release cb flags */ #define MPTCP_PUSH_PENDING 1
@@ -326,6 +327,7 @@ struct mptcp_sock {u32 last_data_sent;u32 last_data_recv;u32 last_ack_recv; +int read_copied;unsigned long timer_ival;u32 token;unsigned long flags;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH mptcp-next v17 3/4] mptcp: sync mptcp skb cb layout with tcp one
In reply to Geliang TangThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] mptcp: TFO skb UAF race and un-rebased copied_seq in subflow_set_remote_key() - [Critical]…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] mptcp: TFO skb UAF race and un-rebased copied_seq in subflow_set_remote_key()
- [Critical] mptcp: subtracting offset from map_seq64 in mptcp_init_skb() corrupts OFO data
- [High] mptcp: 64-bit vs 32-bit seq check in __mptcp_recvmsg_mskq() breaks MSG_PEEK --
commit 62db3c701d55a6fd52b9c1e97a8f06064e99fb14
Author: Paolo Abeni <[email protected]>
mptcp: sync mptcp skb cb layout with tcp one
This commit aligns the MPTCP SKB control buffer layout with TCP by narrowing MPTCP_SKB_CB(skb)->map_seq and end_seq to 32-bit fields while keeping the full 64-bit sequence number in map_seq64. It updates the receive and out-of-order queue paths to use 32-bit sequence comparisons after validating incoming packets against the receive window.
Quoted 4 lines
net/mptcp/protocol.c +0
-0
[ ... ]
net/mptcp/protocol.c static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset) · line 437
Quoted 13 lines
net/mptcp/protocol.c +4
-3@@ -422,11 +429,13 @@ static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset)/* the skb map_seq accounts for the skb offset: * mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq -* value +* value; note that end seq number is only available in 32bits format. */ -MPTCP_SKB_CB (skb)->map_seq =mptcp_subflow_get_mapped_dsn (subflow) - - offset; +MPTCP_SKB_CB (skb)->map_seq64 =mptcp_subflow_get_mapped_dsn (subflow) - + offset; +MPTCP_SKB_CB (skb)->map_seq = (u32 )MPTCP_SKB_CB (skb)->map_seq64;MPTCP_SKB_CB (skb)->end_seq =MPTCP_SKB_CB (skb)->map_seq + skb->len;
[Severity: Critical]
Can subtracting offset from map_seq64 in mptcp_init_skb() corrupt stream data when the subflow SKB has a non-zero offset and carries an out-of-order MPTCP Data Sequence Mapping?
In __mptcp_move_skbs_from_subflow(), offset = tp->copied_seq - TCP_SKB_CB(skb)->seq represents already-consumed or discarded subflow bytes at the front of skb, while mptcp_subflow_get_mapped_dsn(subflow) is the DSN for the byte starting at offset.
When the mapping is in sequence (mapped_dsn == msk->ack_seq == msk->copied_seq), subtracting offset from map_seq64 works because __mptcp_recvmsg_mskq() later computes offset = (u32)msk->copied_seq - MPTCP_SKB_CB(skb)->map_seq and skips the first offset bytes.
However, if offset > 0 and mapped_dsn > msk->ack_seq (out-of-order MPTCP data), subtracting offset shifts map_seq64 and map_seq backwards into the unreceived sequence range [mapped_dsn - offset, mapped_dsn):
1. In __mptcp_move_skb(), if mapped_dsn - offset <= msk->ack_seq < mapped_dsn, the packet takes the in-sequence or partial-packet branch instead of mptcp_data_queue_ofo(), advancing msk->ack_seq and exposing the stale subflow prefix [0, offset) to user-space.
2. If mapped_dsn - offset > msk->ack_seq, mptcp_data_queue_ofo() inserts skb into out_of_order_queue claiming coverage of [mapped_dsn - offset, mapped_dsn + skb->len - offset), which causes legitimate segments for [mapped_dsn - offset, mapped_dsn) to be dropped as duplicate data and causes __mptcp_ofo_queue() to dequeue skb as soon as msk->ack_seq reaches mapped_dsn - offset.
[ ... ]
net/mptcp/protocol.c static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg, · line 2205
Quoted 9 lines
net/mptcp/protocol.c +1
-1@@ -2192,7 +2199,7 @@ static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg,int err;if (msk->copied_seq >=MPTCP_SKB_CB (skb)->map_seq) - offset = msk->copied_seq -MPTCP_SKB_CB (skb)->map_seq; + offset = (u32 )msk->copied_seq -MPTCP_SKB_CB (skb)->map_seq;/* Skip skbs that are entirely before copied_seq */ if (offset >= skb->len) {
[Severity: High]
Does comparing the 64-bit msk->copied_seq against the 32-bit MPTCP_SKB_CB(skb)->map_seq in __mptcp_recvmsg_mskq() break MSG_PEEK across multiple SKBs and across 32-bit sequence wrap-around?
With struct mptcp_skb_cb narrowing map_seq to u32 while msk->copied_seq remains u64, the condition:
if (msk->copied_seq >= MPTCP_SKB_CB(skb)->map_seq) offset = (u32)msk->copied_seq - MPTCP_SKB_CB(skb)->map_seq;
zero-extends MPTCP_SKB_CB(skb)->map_seq to u64. Whenever the upper 32 bits of msk->copied_seq are non-zero (which is typical since subflow->iasn is derived from a 64-bit SHA-256 hash, or during TFO when msk->copied_seq is initialized to -(u64)skb->len), msk->copied_seq >= MPTCP_SKB_CB(skb)->map_seq is unconditionally true.
When recvmsg() is called with MSG_PEEK and sk->sk_receive_queue contains two or more SKBs, msk->copied_seq is not updated after peeking the first SKB. On the second SKB (where map_seq64 > msk->copied_seq), the condition still evaluates to true, so offset = (u32)msk->copied_seq - MPTCP_SKB_CB(skb)->map_seq underflows in 32-bit unsigned arithmetic to a huge value (>= 2^31 > skb->len). That hits:
if (offset >= skb->len) {
*last = skb;
continue;
}skipping all subsequent SKBs in sk->sk_receive_queue (and hanging in sk_wait_data() when MSG_PEEK | MSG_WAITALL or SO_RCVLOWAT spans multiple SKBs).
Conversely, if the upper 32 bits of msk->copied_seq are zero and an SKB straddles a 32-bit wrap boundary (for example, map_seq = 0xfffffff0 and copied_seq wraps to 0x10 after a partial read), 0x10ULL >= 0xfffffff0ULL evaluates to false, leaving offset = 0 and re-reading already-consumed bytes. Should this check compare against MPTCP_SKB_CB(skb)->map_seq64 (or use !before((u32)msk->copied_seq, MPTCP_SKB_CB(skb)->map_seq))?
Quoted 5 lines
net/mptcp/subflow.c +0
-0
net/mptcp/subflow.c static void subflow_set_remote_key(struct mptcp_sock *msk, · line 549
Quoted 9 lines
net/mptcp/subflow.c +2
-1@@ -542,7 +542,8 @@ static void subflow_set_remote_key(struct mptcp_sock *msk,if (skb) {u64 tfo_map_seq = subflow->iasn - skb->len; -WRITE_ONCE (MPTCP_SKB_CB (skb)->map_seq, tfo_map_seq); +WRITE_ONCE (MPTCP_SKB_CB (skb)->map_seq, (u32 )tfo_map_seq); +WRITE_ONCE (MPTCP_SKB_CB (skb)->map_seq64, tfo_map_seq);WRITE_ONCE (MPTCP_SKB_CB (skb)->end_seq, subflow->iasn);if (READ_ONCE (msk->copied_seq) != -(u64 )skb->len)
[Severity: Critical]
What happens in subflow_set_remote_key() on a passive TFO socket if user-space partially or concurrently reads the TFO SKB before the 3rd ACK arrives?
In mptcp_fastopen_subflow_synack_set_params(), the TFO SKB is queued onto sk->sk_receive_queue with map_seq64 = -(u64)skb->len and msk->copied_seq = -(u64)skb->len. There appear to be two issues when the 3rd ACK arrives in check_fully_established() -> __mptcp_subflow_fully_established() -> subflow_set_remote_key():
1. If user-space accepts the socket and partially reads N bytes (0 < N < skb->len) before the 3rd ACK arrives, msk->copied_seq advances to -(u64)skb->len + N while the SKB remains at the head of sk->sk_receive_queue. When subflow_set_remote_key() runs:
if (READ_ONCE(msk->copied_seq) != -(u64)skb->len) return;
it updates the SKB's map_seq, map_seq64, and end_seq to the subflow->iasn sequence space, but returns early without rebasing msk->copied_seq by +subflow->iasn (to tfo_map_seq + N). That leaves msk->copied_seq in the initial 0-based sequence space while map_seq and msk->ack_seq are in the subflow->iasn space, corrupting offset = (u32)msk->copied_seq - MPTCP_SKB_CB(skb)->map_seq on subsequent reads.
2. In BH context, check_fully_established() only acquires mptcp_data_lock(msk):
mptcp_data_lock((struct sock *)msk); __mptcp_subflow_fully_established(msk, subflow, mp_opt); mptcp_data_unlock((struct sock *)msk);
Meanwhile, a concurrent reader in __mptcp_recvmsg_mskq() or mptcp_recv_skb() holds lock_sock(sk) (with sk_lock.slock released) and calls mptcp_eat_recv_skb(), which unlinks and frees the SKB from sk->sk_receive_queue without holding mptcp_data_lock(sk):
__skb_unlink(skb, &sk->sk_receive_queue); skb_attempt_defer_free(skb);
Can subflow_set_remote_key() race with mptcp_eat_recv_skb() and trigger a use-after-free when peeking sk->sk_receive_queue and writing to MPTCP_SKB_CB(skb)?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH mptcp-next v17 2/4] mptcp: remove CB offset field
In reply to Geliang TangThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] mptcp: race and UAF in subflow_set_remote_key() with concurrent readers - [High] mptcp: fix…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] mptcp: race and UAF in subflow_set_remote_key() with concurrent readers
- [High] mptcp: fix partial TFO read sequence tracking in subflow_set_remote_key()
- [High] mptcp: fix MSG_PEEK overlap and wraparound in __mptcp_recvmsg_mskq()
- [High] mptcp: mptcp_init_skb() corrupts stream when skb has positive subflow offset --
commit a818e739b6c749d445de127d77fbde3c037d69af
Author: Paolo Abeni <[email protected]>
mptcp: remove CB offset field
This patch replaces the per-skb offset field in mptcp_skb_cb with a socket-level copied_seq field in struct mptcp_sock to track consumed bytes in the MPTCP receive queue. It updates TFO sequence initialization, skb mapping setup, receive queue consumption, and SIOCINQ hint calculation to compute offsets relative to msk->copied_seq.
Quoted 23 lines
net/mptcp/protocol.c +1
-3@@ -169,7 +169,6 @@ static bool __mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,if (MPTCP_SKB_CB (from)->map_seq !=MPTCP_SKB_CB (to)->end_seq ||unlikely (MPTCP_SKB_CB (to)->cant_coalesce) || -MPTCP_SKB_CB (from)->offset || ((to->len + from->len) > (limit >>3 )) || !skb_try_coalesce (to, from, fragstolen, delta))return false;@@ -416,8 +415,7 @@ static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)skb_set_owner_r (skb, sk); } -static void mptcp_init_skb (struct sock *ssk,struct sk_buff *skb,int offset, -int copy_len) +static void mptcp_init_skb (struct sock *ssk,struct sk_buff *skb,int offset) {struct mptcp_subflow_context *subflow =mptcp_subflow_ctx (ssk);bool has_rxtstamp =TCP_SKB_CB (skb)->has_rxtstamp;
net/mptcp/protocol.c static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset, · line 429
Quoted 9 lines
net/mptcp/protocol.c +3
-3@@ -426,9 +424,9 @@ static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset,* mptcp_subflow_get_mapped_dsn() is based on the current tp->copied_seq * value */ -MPTCP_SKB_CB (skb)->map_seq =mptcp_subflow_get_mapped_dsn (subflow); -MPTCP_SKB_CB (skb)->end_seq =MPTCP_SKB_CB (skb)->map_seq + copy_len; -MPTCP_SKB_CB (skb)->offset = offset; +MPTCP_SKB_CB (skb)->map_seq =mptcp_subflow_get_mapped_dsn (subflow) - + offset; +MPTCP_SKB_CB (skb)->end_seq =MPTCP_SKB_CB (skb)->map_seq + skb->len;
[Severity: High]
Can subtracting offset from mptcp_subflow_get_mapped_dsn(subflow) in mptcp_init_skb(), combined with removing the MPTCP_SKB_CB(from)->offset check in __mptcp_try_coalesce(), corrupt the receive stream when an skb on ssk->sk_receive_queue has a positive subflow offset?
An skb in __mptcp_move_skbs_from_subflow() has offset = tp->copied_seq - TCP_SKB_CB(skb)->seq > 0 when its leading bytes were already consumed or discarded (for example, via subflow_check_data_avail() -> mptcp_subflow_discard_data() when an skb spans two DSS mappings and the first mapping was already received on another subflow, or when a partially overlapping TCP segment crosses a DSS boundary). In that situation, only skb->data[offset..skb->len) belongs to the current mapping starting at mapped_dsn = mptcp_subflow_get_mapped_dsn(subflow), while skb->data[0..offset) does not belong to [mapped_dsn - offset, mapped_dsn).
By setting MPTCP_SKB_CB(skb)->map_seq = mapped_dsn - offset, when the new mapping is out of order (mapped_dsn > msk->ack_seq):
1. If mapped_dsn - offset <= msk->ack_seq < mapped_dsn, __mptcp_move_skb() treats the out-of-order skb as in-sequence or partially overlapping msk->ack_seq, advances msk->ack_seq across the unreceived hole [msk->ack_seq, mapped_dsn), and if mapped_dsn - offset == tail->end_seq, __mptcp_try_coalesce() -> skb_try_coalesce() appends skb->data starting at byte 0 onto the receive queue tail. The discarded prefix bytes in skb->data[0..offset) are then delivered to user-space in place of the missing DSN range, and the genuine data for [msk->ack_seq, mapped_dsn) is later dropped as MPTCP_MIB_DUPDATA.
2. If mapped_dsn - offset > msk->ack_seq, mptcp_data_queue_ofo() inserts or coalesces the skb in msk->out_of_order_queue under the fabricated range [mapped_dsn - offset, end_seq), where it can overwrite or cause drops of legitimate segments covering [mapped_dsn - offset, mapped_dsn) and later deliver skb->data[0..offset) to user-space once msk->ack_seq reaches mapped_dsn - offset.
[ ... ]
Quoted 10 lines
net/mptcp/protocol.c +0
-4@@ -472,10 +470,6 @@ static bool __mptcp_move_skb(struct sock *sk, struct sk_buff *skb)/* Partial packet */ if (after64 (MPTCP_SKB_CB (skb)->end_seq, msk->ack_seq)) { copy_len =MPTCP_SKB_CB (skb)->end_seq - msk->ack_seq; -MPTCP_SKB_CB (skb)->offset += msk->ack_seq - -MPTCP_SKB_CB (skb)->map_seq; -MPTCP_SKB_CB (skb)->map_seq += msk->ack_seq - -MPTCP_SKB_CB (skb)->map_seq; goto insert; }
[ ... ]
Quoted 24 lines
net/mptcp/protocol.c +2
-5@@ -896,12 +890,9 @@ static bool __mptcp_ofo_queue(struct mptcp_sock *msk)if (!tail || !mptcp_try_coalesce (sk, tail, skb)) {int delta = msk->ack_seq -MPTCP_SKB_CB (skb)->map_seq; -/* skip overlapping data, if any */ pr_debug ("uncoalesced seq=%llx ack seq=%llx delta=%d\n" ,MPTCP_SKB_CB (skb)->map_seq, msk->ack_seq, delta); -MPTCP_SKB_CB (skb)->offset += delta; -MPTCP_SKB_CB (skb)->map_seq += delta;__skb_queue_tail (&sk->sk_receive_queue, skb); } msk->bytes_received += end_seq - msk->ack_seq;@@ -2195,11 +2186,22 @@ static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg,int copied =0 ; skb_queue_walk_safe(&sk->sk_receive_queue, skb, tmp) { -u32 delta, offset =MPTCP_SKB_CB (skb)->offset; -u32 data_len = skb->len - offset; +u32 delta, offset =0 ; +u32 data_len;u32 count;int err;
net/mptcp/protocol.c static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg, · line 2203
Quoted 10 lines
net/mptcp/protocol.c +10
-0+if (msk->copied_seq >=MPTCP_SKB_CB (skb)->map_seq) + offset = msk->copied_seq -MPTCP_SKB_CB (skb)->map_seq; + +/* Skip skbs that are entirely before copied_seq */ +if (offset >= skb->len) { + *last = skb; +continue ; + } + + data_len = skb->len - offset;
[Severity: High]
Does __mptcp_recvmsg_mskq() return duplicate or skipped data during MSG_PEEK when sk->sk_receive_queue contains partially overlapping skbs or when sequence numbers wrap across zero?
Because __mptcp_move_skb() and __mptcp_ofo_queue() no longer advance MPTCP_SKB_CB(skb)->map_seq to msk->ack_seq when queuing a partially overlapping skb (and mptcp_init_skb() subtracts the subflow offset from map_seq), a second skb (skb2) in sk->sk_receive_queue can have MPTCP_SKB_CB(skb2)->map_seq < MPTCP_SKB_CB(skb1)->end_seq.
During a normal read, consuming skb1 advances msk->copied_seq to MPTCP_SKB_CB(skb1)->end_seq, so the next iteration computes offset = msk->copied_seq - MPTCP_SKB_CB(skb2)->map_seq and skips the overlapping prefix of skb2. Under MSG_PEEK, however, msk->copied_seq is not advanced as skb1 is peeked. When the loop moves to skb2, it still compares against the unadvanced msk->copied_seq rather than the sequence position reached after skb1, leaving offset at 0 (or smaller than skb1->end_seq - skb2->map_seq) and copying the overlapping prefix of skb2 into the user buffer a second time.
In addition, comparing msk->copied_seq >= MPTCP_SKB_CB(skb)->map_seq with raw unsigned u64 arithmetic instead of !before64(msk->copied_seq, MPTCP_SKB_CB(skb)->map_seq) breaks when sequence numbers cross 0 (for example, on a passive TFO socket that falls back before the MPC ACK, where the TFO skb has map_seq = -(u64)skb1->len and msk->copied_seq = -(u64)skb1->len while the next fallback skb has map_seq = 0, causing msk->copied_seq >= map_seq to evaluate true and skip skb2 with a wrapped offset).
[ ... ]
Quoted 19 lines
net/mptcp/subflow.c +11
-0@@ -533,6 +533,26 @@ static void subflow_set_remote_key(struct mptcp_sock *msk,WRITE_ONCE (msk->ack_seq, subflow->iasn);WRITE_ONCE (msk->can_ack, true);atomic64_set (&msk->rcv_wnd_sent, subflow->iasn); + +if (subflow->is_mptfo) { +struct sock *sk = (struct sock *)msk; +struct sk_buff *skb; + + skb =skb_peek (&sk->sk_receive_queue); +if (skb) { +u64 tfo_map_seq = subflow->iasn - skb->len; + +WRITE_ONCE (MPTCP_SKB_CB (skb)->map_seq, tfo_map_seq); +WRITE_ONCE (MPTCP_SKB_CB (skb)->end_seq, subflow->iasn);
net/mptcp/subflow.c static void subflow_set_remote_key(struct mptcp_sock *msk, · line 556
Quoted 10 lines
net/mptcp/subflow.c +9
-0+ +if (READ_ONCE (msk->copied_seq) != -(u64 )skb->len) +return ; + +WRITE_ONCE (msk->copied_seq, tfo_map_seq); +return ; + } + } +WRITE_ONCE (msk->copied_seq, subflow->iasn); }
[Severity: High]
What happens in subflow_set_remote_key() if user-space partially reads the TFO skb before the 3rd ACK arrives?
In mptcp_fastopen_subflow_synack_set_params(), both MPTCP_SKB_CB(skb)->map_seq and msk->copied_seq are initialized to -(u64)skb->len. If user-space accepts the passive TFO socket and reads count bytes (0 < count < skb->len) via __mptcp_recvmsg_mskq() or __mptcp_read_sock() before the peer's MP_CAPABLE ACK is processed, msk->copied_seq is advanced to -(u64)skb->len + count while the partially consumed skb remains at the head of sk->sk_receive_queue.
When the 3rd ACK arrives, subflow_set_remote_key() updates MPTCP_SKB_CB(skb)->map_seq to tfo_map_seq (subflow->iasn - skb->len) and MPTCP_SKB_CB(skb)->end_seq to subflow->iasn, but then sees READ_ONCE(msk->copied_seq) != -(u64)skb->len and returns early without shifting msk->copied_seq by subflow->iasn (to tfo_map_seq + count).
Doesn't leaving msk->copied_seq at -(u64)skb->len + count while map_seq and msk->ack_seq are in the subflow->iasn sequence space corrupt subsequent offset calculations in __mptcp_recvmsg_mskq(), cause mptcp_recv_skb() to eat and drop unread skbs via mptcp_eat_recv_skb(), and cause mptcp_inq_hint() to return INT_MAX?
[Severity: Critical]
Additionally, can subflow_set_remote_key() race with concurrent user-space readers in __mptcp_recvmsg_mskq() and __mptcp_read_sock() -> mptcp_recv_skb() and trigger a use-after-free on skb or clobber msk->copied_seq?
When the 3rd ACK arrives in BH context, check_fully_established() calls __mptcp_subflow_fully_established() -> subflow_set_remote_key() while holding only mptcp_data_lock(sk) (sk->sk_lock.slock), without checking sock_owned_by_user(sk):
net/mptcp/options.c:check_fully_established() {
...
mptcp_data_lock((struct sock *)msk);
__mptcp_subflow_fully_established(msk, subflow, mp_opt);
mptcp_data_unlock((struct sock *)msk);
}Meanwhile, __mptcp_recvmsg_mskq() and mptcp_recv_skb() / __mptcp_read_sock() run under lock_sock(sk) without holding mptcp_data_lock(sk) while peeking sk->sk_receive_queue, reading MPTCP_SKB_CB(skb)->map_seq and msk->copied_seq, updating msk->copied_seq += count, and unlinking and freeing the skb in mptcp_eat_recv_skb(sk, skb):
1. If mptcp_eat_recv_skb() unlinks and frees the TFO skb right after subflow_set_remote_key() executes skb = skb_peek(&sk->sk_receive_queue), subflow_set_remote_key() will read skb->len and write MPTCP_SKB_CB(skb)->map_seq and MPTCP_SKB_CB(skb)->end_seq on a freed sk_buff.
2. Both __mptcp_recvmsg_mskq() and __mptcp_read_sock() update msk->copied_seq += count before calling mptcp_eat_recv_skb(sk, skb). If a full read of the TFO skb advances msk->copied_seq to 0 and subflow_set_remote_key() runs before mptcp_eat_recv_skb() unlinks skb, skb_peek(&sk->sk_receive_queue) still returns skb, and READ_ONCE(msk->copied_seq) != -(u64)skb->len sees 0 and returns early without setting msk->copied_seq = subflow->iasn.
3. Because subflow_set_remote_key() writes MPTCP_SKB_CB(skb)->map_seq before updating msk->copied_seq, a concurrent reader in __mptcp_recvmsg_mskq() or mptcp_recv_skb() can observe the updated map_seq (subflow->iasn - skb->len) together with the old msk->copied_seq (-(u64)skb->len), or race its non-atomic msk->copied_seq += count update against WRITE_ONCE(msk->copied_seq, ...).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Hi Paolo,
Thank you for your modifications, that's great!
Our CI did some validations and here is its report:
- KVM Validation: normal (except selftest_mptcp_join): Unstable: 1 failed test(s): packetdrill_syscalls ⚠️
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Success! ✅
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Perf: Unstable: 2 failed test(s): error:sched-bbr validation:sched-weak-secondary ⚠️
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/40993318561
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/1f6607073d0f
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1196753
If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:
$ cd [kernel source code]
$ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
--pull always mptcp/mptcp-upstream-virtme-docker:latest \
auto-normalFor more details:
https://github.com/multipath-tcp/mptcp-upstream-virtme-docker
Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)
Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)