From patchwork Tue Aug 27 00:14:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zijian Zhang X-Patchwork-Id: 13778673 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-qv1-f45.google.com (mail-qv1-f45.google.com [209.85.219.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62B571FAA for ; Tue, 27 Aug 2024 00:16:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724717791; cv=none; b=iA3rBR0Qs4x0IchIg9Zp01pJuVSSmYuX+jv0DljjBHcgSURzmmPz9x+rvC5T0dLKsKGZb1lMjPc8BmcSegGQ2eh+NWTSfrohaW/TwWqZl41/hfkKt3AgOEYyT0QyQaQZwpNriIY2dLhJjtRnFh9gtzA0ApViYK6hnbwE1rcfwrw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724717791; c=relaxed/simple; bh=dwXgFkLLxDlJ/psywywm0sXw8F+FfB0iKbQ1oItwNfs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HJYfcklmHCEl5qWhKCBYlayN1r7t37Nk7oKf5wlmIJO20PWLANTK0SMjS+aO9BHIRR59HqstxVnLmTbVMZfJslw0r/lUb5Dy3Bal91sePqQtHoRev1c6wKKkPguTYBEHdbW4CG3B2lYd+oi1OngYPZPrngrppJDZvIJYsrtcy88= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=gJJwMVuf; arc=none smtp.client-ip=209.85.219.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="gJJwMVuf" Received: by mail-qv1-f45.google.com with SMTP id 6a1803df08f44-6bf6606363fso26769856d6.3 for ; Mon, 26 Aug 2024 17:16:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724717788; x=1725322588; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rvaGTDglxkoTbtTs4rY1slGgaJPTK8jyVArxMPfgfwk=; b=gJJwMVufTosef4Y9OZjt3sdIpPdv1fkI9AmtlD0pSvudoslZmASl/JoM/Jh1ErxBYI u5coH+9blADKC2JbLvFe62C6VMUMB5+WXrs8O5XThyWRZampVCmd2p8XNsprgg9mCwVk DIFww9EadWDh/STmkEPOZbU5hJAJlT+Dfs1WCye6ACCYZAbz4RnKbtfM2Ja4sG+jni5X MY/MRT8+/CKqNErfbi7m/qh3fyIu3Q3+QozlNlQHDmANTVabSfPSLyGo6tzbfS/aEkqP W/XPYnW7UUjfQBRjGDOAK/Ub05PBpjXgOlaUU+pQJBbuYa6VQZ4ZvYzbRgsGlJLbJPiA au5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724717788; x=1725322588; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rvaGTDglxkoTbtTs4rY1slGgaJPTK8jyVArxMPfgfwk=; b=NF4zgrSTosZFVgCx+1DeX399+tXBC5lze/PBkNrOdKL3+W0fpMfC3YhRyv1bIImSwT YSWlrZpaRj3/eb9Na3f36KvsraKYnhxZeK2uo4neOcSunjAuNuhGJeYQS+qI6q6DfzVZ /AvoHwrlrfMJfYoBatO3b16GloafBNSJcxtEJhf1w80Afx7rXZG0OzjLKvgnZybfZJXg iapdj6fTn0bXjzD8KcJ7Ez5pvZ58os/WkTapzfotmbYCb+KY8lsybFchVJs4I2SVLsug 11sY0z7peeE3brdID9OC7pmokTdEp/oPVdISlhuD9V8IxnZTYIPPkHEB4zKX2kB8JsPL R9gQ== X-Gm-Message-State: AOJu0YyQAPUmZqffBTYqDrulgkVEaKZep3c8sy5aJfFm/h0wBVVXK9rq O+chpCikQ9vAavqEKT9Y0zBmwIcWKFag+yLHZOVDRHljPuz5m/Lb2LjOTpmolb9RyASkiZ5Su0G B X-Google-Smtp-Source: AGHT+IFbFBMiUYVwMkpOUUbZNRct4zq9d+QQoMIsLLVopZRMJGyE2dwc1oLKOSJFttUfDoFHXfmJ3Q== X-Received: by 2002:a05:6214:418c:b0:6b5:936d:e5e9 with SMTP id 6a1803df08f44-6c16dc86c56mr136602326d6.26.1724717787936; Mon, 26 Aug 2024 17:16:27 -0700 (PDT) Received: from n191-036-066.byted.org ([130.44.212.119]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c162dcda66sm51387136d6.122.2024.08.26.17.16.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Aug 2024 17:16:27 -0700 (PDT) From: zijianzhang@bytedance.com To: bpf@vger.kernel.org Cc: edumazet@google.com, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, mykolal@fb.com, shuah@kernel.org, xiyou.wangcong@gmail.com, wangdongdong.6@bytedance.com, zhoufeng.zf@bytedance.com, Amery Hung , Zijian Zhang Subject: [PATCH bpf-next v1 1/2] bpf: tcp: prevent bpf_reserve_hdr_opt() from growing skb larger than MTU Date: Tue, 27 Aug 2024 00:14:06 +0000 Message-Id: <20240827001407.2476854-2-zijianzhang@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240827001407.2476854-1-zijianzhang@bytedance.com> References: <20240827001407.2476854-1-zijianzhang@bytedance.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Amery Hung This series prevents sockops users from accidentally causing packet drops. This can happen when a BPF_SOCK_OPS_HDR_OPT_LEN_CB program reserves different option lengths in tcp_sendmsg(). Initially, sockops BPF_SOCK_OPS_HDR_OPT_LEN_CB program will be called to reserve a space in tcp_send_mss(), which will return the MSS for TSO. Then, BPF_SOCK_OPS_HDR_OPT_LEN_CB will be called in __tcp_transmit_skb() again to calculate the actual tcp_option_size and skb_push() the total header size. skb->gso_size is restored from TCP_SKB_CB(skb)->tcp_gso_size, which is derived from tcp_send_mss() where we first call HDR_OPT_LEN. If the reserved opt size is smaller than the actual header size, the len of the skb can exceed the MTU. As a result, ip(6)_fragment will drop the packet if skb->ignore_df is not set. To prevent this accidental packet drop, we need to make sure the second call to the BPF_SOCK_OPS_HDR_OPT_LEN_CB program reserves space not more than the first time. Since this cannot be done during verification time, we add a runtime sanity check to have bpf_reserve_hdr_opt return an error instead of causing packet drops later. Signed-off-by: Amery Hung Co-developed-by: Zijian Zhang Signed-off-by: Zijian Zhang --- include/net/tcp.h | 8 ++++++++ net/ipv4/tcp_input.c | 8 -------- net/ipv4/tcp_output.c | 13 +++++++++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 2aac11e7e1cc..e202eeb19be4 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -1058,6 +1058,14 @@ static inline int tcp_skb_mss(const struct sk_buff *skb) return TCP_SKB_CB(skb)->tcp_gso_size; } +/* I wish gso_size would have a bit more sane initialization than + * something-or-zero which complicates things + */ +static inline int tcp_skb_seglen(const struct sk_buff *skb) +{ + return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb); +} + static inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb) { return likely(!TCP_SKB_CB(skb)->eor); diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index e37488d3453f..c1ffe19b0717 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -1550,14 +1550,6 @@ static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev, return true; } -/* I wish gso_size would have a bit more sane initialization than - * something-or-zero which complicates things - */ -static int tcp_skb_seglen(const struct sk_buff *skb) -{ - return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb); -} - /* Shifting pages past head area doesn't work */ static int skb_can_shift(const struct sk_buff *skb) { diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 16c48df8df4c..f5996cdbb2ba 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -1033,10 +1033,19 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb if (unlikely(BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG))) { unsigned int remaining = MAX_TCP_OPTION_SPACE - size; + unsigned int old_remaining; - bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts, &remaining); + if (skb) { + unsigned int reserved_opt_spc; + + reserved_opt_spc = tp->mss_cache - tcp_skb_seglen(skb); + if (reserved_opt_spc < remaining) + remaining = reserved_opt_spc; + } - size = MAX_TCP_OPTION_SPACE - remaining; + old_remaining = remaining; + bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts, &remaining); + size += old_remaining - remaining; } return size; From patchwork Tue Aug 27 00:14:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zijian Zhang X-Patchwork-Id: 13778674 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D95B133E8 for ; Tue, 27 Aug 2024 00:16:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724717793; cv=none; b=EtzTRPQ8tSFXvaAin4/ghcvhVGHff+dt7bqP/7WQZdY7k/0NBT+W+booQhQg4iMbnxxbihdDRANNBqcCXbAPfMRw0owi92A2u1go/aqWqnT7msVnAwik+Rc46ZDBJSRDt83IuuT4M1fDxs7L5JEMugIlGY8eztlXPfovnqN1C1U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724717793; c=relaxed/simple; bh=RxdtVQym2vCbSh2nlk82aJAXg9u7EZsZm+vr5Jo9ilw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k3HFk5ZHalYRd4aNsTx8Iv9VGMmCzsfq1llEFFuu4/nWltvKhQStzY5hnorN93nIiz9sKZY9DPrBj7BJ6/ceRSh7t73LOEcwRq40sjoDjJSymUQnTJLsxI2VVFVk4Rcf3/u4tFL12tn3NfDYEWf6/hTMhHUlYpsgVIMlQ1fEKdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=WVXLE2+2; arc=none smtp.client-ip=209.85.219.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="WVXLE2+2" Received: by mail-yb1-f177.google.com with SMTP id 3f1490d57ef6-e1205de17aaso5143828276.2 for ; Mon, 26 Aug 2024 17:16:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724717789; x=1725322589; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CyzDIeO0OSMdXGqEylULwMz5dVtw2flzpxSJxYZ7Qq4=; b=WVXLE2+2Q6h/KCbahRdUwY3iCGQTIvXQi2Rj3ydytToybIzK7q/bqIj+gsclTo0FK9 cj0IjBz75E5T1m0yCaVoGCgEYDJ4QPp74sYndPwoPaqjszQf4HBBWMuXdUKRz76WxEUt pIVygWAQwRoP0Av3wffPMPzcDxMnCi7KEWpNkEqkq8bQ2num+ji9Eiis3HLbp6fG1Kmp huEjuGIkirq0uydQJgaDKcxY0yR51y31YItO0b/j1M90mdfzID3mF61B59X8/Pfl+rhn jxL4n/5bKRAc2JryTpZko05RfVFLcWneOpTPFP/JviTu5vXfxlMOVU9HhZ0eB8cqLpu5 AGvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724717789; x=1725322589; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CyzDIeO0OSMdXGqEylULwMz5dVtw2flzpxSJxYZ7Qq4=; b=S7IGy3FKVtKwqZEJjsL8lsWTzoDN0qgFCvC7/IvuawLSYDiOkljzV0ElgjY0wawn3x IF51RpDNhY4pJ5/Blh60SIKU+O1Vc8NNNUSkKKfFxbOPZI5xMR2gebF7eHWWb6e2/Due yyBoEyy9tHThQrqMAqsj62MmYG3c4eTwvtBu1/xYwbPgw37Wbr0xSmDiBUJLa8qmB3eI 8cXtTadyeRHt9qyEhLTr6ZfkNRIe5WqQFT5+7RKoa/q/fL3anbg/wYHemnBxh67+TwDC sEXxecjzsqHK1Ov4c7GzOqCJ4R8IhDphDdyhFoL/cXHGH4v5EGtRxZeesYiZNGvIBCvd LS1A== X-Gm-Message-State: AOJu0YwzN52rJi6A2ynXi3x+sROysd7q5WgGdZfN+RMDqu4vjlM2Qmn4 onOaksJwF+0ExzCb8OftA8a5A23MEIC8kWcx1uC87Vpk/pgDm107f5YJN8clkbuEo0800hn/D5W U X-Google-Smtp-Source: AGHT+IEL8YHhMYqZkllj/y8QVubYFEFdE3QxRKNCOhCH9XwJoeplSU3bEyGEppCuN50zLSZfGFqjXw== X-Received: by 2002:a05:6902:c03:b0:e0b:e50a:e7d6 with SMTP id 3f1490d57ef6-e17a869c4c3mr11751314276.57.1724717789492; Mon, 26 Aug 2024 17:16:29 -0700 (PDT) Received: from n191-036-066.byted.org ([130.44.212.119]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c162dcda66sm51387136d6.122.2024.08.26.17.16.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Aug 2024 17:16:29 -0700 (PDT) From: zijianzhang@bytedance.com To: bpf@vger.kernel.org Cc: edumazet@google.com, davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, mykolal@fb.com, shuah@kernel.org, xiyou.wangcong@gmail.com, wangdongdong.6@bytedance.com, zhoufeng.zf@bytedance.com, Amery Hung , Zijian Zhang Subject: [PATCH bpf-next v1 2/2] bpf: selftests: reserve smaller tcp header options than the actual size Date: Tue, 27 Aug 2024 00:14:07 +0000 Message-Id: <20240827001407.2476854-3-zijianzhang@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240827001407.2476854-1-zijianzhang@bytedance.com> References: <20240827001407.2476854-1-zijianzhang@bytedance.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Amery Hung If eBPF users mistakenly reserve smaller header options than the actual size in BPF_SOCK_OPS_HDR_OPT_LEN_CB, bpf_reserve_hdr_opt should return an appropriate error value, and there will be no packet dropping. Signed-off-by: Zijian Zhang Signed-off-by: Amery Hung --- .../bpf/prog_tests/tcp_hdr_options.c | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c b/tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c index 56685fc03c7e..9c250b5bf00a 100644 --- a/tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +++ b/tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c @@ -14,6 +14,7 @@ #include "test_tcp_hdr_options.h" #include "test_tcp_hdr_options.skel.h" #include "test_misc_tcp_hdr_options.skel.h" +#include "test_reserve_tcp_hdr_options.skel.h" #define LO_ADDR6 "::1" #define CG_NAME "/tcpbpf-hdr-opt-test" @@ -25,6 +26,7 @@ static struct bpf_test_option exp_active_fin_in; static struct hdr_stg exp_passive_hdr_stg; static struct hdr_stg exp_active_hdr_stg = { .active = true, }; +static struct test_reserve_tcp_hdr_options *reserve_skel; static struct test_misc_tcp_hdr_options *misc_skel; static struct test_tcp_hdr_options *skel; static int lport_linum_map_fd; @@ -513,6 +515,49 @@ static void misc(void) bpf_link__destroy(link); } +static void reserve_hdr_opt(void) +{ + struct bpf_link *link; + struct sk_fds sk_fds; + char send_msg[1500]; + char recv_msg[sizeof(send_msg)]; + int ret; + + if (!ASSERT_OK(system("ip link set dev lo mtu 1500"), "set dev lo mtu to 1500")) + return; + + lport_linum_map_fd = bpf_map__fd(reserve_skel->maps.lport_linum_map); + + link = bpf_program__attach_cgroup(reserve_skel->progs.reserve_tcp_hdr_options, cg_fd); + if (!ASSERT_OK_PTR(link, "attach_cgroup(reserve_tcp_hdr_options)")) + return; + + if (sk_fds_connect(&sk_fds, false)) { + bpf_link__destroy(link); + return; + } + + ret = send(sk_fds.active_fd, send_msg, sizeof(send_msg), + MSG_EOR); + if (!ASSERT_EQ(ret, sizeof(send_msg), "send(msg)")) + goto check_linum; + + ret = read(sk_fds.passive_fd, recv_msg, sizeof(recv_msg)); + if (!ASSERT_EQ(ret, sizeof(send_msg), "read(msg)")) + goto check_linum; + + if (sk_fds_shutdown(&sk_fds)) + goto check_linum; + + ASSERT_FALSE(reserve_skel->bss->nr_err_reserve, "unexpected nr_err_reserve"); + ASSERT_TRUE(reserve_skel->bss->nr_nospc, "unexpected nr_nospc"); + +check_linum: + ASSERT_FALSE(check_error_linum(&sk_fds), "check_error_linum"); + sk_fds_close(&sk_fds); + bpf_link__destroy(link); +} + struct test { const char *desc; void (*run)(void); @@ -526,6 +571,7 @@ static struct test tests[] = { DEF_TEST(fastopen_estab), DEF_TEST(fin), DEF_TEST(misc), + DEF_TEST(reserve_hdr_opt), }; void test_tcp_hdr_options(void) @@ -540,6 +586,10 @@ void test_tcp_hdr_options(void) if (!ASSERT_OK_PTR(misc_skel, "open and load misc test skel")) goto skel_destroy; + reserve_skel = test_reserve_tcp_hdr_options__open_and_load(); + if (!ASSERT_OK_PTR(reserve_skel, "open and load reserve test skel")) + goto skel_destroy; + cg_fd = test__join_cgroup(CG_NAME); if (!ASSERT_GE(cg_fd, 0, "join_cgroup")) goto skel_destroy; @@ -558,6 +608,7 @@ void test_tcp_hdr_options(void) close(cg_fd); skel_destroy: + test_reserve_tcp_hdr_options__destroy(reserve_skel); test_misc_tcp_hdr_options__destroy(misc_skel); test_tcp_hdr_options__destroy(skel); }