[PATCH] Fix refcount bug in time scheduled paths

Hithashree Bojanala posted 1 patch 3 days, 4 hours ago
net/sctp/sm_sideeffect.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
[PATCH] Fix refcount bug in time scheduled paths
Posted by Hithashree Bojanala 3 days, 4 hours ago
The SCTP heartbeat timer callback can cause a refcount underflow when
rescheduling the timer. The issue occurs when mod_timer() is called
inside sctp_generate_heartbeat_event() to reschedule an already-pending
timer.

The current approach only takes a reference if mod_timer() returns 0
(timer was not pending). However, when rescheduling inside a timer
callback, we're consuming the reference that was held for the current
timer firing. If we reschedule without taking a new reference, the
subsequent timer callback will do sctp_transport_put() without a
corresponding hold, leading to refcount underflow.

The fix is to always take a reference when rescheduling inside a timer
callback, since the callback will always drop a reference at the end.

Reported-by: syzbot+e94b93511bda261f4c43@syzkaller.appspotmail.com
Fixes: https://syzkaller.appspot.com/bug?extid=e94b93511bda261f4c43
Signed-off-by: Hithashree Bojanala <bojanalahithashri@gmail.com>
---
 net/sctp/sm_sideeffect.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/net/sctp/sm_sideeffect.c b/net/sctp/sm_sideeffect.c
index 424f10a6fdba..733617781ed9 100644
--- a/net/sctp/sm_sideeffect.c
+++ b/net/sctp/sm_sideeffect.c
@@ -377,9 +377,10 @@ void sctp_generate_heartbeat_event(struct timer_list *t)
 	if (sock_owned_by_user(sk)) {
 		pr_debug("%s: sock is busy\n", __func__);
 
-		/* Try again later.  */
-		if (!mod_timer(&transport->hb_timer, jiffies + (HZ/20)))
-			sctp_transport_hold(transport);
+		/* Always hold a reference when rescheduling inside timer callback
+		* because this callback will put the reference at the end */
+		sctp_transport_hold(transport);
+		mod_timer(&transport->hb_timer, jiffies + (HZ/20));
 		goto out_unlock;
 	}
 
@@ -388,8 +389,10 @@ void sctp_generate_heartbeat_event(struct timer_list *t)
 	timeout = sctp_transport_timeout(transport);
 	if (elapsed < timeout) {
 		elapsed = timeout - elapsed;
-		if (!mod_timer(&transport->hb_timer, jiffies + elapsed))
-			sctp_transport_hold(transport);
+		/* Always hold a reference when rescheduling inside timer callback
+		* because this callback will put the reference at the end*/
+		sctp_transport_hold(transport);
+		mod_timer(&transport->hb_timer, jiffies + elapsed);
 		goto out_unlock;
 	}
 
-- 
2.47.0
Re: [PATCH] Fix refcount bug in time scheduled paths
Posted by Eric Dumazet 3 days, 4 hours ago
On Fri, Nov 28, 2025 at 8:40 AM Hithashree Bojanala
<bojanalahithashri@gmail.com> wrote:
>
> The SCTP heartbeat timer callback can cause a refcount underflow when
> rescheduling the timer. The issue occurs when mod_timer() is called
> inside sctp_generate_heartbeat_event() to reschedule an already-pending
> timer.
>
> The current approach only takes a reference if mod_timer() returns 0
> (timer was not pending). However, when rescheduling inside a timer
> callback, we're consuming the reference that was held for the current
> timer firing. If we reschedule without taking a new reference, the
> subsequent timer callback will do sctp_transport_put() without a
> corresponding hold, leading to refcount underflow.
>
> The fix is to always take a reference when rescheduling inside a timer
> callback, since the callback will always drop a reference at the end.
>
> Reported-by: syzbot+e94b93511bda261f4c43@syzkaller.appspotmail.com
> Fixes: https://syzkaller.appspot.com/bug?extid=e94b93511bda261f4c43
> Signed-off-by: Hithashree Bojanala <bojanalahithashri@gmail.com>
> ---
>  net/sctp/sm_sideeffect.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/net/sctp/sm_sideeffect.c b/net/sctp/sm_sideeffect.c
> index 424f10a6fdba..733617781ed9 100644
> --- a/net/sctp/sm_sideeffect.c
> +++ b/net/sctp/sm_sideeffect.c
> @@ -377,9 +377,10 @@ void sctp_generate_heartbeat_event(struct timer_list *t)
>         if (sock_owned_by_user(sk)) {
>                 pr_debug("%s: sock is busy\n", __func__);
>
> -               /* Try again later.  */
> -               if (!mod_timer(&transport->hb_timer, jiffies + (HZ/20)))
> -                       sctp_transport_hold(transport);
> +               /* Always hold a reference when rescheduling inside timer callback
> +               * because this callback will put the reference at the end */
> +               sctp_transport_hold(transport);
> +               mod_timer(&transport->hb_timer, jiffies + (HZ/20));
>                 goto out_unlock;
>         }
>
> @@ -388,8 +389,10 @@ void sctp_generate_heartbeat_event(struct timer_list *t)
>         timeout = sctp_transport_timeout(transport);
>         if (elapsed < timeout) {
>                 elapsed = timeout - elapsed;
> -               if (!mod_timer(&transport->hb_timer, jiffies + elapsed))
> -                       sctp_transport_hold(transport);
> +               /* Always hold a reference when rescheduling inside timer callback
> +               * because this callback will put the reference at the end*/
> +               sctp_transport_hold(transport);
> +               mod_timer(&transport->hb_timer, jiffies + elapsed);
>                 goto out_unlock;
>         }

