Skip to contentSkip to conversation

tipc: fix the skb_unshare() in tipc_buf_append()

PATCH In review Cong Wang 3 participants 5 messages Oct 8, 2020 – Oct 10, 2020

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 tipc: fix the skb_unshare() in tipc_buf_append()

skb_unshare() drops a reference count on the old skb unconditionally, so in the failure case, we end up freeing the skb twice here. And because the skb is allocated in fclone and cloned by caller tipc_msg_reassemble(), the consequence is actually freeing the original skb too, thus triggered the UAF by syzbot.

Fix this by replacing this skb_unshare() with skb_cloned()+skb_copy().

Fixes ff48b6222e65 ("tipc: use skb_unshare() instead in tipc_buf_append()")
Reported-and-tested-by [email protected]
Cc Xin Long <[email protected]>
Cc Jon Maloy <[email protected]>
Cc Ying Xue <[email protected]>
---
 net/tipc/msg.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/tipc/msg.c +2 -1
index 52e93ba4d8e2..681224401871 100644
@@ -150,7 +150,8 @@ int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
 	if (fragid == FIRST_FRAGMENT) {
 		if (unlikely(head))
 			goto err;
-		frag = skb_unshare(frag, GFP_ATOMIC);
+		if (skb_cloned(frag))
+			frag = skb_copy(frag, GFP_ATOMIC);
 		if (unlikely(!frag))
 			goto err;
 		head = *headbuf = frag;
Signature · 1 line
2.28.0
#1 lore raw
Quoted 6 lines · Cong Wang

skb_unshare() drops a reference count on the old skb unconditionally, so in the failure case, we end up freeing the skb twice here. And because the skb is allocated in fclone and cloned by caller tipc_msg_reassemble(), the consequence is actually freeing the original skb too, thus triggered the UAF by syzbot.

Do you mean:

                frag = skb_clone(skb, GFP_ATOMIC);

frag = skb_unshare(frag) will free the 'skb' too?

Quoted 30 lines

Fix this by replacing this skb_unshare() with skb_cloned()+skb_copy().

Fixes ff48b6222e65 ("tipc: use skb_unshare() instead in tipc_buf_append()")
Reported-and-tested-by [email protected]
Cc Xin Long <[email protected]>
Cc Jon Maloy <[email protected]>
Cc Ying Xue <[email protected]>
---
 net/tipc/msg.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/tipc/msg.c +2 -2
index 52e93ba4d8e2..681224401871 100644
@@ -150,7 +150,8 @@ int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
        if (fragid == FIRST_FRAGMENT) {
                if (unlikely(head))
                        goto err;
-               frag = skb_unshare(frag, GFP_ATOMIC);
+               if (skb_cloned(frag))
+                       frag = skb_copy(frag, GFP_ATOMIC);
                if (unlikely(!frag))
                        goto err;
                head = *headbuf = frag;
--

2.28.0

author
#2 lore raw
Quoted 11 lines · Xin Long
Quoted 6 lines · Cong Wang

skb_unshare() drops a reference count on the old skb unconditionally, so in the failure case, we end up freeing the skb twice here. And because the skb is allocated in fclone and cloned by caller tipc_msg_reassemble(), the consequence is actually freeing the original skb too, thus triggered the UAF by syzbot.

Do you mean:

                frag = skb_clone(skb, GFP_ATOMIC);

frag = skb_unshare(frag) will free the 'skb' too?

Yes, more precisely, I mean:

new = skb_clone(old)
kfree_skb(new)
kfree_skb(new)

would free 'old' eventually when 'old' is a fast clone. The skb_clone() sets ->fclone_ref to 2 and returns the clone, whose skb->fclone is SKB_FCLONE_CLONE. So, the first call of kfree_skbmem() will just decrease ->fclone_ref by 1, but the second call will trigger kmem_cache_free() which frees _both_ skb's.

Thanks.

Reviewed-by
#3 lore raw
Quoted 20 lines · Cong Wang
Quoted 11 lines · Xin Long
Quoted 6 lines · Cong Wang

skb_unshare() drops a reference count on the old skb unconditionally, so in the failure case, we end up freeing the skb twice here. And because the skb is allocated in fclone and cloned by caller tipc_msg_reassemble(), the consequence is actually freeing the original skb too, thus triggered the UAF by syzbot.

Do you mean:

                frag = skb_clone(skb, GFP_ATOMIC);

frag = skb_unshare(frag) will free the 'skb' too?

Yes, more precisely, I mean:

new = skb_clone(old)
kfree_skb(new)
kfree_skb(new)

Quoted 5 lines

would free 'old' eventually when 'old' is a fast clone. The skb_clone() sets ->fclone_ref to 2 and returns the clone, whose skb->fclone is SKB_FCLONE_CLONE. So, the first call of kfree_skbmem() will just decrease ->fclone_ref by 1, but the second call will trigger kmem_cache_free() which frees _both_ skb's.

Thanks. Didn't notice kfree_skb 'buf' on the err path.

maintainer
#4 lore raw
In reply to Cong Wang
Quoted 6 lines · On Wed, 7 Oct 2020 21:12:50 -0700 Cong Wang wrote:

skb_unshare() drops a reference count on the old skb unconditionally, so in the failure case, we end up freeing the skb twice here. And because the skb is allocated in fclone and cloned by caller tipc_msg_reassemble(), the consequence is actually freeing the original skb too, thus triggered the UAF by syzbot.

Quoted 8 lines

Fix this by replacing this skb_unshare() with skb_cloned()+skb_copy().

Fixes ff48b6222e65 ("tipc: use skb_unshare() instead in tipc_buf_append()")
Reported-and-tested-by [email protected]
Cc Xin Long <[email protected]>
Cc Jon Maloy <[email protected]>
Cc Ying Xue <[email protected]>

Applied and queued for stable, thank you!