]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
tipc: fix double-free in tipc_buf_append()
authorLee Jones <lee@kernel.org>
Tue, 21 Apr 2026 12:45:26 +0000 (13:45 +0100)
committerJakub Kicinski <kuba@kernel.org>
Thu, 23 Apr 2026 18:45:01 +0000 (11:45 -0700)
tipc_msg_validate() can potentially reallocate the skb it is validating,
freeing the old one.  In tipc_buf_append(), it was being called with a
pointer to a local variable which was a copy of the caller's skb
pointer.

If the skb was reallocated and validation subsequently failed, the error
handling path would free the original skb pointer, which had already
been freed, leading to double-free.

Fix this by checking if head now points to a newly allocated reassembled
skb.  If it does, reassign *headbuf for later freeing operations.

Fixes: d618d09a68e4 ("tipc: enforce valid ratio between skb truesize and contents")
Suggested-by: Tung Nguyen <tung.quang.nguyen@est.tech>
Signed-off-by: Lee Jones <lee@kernel.org>
Reviewed-by: Tung Nguyen <tung.quang.nguyen@est.tech>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/tipc/msg.c

index 76284fc538ebdd38d52c879bd4fc58c84abc6371..b0bba0feef564484a1d14ab0994dc69c5f2b16fd 100644 (file)
@@ -177,8 +177,20 @@ int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
 
        if (fragid == LAST_FRAGMENT) {
                TIPC_SKB_CB(head)->validated = 0;
-               if (unlikely(!tipc_msg_validate(&head)))
+
+               /* If the reassembled skb has been freed in
+                * tipc_msg_validate() because of an invalid truesize,
+                * then head will point to a newly allocated reassembled
+                * skb, while *headbuf points to freed reassembled skb.
+                * In such cases, correct *headbuf for freeing the newly
+                * allocated reassembled skb later.
+                */
+               if (unlikely(!tipc_msg_validate(&head))) {
+                       if (head != *headbuf)
+                               *headbuf = head;
                        goto err;
+               }
+
                *buf = head;
                TIPC_SKB_CB(head)->tail = NULL;
                *headbuf = NULL;