Skip to contentSkip to conversation

ax25: Fix issues of ax25_dev and net_device

PATCH Superseded Duoming Zhou 2 participants 5 messages May 9, 2024

Subsystems (2)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover ax25: Fix issues of ax25_dev and net_device

The first patch uses kernel universal linked list to implement ax25_dev_list, which makes the operation of the list easier. The second and third patch fix reference count leak issues of the object "ax25_dev" and "net_device".

Duoming Zhou (3):
ax25: Use kernel universal linked list to implement ax25_dev_list
ax25: Fix reference count leak issues of ax25_dev
ax25: Fix reference count leak issues of net_device

 include/net/ax25.h  |  3 +--
 net/ax25/ax25_dev.c | 50 ++++++++++++++++-----------------------------
 2 files changed, 19 insertions(+), 34 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v6 1/3 ax25: Use kernel universal linked list to implement ax25_dev_list

The origin ax25_dev_list implements its own single linked list, which is complicated and error-prone. For example, when deleting the node of ax25_dev_list in ax25_dev_device_down(), we have to operate on the head node and other nodes separately.

This patch uses kernel universal linked list to replace original ax25_dev_list, which make the operation of ax25_dev_list easier. There are two points that need to notice:

[1] We should add a check to judge whether the list is empty before INIT_LIST_HEAD in ax25_dev_device_up(), otherwise it will empty the list for each new ax25_dev added.

[2] We should do "dev->ax25_ptr = ax25_dev;" and "dev->ax25_ptr = NULL;" while holding the spinlock, otherwise the ax25_dev_device_up() and ax25_dev_device_down() could race, we're not guaranteed to find a match ax25_dev in ax25_dev_device_down().

---
 include/net/ax25.h  |  3 +--
 net/ax25/ax25_dev.c | 43 ++++++++++++++++++-------------------------
 2 files changed, 19 insertions(+), 27 deletions(-)
include/net/ax25.h +1 -2
index 0d939e5aee4..c2a85fd3f5e 100644
@@ -216,7 +216,7 @@ typedef struct {
 struct ctl_table;
 
 typedef struct ax25_dev {
-	struct ax25_dev		*next;
+	struct list_head	list;
 
 	struct net_device	*dev;
 	netdevice_tracker	dev_tracker;
@@ -330,7 +330,6 @@ int ax25_addr_size(const ax25_digi *);
 void ax25_digi_invert(const ax25_digi *, ax25_digi *);
 
 /* ax25_dev.c */
-extern ax25_dev *ax25_dev_list;
 extern spinlock_t ax25_dev_lock;
 
 #if IS_ENABLED(CONFIG_AX25)
net/ax25/ax25_dev.c +18 -25

105 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH net v6 1/3] ax25: Use kernel universal linked list to implement ax25_dev_list

net/ax25/ax25_dev.c void ax25_dev_device_up(struct net_device *dev) · line 57

Quoted 9 lines · Duoming Zhou
net/ax25/ax25_dev.c +2 -0
 		if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
 			res = ax25_dev;
 			ax25_dev_hold(ax25_dev);
@@ -52,6 +53,9 @@ void ax25_dev_device_up(struct net_device *dev)
 {
 	ax25_dev *ax25_dev;
 
+	/* Initialized the list for the first entry */
+	if (!ax25_dev_list.next)

will there be any case where this condition is true ? LIST_HEAD() or list_del() will never make this condition true.

Quoted 3 lines
net/ax25/ax25_dev.c +1 -0
+		INIT_LIST_HEAD(&ax25_dev_list);
 	ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_KERNEL);
 
#3 lore raw

PATCH v6 2/3 ax25: Fix reference count leak issues of ax25_dev

In reply to Duoming Zhou

The ax25_addr_ax25dev() and ax25_dev_device_down() exist a reference count leak issue of the object "ax25_dev".

Memory leak issue in ax25_addr_ax25dev():

The reference count of the object "ax25_dev" can be increased multiple times in ax25_addr_ax25dev(). This will cause a memory leak.

Memory leak issues in ax25_dev_device_down():

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

As for the issue of ax25_addr_ax25dev(), it is impossible for one pointer to be on a list twice. So add a break in ax25_addr_ax25dev(). As for the issue of ax25_dev_device_down(), increase the reference count of ax25_dev once in ax25_dev_device_up() and decrease the reference count of ax25_dev after it is removed from the ax25_dev_list.

Fixes d01ffb9eee4a ("ax25: add refcount in ax25_dev to avoid UAF bugs")
---
 net/ax25/ax25_dev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/ax25/ax25_dev.c +1 -2
index 56b14d240a6..37150f766a0 100644
@@ -39,6 +39,7 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
 		if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
 			res = ax25_dev;
 			ax25_dev_hold(ax25_dev);
+			break;
 		}
 	spin_unlock_bh(&ax25_dev_lock);
 
@@ -91,7 +92,6 @@ void ax25_dev_device_up(struct net_device *dev)
 	list_add(&ax25_dev->list, &ax25_dev_list);
 	dev->ax25_ptr     = ax25_dev;
 	spin_unlock_bh(&ax25_dev_lock);
-	ax25_dev_hold(ax25_dev);
 
 	ax25_register_dev_sysctl(ax25_dev);
 }
@@ -132,7 +132,6 @@ void ax25_dev_device_down(struct net_device *dev)
 unlock_put:
 	dev->ax25_ptr = NULL;
 	spin_unlock_bh(&ax25_dev_lock);
-	ax25_dev_put(ax25_dev);
 	netdev_put(dev, &ax25_dev->dev_tracker);
 	ax25_dev_put(ax25_dev);
 }
Signature · 1 line
2.17.1
#4 lore raw

PATCH v6 3/3 ax25: Fix reference count leak issues of net_device

In reply to Duoming Zhou

The ax25_dev_device_down() exists reference count leak issues of the object "net_device". When the ax25 device is shutting down. The ax25_dev_device_down() drops the reference count of net_device one or zero times depending on if we goto unlock_put or not, which will cause memory leak.

In order to solve the above issue, decrease the reference count of net_device after dev->ax25_ptr is set to null.

Fixes d01ffb9eee4a ("ax25: add refcount in ax25_dev to avoid UAF bugs")
---
 net/ax25/ax25_dev.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
net/ax25/ax25_dev.c +1 -7
index 37150f766a0..f37b143d69b 100644
@@ -121,15 +121,9 @@ void ax25_dev_device_down(struct net_device *dev)
 	list_for_each_entry(s, &ax25_dev_list, list) {
 		if (s == ax25_dev) {
 			list_del(&s->list);
-			goto unlock_put;
+			break;
 		}
 	}
-	dev->ax25_ptr = NULL;
-	spin_unlock_bh(&ax25_dev_lock);
-	ax25_dev_put(ax25_dev);
-	return;
-
-unlock_put:
 	dev->ax25_ptr = NULL;
 	spin_unlock_bh(&ax25_dev_lock);
 	netdev_put(dev, &ax25_dev->dev_tracker);
Signature · 1 line
2.17.1