sk_reset_timer() has been using this construct for years, it can be
called from timer handlers just fine.

Can you explain how you have tested this patch ?

Beware that syzbot reports can sometimes point to some fine piece of
code, that can misbehave
if another layer did a random memory mangling.
Re: [PATCH] Fix refcount bug in time scheduled paths
Posted by Marcelo Ricardo Leitner 3 days, 3 hours ago
On Fri, Nov 28, 2025 at 09:05:05AM -0800, Eric Dumazet wrote:
> On Fri, Nov 28, 2025 at 8:40 AM Hithashree Bojanala
> <bojanalahithashri@gmail.com> wrote:
> >
> > The SCTP heartbeat timer callback can cause a refcount underflow when
> > rescheduling the timer. The issue occurs when mod_timer() is called
> > inside sctp_generate_heartbeat_event() to reschedule an already-pending
> > timer.
> >
> > The current approach only takes a reference if mod_timer() returns 0
> > (timer was not pending). However, when rescheduling inside a timer
> > callback, we're consuming the reference that was held for the current
> > timer firing. If we reschedule without taking a new reference, the
> > subsequent timer callback will do sctp_transport_put() without a
> > corresponding hold, leading to refcount underflow.
> >
> > The fix is to always take a reference when rescheduling inside a timer
> > callback, since the callback will always drop a reference at the end.
> >
> > Reported-by: syzbot+e94b93511bda261f4c43@syzkaller.appspotmail.com
> > Fixes: https://syzkaller.appspot.com/bug?extid=e94b93511bda261f4c43
> > Signed-off-by: Hithashree Bojanala <bojanalahithashri@gmail.com>
> > ---
> >  net/sctp/sm_sideeffect.c | 13 ++++++++-----
> >  1 file changed, 8 insertions(+), 5 deletions(-)
> >
> > diff --git a/net/sctp/sm_sideeffect.c b/net/sctp/sm_sideeffect.c
> > index 424f10a6fdba..733617781ed9 100644
> > --- a/net/sctp/sm_sideeffect.c
> > +++ b/net/sctp/sm_sideeffect.c
> > @@ -377,9 +377,10 @@ void sctp_generate_heartbeat_event(struct timer_list *t)
> >         if (sock_owned_by_user(sk)) {
> >                 pr_debug("%s: sock is busy\n", __func__);
> >
> > -               /* Try again later.  */
> > -               if (!mod_timer(&transport->hb_timer, jiffies + (HZ/20)))
> > -                       sctp_transport_hold(transport);
> > +               /* Always hold a reference when rescheduling inside timer callback
> > +               * because this callback will put the reference at the end */
> > +               sctp_transport_hold(transport);
> > +               mod_timer(&transport->hb_timer, jiffies + (HZ/20));
> >                 goto out_unlock;
> >         }
> >
> > @@ -388,8 +389,10 @@ void sctp_generate_heartbeat_event(struct timer_list *t)
> >         timeout = sctp_transport_timeout(transport);
> >         if (elapsed < timeout) {
> >                 elapsed = timeout - elapsed;
> > -               if (!mod_timer(&transport->hb_timer, jiffies + elapsed))
> > -                       sctp_transport_hold(transport);
> > +               /* Always hold a reference when rescheduling inside timer callback
> > +               * because this callback will put the reference at the end*/
> > +               sctp_transport_hold(transport);
> > +               mod_timer(&transport->hb_timer, jiffies + elapsed);
> >                 goto out_unlock;
> >         }
> 
> sk_reset_timer() has been using this construct for years, it can be
> called from timer handlers just fine.

I was just scratching my head here thinking "waaaat"... Thanks Eric :)

Btw, Hithashree, next time please don't forget to post networking
patches to netdev@ instead, otherwise they won't be picked up.

> 
> Can you explain how you have tested this patch ?
> 
> Beware that syzbot reports can sometimes point to some fine piece of
> code, that can misbehave
> if another layer did a random memory mangling.