IDR fix for potential id mismatch
Subsystems (4)
cover IDR fix for potential id mismatch
When debugging an issue found in drm subsystem (link to the discussion in Link tag), a bug was found in idr library where requesting id in range would return id outside requested range. Didn't see in documentation that this is how idr should behave.
This is an RFC as this library is deprecated but still in use by other subsystems. Is this fix proper?
---
Jan Sokolowski (1):
idr: do not create idr if new id would be outside given range
lib/idr.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
Signature · 1 line
2.43.0
RFC PATCH 1/1 idr: do not create idr if new id would be outside given range
A scenario was found where trying to add id in range 0,1 would return an id of 2, which is outside the range and thus now what the user would expect.
Return -EINVAL if new id would fall outside the range.
--- lib/idr.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
lib/idr.c +12 -0
@@ -74,6 +74,7 @@ EXPORT_SYMBOL_GPL(idr_alloc_u32);* exclude simultaneous writers. * * Return: The newly allocated ID, -ENOMEM if memory allocation failed, +* -EINVAL is start value is less than 0 or if new id would be in wrong range, * or -ENOSPC if no free IDs could be found. */ int idr_alloc (struct idr *idr,void *ptr,int start,int end,gfp_t gfp)
@@ -88,6 +89,11 @@ int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)if (ret)return ret; +if (WARN_ON_ONCE (id < start || (id >= end && end !=0 ))) { +idr_remove (idr, id); +return -EINVAL ; + } +return id; }EXPORT_SYMBOL_GPL (idr_alloc);
@@ -112,6 +118,7 @@ EXPORT_SYMBOL_GPL(idr_alloc);* exclude simultaneous writers. * * Return: The newly allocated ID, -ENOMEM if memory allocation failed, +* -EINVAL if new id would be in wrong range, * or -ENOSPC if no free IDs could be found. */ int idr_alloc_cyclic (struct idr *idr,void *ptr,int start,int end,gfp_t gfp)
@@ -130,6 +137,11 @@ int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)if (err)return err; +if (WARN_ON_ONCE (id < start || (id >= end && end !=0 ))) { +idr_remove (idr, id); +return -EINVAL ; + } + idr->idr_next = id +1 ;return id; }
Signature · 1 line
2.43.0
Matthew Wilcox
maintainerRe: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
lib/idr.c int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp) · line 95
Quoted 8 lines · Jan Sokolowski
lib/idr.c +4
-0@@ -88,6 +89,11 @@ int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)if (ret)return ret; +if (WARN_ON_ONCE (id < start || (id >= end && end !=0 ))) { +idr_remove (idr, id); +return -EINVAL ; + }
This is certainly the wrong way to fix any problem that does exist. And it should return -ENOSPC, not -EINVAL. -EINVAL is "the arguments are wrong", not "the data structure is full".
I'll go read the thread now.
Matthew Wilcox
maintainerRe: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
In reply to Jan SokolowskiQuoted 3 lines · Jan Sokolowski
A scenario was found where trying to add id in range 0,1 would return an id of 2, which is outside the range and thus now what the user would expect.
Can you do a bit better with this bug report? Under what circumstances does this happen? Preferably answer in the form of a test case for the IDR test suite. Here's my attempt to recreate your situation based on what I read in that thread. It doesn't show a problem, so clearly I got something wrong.
To run the test suite, apply this patch, then
$ make -C tools/testing/radix-tree $ ./tools/testing/radix-tree/idr-test
tools/testing/radix-tree/idr-test.c +16 -0
@@ -57,6 +57,21 @@ void idr_alloc_test(void)idr_destroy (&idr); } +void idr_alloc2_test (void ) +{ +int id; +DEFINE_IDR (idr); + + id =idr_alloc (&idr, idr_alloc2_test,0 ,1 ,GFP_KERNEL ); +printf ("id = %d\n" , id); +assert (id ==0 ); + id =idr_alloc (&idr, idr_alloc2_test,0 ,1 ,GFP_KERNEL ); +printf ("id = %d\n" , id); +assert (id == -ENOSPC ); + +idr_destroy (&idr); +} +void idr_replace_test (void ) {DEFINE_IDR (idr);
@@ -409,6 +424,7 @@ void idr_checks(void)idr_replace_test ();idr_alloc_test (); +idr_alloc2_test ();idr_null_test ();idr_nowait_test ();idr_get_next_test (0 );
Re: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
Quoted 10 lines · Matthew Wilcox
Quoted 3 lines · Jan Sokolowski
A scenario was found where trying to add id in range 0,1 would return an id of 2, which is outside the range and thus now what the user would expect.
Can you do a bit better with this bug report? Under what circumstances does this happen? Preferably answer in the form of a test case for the IDR test suite. Here's my attempt to recreate your situation based on what I read in that thread. It doesn't show a problem, so clearly I got something wrong.
According to Jan the observation he has is that this code:
idr_init_base(&idr, 1);
id = idr_alloc(&idr, dummy_ptr, 0, 1, GFP_KERNEL);
Gives him id=2 in return.
That clearly seems to be problematic considering that start=0 and end=1 should either give you id=0 or an error because idr->idr_base should be initialized to 1.
But I'm still not sure if Jan's observation is actually correct, cause I also don't see how that possible happen.
Regards,
Christian.
Quoted 39 lines
To run the test suite, apply this patch, then
$ make -C tools/testing/radix-tree $ ./tools/testing/radix-tree/idr-testtools/testing/radix-tree/idr-test.c +16
-0@@ -57,6 +57,21 @@ void idr_alloc_test(void)idr_destroy (&idr); } +void idr_alloc2_test (void ) +{ +int id; +DEFINE_IDR (idr); + + id =idr_alloc (&idr, idr_alloc2_test,0 ,1 ,GFP_KERNEL ); +printf ("id = %d\n" , id); +assert (id ==0 ); + id =idr_alloc (&idr, idr_alloc2_test,0 ,1 ,GFP_KERNEL ); +printf ("id = %d\n" , id); +assert (id == -ENOSPC ); + +idr_destroy (&idr); +} +void idr_replace_test (void ) {DEFINE_IDR (idr);@@ -409,6 +424,7 @@ void idr_checks(void)idr_replace_test ();idr_alloc_test (); +idr_alloc2_test ();idr_null_test ();idr_nowait_test ();idr_get_next_test (0 );
Matthew Wilcox
maintainerRe: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
Quoted 12 lines · Christian König
Quoted 10 lines · Matthew Wilcox
Quoted 3 lines · Jan Sokolowski
A scenario was found where trying to add id in range 0,1 would return an id of 2, which is outside the range and thus now what the user would expect.
Can you do a bit better with this bug report? Under what circumstances does this happen? Preferably answer in the form of a test case for the IDR test suite. Here's my attempt to recreate your situation based on what I read in that thread. It doesn't show a problem, so clearly I got something wrong.
Quoted 6 lines
According to Jan the observation he has is that this code:
idr_init_base(&idr, 1);
id =idr_alloc(&idr, dummy_ptr, 0, 1, GFP_KERNEL);Gives him id=2 in return.
Hm. That's not what it does for me. It gives me id == 1, which isn't correct! I'll look into that, but it'd be helpful to know what combination of inputs gives us 2.
To be completely clear, here's what I'm looking at:
+void idr_alloc2_test(void)
+{
+ int id;
+ struct idr idr = IDR_INIT_BASE(idr, 1);
+
+ id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL);
+ printf("id = %d\n", id);
and I think that should return -ENOSPC instead of 1, since we told it to allocate exclusive of 1.
Matthew Wilcox
maintainerRe: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
Quoted 3 lines · Matthew Wilcox
Hm. That's not what it does for me. It gives me id == 1, which isn't correct! I'll look into that, but it'd be helpful to know what combination of inputs gives us 2.
Oh, never mind, I see what's happening.
int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp);
so it's passing 0 as 'max' to idr_alloc_u32() which does:
slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base);
and max - base becomes -1 or rather ULONG_MAX, and so we'll literally allocate any number. If the first slot is full, we'll get back 1 and then add 'base' to it, giving 2.
Here's the new test-case:
+void idr_alloc2_test(void)
+{
+ int id;
+ struct idr idr = IDR_INIT_BASE(idr, 1);
+
+ id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL);
+ assert(id == -ENOSPC);
+
+ id = idr_alloc(&idr, idr_alloc2_test, 1, 2, GFP_KERNEL);
+ assert(id == 1);
+
+ id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL);
+ assert(id == -ENOSPC);
+
+ id = idr_alloc(&idr, idr_alloc2_test, 0, 2, GFP_KERNEL);
+ assert(id == -ENOSPC);
+
+ idr_destroy(&idr);
+}
and with this patch, it passes:
+++ b/lib/idr.c
@@ -40,6 +40,8 @@ int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid,
if (WARN_ON_ONCE(!(idr->idr_rt.xa_flags & ROOT_IS_IDR)))
idr->idr_rt.xa_flags |= IDR_RT_MARKER;+ if (max < base)
+ return -ENOSPC;
id = (id < base) ? 0 : id - base;
radix_tree_iter_init(&iter, id);Re: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
Quoted 9 lines · Matthew Wilcox
Quoted 3 lines · Matthew Wilcox
Hm. That's not what it does for me. It gives me id == 1, which isn't correct! I'll look into that, but it'd be helpful to know what combination of inputs gives us 2.
Oh, never mind, I see what's happening.
int
idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
Quoted 8 lines
ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp);so it's passing 0 as 'max' to
idr_alloc_u32()which does:slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base);and max - base becomes -1 or rather
ULONG_MAX, and so we'll literally allocate any number. If the first slot is full, we'll get back 1 and then add 'base' to it, giving 2.
Oh wow, not every day that we stumble over a bug in such a core kernel functionality.
Quoted 27 lines
Here's the new test-case:
(quoted diff) +19
-0+void idr_alloc2_test(void) +{ + int id; + struct idr idr = IDR_INIT_BASE(idr, 1); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL); + assert(id == -ENOSPC); + + id = idr_alloc(&idr, idr_alloc2_test, 1, 2, GFP_KERNEL); + assert(id == 1); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL); + assert(id == -ENOSPC); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 2, GFP_KERNEL); + assert(id == -ENOSPC); + + idr_destroy(&idr); +}and with this patch, it passes:
+++
b/lib/idr.c
Review int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid, · line 47
Quoted 8 lines
(quoted diff) +2
-0@@ -40,6 +40,8 @@ int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid, if (WARN_ON_ONCE(!(idr->idr_rt.xa_flags & ROOT_IS_IDR))) idr->idr_rt.xa_flags |= IDR_RT_MARKER; + if (max < base) + return -ENOSPC; id = (id < base) ? 0 : id - base; radix_tree_iter_init(&iter, id);
Not sure what it's worth but feel free to add Reviewed-by: Christian König <[email protected]> to both the test case and the solution.
Thanks for the quick help,
Christian.
RE: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
In reply to Matthew WilcoxSo, shall I send a V2 of that patch and add you as co-developer there?
Regards
Jan
Quoted 62 lines
-----Original Message-----
From: Matthew Wilcox <[email protected]>
Sent: Thursday, November 27, 2025 3:55 PM
To: Christian König <[email protected]>
Cc: Sokolowski, Jan <[email protected]>; linux- [email protected]; Andrew Morton <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given rangeQuoted 3 lines · Matthew Wilcox
Hm. That's not what it does for me. It gives me id == 1, which isn't correct! I'll look into that, but it'd be helpful to know what combination of inputs gives us 2.
Oh, never mind, I see what's happening.
int
idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp);so it's passing 0 as 'max' to
idr_alloc_u32()which does:slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base);and max - base becomes -1 or rather
ULONG_MAX, and so we'll literally allocate any number. If the first slot is full, we'll get back 1 and then add 'base' to it, giving 2.Here's the new test-case:
(quoted diff) +19
-0+void idr_alloc2_test(void) +{ + int id; + struct idr idr = IDR_INIT_BASE(idr, 1); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL); + assert(id == -ENOSPC); + + id = idr_alloc(&idr, idr_alloc2_test, 1, 2, GFP_KERNEL); + assert(id == 1); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL); + assert(id == -ENOSPC); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 2, GFP_KERNEL); + assert(id == -ENOSPC); + + idr_destroy(&idr); +}and with this patch, it passes:
+++
b/lib/idr.c(quoted diff) +2
-0@@ -40,6 +40,8 @@ int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid, if (WARN_ON_ONCE(!(idr->idr_rt.xa_flags & ROOT_IS_IDR))) idr->idr_rt.xa_flags |= IDR_RT_MARKER; + if (max < base) + return -ENOSPC; id = (id < base) ? 0 : id - base; radix_tree_iter_init(&iter, id);
Matthew Wilcox
maintainerRe: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
Quoted 1 line · Jan
So, shall I send a V2 of that patch and add you as co-developer there?
No. You didn't co-develop anything. You reported the bug, badly.
What I'm trying to do right now is figure out what the syzbot report actually was. In all the DRM specialness, you've lost the original information, so I can't add the original syzbot links. All I can find is https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/6449
which doesn't link to a syzbot report, so that's a dead end.
Quoted 66 lines
Regards
JanQuoted 62 lines
-----Original Message-----
From: Matthew Wilcox <[email protected]>
Sent: Thursday, November 27, 2025 3:55 PM
To: Christian König <[email protected]>
Cc: Sokolowski, Jan <[email protected]>; linux- [email protected]; Andrew Morton <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given rangeQuoted 3 lines · Matthew Wilcox
Hm. That's not what it does for me. It gives me id == 1, which isn't correct! I'll look into that, but it'd be helpful to know what combination of inputs gives us 2.
Oh, never mind, I see what's happening.
int
idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp);so it's passing 0 as 'max' to
idr_alloc_u32()which does:slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base);and max - base becomes -1 or rather
ULONG_MAX, and so we'll literally allocate any number. If the first slot is full, we'll get back 1 and then add 'base' to it, giving 2.Here's the new test-case:
(quoted diff) +19
-0+void idr_alloc2_test(void) +{ + int id; + struct idr idr = IDR_INIT_BASE(idr, 1); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL); + assert(id == -ENOSPC); + + id = idr_alloc(&idr, idr_alloc2_test, 1, 2, GFP_KERNEL); + assert(id == 1); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL); + assert(id == -ENOSPC); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 2, GFP_KERNEL); + assert(id == -ENOSPC); + + idr_destroy(&idr); +}and with this patch, it passes:
+++
b/lib/idr.c(quoted diff) +2
-0@@ -40,6 +40,8 @@ int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid, if (WARN_ON_ONCE(!(idr->idr_rt.xa_flags & ROOT_IS_IDR))) idr->idr_rt.xa_flags |= IDR_RT_MARKER; + if (max < base) + return -ENOSPC; id = (id < base) ? 0 : id - base; radix_tree_iter_init(&iter, id);
RE: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
Quoted 12 lines
-----Original Message-----
From: Matthew Wilcox <[email protected]>
Sent: Friday, November 28, 2025 4:52 PM
To: Sokolowski, Jan <[email protected]>
Cc: Christian König <[email protected]>; linux- [email protected]; Andrew Morton <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
Quoted 5 lines
Quoted 1 line · Jan
So, shall I send a V2 of that patch and add you as co-developer there?
No. You didn't co-develop anything. You reported the bug, badly.
And I've sent a potential patch on how it should've been fixed. That should count for something, right?
Quoted 74 lines
What I'm trying to do right now is figure out what the syzbot report actually was. In all the DRM specialness, you've lost the original information, so I can't add the original syzbot links. All I can find is https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/6449
which doesn't link to a syzbot report, so that's a dead end.Quoted 11 lines
Regards
JanQuoted 8 lines
-----Original Message-----
From: Matthew Wilcox <[email protected]>
Sent: Thursday, November 27, 2025 3:55 PM
To: Christian König <[email protected]>
Cc: Sokolowski, Jan <[email protected]>; linux- [email protected]; Andrew Morton <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH 1/1] idr: do not create idr if new id would beoutside
Quoted 42 lines
Quoted 42 lines
given range
Quoted 3 lines · Matthew Wilcox
Hm. That's not what it does for me. It gives me id == 1, which isn't correct! I'll look into that, but it'd be helpful to know what combination of inputs gives us 2.
Oh, never mind, I see what's happening.
int
idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp);so it's passing 0 as 'max' to
idr_alloc_u32()which does:slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base);and max - base becomes -1 or rather
ULONG_MAX, and so we'll literally allocate any number. If the first slot is full, we'll get back 1 and then add 'base' to it, giving 2.Here's the new test-case:
(quoted diff) +19
-0+void idr_alloc2_test(void) +{ + int id; + struct idr idr = IDR_INIT_BASE(idr, 1); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL); + assert(id == -ENOSPC); + + id = idr_alloc(&idr, idr_alloc2_test, 1, 2, GFP_KERNEL); + assert(id == 1); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 1, GFP_KERNEL); + assert(id == -ENOSPC); + + id = idr_alloc(&idr, idr_alloc2_test, 0, 2, GFP_KERNEL); + assert(id == -ENOSPC); + + idr_destroy(&idr); +}Quoted 4 lines
Review int idr_alloc_u32(struct idr *idr, void *ptr, u32
Quoted 4 lines
and with this patch, it passes:
+++
b/lib/idr.c(quoted diff) +0
-0@@ -40,6 +40,8 @@ int idr_alloc_u32(struct idr *idr, void *ptr, u32*nextid,
Quoted 9 lines
Quoted 8 lines
(quoted diff) +2
-0if (WARN_ON_ONCE(!(idr->idr_rt.xa_flags & ROOT_IS_IDR))) idr->idr_rt.xa_flags |= IDR_RT_MARKER; + if (max < base) + return -ENOSPC; id = (id < base) ? 0 : id - base; radix_tree_iter_init(&iter, id);
Matthew Wilcox
maintainerRe: [RFC PATCH 1/1] idr: do not create idr if new id would be outside given range
Quoted 3 lines · Jan
Quoted 2 lines
No. You didn't co-develop anything. You reported the bug, badly.
And I've sent a potential patch on how it should've been fixed. That should count for something, right?
Literally everything about that patch was wrong. If I'd used any of it, you'd have a point, but the entire approach was wrong.
You _can't_allow the allocation to succeed and then undo it. There might be an RCU-protected reader which would see the intermediate state. And that's an inconsistency we guarantee can't happen; an RCU reader can see the state before the lock, the state after the lock. It must not see a state that never happened.
If you'd written a test case, I'd happily add a co-developed-by tag. But you didn't do that either.