Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp8771718rwi; Tue, 25 Oct 2022 10:33:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7xL1z3GQf6GuQj7ibULCiByQyQFaf/pgs8Bo2nxvUANtJFHqqol7mz3yh8KYEcTsib2t7h X-Received: by 2002:a63:2dc3:0:b0:46b:3cf1:e16c with SMTP id t186-20020a632dc3000000b0046b3cf1e16cmr33616413pgt.556.1666719215890; Tue, 25 Oct 2022 10:33:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666719215; cv=none; d=google.com; s=arc-20160816; b=S7XBAtkUyOSasaEZ4t6wpTDeOaAp1EkjC4s+TqOhjPgW/zeztvHUq2pvQW0/MhouWA qPFLwuhKLy2NZaiqg8xWNchDh8lqZW2obWTc/EWdKy+nD52vm4HCPSlGpuo4goxGt283 vdwXCCSKXiZIwFZk6nB1yiUczb9zIdxMJD5SrWr003cWuCFxv2J8MgkeSE9AuFw03Izu 3Viwaj2M4HQ8WVoczP7XjQuOOaXcc+o6s++BmjgSAyzods9C00/KR+Kky2wqG5kTP3zE T6dp3G0zash7yo7UZI+snseNk6RTVtDZ2hPoWbtrAuzC/VK1qpoRbkHFvg/xOTHiZvRi 2FBQ== 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=pumeYs4594NapWusrOR3w2AtJdOwekcl7jfUA3nCRb8=; b=0u2rI6pM+ezQPiZptgtKYkb6oFwBl4uXqB9MwhxB0ApMmbWjM9Wla+ZhoxnK/jvt58 H5f5YFD9bGk6UTR7x5SCZTQYBC2odp3FjoDBUA4Iu27GAtRJoDJ2HvO6MVZnAFbLUWOf 0G2vc/mrtzNHgF2gntEiHHE6IL7lbnIRa/mcp4wP8GpSMb9k4ZcwVRd5JVtWhgC1g3VQ 6UxeNysUaGJnUP0LpxeeEwsGPUlMLG/7WSQPdCCzke8fXJSE1LRuGF6H8u/xNihhTTX4 Tp80WKRtVVbjUYW3n4GugPs9QV4QrhajJBc5Y+A4l0W12z2Mb2fSzAHfS+7Ty39HynzX meRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=IuS0pwxk; 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 g22-20020a056a001a1600b0056282b4a984si3997343pfv.126.2022.10.25.10.33.23; Tue, 25 Oct 2022 10:33:35 -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=IuS0pwxk; 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 S230035AbiJYQ6Q (ORCPT + 99 others); Tue, 25 Oct 2022 12:58:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229800AbiJYQ6N (ORCPT ); Tue, 25 Oct 2022 12:58:13 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A7D3100BC4 for ; Tue, 25 Oct 2022 09:58:12 -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 dfw.source.kernel.org (Postfix) with ESMTPS id E5E5361A36 for ; Tue, 25 Oct 2022 16:57:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C254DC433D6; Tue, 25 Oct 2022 16:57:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666717074; bh=TOr7/IBl9W+Hpc/R8LxJAyq9QpYQbWCv3WJvy2nka0k=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=IuS0pwxkNWhb/Pc3fD1YeILTwYuQDWG7J6hJ35bK6Ucsyh68N7Gpf7db0oIcWSg5J +A52AVp6gq97BqnXsSXSdptDSLkW3H2w/eBLbjyhblutXXWvXsvxCZQTb1o8df8D0g X2/xsNKaI+imRsgqBblFS8hu7gUdhG4pmfgst1Zw= Date: Tue, 25 Oct 2022 18:57:51 +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 v3] kset: fix memory leak when kset_register() returns error Message-ID: References: <20221025071549.1280528-1-yangyingliang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221025071549.1280528-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,URIBL_BLOCKED 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 Tue, Oct 25, 2022 at 03:15:49PM +0800, Yang Yingliang wrote: > Inject fault while loading module, kset_register() may fail. > If it fails, the kset.kobj.name allocated by kobject_set_name() > which must be called before a call to kset_register() may be > leaked, since refcount of kobj was set in kset_init(). > > To mitigate this, we free the name in kset_register() when an > error is encountered, i.e. when kset_register() returns an error. > > A kset may be embedded in a larger structure which may be dynamically > allocated in callers, it needs to be freed in ktype.release() or error > path in callers, in this case, 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 to avoid accessing bad pointer in callers. > > With this fix, the callers don't need care about freeing the name > and may call kset_put() if kset_register() fails. > > Suggested-by: Luben Tuikov > Signed-off-by: Yang Yingliang > --- > v2 -> v3: > Update commit message and comment of kset_register(). > > v1 -> v2: > Free name inside of kset_register() instead of calling kset_put() > in drivers. Thank you for all of this, it's a much nicer and cleaner fix than forcing all callers to try to handle it instead. greg k-h