]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
tls: Avoid evaluating freed skb in tls_sw_read_sock() loop
authorChuck Lever <chuck.lever@oracle.com>
Thu, 4 Jun 2026 17:48:24 +0000 (13:48 -0400)
committerJakub Kicinski <kuba@kernel.org>
Tue, 9 Jun 2026 03:10:20 +0000 (20:10 -0700)
tls_sw_read_sock() ends its receive loop with while (skb), but
the else branch in the body calls consume_skb(skb) before the
predicate is re-evaluated. A pointer becomes indeterminate when
the object it points to reaches end-of-lifetime (C2011 6.2.4p2),
and using an indeterminate value is undefined behavior (Annex
J.2). The pointer is not dereferenced today -- the predicate
either exits the loop or skb is overwritten at the top of the
next iteration -- but any future change that adds a dereference
between consume_skb() and the predicate would silently introduce
a use-after-free.

Replace the do/while form with an explicit for(;;) loop so
termination happens through a break statement rather than
predicate evaluation of a freed pointer.

Cc: Sagi Grimberg <sagi@grimberg.me>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
Reviewed-by: Hannes Reinecke <hare@kernel.org>
Reviewed-by: Sabrina Dubroca <sd@queasysnail.net>
Link: https://patch.msgid.link/20260604-tls-read-sock-v12-1-b114efa6e3e2@oracle.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/tls/tls_sw.c

index 964ebc268ee46e79cc4244a7970237c7a7a367c0..8e4e57721335498542da5af645aaa385a40d998a 100644 (file)
@@ -2383,7 +2383,7 @@ int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,
                goto read_sock_end;
 
        decrypted = 0;
-       do {
+       for (;;) {
                if (!skb_queue_empty(&ctx->rx_list)) {
                        skb = __skb_dequeue(&ctx->rx_list);
                        rxm = strp_msg(skb);
@@ -2435,9 +2435,9 @@ int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,
                } else {
                        consume_skb(skb);
                        if (!desc->count)
-                               skb = NULL;
+                               break;
                }
-       } while (skb);
+       }
 
 read_sock_end:
        tls_rx_reader_release(sk, ctx);