diff options
author | Lai Jiangshan <laijs@cn.fujitsu.com> | 2014-06-06 17:37:10 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-06-06 19:08:12 -0400 |
commit | 3afb69cb5572b3c8c898c00880803cf1a49852c4 (patch) | |
tree | 565e9e1e1ba653ae7a45eb3e55aa68e9bfb3bd87 /lib | |
parent | e1bebcf41ed0aa15f11cec186cbd5141730bcafc (diff) |
idr: fix overflow bug during maximum ID calculation at maximum height
idr_replace() open-codes the logic to calculate the maximum valid ID
given the height of the idr tree; unfortunately, the open-coded logic
doesn't account for the fact that the top layer may have unused slots
and over-shifts the limit to zero when the tree is at its maximum
height.
The following test code shows it fails to replace the value for
id=((1<<27)+42):
static void test5(void)
{
int id;
DEFINE_IDR(test_idr);
#define TEST5_START ((1<<27)+42) /* use the highest layer */
printk(KERN_INFO "Start test5\n");
id = idr_alloc(&test_idr, (void *)1, TEST5_START, 0, GFP_KERNEL);
BUG_ON(id != TEST5_START);
TEST_BUG_ON(idr_replace(&test_idr, (void *)2, TEST5_START) != (void *)1);
idr_destroy(&test_idr);
printk(KERN_INFO "End of test5\n");
}
Fix the bug by using idr_max() which correctly takes into account the
maximum allowed shift.
sub_alloc() shares the same problem and may incorrectly fail with
-EAGAIN; however, this bug doesn't affect correct operation because
idr_get_empty_slot(), which already uses idr_max(), retries with the
increased @id in such cases.
[tj@kernel.org: Updated patch description.]
Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
Acked-by: Tejun Heo <tj@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/idr.c | 8 |
1 files changed, 3 insertions, 5 deletions
@@ -249,7 +249,7 @@ static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa, | |||
249 | id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1; | 249 | id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1; |
250 | 250 | ||
251 | /* if already at the top layer, we need to grow */ | 251 | /* if already at the top layer, we need to grow */ |
252 | if (id >= 1 << (idp->layers * IDR_BITS)) { | 252 | if (id > idr_max(idp->layers)) { |
253 | *starting_id = id; | 253 | *starting_id = id; |
254 | return -EAGAIN; | 254 | return -EAGAIN; |
255 | } | 255 | } |
@@ -811,12 +811,10 @@ void *idr_replace(struct idr *idp, void *ptr, int id) | |||
811 | if (!p) | 811 | if (!p) |
812 | return ERR_PTR(-EINVAL); | 812 | return ERR_PTR(-EINVAL); |
813 | 813 | ||
814 | n = (p->layer+1) * IDR_BITS; | 814 | if (id > idr_max(p->layer + 1)) |
815 | |||
816 | if (id >= (1 << n)) | ||
817 | return ERR_PTR(-EINVAL); | 815 | return ERR_PTR(-EINVAL); |
818 | 816 | ||
819 | n -= IDR_BITS; | 817 | n = p->layer * IDR_BITS; |
820 | while ((n > 0) && p) { | 818 | while ((n > 0) && p) { |
821 | p = p->ary[(id >> n) & IDR_MASK]; | 819 | p = p->ary[(id >> n) & IDR_MASK]; |
822 | n -= IDR_BITS; | 820 | n -= IDR_BITS; |