Re: [PATCH 1/8] idr: fix overflow bug for the max-high layer

From: Tejun Heo
Date: Fri Apr 18 2014 - 12:29:49 EST


Hello,

Subject: idr: fix overflow bug during maximum ID calculation at maximum height

On Fri, Apr 18, 2014 at 08:49:48PM +0800, Lai Jiangshan wrote:
> In idr_replace(), when the top layer is the max-high layer(p->layer == 3),
> The "(1 << n)" will overflow and the result is 0, it causes idr_replace()
> return -EINVAL even the id is actually valid.

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");
> }
>
> Fixed the bug by using idr_max() instead of the incorrect open code.

Fix the bug by using idr_max() which correctly takes into account the
maximum allowed shift.

> There is the same problem in sub_alloc(). The overflow causes sub_alloc()
> returns -EAGAIN unexpectedly. But the idr_get_empty_slot() will call it
> again with increased @id. So the bug is hided.

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.

> CC: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Lai Jiangshan <laijs@xxxxxxxxxxxxxx>

Acked-by: Tejun Heo <tj@xxxxxxxxxx>

Thanks.

--
tejun
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/