linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -next] mm/memcg: Use struct_size() helper in kzalloc()
@ 2021-12-16  2:20 Wang Weiyang
  2021-12-16  9:44 ` Muchun Song
  0 siblings, 1 reply; 2+ messages in thread
From: Wang Weiyang @ 2021-12-16  2:20 UTC (permalink / raw)
  To: hannes, mhocko, vdavydov.dev, akpm; +Cc: cgroups, linux-mm, linux-kernel

Make use of the struct_size() helper instead of an open-coded version, in
order to avoid any potential type mistakes or integer overflows that, in
the worst scenario, could lead to heap overflows.

Link: https://github.com/KSPP/linux/issues/160
Signed-off-by: Wang Weiyang <wangweiyang2@huawei.com>
---
 mm/memcontrol.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 54afa29145e8..ec4b677cdcad 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -5129,15 +5129,11 @@ static void mem_cgroup_free(struct mem_cgroup *memcg)
 static struct mem_cgroup *mem_cgroup_alloc(void)
 {
 	struct mem_cgroup *memcg;
-	unsigned int size;
 	int node;
 	int __maybe_unused i;
 	long error = -ENOMEM;
 
-	size = sizeof(struct mem_cgroup);
-	size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
-
-	memcg = kzalloc(size, GFP_KERNEL);
+	memcg = kzalloc(struct_size(memcg, nodeinfo, nr_node_ids), GFP_KERNEL);
 	if (!memcg)
 		return ERR_PTR(error);
 
-- 
2.17.1



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH -next] mm/memcg: Use struct_size() helper in kzalloc()
  2021-12-16  2:20 [PATCH -next] mm/memcg: Use struct_size() helper in kzalloc() Wang Weiyang
@ 2021-12-16  9:44 ` Muchun Song
  0 siblings, 0 replies; 2+ messages in thread
From: Muchun Song @ 2021-12-16  9:44 UTC (permalink / raw)
  To: Wang Weiyang
  Cc: Johannes Weiner, Michal Hocko, Vladimir Davydov, Andrew Morton,
	Cgroups, Linux Memory Management List, LKML

On Thu, Dec 16, 2021 at 10:21 AM Wang Weiyang <wangweiyang2@huawei.com> wrote:
>
> Make use of the struct_size() helper instead of an open-coded version, in
> order to avoid any potential type mistakes or integer overflows that, in
> the worst scenario, could lead to heap overflows.
>
> Link: https://github.com/KSPP/linux/issues/160
> Signed-off-by: Wang Weiyang <wangweiyang2@huawei.com>

Reviewed-by: Muchun Song <songmuchun@bytedance.com>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-12-16  9:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-16  2:20 [PATCH -next] mm/memcg: Use struct_size() helper in kzalloc() Wang Weiyang
2021-12-16  9:44 ` Muchun Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox