From 12904db548bdd8011895fe071d7a420ccc6584f8 Mon Sep 17 00:00:00 2001 From: MoYuanhao Date: Thu, 5 Dec 2024 10:18:15 +0800 Subject: [PATCH net v2] tcp: Check space before adding MPTCP options Ensure enough space before adding MPTCP options in tcp_syn_options() Added a check to verify sufficient remaining space before inserting MPTCP options in SYN packets. This prevents issues when space is insufficient. Fixes: cec37a6e41aa ("mptcp: Handle MP_CAPABLE options for outgoing connections") Signed-off-by: MoYuanhao --- net/ipv4/tcp_output.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 5485a70b5fe5..401eb14c870d 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -792,6 +792,21 @@ static void smc_set_option_cond(const struct tcp_sock *tp, #endif } +static void mptcp_set_option(struct sock *sk, const struct sk_buff *skb, + struct tcp_out_options *opts, unsigned int *remaining) +{ + if (sk_is_mptcp(sk)) { + unsigned int size; + + if (mptcp_syn_options(sk, skb, &size, &opts->mptcp)) { + if (*remaining >= size) { + opts->options |= OPTION_MPTCP; + *remaining -= size; + } + } + } +} + static void mptcp_set_option_cond(const struct request_sock *req, struct tcp_out_options *opts, unsigned int *remaining) @@ -879,14 +894,7 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb, smc_set_option(tp, opts, &remaining); - if (sk_is_mptcp(sk)) { - unsigned int size; - - if (mptcp_syn_options(sk, skb, &size, &opts->mptcp)) { - opts->options |= OPTION_MPTCP; - remaining -= size; - } - } + mptcp_set_option(sk,skb,opts, &remaining); bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts, &remaining); -- 2.25.1