Linux HAM/Amateur Radio development
 help / color / mirror / Atom feed
* [PATCH net] ax25: Fix refcount leak issues of ax25_dev
@ 2024-05-01  6:02 Duoming Zhou
  2024-05-01 17:33 ` Markus Elfring
                   ` (3 more replies)
  0 siblings, 4 replies; 58+ messages in thread
From: Duoming Zhou @ 2024-05-01  6:02 UTC (permalink / raw)
  To: linux-hams
  Cc: netdev, linux-kernel, pabeni, kuba, edumazet, davem, jreuter,
	dan.carpenter, lars, Duoming Zhou

There are two scenarios that might cause refcount leak
issues of ax25_dev.

Scenario one:

The refcount of ax25_dev potentially increase more
than once in ax25_addr_ax25dev(), which will cause
memory leak.

In order to fix the above issue, only increase the
refcount of ax25_dev once, when the res is not null.

Scenario two:

The original code sets the refcount of ax25_dev to 1
in the initial stage and then increase the refcount
when the ax25_dev is added to the ax25_dev_list. As a
result, the refcount of ax25_dev is 2. But when the
device is shutting down. The ax25_dev_device_down()
drops the refcount once or twice depending on if we
goto unlock_put or not, which will cause memory leak.

In order to mitigate the above issues, only increase
the refcount of ax25_dev when the ax25_dev is added
to the ax25_dev_list and decrease the refcount of
ax25_dev after it is removed from the ax25_dev_list.

What's more, the ax25_dev should not be deallocated
directly by kfree() in ax25_dev_free(), replace it
with ax25_dev_put() instead.

Fixes: d01ffb9eee4a ("ax25: add refcount in ax25_dev to avoid UAF bugs")
Reported by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
---
 net/ax25/ax25_dev.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/net/ax25/ax25_dev.c b/net/ax25/ax25_dev.c
index 282ec581c07..0e6dd98d3fa 100644
--- a/net/ax25/ax25_dev.c
+++ b/net/ax25/ax25_dev.c
@@ -37,8 +37,9 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
 	for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next)
 		if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
 			res = ax25_dev;
-			ax25_dev_hold(ax25_dev);
 		}
+	if (res)
+		ax25_dev_hold(res);
 	spin_unlock_bh(&ax25_dev_lock);
 
 	return res;
@@ -58,7 +59,6 @@ void ax25_dev_device_up(struct net_device *dev)
 		return;
 	}
 
-	refcount_set(&ax25_dev->refcount, 1);
 	dev->ax25_ptr     = ax25_dev;
 	ax25_dev->dev     = dev;
 	netdev_hold(dev, &ax25_dev->dev_tracker, GFP_KERNEL);
@@ -88,7 +88,7 @@ void ax25_dev_device_up(struct net_device *dev)
 	ax25_dev->next = ax25_dev_list;
 	ax25_dev_list  = ax25_dev;
 	spin_unlock_bh(&ax25_dev_lock);
-	ax25_dev_hold(ax25_dev);
+	refcount_set(&ax25_dev->refcount, 1);
 
 	ax25_register_dev_sysctl(ax25_dev);
 }
@@ -135,7 +135,6 @@ void ax25_dev_device_down(struct net_device *dev)
 
 unlock_put:
 	spin_unlock_bh(&ax25_dev_lock);
-	ax25_dev_put(ax25_dev);
 	dev->ax25_ptr = NULL;
 	netdev_put(dev, &ax25_dev->dev_tracker);
 	ax25_dev_put(ax25_dev);
@@ -208,7 +207,7 @@ void __exit ax25_dev_free(void)
 		s        = ax25_dev;
 		netdev_put(ax25_dev->dev, &ax25_dev->dev_tracker);
 		ax25_dev = ax25_dev->next;
-		kfree(s);
+		ax25_dev_put(s);
 	}
 	ax25_dev_list = NULL;
 	spin_unlock_bh(&ax25_dev_lock);
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 58+ messages in thread
* Kernel 6.9.1 AX.25 Crash
@ 2024-05-20  2:55 Chris Maness
  0 siblings, 0 replies; 58+ messages in thread
From: Chris Maness @ 2024-05-20  2:55 UTC (permalink / raw)
  To: linux-hams

Since you guys have been working on this, I have started playing
around with slackware-current to check on your progress.  I setup
LinFBB with one pseudo tty  port generated with ax25ipd.  It does
release the socket after connection, and worked for a couple of hours.
However, it crashed hard with no terminal dump when I was forwarding
some bulls over AX.25 over UDP to the ax25ipd.  This does involve the
AX.25 stack as FBB needs an ax0 port.

If anyone is interested in reproducing, I can give you the details on
how I triggered it.

-Chris KQ6UP

-- 
Thanks,
Chris Maness

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

end of thread, other threads:[~2024-06-08 15:23 UTC | newest]

Thread overview: 58+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-01  6:02 [PATCH net] ax25: Fix refcount leak issues of ax25_dev Duoming Zhou
2024-05-01 17:33 ` Markus Elfring
2024-05-01 17:43 ` Dan Carpenter
2024-05-02  4:35   ` duoming
2024-05-02  7:56     ` Dan Carpenter
2024-05-02  9:30       ` Paolo Abeni
2024-05-02  1:29 ` Lars Kellogg-Stedman
2024-05-03 20:36   ` Dan Carpenter
2024-05-03 23:40     ` Lars Kellogg-Stedman
2024-05-04 12:16       ` Dan Carpenter
2024-05-04 22:16         ` Lars Kellogg-Stedman
2024-05-07  3:18           ` Lars Kellogg-Stedman
2024-05-07  8:08             ` Dan Carpenter
2024-05-07  9:04               ` duoming
2024-05-08 18:27                 ` Dan Carpenter
2024-05-09  1:40               ` duoming
2024-05-23 12:30               ` Lars Kellogg-Stedman
2024-05-07  6:38     ` Dan Carpenter
2024-05-15  9:52   ` duoming
2024-05-16 19:20     ` Lars Kellogg-Stedman
     [not found]       ` <CANnsUMHTZ_P4-C2iGdbakcp_Xk5c-aCO5kYEvaBdOcsaSnK5Pg@mail.gmail.com>
2024-05-20 14:42         ` Kernel 6.9.1 AX.25 Crash Lars Kellogg-Stedman
2024-05-20 14:49           ` Chris Maness
2024-05-21  0:11           ` Chris Maness
2024-05-21  0:48             ` Lars Kellogg-Stedman
2024-05-21  1:38               ` Chris Maness
2024-05-21  2:12               ` Dan Cross
2024-05-21  3:07                 ` Chris Maness
2024-05-21 11:47                   ` Dave Hibberd
2024-05-21 12:03                     ` Chris Maness
2024-05-21 19:20                       ` Mike Quin
2024-05-21 23:02                         ` Chris Maness
2024-05-21 23:07                           ` Mike Quin
2024-05-21 23:17                             ` Chris Maness
2024-05-21 15:53               ` David Ranch
2024-05-21 16:28                 ` Chris Maness
2024-05-21 18:12                 ` Lars Kellogg-Stedman
2024-05-22 15:32                   ` Dan Cross
2024-05-22 16:07                     ` Chris Maness
2024-05-22 17:06                       ` David Ranch
2024-05-22 18:04                         ` Dan Cross
2024-05-22 17:54                       ` Dan Cross
2024-05-22 18:17                     ` Lars Kellogg-Stedman
2024-05-22 18:37                       ` Dan Cross
2024-05-22 19:23                         ` Lars Kellogg-Stedman
2024-05-22 22:22                           ` David Ranch
2024-05-22 22:32                             ` Lars Kellogg-Stedman
2024-05-23  0:54                             ` Userspace AX.25 stack library [was Re: Kernel 6.9.1 AX.25 Crash] Stuart Longland VK4MSL
2024-05-23 16:32                               ` Dan Cross
2024-05-22 16:10                   ` Kernel 6.9.1 AX.25 Crash David Ranch
2024-05-22 18:13                     ` Lars Kellogg-Stedman
2024-06-07 12:01                   ` Lars Kellogg-Stedman
2024-06-07 13:49                     ` David Ranch
2024-06-08  1:59                       ` Lars Kellogg-Stedman
2024-06-08 15:23                         ` David Ranch
2024-05-23 10:30               ` Dan Carpenter
2024-05-27 17:59           ` Chris Maness
2024-05-04 11:04 ` [PATCH net] ax25: Fix refcount leak issues of ax25_dev Dan Carpenter
  -- strict thread matches above, loose matches on Subject: below --
2024-05-20  2:55 Kernel 6.9.1 AX.25 Crash Chris Maness

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox