From patchwork Mon Oct 17 03:00:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Zetao X-Patchwork-Id: 3172 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1227348wrs; Sun, 16 Oct 2022 19:00:17 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4XACT2FcIY0LwnNOwW/3GcOeNH0Jvw5LfA7uEYzMqDFDP1g+axP0qqMGJ1z3xRLxtZ0GRf X-Received: by 2002:a17:902:7c8c:b0:17f:7565:4a2d with SMTP id y12-20020a1709027c8c00b0017f75654a2dmr9703549pll.65.1665972017148; Sun, 16 Oct 2022 19:00:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665972017; cv=none; d=google.com; s=arc-20160816; b=eaCacc53hzYnryzPrmUP79K/7o+SPr5oVnEr6EQ+KWq4pns7nfp5fz7autVW+zuMh9 XDlpXtIh2ngndk10bKXOWS4RdT8Y7rT21574gBy7wRX978qzakPiPkmAqnn7i1zVfEbe tld3iMRHftRwSVMwA9q/G28n+0tGx89759uzJswj+H9sTzHeh3oB3q07mQLNWAnZDFgn ajns+cx2UTbsikiSEcutARH18dkK3QQoWZu0UwQwcC4eCQJYZneI1OnXLd/2jZpMRdyK owJeTZGyH5x3eUZosJslR0V0l//zXO5Vga2GUhi8XUbi97AVNqpwa4mWhSRAfz/vV7G4 p02A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=RMRjVFQ6IFOPdRGryfRtqsNAhEL6M3MRZJKzV9n7Op8=; b=w6VxGpNwOlCLunrwXrdQZ9Qc4FkMs0dJ+CYHBbBZHxRnNj/BAaFCHNDRyQPcUsO7cj rBIHtaNsvrdMTUcfJk7yNCID+c6kWMtahEGzUKL4Hu4HqI7ypfmjL9djupo1CCe4wArP YWx1METWNxKdUvPDnPQZxRpYLRNUtGDJq2ufZ41dzOCAb69teC6TbcdjCKWGDDI/ZUaP kFfgbkERz2emxXGuL7+gjT0pcPntc/GHMnhkLBTnyDUaT5PI3K8+Jxenq7GEOeYtn+gE LvVv+/r5cDPrvE8duOyiYdcwBIDEQJew9Ik21AvT6w1UWogeqdRdWFaWGmUQ0PogoJC+ oskg== 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o37-20020a634e65000000b0043bd8452080si9994465pgl.313.2022.10.16.19.00.03; Sun, 16 Oct 2022 19:00:17 -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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230047AbiJQB5P (ORCPT + 99 others); Sun, 16 Oct 2022 21:57:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229648AbiJQB5N (ORCPT ); Sun, 16 Oct 2022 21:57:13 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C00CB4151F; Sun, 16 Oct 2022 18:57:11 -0700 (PDT) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MrKkN4fmjzmV9f; Mon, 17 Oct 2022 09:52:28 +0800 (CST) Received: from huawei.com (10.175.101.6) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 17 Oct 2022 09:57:08 +0800 From: Li Zetao To: , , CC: , , Subject: [PATCH] xfs: Fix unreferenced object reported by kmemleak in xfs_sysfs_init() Date: Mon, 17 Oct 2022 11:00:29 +0800 Message-ID: <20221017030029.3876512-1-lizetao1@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Originating-IP: [10.175.101.6] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746898273349994687?= X-GMAIL-MSGID: =?utf-8?q?1746898273349994687?= kmemleak reported a sequence of memory leaks, and one of them indicated we failed to free a pointer: comm "mount", pid 19610, jiffies 4297086464 (age 60.635s) hex dump (first 8 bytes): 73 64 61 00 81 88 ff ff sda..... backtrace: [<00000000d77f3e04>] kstrdup_const+0x46/0x70 [<00000000e51fa804>] kobject_set_name_vargs+0x2f/0xb0 [<00000000247cd595>] kobject_init_and_add+0xb0/0x120 [<00000000f9139aaf>] xfs_mountfs+0x367/0xfc0 [<00000000250d3caf>] xfs_fs_fill_super+0xa16/0xdc0 [<000000008d873d38>] get_tree_bdev+0x256/0x390 [<000000004881f3fa>] vfs_get_tree+0x41/0xf0 [<000000008291ab52>] path_mount+0x9b3/0xdd0 [<0000000022ba8f2d>] __x64_sys_mount+0x190/0x1d0 As mentioned in kobject_init_and_add() comment, if this function returns an error, kobject_put() must be called to properly clean up the memory associated with the object. Apparently, xfs_sysfs_init() does not follow such a requirement. When kobject_init_and_add() returns an error, the space of kobj->kobject.name alloced by kstrdup_const() is unfree, which will cause the above stack. Fix it by adding kobject_put() when kobject_init_and_add returns an error. Fixes: a31b1d3d89e4 ("xfs: add xfs_mount sysfs kobject") Signed-off-by: Li Zetao Reviewed-by: Darrick J. Wong --- fs/xfs/xfs_sysfs.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fs/xfs/xfs_sysfs.h b/fs/xfs/xfs_sysfs.h index 43585850f154..513095e353a5 100644 --- a/fs/xfs/xfs_sysfs.h +++ b/fs/xfs/xfs_sysfs.h @@ -33,10 +33,15 @@ xfs_sysfs_init( const char *name) { struct kobject *parent; + int err; parent = parent_kobj ? &parent_kobj->kobject : NULL; init_completion(&kobj->complete); - return kobject_init_and_add(&kobj->kobject, ktype, parent, "%s", name); + err = kobject_init_and_add(&kobj->kobject, ktype, parent, "%s", name); + if (err) + kobject_put(&kobj->kobject); + + return err; } static inline void