Skip to content

Commit 799cbb1

Browse files
mrpregregkh
authored andcommitted
bpf, ktls: Fix data corruption when using bpf_msg_pop_data() in ktls
[ Upstream commit 178f6a5 ] 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: d3b18ad ("tls: add bpf support to sk_msg handling") Reported-by: Cong Wang <[email protected]> Signed-off-by: Jiayuan Chen <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: John Fastabend <[email protected]> Acked-by: Jakub Kicinski <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Sasha Levin <[email protected]>
1 parent 30ed50a commit 799cbb1

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

net/tls/tls_sw.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,19 @@ static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
872872
delta = msg->sg.size;
873873
psock->eval = sk_psock_msg_verdict(sk, psock, msg);
874874
delta -= msg->sg.size;
875+
876+
if ((s32)delta > 0) {
877+
/* It indicates that we executed bpf_msg_pop_data(),
878+
* causing the plaintext data size to decrease.
879+
* Therefore the encrypted data size also needs to
880+
* correspondingly decrease. We only need to subtract
881+
* delta to calculate the new ciphertext length since
882+
* ktls does not support block encryption.
883+
*/
884+
struct sk_msg *enc = &ctx->open_rec->msg_encrypted;
885+
886+
sk_msg_trim(sk, enc, enc->sg.size - delta);
887+
}
875888
}
876889
if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
877890
!enospc && !full_record) {

0 commit comments

Comments
 (0)