Re: [syzbot] [can?] WARNING: refcount bug in j1939_session_put

From: Edward Adam Davis
Date: Wed Aug 07 2024 - 03:01:36 EST


Fixes: c9c0ee5f20c5 ("net: skbuff: Skip early return in skb_unref when debugging")

Root cause: In commit c9c0ee5f20c5, There are following rules:
In debug builds (CONFIG_DEBUG_NET set), the reference count is always decremented, even when it's 1

This rule will cause the reference count to be 0 after calling skc_unref,
which will affect the release of skb.

#syz test: net-next 743ff02152bc

diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 4be73de5033c..712c0ae4f329 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -279,6 +279,8 @@ static void j1939_session_destroy(struct j1939_session *session)
while ((skb = skb_dequeue(&session->skb_queue)) != NULL) {
/* drop ref taken in j1939_session_skb_queue() */
skb_unref(skb);
+
+ printk("refcnt: %d, skb: %p, %s\n", refcount_read(&skb->users), skb, __func__);
kfree_skb(skb);
}
__j1939_session_drop(session);
@@ -341,6 +343,7 @@ static void j1939_session_skb_drop_old(struct j1939_session *session)
if ((do_skcb->offset + do_skb->len) < offset_start) {
__skb_unlink(do_skb, &session->skb_queue);
/* drop ref taken in j1939_session_skb_queue() */
+ printk("refcnt: %d, skb: %p, %s\n", refcount_read(&do_skb->users), do_skb, __func__);
skb_unref(do_skb);
spin_unlock_irqrestore(&session->skb_queue.lock, flags);

@@ -365,6 +368,7 @@ void j1939_session_skb_queue(struct j1939_session *session,
skcb->flags |= J1939_ECU_LOCAL_SRC;

skb_get(skb);
+ printk("refcnt: %d, skb: %p, %s\n", refcount_read(&skb->users), skb, __func__);
skb_queue_tail(&session->skb_queue, skb);
}

@@ -1505,7 +1509,7 @@ static struct j1939_session *j1939_session_new(struct j1939_priv *priv,
session->state = J1939_SESSION_NEW;

skb_queue_head_init(&session->skb_queue);
- skb_queue_tail(&session->skb_queue, skb);
+ j1939_session_skb_queue(session, skb);

skcb = j1939_skb_to_cb(skb);
memcpy(&session->skcb, skcb, sizeof(session->skcb));