From nobody Fri Dec 19 14:35:21 2025 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A2B7291860 for ; Fri, 23 May 2025 13:19:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748006397; cv=none; b=Rnlosmc8O9qIJyHloMokm8LU4TIHMY0rngoIPU8kWBJ9Suer35BXiwjd0SgL5QcUDMxfbd8NtQnGlnXVLjSN73cbRDVBlhTvterhdwY4Mk3FXWHpVcTCMB7IM030/ISzGTxBUmHKS0x2DOJy03wFWbXq71rotp7qp0aEWzMB5RI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748006397; c=relaxed/simple; bh=cUVyWTx/HDZ+inIet40Ao5CCgMDXJWiH/SS6FNsjfhY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lvTs1MQPhN5c4Ebujm4Ryt5g/wNXOfl4Y5Bs8KwYrdGj/bDeJl5Mu09wJTbyz+fxAshED5eEJmqpRCz6Woih9aqCo3EF97FhvUrxmdPP167S4SlaJN0Dg3d5EhtAvWjfJRqChqI/9ijiVY1DsfbWpDCV0imTvtmJDfFJ5cBJJo4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=CCfTbLxg; arc=none smtp.client-ip=95.215.58.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="CCfTbLxg" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1748006393; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ugnhzW2LWyfY6qKIUf+gCbtelrrAjWgMGWIW+Vk3rkk=; b=CCfTbLxgpX5C82Lw3h7m2RS5TTahy+/pvVCHACKdQgk5VlaQNEZr7b5mzNyEoWylpDLclZ BpMXcIm9jvSgkNe9wtCwOKyYMjGRUlPY6opLgXGOQzepFmyaHd1Wr2KJ3lIqj7+1PTi8f1 TV3KLE8DgxDCnhX0vRmyeRZyHnIcJ3s= From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , Cong Wang , Boris Pismenny , John Fastabend , Jakub Kicinski , "David S. Miller" , Eric Dumazet , Paolo Abeni , Simon Horman , Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Ihor Solodrai , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v1 1/2] bpf,ktls: Fix data corruption when using bpf_msg_pop_data() in ktls Date: Fri, 23 May 2025 21:18:58 +0800 Message-ID: <20250523131915.19349-2-jiayuan.chen@linux.dev> In-Reply-To: <20250523131915.19349-1-jiayuan.chen@linux.dev> References: <20250523131915.19349-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" When sending plaintext data, we initially calculated the corresponding ciphertext length. However, if we later reduced the plaintext data length via socket policy, we failed to recalculate the ciphertext length. This results in transmitting buffers containing uninitialized data during ciphertext transmission. This causes uninitialized bytes to be appended after a complete "Application Data" packet, leading to errors on the receiving end when parsing TLS record. Fixes: d3b18ad31f93 ("tls: add bpf support to sk_msg handling") Reported-by: Cong Wang Signed-off-by: Jiayuan Chen Reviewed-by: John Fastabend --- net/tls/tls_sw.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c index fc88e34b7f33..b23a4655be6a 100644 --- a/net/tls/tls_sw.c +++ b/net/tls/tls_sw.c @@ -872,6 +872,21 @@ static int bpf_exec_tx_verdict(struct sk_msg *msg, str= uct sock *sk, delta =3D msg->sg.size; psock->eval =3D sk_psock_msg_verdict(sk, psock, msg); delta -=3D msg->sg.size; + + if ((s32)delta > 0) { + /* It indicates that we executed bpf_msg_pop_data(), + * causing the plaintext data size to decrease. + * Therefore the encrypted data size also needs to + * correspondingly decrease. We only need to subtract + * delta to calculate the new ciphertext length since + * ktls does not support block encryption. + */ + if (!WARN_ON_ONCE(!ctx->open_rec)) { + struct sk_msg *enc =3D &ctx->open_rec->msg_encrypted; + + sk_msg_trim(sk, enc, enc->sg.size - delta); + } + } } if (msg->cork_bytes && msg->cork_bytes > msg->sg.size && !enospc && !full_record) { --=20 2.47.1 From nobody Fri Dec 19 14:35:21 2025 Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BBB3292097 for ; Fri, 23 May 2025 13:20:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748006412; cv=none; b=us42pEonMP65EHQi5qQ2QHgGUH02Yrwq8qx01H2OLJvIMo05uLQDwklATDH/uF7iTX8ECupAmCPdOWVSG+/FepIYUaOXxNYrn8fHjWBB3Hs48DzGDWvqqg2q0Djh7wonpr777+XwbMFvGKSS16YNLtnHHpc/JBtq2JGrBZhiAF0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748006412; c=relaxed/simple; bh=iG0pvVHCIniSa6/K288JCYz9/zRHjQOB+W412xcg368=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aQEY7Q+IcJBq275ug/Q9XpBfoIsitufcUx7BQQKIv7loIBH47T4pNVyfkKmHYQSnQ9bI5AJtxnPagirYW9kVGCOd3BXwlpxKbiMBjUjD9KUet6hM3O5CasmnSIpCgOX6/rONWKbyek9JltnfFEapP0HqvER0yZItgdVNh1vp5kg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=mNJCnvPW; arc=none smtp.client-ip=95.215.58.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="mNJCnvPW" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1748006408; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TtCaT6fa/kicq1jIlhQVO8eyqlgwQfuRi8HxKyRVUBs=; b=mNJCnvPW0SRYYq9/bPn+7gDbbXwuFmw/XqeoRrlNnuNwxkXrR33Fq+TYBFRRmNNsXbOAMb NMs44y07xpu7A9dMFwAnKRQ4iizu+YN4XjF2lhiOe4vA5b1PXDjlEkowEe4/tGtE5vwvGa kOss1VjTg9bRmXYsUswSwgR1DPKCweE= From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , Boris Pismenny , John Fastabend , Jakub Kicinski , "David S. Miller" , Eric Dumazet , Paolo Abeni , Simon Horman , Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Ihor Solodrai , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v1 2/2] selftests/bpf: Add test to cover ktls with bpf_msg_pop_data Date: Fri, 23 May 2025 21:18:59 +0800 Message-ID: <20250523131915.19349-3-jiayuan.chen@linux.dev> In-Reply-To: <20250523131915.19349-1-jiayuan.chen@linux.dev> References: <20250523131915.19349-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" The selftest can reproduce an issue where using bpf_msg_pop_data() in ktls causes errors on the receiving end. Signed-off-by: Jiayuan Chen Reviewed-by: John Fastabend --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 91 +++++++++++++++++++ .../selftests/bpf/progs/test_sockmap_ktls.c | 4 + 2 files changed, 95 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/= testing/selftests/bpf/prog_tests/sockmap_ktls.c index b6c471da5c28..b87e7f39e15a 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -314,6 +314,95 @@ static void test_sockmap_ktls_tx_no_buf(int family, in= t sotype, bool push) test_sockmap_ktls__destroy(skel); } =20 +static void test_sockmap_ktls_tx_pop(int family, int sotype) +{ + char msg[37] =3D "0123456789abcdefghijklmnopqrstuvwxyz\0"; + int c =3D 0, p =3D 0, one =3D 1, sent, recvd; + struct test_sockmap_ktls *skel; + int prog_fd, map_fd; + char rcv[50] =3D {0}; + int err; + int i, m, r; + + skel =3D test_sockmap_ktls__open_and_load(); + if (!ASSERT_TRUE(skel, "open ktls skel")) + return; + + err =3D create_pair(family, sotype, &c, &p); + if (!ASSERT_OK(err, "create_pair()")) + goto out; + + prog_fd =3D bpf_program__fd(skel->progs.prog_sk_policy); + map_fd =3D bpf_map__fd(skel->maps.sock_map); + + err =3D bpf_prog_attach(prog_fd, map_fd, BPF_SK_MSG_VERDICT, 0); + if (!ASSERT_OK(err, "bpf_prog_attach sk msg")) + goto out; + + err =3D bpf_map_update_elem(map_fd, &one, &c, BPF_NOEXIST); + if (!ASSERT_OK(err, "bpf_map_update_elem(c)")) + goto out; + + err =3D init_ktls_pairs(c, p); + if (!ASSERT_OK(err, "init_ktls_pairs(c, p)")) + goto out; + + struct { + int pop_start; + int pop_len; + } pop_policy[] =3D { + /* trim the start */ + {0, 2}, + {0, 10}, + {1, 2}, + {1, 10}, + /* trim the end */ + {35, 2}, + /* New entries should be added before this line */ + {-1, -1}, + }; + + i =3D 0; + while (pop_policy[i].pop_start >=3D 0) { + skel->bss->pop_start =3D pop_policy[i].pop_start; + skel->bss->pop_end =3D pop_policy[i].pop_len; + + sent =3D send(c, msg, sizeof(msg), 0); + if (!ASSERT_EQ(sent, sizeof(msg), "send(msg)")) + goto out; + + recvd =3D recv_timeout(p, rcv, sizeof(rcv), MSG_DONTWAIT, 1); + if (!ASSERT_EQ(recvd, sizeof(msg) - pop_policy[i].pop_len, "pop len mism= atch")) + goto out; + + /* verify the data + * msg: 0123456789a bcdefghij klmnopqrstuvwxyz + * | | + * popped data + */ + for (m =3D 0, r =3D 0; m < sizeof(msg);) { + /* skip checking the data that has been popped */ + if (m >=3D pop_policy[i].pop_start && + m <=3D pop_policy[i].pop_start + pop_policy[i].pop_len - 1) { + m++; + continue; + } + + if (!ASSERT_EQ(msg[m], rcv[r], "data mismatch")) + goto out; + m++; + r++; + } + i++; + } +out: + if (c) + close(c); + if (p) + close(p); + test_sockmap_ktls__destroy(skel); +} + static void run_tests(int family, enum bpf_map_type map_type) { int map; @@ -338,6 +427,8 @@ static void run_ktls_test(int family, int sotype) test_sockmap_ktls_tx_cork(family, sotype, true); if (test__start_subtest("tls tx egress with no buf")) test_sockmap_ktls_tx_no_buf(family, sotype, true); + if (test__start_subtest("tls tx with pop")) + test_sockmap_ktls_tx_pop(family, sotype); } =20 void test_sockmap_ktls(void) diff --git a/tools/testing/selftests/bpf/progs/test_sockmap_ktls.c b/tools/= testing/selftests/bpf/progs/test_sockmap_ktls.c index 8bdb9987c0c7..83df4919c224 100644 --- a/tools/testing/selftests/bpf/progs/test_sockmap_ktls.c +++ b/tools/testing/selftests/bpf/progs/test_sockmap_ktls.c @@ -7,6 +7,8 @@ int cork_byte; int push_start; int push_end; int apply_bytes; +int pop_start; +int pop_end; =20 struct { __uint(type, BPF_MAP_TYPE_SOCKMAP); @@ -22,6 +24,8 @@ int prog_sk_policy(struct sk_msg_md *msg) bpf_msg_cork_bytes(msg, cork_byte); if (push_start > 0 && push_end > 0) bpf_msg_push_data(msg, push_start, push_end, 0); + if (pop_start >=3D 0 && pop_end > 0) + bpf_msg_pop_data(msg, pop_start, pop_end, 0); =20 return SK_PASS; } --=20 2.47.1