All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC] ax25: Fix (more) netdev refcount issues
@ 2024-04-26 21:29 Lars Kellogg-Stedman
  2024-04-27  8:48 ` Dan Carpenter
  0 siblings, 1 reply; 6+ messages in thread
From: Lars Kellogg-Stedman @ 2024-04-26 21:29 UTC (permalink / raw)
  To: linux-hams

Folks,

I'm posting the following patch here before sending it to netdev in the
hopes that someone can take a look at the change and comment on the
correctness. This resolves all of the issues I've been experiencing
recently with ax.25.

...

When closing a socket, the ax.25 code releases references via
netdev_put() and ax25_dev_put(). In the case when the socket was the
result of an incoming connection, these references were never allocated in
the first place, causing underflows in both ax25_dev->refcount and
ax25_dev->dev->refcnt_tracker->untracked. This would result in a variety of
errors:

- After an initial connection and then again after several subsequent
  connections:

      refcount_t: decrement hit 0; leaking memory.

- After several subsequent connections:

      refcount_t: underflow; use-after-free.

A typical call trace for the above two issues would look like:

    Call Trace:
    <TASK>
    ? show_regs+0x64/0x70
    ? __warn+0x83/0x120
    ? refcount_warn_saturate+0xb2/0x100
    ? report_bug+0x158/0x190
    ? prb_read_valid+0x20/0x30
    ? handle_bug+0x3e/0x70
    ? exc_invalid_op+0x1c/0x70
    ? asm_exc_invalid_op+0x1f/0x30
    ? refcount_warn_saturate+0xb2/0x100
    ? refcount_warn_saturate+0xb2/0x100
    ax25_release+0x2ad/0x360
    __sock_release+0x35/0xa0
    sock_close+0x19/0x20
    [...]

On reboot, the kernel would get stuck in an infinite loop:

    unregister_netdevice: waiting for ax1 to become free. Usage count = 0

The attached patch corrects all three of the above problems by ensuring
that we call netdev_hold() and ax25_dev_hold() for incoming connections.

Signed-off-by: Lars Kellogg-Stedman <lars@oddbit.com>
---
 net/ax25/ax25_in.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/ax25/ax25_in.c b/net/ax25/ax25_in.c
index 1cac25aca63..35a55ad05f2 100644
--- a/net/ax25/ax25_in.c
+++ b/net/ax25/ax25_in.c
@@ -411,6 +411,8 @@ static int ax25_rcv(struct sk_buff *skb, struct net_device *dev,
 	ax25->state = AX25_STATE_3;
 
 	ax25_cb_add(ax25);
+	netdev_hold(ax25_dev->dev, &ax25->dev_tracker, GFP_ATOMIC);
+	ax25_dev_hold(ax25_dev);
 
 	ax25_start_heartbeat(ax25);
 	ax25_start_t3timer(ax25);
-- 
2.44.0

-- 
Lars Kellogg-Stedman <lars@oddbit.com> | larsks @ {irc,twitter,github}
http://blog.oddbit.com/                | N1LKS

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-04-30 10:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-26 21:29 [PATCH RFC] ax25: Fix (more) netdev refcount issues Lars Kellogg-Stedman
2024-04-27  8:48 ` Dan Carpenter
2024-04-27 17:15   ` Lars Kellogg-Stedman
2024-04-28 11:01     ` Dan Carpenter
2024-04-29 18:06       ` Lars Kellogg-Stedman
2024-04-30 10:08         ` Dan Carpenter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.