Skip to contentSkip to conversation

idr: prevent NULL deref on lookups before insertions

PATCH In review Sasha Levin 5 participants 26 messages Feb 20, 2013 – Feb 21, 2013

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

PATCH 1/1 idr: prevent NULL deref on lookups before insertions

'hint' will be NULL if we're looking up before adding anything to the IDR.

---
 include/linux/idr.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/idr.h +1 -1
index aed2a0c..a6f38b5 100644
@@ -113,7 +113,7 @@ static inline void *idr_find(struct idr *idr, int id)
 {
 	struct idr_layer *hint = rcu_dereference_raw(idr->hint);
 
-	if ((id & ~IDR_MASK) == hint->prefix)
+	if (hint && (id & ~IDR_MASK) == hint->prefix)
 		return rcu_dereference_raw(hint->ary[id & IDR_MASK]);
 
 	return idr_find_slowpath(idr, id);
Signature · 1 line
1.8.1.2
#1 lore raw

Hello, Sasha.

Quoted 4 lines · Sasha Levin

'hint' will be NULL if we're looking up before adding anything to the IDR.

Andy Shevchenko already posted fix (and free_layer() needs an update too).

Thanks.

Signature · 1 line
tejun
#2 lore raw
Quoted 9 lines · Tejun Heo

Hello, Sasha.

Quoted 4 lines · Sasha Levin

'hint' will be NULL if we're looking up before adding anything to the IDR.

Andy Shevchenko already posted fix (and free_layer() needs an update too).

Sorry about that, didn't see one on lkml so I've sent mine.

What about a patch that deals with:

[   67.992946] ------------[ cut here ]------------
[   67.994133] WARNING: at lib/idr.c:669 idr_find_slowpath+0x36/0x170()
[   67.999650] Modules linked in:
[   68.000469] Pid: 7156, comm: trinity Tainted: G        W    3.8.0-next-20130220-sasha-00006-g3d6f01c-dirty #6
[   68.005136] Call Trace:
[   68.005719]  [<ffffffff8110c96b>] warn_slowpath_common+0x8b/0xc0
[   68.011168]  [<ffffffff8110c9b5>] warn_slowpath_null+0x15/0x20
[   68.012488]  [<ffffffff81a23506>] idr_find_slowpath+0x36/0x170
[   68.013805]  [<ffffffff8113bbcf>] __lock_timer+0x12f/0x240
[   68.015686]  [<ffffffff8113baa0>] ? posix_timer_fn+0xd0/0xd0
[   68.016946]  [<ffffffff8113c64c>] sys_timer_delete+0x2c/0x180
[   68.018478]  [<ffffffff83dab0f5>] ? tracesys+0x7e/0xe6
[   68.019647]  [<ffffffff83dab158>] tracesys+0xe1/0xe6
[   68.020941] ---[ end trace 6d02b1896fa2505b ]---

Thanks,
Sasha

#3 lore raw

PATCH 1/1 posix-timer: don't call idr_find() w/ negative ID

Recent idr updates make idr_find() trigger WARN_ON_ONCE() before returning NULL when a negative ID is specified. Apparently, posix-timer::__lock_timer() was depending on idr_find() returning NULL on negative ID, thus triggering the new WARN_ON_ONCE(). Make __lock_timer() first check whether @timer_id is negative and return NULL without invoking idr_find() if so.

Note that the previous code was theoretically broken. idr_find() masked off the sign bit before performing lookup and if the matching IDs were in use, it would have returned pointer for the incorrect entry.

---
Sasha, can you please test whether this makes the warning go away?

Thanks.

 kernel/posix-timers.c |    3 +++
 1 file changed, 3 insertions(+)
kernel/posix-timers.c +3 -0
index b51bb08..92465f9 100644
@@ -637,6 +637,9 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
 {
 	struct k_itimer *timr;
 
+	if ((int)timer_id < 0)
+		return NULL;
+
 	rcu_read_lock();
 	timr = idr_find(&posix_timers_id, (int)timer_id);
 	if (timr) {
maintainer
#4 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

Quoted 17 lines · On Wed, 20 Feb 2013 13:01:16 -0800 Tejun Heo <[email protected]> wrote:

Recent idr updates make idr_find() trigger WARN_ON_ONCE() before returning NULL when a negative ID is specified. Apparently, posix-timer::__lock_timer() was depending on idr_find() returning NULL on negative ID, thus triggering the new WARN_ON_ONCE(). Make __lock_timer() first check whether @timer_id is negative and return NULL without invoking idr_find() if so.

Note that the previous code was theoretically broken. idr_find() masked off the sign bit before performing lookup and if the matching IDs were in use, it would have returned pointer for the incorrect entry.

...

kernel/posix-timers.c static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags) · line 645

Quoted 9 lines
kernel/posix-timers.c +3 -0
@@ -637,6 +637,9 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
 {
 	struct k_itimer *timr;
 
+	if ((int)timer_id < 0)
+		return NULL;
+
 	rcu_read_lock();
 	timr = idr_find(&posix_timers_id, (int)timer_id);
 	if (timr) {

This is a bit risky - if some arch defines timer_t to be a u64 then we will incorrectly treat 0x0000 0001 ffff ffff as a negative number. (That's a lot of timers!)

A fancy way of avoiding this is

	if (timer_id & ((typeof timer_id)1 << (sizeof(timer_id) - 1)))

(approximately ;))

But I think casting to (long) should be good enough?

#5 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

Hello, Andrew.

Quoted 15 lines · Andrew Morton

kernel/posix-timers.c static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags) · line 645

Quoted 10 lines
kernel/posix-timers.c +3 -0
@@ -637,6 +637,9 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
 {
 	struct k_itimer *timr;
 
+	if ((int)timer_id < 0)
+		return NULL;
+
 	rcu_read_lock();
 	timr = idr_find(&posix_timers_id, (int)timer_id);
 	if (timr) {

This is a bit risky - if some arch defines timer_t to be a u64 then we will incorrectly treat 0x0000 0001 ffff ffff as a negative number. (That's a lot of timers!)

Quoted 7 lines

A fancy way of avoiding this is

	if (timer_id & ((typeof timer_id)1 << (sizeof(timer_id) - 1)))

(approximately ;))

But I think casting to (long) should be good enough?

Sans WARN_ON_ONCE(), the code would behave the same as before, which in turn, from what I can tell, is the behavior the code intended to implement before idr_alloc() conversion.

If timer_id is being allocated from idr, a valid id can never go over INT_MAX and returning NULL for any ID above that is the correct behavior, I think. If timer_t is larger than int, both (int) and (long) castings wouldn't be useful. Both will miss (1LU << 33) + 1 and idr_find() will end up looking for 1.

If we want to be strict, we would have to do, I think,

if ((unsigned long long)timer_t > INT_MAX)

hopefully with some comments. That said, if I'm grepping it right, all archs define timer_t as int, so maybe we're just being paranoid.

Thanks.

Signature · 1 line
tejun
maintainer
#6 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

Quoted 36 lines · On Wed, 20 Feb 2013 13:37:01 -0800 Tejun Heo <[email protected]> wrote:

Hello, Andrew.

Quoted 15 lines · Andrew Morton

kernel/posix-timers.c static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags) · line 645

Quoted 10 lines
kernel/posix-timers.c +3 -0
@@ -637,6 +637,9 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
 {
 	struct k_itimer *timr;
 
+	if ((int)timer_id < 0)
+		return NULL;
+
 	rcu_read_lock();
 	timr = idr_find(&posix_timers_id, (int)timer_id);
 	if (timr) {

This is a bit risky - if some arch defines timer_t to be a u64 then we will incorrectly treat 0x0000 0001 ffff ffff as a negative number. (That's a lot of timers!)

Quoted 7 lines

A fancy way of avoiding this is

	if (timer_id & ((typeof timer_id)1 << (sizeof(timer_id) - 1)))

(approximately ;))

But I think casting to (long) should be good enough?

Sans WARN_ON_ONCE(), the code would behave the same as before, which in turn, from what I can tell, is the behavior the code intended to implement before idr_alloc() conversion.

If timer_id is being allocated from idr, a valid id can never go over INT_MAX and returning NULL for any ID above that is the correct behavior, I think. If timer_t is larger than int, both (int) and (long) castings wouldn't be useful. Both will miss (1LU << 33) + 1 and idr_find() will end up looking for 1.

Quoted 7 lines

If we want to be strict, we would have to do, I think,

if ((unsigned long long)timer_t > INT_MAX)

hopefully with some comments. That said, if I'm grepping it right, all archs define timer_t as int, so maybe we're just being paranoid.

Sure, it's unlikely to cause a problem in practice. Maybe five years from now, after idr has been cleaned up and switched to 64-bit, we've left a little hand grenade for someone. It would be good to future-safe it in some fashion.

I wonder if we should add some generic facility to handle this:

/*
* Query whether an unsigned type is `negative' when we don't know its size */
#define msb_is_set(v) { implementation goes here ;) }

Maybe not justified, dunno...

#7 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

Quoted 5 lines · Andrew Morton

Sure, it's unlikely to cause a problem in practice. Maybe five years from now, after idr has been cleaned up and switched to 64-bit, we've left a little hand grenade for someone. It would be good to future-safe it in some fashion.

Quoted 8 lines

I wonder if we should add some generic facility to handle this:

/*
* Query whether an unsigned type is `negative' when we don't know its size */
#define msb_is_set(v) { implementation goes here ;) }

Maybe not justified, dunno...

The thing is it's not only about MSB. Any bits beyond 1 << 31 are problematic, so we want a range test.

Thanks.

Signature · 1 line
tejun
#8 lore raw

PATCH 1/1 posix-timer: don't call idr_find() w/ out-of-range ID

When idr_find() is fed a negative ID, it used to look up the ID ignoring the sign bit before recent ("idr: remove MAX_IDR_MASK and move left MAX_IDR_* into idr.c") patch, and triggers WARN_ON_ONCE() after it.

__lock_timer() feeds timer_id from userland directly to idr_find() without sanitizing it which can trigger the above malfunctions. Add a range check on @timer_id before invoking idr_find() in __lock_timer().

While timer_t is defined as int by all archs at the moment, Andrew worries that it may be defined as a larger type later on. Make the test cover larger integers too so that it at least is guaranteed to not return the wrong timer.

---
 kernel/posix-timers.c |    7 +++++++
 1 file changed, 7 insertions(+)
kernel/posix-timers.c +7 -0
index b51bb08..6edbb2c 100644
@@ -637,6 +637,13 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
 {
 	struct k_itimer *timr;
 
+	/*
+	 * timer_t could be any type >= int and we want to make sure any
+	 * @timer_id outside positive int range fails lookup.
+	 */
+	if ((unsigned long long)timer_id > INT_MAX)
+		return NULL;
+
 	rcu_read_lock();
 	timr = idr_find(&posix_timers_id, (int)timer_id);
 	if (timr) {
#9 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ out-of-range ID

Quoted 5 lines · Tejun Heo

When idr_find() is fed a negative ID, it used to look up the ID ignoring the sign bit before recent ("idr: remove MAX_IDR_MASK and move left MAX_IDR_* into idr.c") patch, and triggers WARN_ON_ONCE() after it.

Quoted 8 lines

__lock_timer() feeds timer_id from userland directly to idr_find() without sanitizing it which can trigger the above malfunctions. Add a range check on @timer_id before invoking idr_find() in __lock_timer().

While timer_t is defined as int by all archs at the moment, Andrew worries that it may be defined as a larger type later on. Make the test cover larger integers too so that it at least is guaranteed to not return the wrong timer.

Again. I totaly agree with that for stable, but I disagree that this is the real fix.

idr_find() should simply return NULL, if "id < 0". Is it that hard?

Thanks,

tglx

#10 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ out-of-range ID

Hello, Thomas.

Quoted 1 line · Thomas Gleixner

idr_find() should simply return NULL, if "id < 0". Is it that hard?

It already does but w/ WARN_ON_ONCE(). The WARN_ON_ONCE() is there mostly as a transitional precaution. As the previous idr_find() behavior was extremely weird, just in case someone was depending on ignoring the msb for example, it would be nice to be able to identify the ones which are passing in negative ids, at least for a release cycle or two.

Thanks.

Signature · 1 line
tejun
#11 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ out-of-range ID

Quoted 11 lines · Tejun Heo

Hello, Thomas.

Quoted 1 line · Thomas Gleixner

idr_find() should simply return NULL, if "id < 0". Is it that hard?

It already does but w/ WARN_ON_ONCE(). The WARN_ON_ONCE() is there mostly as a transitional precaution. As the previous idr_find() behavior was extremely weird, just in case someone was depending on ignoring the msb for example, it would be nice to be able to identify the ones which are passing in negative ids, at least for a release cycle or two.

No objection, but please add this to the changelog and the comment.

Thanks,

tglx

#12 lore raw

PATCH 1/1 posix-timer: don't call idr_find() w/ out-of-range ID

In reply to Tejun Heo

When idr_find() is fed a negative ID, it used to look up the ID ignoring the sign bit before recent ("idr: remove MAX_IDR_MASK and move left MAX_IDR_* into idr.c") patch, and triggers WARN_ON_ONCE() after it.

__lock_timer() feeds timer_id from userland directly to idr_find() without sanitizing it which can trigger the above malfunctions. Add a range check on @timer_id before invoking idr_find() in __lock_timer().

While timer_t is defined as int by all archs at the moment, Andrew worries that it may be defined as a larger type later on. Make the test cover larger integers too so that it at least is guaranteed to not return the wrong timer.

Note that WARN_ON_ONCE() in idr_find() on id < 0 is transitional precaution while moving away from ignoring MSB. Once it's gone we can remove the guard as long as timer_t isn't larger than int.

---
Given that larger timer_t is possible, at least theoretically, it probably is better to keep the guard even if idr_find() is later updated, so not marking the guard as to be removed.

Will send a separate patch to add comment on top of WARN_ON_ONCE() in idr_find().

Thanks.

 kernel/posix-timers.c |    7 +++++++
 1 file changed, 7 insertions(+)
kernel/posix-timers.c +7 -0
index b51bb08..6edbb2c 100644
@@ -637,6 +637,13 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
 {
 	struct k_itimer *timr;
 
+	/*
+	 * timer_t could be any type >= int and we want to make sure any
+	 * @timer_id outside positive int range fails lookup.
+	 */
+	if ((unsigned long long)timer_id > INT_MAX)
+		return NULL;
+
 	rcu_read_lock();
 	timr = idr_find(&posix_timers_id, (int)timer_id);
 	if (timr) {
#13 lore raw

PATCH 1/1 idr: explain WARN_ON_ONCE() on negative IDs out-of-range ID

Until recently, when an negative ID is specified, idr functions used to ignore the sign bit and proceeded with the operation with the rest of bits, which is bizarre and error-prone. The behavior recently got changed so that negative IDs are treated as invalid but we're triggering WARN_ON_ONCE() on negative IDs just in case somebody was depending on the sign bit being ignored, so that those can be detected and fixed easily.

We only need this for a while. Explain why WARN_ON_ONCE()s are there and that they can be removed later.

Cc Thomas Gleixner <[email protected]>
---
 lib/idr.c |   10 ++++++++++
 1 file changed, 10 insertions(+)
lib/idr.c +10 -0
index 5c772dc..134a61a 100644
@@ -569,6 +569,7 @@ void idr_remove(struct idr *idp, int id)
 	struct idr_layer *p;
 	struct idr_layer *to_free;
 
+	/* see comment in idr_find_slowpath() */
 	if (WARN_ON_ONCE(id < 0))
 		return;
 
@@ -666,6 +667,14 @@ void *idr_find_slowpath(struct idr *idp, int id)
 	int n;
 	struct idr_layer *p;
 
+	/*
+	 * If @id is negative, idr_find() used to ignore the sign bit and
+	 * performed lookup with the rest of bits, which is weird and can
+	 * lead to very obscure bugs.  We're now returning NULL for all
+	 * negative IDs but just in case somebody was depending on the sign
+	 * bit being ignored, let's trigger WARN_ON_ONCE() so that they can
+	 * be detected and fixed.  WARN_ON_ONCE() can later be removed.
+	 */
 	if (WARN_ON_ONCE(id < 0))
 		return NULL;
 
@@ -815,6 +824,7 @@ void *idr_replace(struct idr *idp, void *ptr, int id)
 	int n;
 	struct idr_layer *p, *old_p;
 
+	/* see comment in idr_find_slowpath() */
 	if (WARN_ON_ONCE(id < 0))
 		return ERR_PTR(-EINVAL);
 
Acked-by on v1 1/1
#14 lore raw

Re: [PATCH] idr: explain WARN_ON_ONCE() on negative IDs out-of-range ID

Quoted 8 lines · Tejun Heo

Until recently, when an negative ID is specified, idr functions used to ignore the sign bit and proceeded with the operation with the rest of bits, which is bizarre and error-prone. The behavior recently got changed so that negative IDs are treated as invalid but we're triggering WARN_ON_ONCE() on negative IDs just in case somebody was depending on the sign bit being ignored, so that those can be detected and fixed easily.

Quoted 4 lines

We only need this for a while. Explain why WARN_ON_ONCE()s are there and that they can be removed later.

#15 lore raw

[tip:timers/urgent] posix-timer: Don't call idr_find() with out-of-range ID

In reply to Tejun Heo
posix-timer: Don't call idr_find() with out-of-range ID
Commit-ID:  e182bb38d7db7494fa5dcd82da17fe0dedf60ecf
Gitweb:     http://git.kernel.org/tip/e182bb38d7db7494fa5dcd82da17fe0dedf60ecf
Author:     Tejun Heo <[email protected]>
AuthorDate: Wed, 20 Feb 2013 15:24:12 -0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 21 Feb 2013 17:28:29 +0100

posix-timer: Don't call idr_find() with out-of-range ID

When idr_find() was fed a negative ID, it used to look up the ID ignoring the sign bit before recent ("idr: remove MAX_IDR_MASK and move left MAX_IDR_* into idr.c") patch. Now a negative ID triggers a WARN_ON_ONCE().

__lock_timer() feeds timer_id from userland directly to idr_find() without sanitizing it which can trigger the above malfunctions. Add a range check on @timer_id before invoking idr_find() in __lock_timer().

While timer_t is defined as int by all archs at the moment, Andrew worries that it may be defined as a larger type later on. Make the test cover larger integers too so that it at least is guaranteed to not return the wrong timer.

Note that WARN_ON_ONCE() in idr_find() on id < 0 is transitional precaution while moving away from ignoring MSB. Once it's gone we can remove the guard as long as timer_t isn't larger than int.

---
 kernel/posix-timers.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/posix-timers.c +7 -0
index 10349d5..7edfe4b 100644
@@ -639,6 +639,13 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
 {
 	struct k_itimer *timr;
 
+	/*
+	 * timer_t could be any type >= int and we want to make sure any
+	 * @timer_id outside positive int range fails lookup.
+	 */
+	if ((unsigned long long)timer_id > INT_MAX)
+		return NULL;
+
 	rcu_read_lock();
 	timr = idr_find(&posix_timers_id, (int)timer_id);
 	if (timr) {
#16 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

In reply to Andrew Morton
Quoted 8 lines · Andrew Morton

I wonder if we should add some generic facility to handle this:

/*
* Query whether an unsigned type is `negative' when we don't know its size */
#define msb_is_set(v) { implementation goes here ;) }

Maybe not justified, dunno...

I do think that if you have to resort to using something like that there's something terribly wrong with the code somewhere else, and that other thing should be fixed first.

Maybe digging into the timers code and seeing why this is needed there will prove me wrong...

Thanks,
Sasha

#17 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

Quoted 6 lines · Sasha Levin

I do think that if you have to resort to using something like that there's something terribly wrong with the code somewhere else, and that other thing should be fixed first.

Maybe digging into the timers code and seeing why this is needed there will prove me wrong...

The problem is that userland can feed us any timer_t which makes it necessary to properly sanitize the value before using it.

Thanks.

Signature · 1 line
tejun
#18 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

Quoted 10 lines · Tejun Heo
Quoted 6 lines · Sasha Levin

I do think that if you have to resort to using something like that there's something terribly wrong with the code somewhere else, and that other thing should be fixed first.

Maybe digging into the timers code and seeing why this is needed there will prove me wrong...

The problem is that userland can feed us any timer_t which makes it necessary to properly sanitize the value before using it.

Why can the timer be negative in the first place though? Why isn't the timer defined as an 'unsigned int' instead of an 'int' so that all values of timer would be legitimate?

Thanks,
sasha

#19 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

Quoted 3 lines · Sasha Levin

Why can the timer be negative in the first place though? Why isn't the timer defined as an 'unsigned int' instead of an 'int' so that all values of timer would be legitimate?

No idea but it's userland visible thing. No point in arguing about it.

Thanks.

Signature · 1 line
tejun
#20 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

In reply to Andrew Morton
Quoted 11 lines · Andrew Morton
Quoted 3 lines · On Wed, 20 Feb 2013 13:37:01 -0800 Tejun Heo <[email protected]> wrote:

hopefully with some comments. That said, if I'm grepping it right, all archs define timer_t as int, so maybe we're just being paranoid.

Sure, it's unlikely to cause a problem in practice. Maybe five years from now, after idr has been cleaned up and switched to 64-bit, we've left a little hand grenade for someone. It would be good to future-safe it in some fashion.

Quoted 8 lines

I wonder if we should add some generic facility to handle this:

/*
* Query whether an unsigned type is `negative' when we don't know its size */
#define msb_is_set(v) { implementation goes here ;) }

Maybe not justified, dunno...

First of all the compiler should warn you about the truncation. And aside of that:

+ BUILD_BUG_ON(sizeof(timer_t) != sizeof(int));

and then we can worry about that esoteric architecture when that thing triggers?

Aside of that, due to the pending checkpoint/restore stuff this user might be gone in the near future.

Thanks,

tglx

#21 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

In reply to Tejun Heo
Quoted 12 lines · Tejun Heo

Recent idr updates make idr_find() trigger WARN_ON_ONCE() before returning NULL when a negative ID is specified. Apparently, posix-timer::__lock_timer() was depending on idr_find() returning NULL on negative ID, thus triggering the new WARN_ON_ONCE(). Make __lock_timer() first check whether @timer_id is negative and return NULL without invoking idr_find() if so.

Note that the previous code was theoretically broken. idr_find() masked off the sign bit before performing lookup and if the matching IDs were in use, it would have returned pointer for the incorrect entry.

Quoted 5 lines

---
Sasha, can you please test whether this makes the warning go away?

Looks like it did.

Thanks,
Sasha

#22 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

In reply to Tejun Heo
Quoted 6 lines · Tejun Heo

Recent idr updates make idr_find() trigger WARN_ON_ONCE() before returning NULL when a negative ID is specified. Apparently, posix-timer::__lock_timer() was depending on idr_find() returning NULL on negative ID, thus triggering the new WARN_ON_ONCE(). Make __lock_timer() first check whether @timer_id is negative and return NULL without invoking idr_find() if so.

I can grumpily accept the patch below as a quick hack fix, which can go to stable as well, but not with such a patently misleading changelog.

The changelog wants to document, that this is not a proper fix at all and just a quick hack which can be nonintrusively applied to stable.

Quoted 4 lines

Note that the previous code was theoretically broken. idr_find() masked off the sign bit before performing lookup and if the matching IDs were in use, it would have returned pointer for the incorrect entry.

Brilliant code that. What's the purpose of having the idr id as an "int" and then masking off the sign bit instead of simply refusing negative id values in the idr code itself or simply making the id "unsigned int" ?

Just look at the various f*ckedup users of MAX_IDR_MASK. Example:

drivers/pps/pps.c:

        err = idr_get_new(&pps_idr, pps, &pps->id);
        mutex_unlock(&pps_idr_lock);

        if (err < 0)
                return err;

        pps->id &= MAX_IDR_MASK;

Why the heck is that necessary? Either we get an error code and we don't care about pps->id or idr_get_new() sets pps->id to a valid idr id. If the idr code really returns a _valid_ id with the sign bit set, then the idr code is broken beyond repair and needs to be fixed instead of propagating that insanity all over the users.

Thanks,

tglx

#23 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

Hey, Thomas.

Quoted 6 lines · Thomas Gleixner

I can grumpily accept the patch below as a quick hack fix, which can go to stable as well, but not with such a patently misleading changelog.

The changelog wants to document, that this is not a proper fix at all and just a quick hack which can be nonintrusively applied to stable.

I'm not sure about what type timer_t can be but if it can actually be u64 as Andrew suggests, we probably want a different test guarding it.

Quoted 9 lines
Quoted 4 lines

Note that the previous code was theoretically broken. idr_find() masked off the sign bit before performing lookup and if the matching IDs were in use, it would have returned pointer for the incorrect entry.

Brilliant code that. What's the purpose of having the idr id as an "int" and then masking off the sign bit instead of simply refusing negative id values in the idr code itself or simply making the id "unsigned int" ?

Beats me. The code has been like that since the beginning. One of the many oddities of idr implementation. Patch to remove MAX_IDR_MASK is already queued in -mm w/ other idr updates.

Thanks.

Signature · 1 line
tejun
#24 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

Quoted 18 lines · Tejun Heo

Hey, Thomas.

Quoted 6 lines · Thomas Gleixner

I can grumpily accept the patch below as a quick hack fix, which can go to stable as well, but not with such a patently misleading changelog.

The changelog wants to document, that this is not a proper fix at all and just a quick hack which can be nonintrusively applied to stable.

I'm not sure about what type timer_t can be but if it can actually be u64 as Andrew suggests, we probably want a different test guarding it.

Quoted 5 lines
Quoted 4 lines

Note that the previous code was theoretically broken. idr_find() masked off the sign bit before performing lookup and if the matching IDs were in use, it would have returned pointer for the incorrect entry.

Quoted 8 lines
Quoted 4 lines

Brilliant code that. What's the purpose of having the idr id as an "int" and then masking off the sign bit instead of simply refusing negative id values in the idr code itself or simply making the id "unsigned int" ?

Beats me. The code has been like that since the beginning. One of the many oddities of idr implementation. Patch to remove MAX_IDR_MASK is already queued in -mm w/ other idr updates.

Missed that, but good to know that this insanity is going to be gone soon.

Thanks,

tglx

#25 lore raw

Re: [PATCH] posix-timer: don't call idr_find() w/ negative ID

Quoted 2 lines · Thomas Gleixner

Missed that, but good to know that this insanity is going to be gone soon.

Yeah, idr at least shouldn't be insane interface-wise. Its implementation still makes me want to drive pencils into my eyes and ida still needs some love tho. Some other time, hopefully.

Signature · 1 line
tejun