Skip to contentSkip to conversation

ax25: fix reference counting issue of ax25_dev

PATCH In review Duoming Zhou 3 participants 9 messages May 2, 2024 – May 3, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover ax25: fix reference counting issue of ax25_dev

The first patch changes kfree in ax25_dev_free to ax25_dev_put, because the ax25_dev is managed by reference counting.

The second patch fixes potential reference counting leak issue in ax25_addr_ax25dev.

You can see the former discussion in the following link:
https://lore.kernel.org/netdev/[email protected]/

Duoming Zhou (2):
ax25: change kfree in ax25_dev_free to ax25_dev_free
ax25: fix potential reference counting leak in ax25_addr_ax25dev

 net/ax25/ax25_dev.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/2 ax25: change kfree in ax25_dev_free to ax25_dev_free

The ax25_dev is managed by reference counting, so it 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")
---
 net/ax25/ax25_dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ax25/ax25_dev.c +1 -1
index 282ec581c07..07723095c60 100644
@@ -208,7 +208,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);
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH net v2 1/2] ax25: change kfree in ax25_dev_free to ax25_dev_free

Quoted 3 lines

The ax25_dev is managed by reference counting, so it should not be deallocated directly by kfree() in ax25_dev_free(), replace it with ax25_dev_put() instead.

You repeated a wording mistake in the summary phrase from a previous cover letter. Please avoid confusion about desired code replacements.
How do you think about to append parentheses to involved function names?

Would you find the following change description a bit nicer?

The object “ax25_dev” is managed by reference counting.
Thus it should not be directly released by a kfree() call in ax25_dev_free(). Replace it with a ax25_dev_put() call instead.

Would you like to extend patch version descriptions (or changelogs) accordingly?

Regards,
Markus

#3 lore raw

Re: [PATCH net v2 1/2] ax25: change kfree in ax25_dev_free to ax25_dev_free

Quoted 5 lines · Markus Elfring
Quoted 3 lines

The ax25_dev is managed by reference counting, so it should not be deallocated directly by kfree() in ax25_dev_free(), replace it with ax25_dev_put() instead.

You repeated a wording mistake in the summary phrase from a previous cover letter.

Yeah. That's true. The subject should be changed to:

Subject: [PATCH] ax25: change kfree() in ax25_dev_free() to ax25_dev_put()

Another option would be:

Subject: [PATCH] ax25: use ax25_dev_put() in ax25_dev_free()

Otherwise the commit message is okay as-is.

regards,
dan carpenter

#4 lore raw

PATCH v2 2/2 ax25: fix potential reference counting leak in ax25_addr_ax25dev

In reply to Duoming Zhou

The reference counting 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 reference counting of ax25_dev once, when the res is not null.

Fixes d01ffb9eee4a ("ax25: add refcount in ax25_dev to avoid UAF bugs")
---
 net/ax25/ax25_dev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/ax25/ax25_dev.c +2 -1
index 07723095c60..7c2ea7309b0 100644
@@ -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;
Signature · 1 line
2.17.1
#5 lore raw

Re: [PATCH net v2 2/2] ax25: fix potential reference counting leak in ax25_addr_ax25dev

How do you think about to append parentheses to the function name in the summary phrase?

Quoted 5 lines

The reference counting 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 reference counting of ax25_dev once, when the res is not null.

Would you find the following change description a bit nicer?

The reference counter of the object “ax25_dev” can be increased multiple times in ax25_addr_ax25dev(). This will cause a memory leak so far.

Thus move a needed function call behind a for loop
and increase the reference counter only when the local variable “res” is not a null pointer.

…

Review ax25_dev *ax25_addr_ax25dev(ax25_address *addr)

Quoted 2 lines

+++ b/net/ax25/ax25_dev.c

(quoted diff) +0 -0
@@ -37,8 +37,9 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr)

…

Would you like to omit curly brackets in the affected function implementation?

Regards,
Markus

#6 lore raw

Re: [PATCH net v2 2/2] ax25: fix potential reference counting leak in ax25_addr_ax25dev

Yeah, it's true that we should delete the curly braces around the if block. Otherwise checkpatch.pl -f will complain.

The commit message is fine as-is. Please stop nit-picking.

regards,
dan carpenter

#7 lore raw

Re: [v2 2/2] ax25: fix potential reference counting leak in ax25_addr_ax25dev

Quoted 1 line

The commit message is fine as-is. Please stop nit-picking.

I dare to point recurring improvement possibilities out.
I became curious if the change acceptance will ever grow accordingly.

Regards,
Markus

Reviewed-by on the v2 series
#8 lore raw
In reply to Duoming Zhou
Quoted 9 lines · Duoming Zhou

The first patch changes kfree in ax25_dev_free to ax25_dev_put, because the ax25_dev is managed by reference counting.

The second patch fixes potential reference counting leak issue in ax25_addr_ax25dev.

You can see the former discussion in the following link:
https://lore.kernel.org/netdev/[email protected]/

regards,
dan carpenter