Re: [PATCH] slab: warning if total alloc size overflow

From: Andrew Morton
Date: Tue Feb 14 2012 - 03:53:12 EST


On Tue, 14 Feb 2012 15:28:19 +0800 Yang Bai <hamo.by@xxxxxxxxx> wrote:

> Before, if the total alloc size is overflow,
> we just return NULL like alloc fail. But they
> are two different type problems. The former looks
> more like a programming problem. So add a warning
> here.
>
> Signed-off-by: Yang Bai <hamo.by@xxxxxxxxx>
> ---
> include/linux/slab.h | 4 +++-
> 1 files changed, 3 insertions(+), 1 deletions(-)
>
> diff --git a/include/linux/slab.h b/include/linux/slab.h
> index 573c809..5865237 100644
> --- a/include/linux/slab.h
> +++ b/include/linux/slab.h
> @@ -242,8 +242,10 @@ size_t ksize(const void *);
> */
> static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
> {
> - if (size != 0 && n > ULONG_MAX / size)
> + if (size != 0 && n > ULONG_MAX / size) {
> + WARN(1, "Alloc memory size (%lu * %lu) overflow.", n, size);
> return NULL;
> + }
> return __kmalloc(n * size, flags | __GFP_ZERO);
> }

One of the applications of kcalloc() is to prevent userspace from
causing a multiplicative overflow (and then perhaps causing an
overwrite beyond the end of the allocated memory).

With this patch, we've just handed the user a way of spamming the logs
at 1MHz. This is bad.


Also, please let's not randomly add debug stuff in places where we've
never demonstrated a need for it.
--
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/