lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Date:	Thu, 7 Apr 2011 14:19:42 -0700
From:	Andrew Morton <akpm@...ux-foundation.org>
To:	Ilia Mirkin <imirkin@...m.mit.edu>
Cc:	drbd-dev@...ts.linbit.com, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] lru_cache: Use correct type in sizeof for allocation

On Sun, 20 Feb 2011 17:45:14 -0500
Ilia Mirkin <imirkin@...m.mit.edu> wrote:

> This has no actual effect, since sizeof(struct hlist_head) ==
> sizeof(struct hlist_head *), but it's still the wrong type to use.
> 
> The semantic match that finds this problem:
> // <smpl>
> @@
> type T;
> identifier x;
> @@
> T *x;
> ...
> * x = kzalloc(... * sizeof(T*) * ..., ...);
> // </smpl>
> 
> ...
>
> --- a/lib/lru_cache.c
> +++ b/lib/lru_cache.c
> @@ -84,7 +84,7 @@ struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
>  	if (e_count > LC_MAX_ACTIVE)
>  		return NULL;
>  
> -	slot = kzalloc(e_count * sizeof(struct hlist_head*), GFP_KERNEL);
> +	slot = kzalloc(e_count * sizeof(struct hlist_head), GFP_KERNEL);
>  	if (!slot)
>  		goto out_fail;
>  	element = kzalloc(e_count * sizeof(struct lc_element *), GFP_KERNEL);

This is one of the reasons why I think it's better to use

	foo = kmalloc(sizeof(*foo));

Then, you just *know* it's correct by looking at the code and you don't
need to scroll up and double-check the type of foo.

The code as you have it is still vulnerable to multiplicative overflow.
So, to be really really correct,


--- a/lib/lru_cache.c~lru_cache-use-correct-type-in-sizeof-for-allocation-fix
+++ a/lib/lru_cache.c
@@ -84,7 +84,7 @@ struct lru_cache *lc_create(const char *
 	if (e_count > LC_MAX_ACTIVE)
 		return NULL;
 
-	slot = kzalloc(e_count * sizeof(struct hlist_head), GFP_KERNEL);
+	slot = kcalloc(e_count, sizeof(struct hlist_head), GFP_KERNEL);
 	if (!slot)
 		goto out_fail;
 	element = kzalloc(e_count * sizeof(struct lc_element *), GFP_KERNEL);
_

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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