Skip to contentSkip to conversation

lib/idr: fix infinite loop in idr_get_next()

PATCH Superseded Josh Law 2 participants 3 messages Mar 12

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Josh Law

author
#0 lore raw

PATCH v2 1/1 lib/idr: fix infinite loop in idr_get_next()

From: Josh Law <[email protected]>

In idr_get_next(), if the returned id from idr_get_next_ul() is greater than INT_MAX, the function issues a warning and returns NULL without updating the *nextid pointer. This causes a soft lockup for any caller iterating over an IDR (e.g. via idr_for_each_entry) because they will receive NULL, fail to advance their index, and repeatedly query the same state forever.

Fix this by setting *nextid to INT_MAX when the bounds check fails, ensuring the caller's iteration will terminate.

---
 lib/idr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/idr.c +3 -1
index f25bd2b9e9a4..07098eb4ddc3 100644
@@ -268,8 +268,10 @@ void *idr_get_next(struct idr *idr, int *nextid)
 	unsigned long id = *nextid;
 	void *entry = idr_get_next_ul(idr, &id);
 
-	if (WARN_ON_ONCE(id > INT_MAX))
+	if (WARN_ON_ONCE(id > INT_MAX)) {
+		*nextid = INT_MAX;
 		return NULL;
+	}
 	*nextid = id;
 	return entry;
 }
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 11 lines · Josh Law

From: Josh Law <[email protected]>

In idr_get_next(), if the returned id from idr_get_next_ul() is greater than INT_MAX, the function issues a warning and returns NULL without updating the *nextid pointer. This causes a soft lockup for any caller iterating over an IDR (e.g. via idr_for_each_entry) because they will receive NULL, fail to advance their index, and repeatedly query the same state forever.

Fix this by setting *nextid to INT_MAX when the bounds check fails, ensuring the caller's iteration will terminate.

Can you add a test-case for this? And the IDA patch you also sent?

author
#2 lore raw

12 Mar 2026 17:26:44 Matthew Wilcox <[email protected]>:

Quoted 10 lines · Josh Law
Quoted 9 lines

From: Josh Law <[email protected]>

In idr_get_next(), if the returned id from idr_get_next_ul() is greater than INT_MAX, the function issues a warning and returns NULL without updating the *nextid pointer. This causes a soft lockup for any caller iterating over an IDR (e.g. via idr_for_each_entry) because they will receive NULL, fail to advance their index, and repeatedly query the same state forever.

Quoted 4 lines
Quoted 2 lines

Fix this by setting *nextid to INT_MAX when the bounds check fails, ensuring the caller's iteration will terminate.

Can you add a test-case for this?  And the IDA patch you also sent?

Lmao you and your test cases, absolutely ill add some test cases