Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp7195244rwi; Mon, 24 Oct 2022 11:03:43 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5mgqcgAOXvvbsdr+VrSPePRgjnw0JxKiiGuaytSO6w6N3HzPzOmuo5bU96IPoVGjJhjBzT X-Received: by 2002:a17:90b:4f4a:b0:20c:64e2:2bf1 with SMTP id pj10-20020a17090b4f4a00b0020c64e22bf1mr41346251pjb.30.1666634623471; Mon, 24 Oct 2022 11:03:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666634623; cv=none; d=google.com; s=arc-20160816; b=qcO4H4tq27gcZGH0UQoNY0tmCZf3/Fts2tZiUbuhcoFjxUxt5ujkbuSdYm4VkphJKM cj+6uzrEI7zyl1xb12JsvzRSuUPjKujhQrJ7YOj3ZfJY2b+JuCedgy9LEL9CMe/svyPd LJwcnSe7N7gJIwoWRO5OHmtjIqg/RJGN++k/gddpnSDedQM7+W/5eG3SLIFf7GWnGBdD C+8gElWCFBAP5ydu2ISikKZ1NZRVV3vaGYv2UVIMCsDnaOhJq5JQLF60mDGODs2KtFyj Yu4e/wjC4L1leFa0NuXgssbfTFdU2bqW4fUvnyjQ3b/pBuKJXnSuppQjdeOC8MTCpE8A wDfg== 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=4WiB3jTzOcy/T6gXm1iSeVeIuraRw6twHudoz+j2EAw=; b=LbrAyDEcjUu9eUxMZgrCs+zxrBFIhOKmZ6BWtdVpUlSKhFin+4Kd3s0F7s5UrRq3s/ bTFTC4xCv0LJ19H4keHRdFi86q4J+eAsywZuuOBg8uvfNYoHsKDDNqf0rdfRwY6O7Ke/ a322NwSv2fQZsgoz473qiq8E0/r+OrxS59W9P2ehiZ63jSgO6h2qH0tSXKz0e00miwb8 2P90g+dZY4ZZTN4Gf92CKzahGoGtwwax9KHjK6XGGmXOLUNsGea/syDxAW9PADeISB38 9xVnSop+Bh7IJSZ5E0xN3OLxaBoqij49/KG3tKG0MRDpZuTtceDEjDv8MBi0ZppNIbia M1FA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=a2H9OWmU; 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 i12-20020aa78d8c000000b00536bbfa4994si247884pfr.345.2022.10.24.11.03.32; Mon, 24 Oct 2022 11:03:43 -0700 (PDT) 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=a2H9OWmU; 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 S232505AbiJXQqA (ORCPT + 99 others); Mon, 24 Oct 2022 12:46:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234764AbiJXQor (ORCPT ); Mon, 24 Oct 2022 12:44:47 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBE4014138A for ; Mon, 24 Oct 2022 08:30:56 -0700 (PDT) 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 ams.source.kernel.org (Postfix) with ESMTPS id 9C664B81B75 for ; Mon, 24 Oct 2022 13:52:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1728C433C1; Mon, 24 Oct 2022 13:52:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666619524; bh=QIHnXVxCIGETsd0dywR9WOXQe2R85leBxjgt9psJDlE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=a2H9OWmUuPXe1nsvDHzMz7pk28v9U01aePXWCqE/PYv8mU+b3XDQLri0CqGVaRsJz jDX3YxQNP/rC/SyUqCZ86yUqxIXbKkp05GDQaItPKZLX+Z/tQqtvt0HtDKDldCwU2Z RC0760eEL3DTVNddcLXXtAu1W45/R1VDk/n+VsGo= Date: Mon, 24 Oct 2022 15:52:56 +0200 From: Greg KH To: Yang Yingliang Cc: linux-kernel@vger.kernel.org, qemu-devel@nongnu.org, linux-f2fs-devel@lists.sourceforge.net, linux-erofs@lists.ozlabs.org, ocfs2-devel@oss.oracle.com, linux-mtd@lists.infradead.org, amd-gfx@lists.freedesktop.org, rafael@kernel.org, somlo@cmu.edu, mst@redhat.com, jaegeuk@kernel.org, chao@kernel.org, hsiangkao@linux.alibaba.com, huangjianan@oppo.com, mark@fasheh.com, jlbec@evilplan.org, joseph.qi@linux.alibaba.com, akpm@linux-foundation.org, alexander.deucher@amd.com, luben.tuikov@amd.com, richard@nod.at, liushixin2@huawei.com Subject: Re: [PATCH v2] kset: fix memory leak when kset_register() returns error Message-ID: References: <20221024121910.1169801-1-yangyingliang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221024121910.1169801-1-yangyingliang@huawei.com> X-Spam-Status: No, score=-7.6 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 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 Mon, Oct 24, 2022 at 08:19:10PM +0800, Yang Yingliang wrote: > Inject fault while loading module, kset_register() may fail. > If it fails, the name allocated by kobject_set_name() which > is called before kset_register() is leaked, because refcount > of kobject is hold in kset_init(). > > As a kset may be embedded in a larger structure which needs > be freed in release() function or error path in callers, we > can not call kset_put() in kset_register(), or it will cause > double free, so just call kfree_const() to free the name and > set it to NULL. > > With this fix, the callers don't need to care about the name > freeing and call an extra kset_put() if kset_register() fails. > > Suggested-by: Luben Tuikov > Signed-off-by: Yang Yingliang > --- > v1 -> v2: > Free name inside of kset_register() instead of calling kset_put() > in drivers. > --- > lib/kobject.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff --git a/lib/kobject.c b/lib/kobject.c > index a0b2dbfcfa23..3409a89c81e5 100644 > --- a/lib/kobject.c > +++ b/lib/kobject.c > @@ -834,6 +834,9 @@ EXPORT_SYMBOL_GPL(kobj_sysfs_ops); > /** > * kset_register() - Initialize and add a kset. > * @k: kset. > + * > + * NOTE: On error, the kset.kobj.name allocated by() kobj_set_name() > + * which is called before kset_register() in caller need be freed. This comment doesn't make any sense anymore. No caller needs to worry about this, right? > */ > int kset_register(struct kset *k) > { > @@ -844,8 +847,11 @@ int kset_register(struct kset *k) > > kset_init(k); > err = kobject_add_internal(&k->kobj); > - if (err) > + if (err) { > + kfree_const(k->kobj.name); > + k->kobj.name = NULL; Why are you setting the name here to NULL? thanks, greg k-h