[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240501060218.32898-1-duoming@zju.edu.cn>
Date: Wed, 1 May 2024 14:02:18 +0800
From: Duoming Zhou <duoming@....edu.cn>
To: linux-hams@...r.kernel.org
Cc: netdev@...r.kernel.org,
linux-kernel@...r.kernel.org,
pabeni@...hat.com,
kuba@...nel.org,
edumazet@...gle.com,
davem@...emloft.net,
jreuter@...na.de,
dan.carpenter@...aro.org,
lars@...bit.com,
Duoming Zhou <duoming@....edu.cn>
Subject: [PATCH net] ax25: Fix refcount leak issues of ax25_dev
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@...aro.org>
Signed-off-by: Duoming Zhou <duoming@....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
Powered by blists - more mailing lists