linux-hams.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Duoming Zhou <duoming@zju.edu.cn>
To: linux-hams@vger.kernel.org
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	pabeni@redhat.com, kuba@kernel.org, edumazet@google.com,
	davem@davemloft.net, jreuter@yaina.de, dan.carpenter@linaro.org,
	lars@oddbit.com, Duoming Zhou <duoming@zju.edu.cn>
Subject: [PATCH net] ax25: Fix refcount leak issues of ax25_dev
Date: Wed,  1 May 2024 14:02:18 +0800	[thread overview]
Message-ID: <20240501060218.32898-1-duoming@zju.edu.cn> (raw)

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


             reply	other threads:[~2024-05-01  6:02 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-01  6:02 Duoming Zhou [this message]
2024-05-01 17:33 ` [PATCH net] ax25: Fix refcount leak issues of ax25_dev 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-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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240501060218.32898-1-duoming@zju.edu.cn \
    --to=duoming@zju.edu.cn \
    --cc=dan.carpenter@linaro.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=jreuter@yaina.de \
    --cc=kuba@kernel.org \
    --cc=lars@oddbit.com \
    --cc=linux-hams@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).