[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <20220306172859.2a35d07284b6aee209b9dcfb@linux-foundation.org>
Date: Sun, 6 Mar 2022 17:28:59 -0800
From: Andrew Morton <akpm@...ux-foundation.org>
To: xkernel.wang@...mail.com
Cc: linux@...inikbrodowski.net, pombredanne@...b.com,
gregkh@...uxfoundation.org, arnd@...db.de,
luc.vanoostenryck@...il.com, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] init/initramfs.c: check the return value of kstrdup()
On Fri, 4 Mar 2022 17:27:34 +0800 xkernel.wang@...mail.com wrote:
> From: Xiaoke Wang <xkernel.wang@...mail.com>
>
> kstrdup() is also a memory allocation function which is similar
> with kmalloc() in some way. Once some internal memory errors
> happen, it will return NULL. It is better to check the return
> value of it so to catch the memory error in time.
>
> ...
>
> --- a/init/initramfs.c
> +++ b/init/initramfs.c
> @@ -139,8 +139,12 @@ static void __init dir_add(const char *name, time64_t mtime)
> struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
> if (!de)
> panic_show_mem("can't allocate dir_entry buffer");
> - INIT_LIST_HEAD(&de->list);
> de->name = kstrdup(name, GFP_KERNEL);
> + if (!de->name) {
> + kfree(de);
> + panic_show_mem("can't duplicate dir name");
> + }
> + INIT_LIST_HEAD(&de->list);
> de->mtime = mtime;
> list_add(&de->list, &dir_list);
We often assume that memory allocations cannot fail in __init code. If
the kernel runs out of memory at this stage, we have very deep problems
and it's virtually impossible that execution would have got this far.
Powered by blists - more mailing lists