From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) (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 83D5F29209A; Fri, 23 May 2025 13:19:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748006397; cv=none; b=l5e3ChuoD90WWsaq8gb1Mh7P4JQBwQUAST6/Ke9ME7gh0l85UgwnfvBBGqipCKrupIJ9wVxEnlMGZyB1h3qO6uIP0xoDvJImy0AjNBH7KXDQD7eCOqYN9rUrD6BYXvZnHOkdlmHjycLDjPoOlpE97didvPg2L6EiyTgdd199KDw= 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.176 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: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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 --- 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, struct sock *sk, delta = msg->sg.size; psock->eval = sk_psock_msg_verdict(sk, psock, msg); delta -= 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 = &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) { -- 2.47.1