Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp5978147rwi; Tue, 18 Oct 2022 06:43:44 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6RqpyO0/HaSvQsjvBfs0GDn4zlrlvq8oWDRFtzd2zn2itDoX1MXZHl1o2nQW4YtbtkgjZN X-Received: by 2002:a63:4651:0:b0:43c:1cb7:5c09 with SMTP id v17-20020a634651000000b0043c1cb75c09mr2760018pgk.259.1666100623789; Tue, 18 Oct 2022 06:43:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666100623; cv=none; d=google.com; s=arc-20160816; b=pCdnf+5iJVD13BZYJcPQVllQ2hBOf4e61MYNCktifAwZ+mYUqOzgH/YzZD4FZBEcDG c7Vdbok4Dh3LcDGvs/SYoFiNIO6OPFPvadZ1dPBjZX0v8spos7ESOR1aBUeGettylT4x VrLXHJi6P85uI/+gxUji89oaWthmdk2xkv+uaUXlA/61UG4FTeePkUAFxvVJKVm2SPl7 qInFn/gE+KSpbKL4cQ21rscE6pI90UySzrBA8Qkl+JeBHV2zQ3vNGtpd2b6VgipXcVIr 4WIqx82WokIMBVcmTqfPxR9jmU9w217WP1CIcht8a2T1dfedBtrwUZcZCHmFJDskxGsJ EPQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=PUBekTQHt2SxiJqCiVcwR5xiEB8IqcQO+R3n67lwjMI=; b=Vb6EMri/SRkF5M7apQV+uQSrfE4T+fC9IpFJh7tV6ad2gcR8kTnqlUgEP9hZ5iOLfl 4DULwTWLTLKUrsaNsSC3DsZNIMGE9bH2l5OL2sG4/1/0HC0eNBYi+BEDwbsQYq30Cog5 bbGa6rUjx8O0XnVQjaL9uzd0dkEZcBnzBHrOymCxf6eOHOlIsU3SSmELPIG+xe6RnGd3 xohzTVQc32EoMC6nHQPTguuRbYmm4zwL9c6azf1zifRhS9lh5LAmfllrIirlnV6OsjWA dZKeb1JqpcXHQ8I6t9vCh7HKPofs4KvFyR/17l7jHy6d6tusj/PpCqsM3TVs1NkTbFrR ynGQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k27-20020a637b5b000000b0045328a24029si14655817pgn.302.2022.10.18.06.43.30; Tue, 18 Oct 2022 06:43: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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230441AbiJRNjr (ORCPT + 99 others); Tue, 18 Oct 2022 09:39:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229896AbiJRNjn (ORCPT ); Tue, 18 Oct 2022 09:39:43 -0400 Received: from out30-42.freemail.mail.aliyun.com (out30-42.freemail.mail.aliyun.com [115.124.30.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BDCA17E36 for ; Tue, 18 Oct 2022 06:39:42 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046060;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0VSWMbx8_1666100376; Received: from 30.39.171.32(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0VSWMbx8_1666100376) by smtp.aliyun-inc.com; Tue, 18 Oct 2022 21:39:38 +0800 Message-ID: Date: Tue, 18 Oct 2022 21:39:36 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.3.3 Subject: Re: [PATCH] ocfs2: possible memory leak in mlog_sys_init() Content-Language: en-US To: Yang Yingliang , linux-kernel@vger.kernel.org, ocfs2-devel@oss.oracle.com Cc: mark@fasheh.com, jlbec@evilplan.org, gregkh@linuxfoundation.org, akpm@linux-foundation.org References: <20221018075213.736562-1-yangyingliang@huawei.com> <09bb2844-e20a-98e8-c2af-5b6c4795d48e@huawei.com> From: Joseph Qi In-Reply-To: <09bb2844-e20a-98e8-c2af-5b6c4795d48e@huawei.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY, USER_IN_DEF_SPF_WL 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 10/18/22 6:33 PM, Yang Yingliang wrote: > Hi, > > On 2022/10/18 17:02, Joseph Qi wrote: >> Hi, >> >> On 10/18/22 3:52 PM, Yang Yingliang wrote: >>> Inject fault while probing module, kset_register() may fail, >>> if it fails, but the refcount of kobject is not decreased to >>> 0, the name allocated in kobject_set_name() is leaked. Fix >>> this by calling kset_put(), so that name can be freed in >>> callback function kobject_cleanup(). >>> >>> unreferenced object 0xffff888100da9348 (size 8): >>>    comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) >>>    hex dump (first 8 bytes): >>>      6c 6f 67 6d 61 73 6b 00                          logmask. >>>    backtrace: >>>      [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0 >>>      [<000000007c491a9e>] kstrdup+0x3a/0x70 >>>      [<0000000015719a3b>] kstrdup_const+0x63/0x80 >>>      [<0000000084e458ea>] kvasprintf_const+0x149/0x180 >>>      [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150 >>>      [<000000005f48eeac>] kobject_set_name+0xab/0xe0 >>> >>> Fixes: 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset") >>> Signed-off-by: Yang Yingliang >>> --- >>>   fs/ocfs2/cluster/masklog.c | 7 ++++++- >>>   1 file changed, 6 insertions(+), 1 deletion(-) >>> >>> diff --git a/fs/ocfs2/cluster/masklog.c b/fs/ocfs2/cluster/masklog.c >>> index 563881ddbf00..7f9ba816d955 100644 >>> --- a/fs/ocfs2/cluster/masklog.c >>> +++ b/fs/ocfs2/cluster/masklog.c >>> @@ -156,6 +156,7 @@ static struct kset mlog_kset = { >>>   int mlog_sys_init(struct kset *o2cb_kset) >>>   { >>>       int i = 0; >>> +    int ret; >>>         while (mlog_attrs[i].attr.mode) { >>>           mlog_default_attrs[i] = &mlog_attrs[i].attr; >>> @@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset) >>>         kobject_set_name(&mlog_kset.kobj, "logmask"); >>>       mlog_kset.kobj.kset = o2cb_kset; >>> -    return kset_register(&mlog_kset); >>> +    ret = kset_register(&mlog_kset); >> If register fails, it will call unregister in o2cb_sys_init(), which >> will put kobject. > They are different ksets, the kset unregistered in o2cb_sys_init() is 'o2cb_kset', the > kset used to registered in mlog_sys_init() is 'mlog_kset', and they hold difference > refcounts. > Yes, you are right. I've mixed the two ksets up. In theory, kset_register() may return error because of a NULL kset, so here we may not call kset_put() directly, I'm not sure if a static checker will happy. Though this can't happen since it's already statically allocated... Thanks, Joseph