Re: [PATCH RFC v2] slab: introduce kmalloc_array
From: Christoph Lameter
Date: Mon Feb 13 2012 - 15:27:58 EST
On Mon, 13 Feb 2012, Dan Carpenter wrote:
>
> The point was that there are a bunch of places where we have had
> integer overflows caused by doing kmalloc(x * sizeof(struct whatever)).
> For kzalloc(x * sizeof(struct whatever)), you just write it like
> kcalloc(x, sizeof(struct whatever)) and avoid the overflow, but we
> don't have a non-zeroing version of kcalloc() to do that.
>
> Probably once we have the kmalloc_array() and people start using it,
> we get a bunch of overflow checking automatically and it's a kernel
> hardenning thing. As well we could remove the duplicative checking
> so it's a cleanup.
Could you just do a macro that can be used in any location where the
size of an array needs to be calculation. For example:
SAFE_ARRAY_SIZE(<nr>,<struct>)
So you'd do
kmalloc_node(SAFE_ARRAY_SIZE(10, struct page), 2, GFP_KERNEL)
or if you want multiple dimensions
SAFE_ARRAY_SIZE_2(<nr1>,<nr2>,<struct>)
?
> > If you add these variants then please think
> > about the necessity to add other variants (like the kmalloc_node() NUMA
> > call) etc in the future.
> >
>
> We don't have a kcalloc_node(), so I don't think this is likely to
> be a big issue.
Yes and so if you need to allocate on a particular node then you need to
do the calculation manually and therefore may not check for overflows.
Get rid of kcalloc and replace it with
kzalloc(SAFE_ARRAY_SIZE(x, y), ....)
?
--
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/