Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2126943pxm; Fri, 4 Mar 2022 09:52:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJxAORQ6AsvanrBiA5fFgVKuapQdtf11GG1CeJm5kHCoY4uaA8pGIoGDFHXyD+JI6KkG4gEw X-Received: by 2002:a17:902:da87:b0:14f:e6cc:541d with SMTP id j7-20020a170902da8700b0014fe6cc541dmr42041676plx.133.1646416323024; Fri, 04 Mar 2022 09:52:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646416323; cv=none; d=google.com; s=arc-20160816; b=lA2cutkDy5GXnicTIjWuzS+lpzVkHrKjgphm8TpZolyXB+2VC2UCLp/6sIT1CQWpaT cfCrDKi1cC0STbG48rpDU093cZH5n0BtW4oULeJz6bVDBXI+APEfWaZOw0NUPz/ZTTlF UDfcpvxLG0ujlf4SyX/VRhbXY5HoKPybk9NJ9pnH+LlOWY4NnYl/dYtpqDIkR7T5DrrP 6aF3xfalGQjBWe3x3p89+d8uf+jK73G3A1nfmx7onxwwkTFQhwPVCRg93ZIEUvkkwK0L f6zew/zgnPu9siYXBs9AjCWygETgFGwLP67odwhe0CAH7fjYXPzG+Q5Zs31iKflk2qqD T4YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=sSZiNbjHMgbCHZijTuqpgLd5Fh0YA4Il5sDfmvMlIQI=; b=rOpHByAbdtpv6OyJb9o+fRdsvLy1U870MlJlrKeQS9y4QKMtz7RD/jWs4is5IjWv4L S6dY5KXF+w4GL1jH3Ce9ud6Zgf3aKVjCO+olS8VqRgFpOGWSI0Des9H8hNehXJ7Knf0t ufEP9dsMLkrO+Xw4HjyYOvM5Ju5k7xe566vLRneyA+9iZChf+ss1Wzjd2RJL1d9LBrrL nK97Jup+tZVuxrXEglJIjIG7vjJxSLqtcRe3pnJ+2SdIz02PceD/zDrHXYpXpJdCyGM6 qb/aS3JZSDIgeR0H0HAn3ySO/461DpmcxvjZgDR650j5gZCh6Uk9ujYEt2Kq9fOGAYrt Dqmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MiFWKHd1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hk9-20020a17090b224900b001bf1e011e98si167666pjb.171.2022.03.04.09.51.46; Fri, 04 Mar 2022 09:52:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MiFWKHd1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232862AbiCDOPG (ORCPT + 99 others); Fri, 4 Mar 2022 09:15:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230479AbiCDOPE (ORCPT ); Fri, 4 Mar 2022 09:15:04 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76E5462E0 for ; Fri, 4 Mar 2022 06:14:16 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E37DB612A5 for ; Fri, 4 Mar 2022 14:14:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8EC4CC340E9; Fri, 4 Mar 2022 14:14:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646403255; bh=tOSwy/IqvLdbW9FFy93gGyEiwPOEyRNiwVpCXRt6x/c=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MiFWKHd1pf6MNX2q7gNhBlxfMu17suulJquUywyJOWN5XJwQYFVDdBpr50Jg7qKvK tXiEyW/yxfnpz1QlliiMeDq/DdXGQ0roFn/ZAaoN7PbRn+WnnGZUOPSUR4iaPrGQom WPLr6zwE/LP1I1Jeqm3iH5ayIPr9TTGx2ewKX/L0= Date: Fri, 4 Mar 2022 15:14:10 +0100 From: Greg KH To: xkernel.wang@foxmail.com Cc: linux@dominikbrodowski.net, akpm@linux-foundation.org, pombredanne@nexb.com, arnd@arndb.de, luc.vanoostenryck@gmail.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH] init/initramfs.c: check the return value of kstrdup() Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_RED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 04, 2022 at 05:27:34PM +0800, xkernel.wang@foxmail.com wrote: > From: Xiaoke Wang > > 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. > > Signed-off-by: Xiaoke Wang > --- > init/initramfs.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/init/initramfs.c b/init/initramfs.c > index a842c05..49deffb 100644 > --- 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) { How can this fail? Have you ever hit this in real life? > + kfree(de); > + panic_show_mem("can't duplicate dir name"); Why are you freeing memory if you are panicing? How was this tested? thanks, greg k-h