Skip to contentSkip to conversation

NFC: Fix error handling in nfc_genl_dump_targets

PATCH In review Ma Ke 2 participants 3 messages Nov 21, 2025 – Nov 24, 2025

Subsystems (4)
lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH 1/1 NFC: Fix error handling in nfc_genl_dump_targets

nfc_genl_dump_targets() increments the device reference count via nfc_get_device() but fails to decrement it properly. nfc_get_device() calls class_find_device() which internally calls get_device() to increment the reference count. No corresponding put_device() is made to decrement the reference count.

Add proper reference count decrementing using nfc_put_device() when the dump operation completes or encounters an error, ensuring balanced reference counting.

Found by code review.

Fixes 4d12b8b129f1 ("NFC: add nfc generic netlink interface")
---
 net/nfc/netlink.c | 5 +++++
 1 file changed, 5 insertions(+)
net/nfc/netlink.c +5 -0
index a18e2c503da6..9ae138ee91dd 100644
@@ -159,6 +159,11 @@ static int nfc_genl_dump_targets(struct sk_buff *skb,
 
 	cb->args[0] = i;
 
+	if (rc < 0 || i >= dev->n_targets) {
+		nfc_put_device(dev);
+		cb->args[1] = 0;
+	}
+
 	return skb->len;
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 1 line · Ma Ke

nfc_genl_dump_targets() increments the device reference count via

Only in some cases, but you drop it unconditionally.

Quoted 10 lines

nfc_get_device() but fails to decrement it properly. nfc_get_device() calls class_find_device() which internally calls get_device() to increment the reference count. No corresponding put_device() is made to decrement the reference count.

Add proper reference count decrementing using nfc_put_device() when the dump operation completes or encounters an error, ensuring balanced reference counting.

Found by code review.

Drop, there is no point nor need to say that humans did the work. This actually rather suggests you used LLM and disguise your finding as "code review".

No, LLM is not code review.

Quoted 13 lines
Fixes 4d12b8b129f1 ("NFC: add nfc generic netlink interface")
---
 net/nfc/netlink.c | 5 +++++
 1 file changed, 5 insertions(+)
net/nfc/netlink.c +0 -0
index a18e2c503da6..9ae138ee91dd 100644

net/nfc/netlink.c static int nfc_genl_dump_targets(struct sk_buff *skb, · line 164

Quoted 6 lines
net/nfc/netlink.c +3 -0
@@ -159,6 +159,11 @@ static int nfc_genl_dump_targets(struct sk_buff *skb,
 
 	cb->args[0] = i;
 
+	if (rc < 0 || i >= dev->n_targets) {
+		nfc_put_device(dev);
+		cb->args[1] = 0;

Did you test it?

Best regards,
Krzysztof

#2 lore raw
Quoted 14 lines · Krzysztof Kozlowski
Quoted 1 line · Ma Ke

nfc_genl_dump_targets() increments the device reference count via

Only in some cases, but you drop it unconditionally.

Quoted 9 lines

nfc_get_device() but fails to decrement it properly. nfc_get_device() calls class_find_device() which internally calls get_device() to increment the reference count. No corresponding put_device() is made to decrement the reference count.

Add proper reference count decrementing using nfc_put_device() when the dump operation completes or encounters an error, ensuring balanced reference counting.

Quoted 7 lines
Quoted 1 line

Found by code review.

Drop, there is no point nor need to say that humans did the work. This actually rather suggests you used LLM and disguise your finding as "code review".

No, LLM is not code review.

Looks like LLM.

Quoted 15 lines
Quoted 15 lines
Fixes 4d12b8b129f1 ("NFC: add nfc generic netlink interface")
---
 net/nfc/netlink.c | 5 +++++
 1 file changed, 5 insertions(+)
net/nfc/netlink.c +0 -0
index a18e2c503da6..9ae138ee91dd 100644
@@ -159,6 +159,11 @@ static int nfc_genl_dump_targets(struct sk_buff *skb,
 
Quoted 7 lines

net/nfc/netlink.c static int nfc_genl_dump_targets(struct sk_buff *skb, · line 164

Quoted 5 lines
net/nfc/netlink.c +3 -0
 	cb->args[0] = i;
 
+	if (rc < 0 || i >= dev->n_targets) {
+		nfc_put_device(dev);
+		cb->args[1] = 0;

Did you test it?

I am pretty sure this is double put and thus bug. There is put in done().

Best regards,
Krzysztof