mptcp: fixes for SIOCINQ
Subsystems (2)
Geliang Tang
reviewerv2 cover mptcp: fixes for SIOCINQ
From: Geliang Tang <[email protected]>
v2:
- patch 3: drop mptcp_data_fin_seq(), reuse mptcp_data_fin()
- u64 return couldn't distinguish "no DATA_FIN" from "DATA_FIN at seq 0"
- mptcp_inq_hint() checks rcv_data_fin/TCP_CLOSE/RCV_SHUTDOWN first
The goal of this series is to make MPTCP's SIOCINQ fully consistent with TCP's: both report the number of readable data bytes, excluding the FIN byte.
Patch 1 fixes mptcp_pending_data_fin() to use >= instead of == so that DATA_FIN is processed when ack_seq advances past rcv_data_fin_seq due to OFO queue processing.
Patch 2 adds missing smp_wmb/smp_rmb barriers for rcv_data_fin_seq on weakly-ordered architectures.
Patch 3 fixes mptcp_inq_hint() INT_MAX overflow on passive TFO by using mptcp_data_avail() instead of ack_seq minus map_seq, and reuses mptcp_data_fin() to guard the FIN byte (+1).
Patch 4 adds mptcp_inq() that excludes the FIN byte, matching TCP's tcp_inq() behavior, and switches SIOCINQ to it.
Geliang Tang (4):
mptcp: handle ack_seq advancing past rcv_data_fin_seq
mptcp: add missing memory barriers for rcv_data_fin_seq
mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
mptcp: add mptcp_inq() for SIOCINQ
net/mptcp/options.c | 1 + net/mptcp/protocol.c | 62 +++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 21 deletions(-)
Signature · 1 line
2.55.0
Geliang Tang
reviewerPATCH v2 1/4 mptcp: handle ack_seq advancing past rcv_data_fin_seq
From: Geliang Tang <[email protected]>
Change mptcp_pending_data_fin() to check if ack_seq >= rcv_data_fin_seq instead of ack_seq == rcv_data_fin_seq. This handles the case where out-of-order segments in the OFO queue advance ack_seq past rcv_data_fin_seq before mptcp_pending_data_fin() runs.
With the strict equality check, if ack_seq advances past rcv_data_fin_seq due to OFO processing, the DATA_FIN would never be processed. In ESTABLISHED state, this causes read() to never return EOF since RCV_SHUTDOWN is not set. In FIN_WAIT1 or TCP_FIN_WAIT2, the connection hangs until the close timer expires.
The new check using !before64(ack_seq, rcv_data_fin_seq) ensures DATA_FIN is processed even when ack_seq has moved past it.
--- net/mptcp/protocol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/mptcp/protocol.c +1 -1
@@ -569,7 +569,7 @@ static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {u64 rcv_data_fin_seq =READ_ONCE (msk->rcv_data_fin_seq); -if (READ_ONCE (msk->ack_seq) == rcv_data_fin_seq) { +if (!before64 (READ_ONCE (msk->ack_seq), rcv_data_fin_seq)) {if (seq) *seq = rcv_data_fin_seq;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH mptcp-net v2 1/4] mptcp: handle ack_seq advancing past rcv_data_fin_seq
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 2 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The patch now lets mptcp_check_data_fin() run when msk->ack_seq is already past rcv_data_fin_seq, but the function still does…
- [High] Before this patch, the strict ack_seq == rcv_data_fin_seq test in mptcp_pending_data_fin() was the only positional check on an…
- [Medium] The commit message says that out-of-order segments in the OFO queue can push msk->ack_seq past rcv_data_fin_seq before…
--
mptcp: handle ack_seq advancing past rcv_data_fin_seq
This commit relaxes the check in mptcp_pending_data_fin() from ack_seq == rcv_data_fin_seq to !before64(ack_seq, rcv_data_fin_seq). The goal is to still process a DATA_FIN after OFO processing has moved ack_seq past rcv_data_fin_seq.
Quoted 3 lines
This handles the case where
out-of-order segments in the OFO queue advanceack_seqpastrcv_data_fin_seqbeforemptcp_pending_data_fin()runs.
[Severity: Medium]
Can this happen with a peer that follows the protocol?
rcv_data_fin_seq is the DSN of the DATA_FIN byte itself. get_mapping_status() trims the mapped payload so that it ends right at that DSN:
net/mptcp/subflow.c:get_mapping_status() {
...
mptcp_update_rcv_data_fin(msk, data_fin_seq, mpext->dsn64);
...
/* Adjust for DATA_FIN using 1 byte of sequence space */
data_len--;
...
}The Linux sender places the DATA_FIN at write_seq - 1 in mptcp_write_data_fin(), which is after all data. The fallback dummy FIN is at map_seq + map_data_len, which is where the last mapping ends.
msk->ack_seq only advances in two ways:
- to the end_seq of received data, in __mptcp_move_skb() and
__mptcp_ofo_queue()
- by 1 when the DATA_FIN is consumed
A DATA_FIN retransmitted after that is rejected by the state check in mptcp_pending_data_fin(), because CLOSE_WAIT, CLOSING and CLOSE are not accepted states.
So OFO processing can bring ack_seq up to rcv_data_fin_seq, but it doesn't seem able to push it past. The old == check would match in that case. To get ack_seq > rcv_data_fin_seq, it looks like you need a peer that sends data at or beyond its own DATA_FIN, or a forged or stale DATA_FIN.
Could the commit message describe the real scenario, ideally with a reproducer? As written, it reads like a fix for a common OFO ordering bug. The change actually relaxes validation of input from a misbehaving peer or a forger, and that leads to the two issues below.
net/mptcp/protocol.c static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) · line 571
Quoted 10 lines
net/mptcp/protocol.c +1
-1@@ -568,7 +568,7 @@ static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq) (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {u64 rcv_data_fin_seq =READ_ONCE (msk->rcv_data_fin_seq); -if (READ_ONCE (msk->ack_seq) == rcv_data_fin_seq) { +if (!before64 (READ_ONCE (msk->ack_seq), rcv_data_fin_seq)) {
[Severity: High]
With this change, mptcp_check_data_fin() can run while msk->ack_seq is already past rcv_data_fin_seq. It still does this, though:
net/mptcp/protocol.c:mptcp_check_data_fin() {
...
if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
...
}The +1 was only correct while ack_seq == rcv_data_fin_seq. The rcv_data_fin_seq value filled in by mptcp_pending_data_fin() is never used.
Say ack_seq is A and the DATA_FIN DSN is F, with F < A. Won't ack_seq end up as A + 1, rather than F + 1 or staying at A?
Three things seem to follow from that.
First, the DATA_ACK sent by mptcp_send_ack() acknowledges A + 1. If the peer's snd_nxt is A, ack_update_msk() on the peer ignores it:
if (unlikely(before64(snd_nxt, old_snd_una) || after64(new_snd_una, snd_nxt))) new_snd_una = old_snd_una;
Could the peer's DATA_FIN then never be acked?
Second, the msk moves to CLOSE_WAIT. __mptcp_add_backlog() only drops data in TCP_CLOSE, so a later in-order skb with map_seq == A is still accepted. __mptcp_move_skb() would then treat it as a partial packet:
/* Partial packet */
if (after64(MPTCP_SKB_CB(skb)->end_seq, msk->ack_seq)) {
copy_len = MPTCP_SKB_CB(skb)->end_seq - msk->ack_seq;
...Is byte A of the stream silently lost here?
Third, any bytes in [F, A) already on sk_receive_queue are passed to userspace before EOF, even though the peer marked the end of the stream at F.
Should ack_seq be derived from rcv_data_fin_seq here? Or should ack_seq > rcv_data_fin_seq be treated as a protocol violation?
As far as I can tell, no later patch in the series touches mptcp_check_data_fin(), so the final tree still adds 1 unconditionally.
[Severity: High]
Before this patch, the strict == test was the only check on where an incoming DATA_FIN sits. Now, does any DATA_FIN whose DSN is at or behind ack_seq get acted on immediately?
A zero-length subflow segment that carries a DSS DATA_FIN with data_len == 1 reaches mptcp_update_rcv_data_fin() directly:
net/mptcp/options.c:mptcp_incoming_options() {
...
if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
if (mp_opt.data_fin && mp_opt.data_len == 1 &&
mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
mptcp_schedule_work((struct sock *)msk);
...
}No payload, mapping or checksum covers that DSN. mptcp_update_rcv_data_fin() stores it through mptcp_expand_seq() without checking it against ack_seq:
- with dsn64, the raw value is stored
- with 32-bit DSNs, any value in the 2^31 window behind ack_seq expands to a past DSN
So a forged or stale DATA_FIN used to need the exact 64-bit DSN. Now roughly half of the DSN space works, for example DSN 0 with dsn64.
mptcp_check_data_fin() then sets RCV_SHUTDOWN and sends a DATA_ACK. It also changes the msk state:
- ESTABLISHED goes to CLOSE_WAIT
- FIN_WAIT1 goes to CLOSING
- FIN_WAIT2 calls mptcp_shutdown_subflows() and goes to TCP_CLOSE
Together with the unconditional ack_seq + 1 above, the next legitimate in-order skb would also lose one byte.
Could a misbehaving peer truncate the stream this way? Could an off-path attacker who can blind-inject an in-window zero-length segment do the same? For comparison, plain TCP only acts on a FIN at rcv_nxt.
It looks like the relaxed !before64() check is also kept in the mptcp_data_fin() helper later in the series.
Quoted 3 lines
net/mptcp/protocol.c +0
-0if (seq) *seq = rcv_data_fin_seq;
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795348666.git.tanggeliang%40kylinos.cn
Geliang Tang
reviewerPATCH v2 2/4 mptcp: add missing memory barriers for rcv_data_fin_seq
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
mptcp_update_rcv_data_fin() writes rcv_data_fin_seq before setting rcv_data_fin, but there is no write barrier to ensure the ordering is visible to concurrent readers.
mptcp_pending_data_fin() checks rcv_data_fin and then reads rcv_data_fin_seq, but has no read barrier. On weakly-ordered architectures, the reader could observe rcv_data_fin == 1 with a stale (or zero) rcv_data_fin_seq, causing mptcp_check_data_fin() to either process the DATA_FIN prematurely or skip it entirely.
Add smp_wmb() in mptcp_update_rcv_data_fin() between the two writes and a matching smp_rmb() in mptcp_pending_data_fin() between the two reads.
--- net/mptcp/options.c | 1 + net/mptcp/protocol.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
net/mptcp/options.c +1 -0
@@ -1192,6 +1192,7 @@ bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool usWRITE_ONCE (msk->rcv_data_fin_seq,mptcp_expand_seq (READ_ONCE (msk->ack_seq), data_fin_seq, use_64bit)); +smp_wmb ();/* ensure rcv_data_fin_seq is visible before rcv_data_fin */ WRITE_ONCE (msk->rcv_data_fin,1 );return true;
net/mptcp/protocol.c +3 -1
@@ -567,8 +567,10 @@ static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)if (READ_ONCE (msk->rcv_data_fin) && ((1 <<inet_sk_state_load (sk)) & (TCPF_ESTABLISHED |TCPF_FIN_WAIT1 |TCPF_FIN_WAIT2 ))) { -u64 rcv_data_fin_seq =READ_ONCE (msk->rcv_data_fin_seq); +u64 rcv_data_fin_seq; +smp_rmb ();/* pair with smp_wmb in mptcp_update_rcv_data_fin */ + rcv_data_fin_seq =READ_ONCE (msk->rcv_data_fin_seq);if (!before64 (READ_ONCE (msk->ack_seq), rcv_data_fin_seq)) {if (seq) *seq = rcv_data_fin_seq;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH mptcp-net v2 2/4] mptcp: add missing memory barriers for rcv_data_fin_seq
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message overstates one failure mode.
- [Low] The commit author address does not match the Signed-off-by address.
--
mptcp: add missing memory barriers for rcv_data_fin_seq
This commit adds an smp_wmb() between the rcv_data_fin_seq and rcv_data_fin stores in mptcp_update_rcv_data_fin(). It also adds a matching smp_rmb() between the two loads in mptcp_pending_data_fin(), so readers can't see rcv_data_fin == 1 together with a stale rcv_data_fin_seq.
Quoted 4 lines
On weakly-ordered
architectures, the reader could observercv_data_fin== 1 with a stale (or zero)rcv_data_fin_seq, causingmptcp_check_data_fin()to either process theDATA_FINprematurely or skip it entirely.
[Severity: Low]
Can the "skip it entirely" case actually happen?
In mptcp_check_data_fin(), rcv_data_fin is only cleared on the true branch of mptcp_pending_data_fin():
net/mptcp/protocol.c:mptcp_check_data_fin() {
...
if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
WRITE_ONCE(msk->rcv_data_fin, 0);
...
}So a false negative leaves the DATA_FIN pending. Each writer path also forces another evaluation later. mptcp_incoming_options() and the subflow.c callers call mptcp_schedule_work() after mptcp_update_rcv_data_fin():
mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64)) mptcp_schedule_work((struct sock *)msk);
The other subflow.c writers are followed on the same CPU by the check in move_skbs_to_msk():
if (mptcp_pending_data_fin(sk, NULL)) mptcp_schedule_work(sk);
Quoted 1 line
From that, a stale read only seems to delay the
DATA_FIN.
The real problem looks like the premature case. If the reader sees rcv_data_fin == 1 with the initial rcv_data_fin_seq of 0, then !before64(ack_seq, 0) is true whenever bit 63 of ack_seq is clear. mptcp_check_data_fin() then bumps ack_seq, sets RCV_SHUTDOWN and moves the msk to CLOSE_WAIT, CLOSING or CLOSE before all data has arrived. The application would see an early EOF or truncated data.
Since this carries a Fixes: tag, could the commit message describe that user-visible effect instead of the "skip it entirely" wording?
Quoted 1 line
Signed-off-by Geliang Tang <[email protected]>
[Severity: Low]
This isn't a bug, but the author address doesn't match the sign-off. The author is "Geliang Tang <[email protected]>", and the only Signed-off-by is "Geliang Tang <[email protected]>".
checkpatch.pl will warn about this with FROM_SIGN_OFF_MISMATCH. The in-body "From: Geliang Tang <[email protected]>" line may have been dropped.
Could that From: line be restored, or a matching Signed-off-by added? The other patches in the series seem to have the same mismatch.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795348666.git.tanggeliang%40kylinos.cn
Geliang Tang
reviewerPATCH v2 3/4 mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Fix mptcp_inq_hint() to compute available bytes using mptcp_data_avail() (bytes_received - bytes_consumed) instead of ack_seq minus the head skb's map_seq.
On passive TFO connections, the TFO skb has map_seq set to -skb->len. Without the removed __mptcp_fastopen_gen_msk_ackseq() to adjust the TFO skb's map_seq, mptcp_inq_hint() computes "ack_seq - map_seq = iasn + len", almost always >= INT_MAX, causing SIOCINQ/FIONREAD and TCP_CM_INQ to report INT_MAX. This also breaks receive buffer autotuning, as mptcp_rcv_space_adjust() uses mptcp_inq_hint() to measure consumed data. The bogus hint prevents mptcp_rcvbuf_grow() from being called until the entire TFO skb has been read.
Using mptcp_data_avail() avoids this by tracking bytes directly. Introduce a mptcp_data_fin() helper to check if ack_seq has passed rcv_data_fin_seq. The FIN byte (+1) is guarded by checking rcv_data_fin, TCP_CLOSE, or RCV_SHUTDOWN before calling mptcp_data_fin(). Also reset rcv_data_fin_seq to 0 in mptcp_disconnect().
--- net/mptcp/protocol.c | 47 +++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 22 deletions(-)
net/mptcp/protocol.c +25 -22
@@ -559,6 +559,21 @@ static void mptcp_check_data_fin_ack(struct sock *sk) } } +static bool mptcp_data_fin (const struct mptcp_sock *msk,u64 *seq) +{ +u64 rcv_data_fin_seq; + +smp_rmb ();/* pair with smp_wmb() in mptcp_update_rcv_data_fin() */ + rcv_data_fin_seq =READ_ONCE (msk->rcv_data_fin_seq); +if (!before64 (READ_ONCE (msk->ack_seq), rcv_data_fin_seq)) { +if (seq) + *seq = rcv_data_fin_seq; + +return true; + } +return false; +} +/* can be called with no lock acquired */ static bool mptcp_pending_data_fin (struct sock *sk,u64 *seq) {
@@ -567,16 +582,7 @@ static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)if (READ_ONCE (msk->rcv_data_fin) && ((1 <<inet_sk_state_load (sk)) & (TCPF_ESTABLISHED |TCPF_FIN_WAIT1 |TCPF_FIN_WAIT2 ))) { -u64 rcv_data_fin_seq; - -smp_rmb ();/* pair with smp_wmb in mptcp_update_rcv_data_fin */ - rcv_data_fin_seq =READ_ONCE (msk->rcv_data_fin_seq); -if (!before64 (READ_ONCE (msk->ack_seq), rcv_data_fin_seq)) { -if (seq) - *seq = rcv_data_fin_seq; - -return true; - } +return mptcp_data_fin (msk, seq); }return false;
@@ -2407,22 +2413,18 @@ static bool mptcp_move_skbs(struct sock *sk)static unsigned int mptcp_inq_hint (const struct sock *sk) {const struct mptcp_sock *msk =mptcp_sk (sk); -const struct sk_buff *skb; - - skb =skb_peek (&sk->sk_receive_queue); -if (skb) { -u64 hint_val =READ_ONCE (msk->ack_seq) -MPTCP_SKB_CB (skb)->map_seq; +u64 hint_val =mptcp_data_avail (msk); -if (hint_val >=INT_MAX ) -return INT_MAX ; +if (hint_val >=INT_MAX ) +return INT_MAX ; -return (unsigned int )hint_val; +if (READ_ONCE (msk->rcv_data_fin) || + sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN )) { +if (mptcp_data_fin (msk,NULL )) + hint_val +=1 ; } -if (sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN )) -return 1 ; - -return 0 ; +return (unsigned int )hint_val; } static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
@@ -3682,6 +3684,7 @@ static int mptcp_disconnect(struct sock *sk, int flags)WRITE_ONCE (msk->can_ack, false);WRITE_ONCE (msk->fully_established, false);WRITE_ONCE (msk->rcv_data_fin, false); +WRITE_ONCE (msk->rcv_data_fin_seq,0 );WRITE_ONCE (msk->snd_data_fin_enable, false);WRITE_ONCE (msk->rcv_fastclose, false);WRITE_ONCE (msk->use_64bit_ack, false);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH mptcp-net v2 3/4] mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mptcp: mptcp_inq_hint() compares ack_seq with unset rcv_data_fin_seq --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mptcp: mptcp_inq_hint() compares ack_seq with unset rcv_data_fin_seq
--
commit 91eba8ecdb62213836985c3edebbb4b735538410
Author: Geliang Tang <[email protected]>
mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
This commit updates mptcp_inq_hint() to compute available bytes from mptcp_data_avail() and adds a mptcp_data_fin() helper to check whether ack_seq has passed rcv_data_fin_seq.
Quoted 22 lines
net/mptcp/protocol.c +14
-0@@ -558,6 +558,21 @@ static void mptcp_check_data_fin_ack(struct sock *sk) } } +static bool mptcp_data_fin (const struct mptcp_sock *msk,u64 *seq) +{ +u64 rcv_data_fin_seq; + +smp_rmb ();/* pair with smp_wmb() in mptcp_update_rcv_data_fin() */ + rcv_data_fin_seq =READ_ONCE (msk->rcv_data_fin_seq); +if (!before64 (READ_ONCE (msk->ack_seq), rcv_data_fin_seq)) { +if (seq) + *seq = rcv_data_fin_seq; + +return true; + } +return false; +}
[ ... ]
Quoted 19 lines
net/mptcp/protocol.c +5
-8@@ -2364,22 +2370,18 @@ static bool mptcp_move_skbs(struct sock *sk)static unsigned int mptcp_inq_hint (const struct sock *sk) {const struct mptcp_sock *msk =mptcp_sk (sk); -const struct sk_buff *skb; - - skb =skb_peek (&sk->sk_receive_queue); -if (skb) { -u64 hint_val =READ_ONCE (msk->ack_seq) -MPTCP_SKB_CB (skb)->map_seq; +u64 hint_val =mptcp_data_avail (msk); -if (hint_val >=INT_MAX ) -return INT_MAX ; +if (hint_val >=INT_MAX ) +return INT_MAX ; -return (unsigned int )hint_val; +if (READ_ONCE (msk->rcv_data_fin) || + sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN )) {
net/mptcp/protocol.c static bool mptcp_move_skbs(struct sock *sk) · line 2385
Quoted 10 lines
net/mptcp/protocol.c +3
-4+if (mptcp_data_fin (msk,NULL )) + hint_val +=1 ; } -if (sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN )) -return 1 ; - -return 0 ; +return (unsigned int )hint_val; }
[Severity: Medium]
Can mptcp_inq_hint() call mptcp_data_fin() when msk->rcv_data_fin is false and msk->rcv_data_fin_seq has never been set?
When a socket enters TCP_CLOSE or sets RCV_SHUTDOWN without receiving a DATA_FIN (for example via shutdown(fd, SHUT_RD) or a peer reset), msk->rcv_data_fin_seq is still 0. Because before64() uses signed 64-bit modular arithmetic, !before64(msk->ack_seq, 0) tests (__s64)msk->ack_seq >= 0, and subflow_set_remote_key() seeds msk->ack_seq from a 64-bit SHA-256 hash.
Depending on bit 63 of msk->ack_seq, mptcp_inq_hint() will either omit the +1 EOF indication on a closed or shut-down empty socket or add a spurious +1 while unread data is still queued after SHUT_RD. Checking sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN) directly for the +1, and only calling mptcp_data_fin() when READ_ONCE(msk->rcv_data_fin) is true, would avoid comparing against an unset rcv_data_fin_seq.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH mptcp-net v2 3/4] mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
In reply to Geliang TangThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The FIN-byte guard in mptcp_inq_hint() (net/mptcp/protocol.c) is wrong.
--
mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
This patch changes mptcp_inq_hint() to compute the available bytes with mptcp_data_avail(), instead of ack_seq minus the head skb's map_seq. The old calculation overflowed on passive TFO connections. The patch also moves the DATA_FIN sequence check into a new mptcp_data_fin() helper, adds one byte for the FIN, and resets rcv_data_fin_seq in mptcp_disconnect().
Quoted 23 lines
net/mptcp/protocol.c +15
-0@@ -558,6 +558,21 @@ static void mptcp_check_data_fin_ack(struct sock *sk) } } +static bool mptcp_data_fin (const struct mptcp_sock *msk,u64 *seq) +{ +u64 rcv_data_fin_seq; + +smp_rmb ();/* pair with smp_wmb() in mptcp_update_rcv_data_fin() */ + rcv_data_fin_seq =READ_ONCE (msk->rcv_data_fin_seq); +if (!before64 (READ_ONCE (msk->ack_seq), rcv_data_fin_seq)) { +if (seq) + *seq = rcv_data_fin_seq; + +return true; + } +return false; +} +
[ ... ]
Quoted 19 lines
net/mptcp/protocol.c +5
-8@@ -2364,22 +2370,18 @@ static bool mptcp_move_skbs(struct sock *sk)static unsigned int mptcp_inq_hint (const struct sock *sk) {const struct mptcp_sock *msk =mptcp_sk (sk); -const struct sk_buff *skb; - - skb =skb_peek (&sk->sk_receive_queue); -if (skb) { -u64 hint_val =READ_ONCE (msk->ack_seq) -MPTCP_SKB_CB (skb)->map_seq; +u64 hint_val =mptcp_data_avail (msk); -if (hint_val >=INT_MAX ) -return INT_MAX ; +if (hint_val >=INT_MAX ) +return INT_MAX ; -return (unsigned int )hint_val; +if (READ_ONCE (msk->rcv_data_fin) || + sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN )) {
net/mptcp/protocol.c static bool mptcp_move_skbs(struct sock *sk) · line 2385
Quoted 10 lines
net/mptcp/protocol.c +3
-4+if (mptcp_data_fin (msk,NULL )) + hint_val +=1 ; } -if (sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN )) -return 1 ; - -return 0 ; +return (unsigned int )hint_val; }
[Severity: Medium]
Can the TCP_CLOSE and RCV_SHUTDOWN arms of this condition add a FIN byte that was never received?
The only function that writes a real value to rcv_data_fin_seq is mptcp_update_rcv_data_fin() in net/mptcp/options.c. It sets the field together with rcv_data_fin:
WRITE_ONCE(msk->rcv_data_fin_seq, mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit)); smp_wmb(); /* ensure rcv_data_fin_seq is visible before rcv_data_fin */ WRITE_ONCE(msk->rcv_data_fin, 1);
The msk can reach TCP_CLOSE or RCV_SHUTDOWN without any DATA_FIN. One example is mptcp_check_fastclose() after the peer sends MP_FASTCLOSE:
mptcp_set_state(sk, TCP_CLOSE); WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
A subflow reset or error propagation, or a local shutdown(SHUT_RD), leads to the same state.
In these cases rcv_data_fin_seq is still 0, so mptcp_data_fin() evaluates !before64(ack_seq, 0). That reduces to (s64)ack_seq >= 0.
subflow_set_remote_key() sets msk->ack_seq to subflow->iasn, which is derived from a hash of the peer's key. The result therefore looks random per connection. Fallback connections start with ack_seq at 0, so for them the check is always true.
There seem to be two possible outcomes:
- top bit of ack_seq clear: N queued bytes are reported as N + 1
- top bit set: an empty queue in TCP_CLOSE or RCV_SHUTDOWN reports 0, where the old code returned 1
The commit message says:
The FIN byte (+1) is guarded by checking rcv_data_fin, TCP_CLOSE, or RCV_SHUTDOWN before calling mptcp_data_fin().
Only the rcv_data_fin arm seems to guarantee that rcv_data_fin_seq holds a valid value.
The new reset in mptcp_disconnect() has the same problem:
WRITE_ONCE(msk->rcv_data_fin_seq, 0);
0 doesn't work as a "no FIN" marker here, because the comparison is modular against ack_seq.
The TCP_CLOSE and RCV_SHUTDOWN arms also skip the rcv_data_fin read that the smp_rmb() in mptcp_data_fin() is meant to pair with. Subflow context writes rcv_data_fin_seq without holding the msk lock. A concurrent or torn (on 32-bit) value could therefore feed the same comparison.
This still seems to be present at the end of the series. The later patch "mptcp: add mptcp_inq() for SIOCINQ" moves SIOCINQ to mptcp_inq(). However, mptcp_inq_hint() still feeds the TCP_CM_INQ cmsg in mptcp_recvmsg():
unsigned int inq = mptcp_inq_hint(sk);
It also feeds the receive buffer autotuning in mptcp_rcv_space_adjust():
copied -= mptcp_inq_hint(sk);
Should the +1 only be added when rcv_data_fin is set?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795348666.git.tanggeliang%40kylinos.cn
Geliang Tang
reviewerPATCH v2 4/4 mptcp: add mptcp_inq() for SIOCINQ
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Add mptcp_inq() to compute readable bytes at the MPTCP level using mptcp_data_avail() (bytes_received - bytes_consumed). It returns 0 during handshake (TCPF_SYN_SENT/TCPF_SYN_RECV), INT_MAX when avail >= INT_MAX, otherwise the actual byte count.
Unlike mptcp_inq_hint(), mptcp_inq() does not include the FIN byte. Switch SIOCINQ from mptcp_inq_hint() to mptcp_inq() so that FIONREAD reports N (not N+1) when N bytes are unread with DATA_FIN pending, matching TCP's tcp_ioctl() which subtracts the FIN byte.
--- net/mptcp/protocol.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +16 -1
@@ -4182,6 +4182,21 @@ static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)return (int )delta; } +static int mptcp_inq (struct sock *sk) +{ +const struct mptcp_sock *msk =mptcp_sk (sk); +u64 avail; + +if ((1 << sk->sk_state) & (TCPF_SYN_SENT |TCPF_SYN_RECV )) +return 0 ; + + avail =mptcp_data_avail (msk); +if (avail >=INT_MAX ) +return INT_MAX ; + +return (unsigned int )avail; +} +static int mptcp_ioctl (struct sock *sk,int cmd,int *karg) {struct mptcp_sock *msk =mptcp_sk (sk);
@@ -4195,7 +4210,7 @@ static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)lock_sock (sk);if (mptcp_move_skbs (sk))mptcp_cleanup_rbuf (msk,0 ); - *karg =mptcp_inq_hint (sk); + *karg =mptcp_inq (sk);release_sock (sk);break ;case SIOCOUTQ :
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH mptcp-net v2 4/4] mptcp: add mptcp_inq() for SIOCINQ
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The From:/Signed-off-by email addresses do not match.
- [Low] No in-tree selftest covers the behaviour this patch changes.
--
mptcp: add mptcp_inq() for SIOCINQ
This commit adds mptcp_inq(). It reports the readable bytes at the MPTCP level as bytes_received - bytes_consumed and does not count the DATA_FIN byte. SIOCINQ now uses mptcp_inq() instead of mptcp_inq_hint(), so FIONREAD matches tcp_ioctl() when DATA_FIN is pending.
Quoted 3 lines
Co-developed-by Zqiang <[email protected]>Signed-off-by Zqiang <[email protected]>Signed-off-by Geliang Tang <[email protected]>
[Severity: Low]
This isn't a bug, but the author address and the author's Signed-off-by address are different:
Author: Geliang Tang <[email protected]>
checkpatch doesn't read .mailmap, so this patch triggers its FROM_SIGN_OFF_MISMATCH warning.
Could the author and the sign-off use the same address?
Quoted 4 lines
net/mptcp/protocol.c +0
-0
[ ... ]
net/mptcp/protocol.c static int mptcp_ioctl(struct sock *sk, int cmd, int *karg) · line 4171
Quoted 6 lines
net/mptcp/protocol.c +1
-1@@ -4153,7 +4168,7 @@ static int mptcp_ioctl(struct sock *sk, int cmd, int *karg)lock_sock (sk);if (mptcp_move_skbs (sk))mptcp_cleanup_rbuf (msk,0 ); - *karg =mptcp_inq_hint (sk); + *karg =mptcp_inq (sk);
[Severity: Low]
This isn't a bug, but does any selftest cover the FIONREAD change made here?
The MPTCP selftests call FIONREAD in only one place. It is in process_one_client() in tools/testing/selftests/net/mptcp/mptcp_inq.c, inside the polling loop that runs before the peer shuts down:
ret = ioctl(fd, FIONREAD, &queued); ... if (queued > expect_len)
No DATA_FIN has arrived at that point, so mptcp_inq_hint() and mptcp_inq() return the same value.
After the "shut"/"closed" exchange, one data byte and the DATA_FIN are pending. That is the only state where this patch changes the result. The test only checks the TCP_CM_INQ cmsg there:
iov.iov_len = 1; ret = recvmsg(fd, &msg, 0); ... /* tcp_inq should be 1 due to received fin. */ assert(tcp_inq == 1);
The cmsg value still comes from mptcp_inq_hint() in mptcp_recvmsg(), which this patch doesn't change. So the selftest passes with both the old N+1 result and the new N result.
Would it make sense to add a FIONREAD check that expects 1 just before that recvmsg() call in process_one_client()? The old code would return 2 there.
Quoted 3 lines
net/mptcp/protocol.c +0
-0release_sock (sk);break ;case SIOCOUTQ :
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795348666.git.tanggeliang%40kylinos.cn
Hi Geliang,
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): Success! ✅
- 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: 1 failed test(s): regression:sched-mem-pressure ⚠️ (1 improvements, 105 new outliers to review)
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/45022623344
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/04f1867918c0
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1218606
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)