Message ID | 20221026082803.3458760-1-yangyingliang@huawei.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp132326wru; Wed, 26 Oct 2022 01:37:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5omi5J//VHtxJw682i33flsO3+Eptj+ke9gFhdMX4s556BwKlwDFImgKzSg/LRmDkcJpV0 X-Received: by 2002:a17:906:8451:b0:79d:af69:8a20 with SMTP id e17-20020a170906845100b0079daf698a20mr22140850ejy.406.1666773432706; Wed, 26 Oct 2022 01:37:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666773432; cv=none; d=google.com; s=arc-20160816; b=oA2yQ+xLGz9bQOmqA9wRGbuDOoXWkc1OtdQSm2i25ibAimQ0B0xJ/OmbhCTXFe5rnM GpUId8oV9CaKVzhItYOiSxiob2hti+ZSisJTczUCGCgrbIzHPLI4Zogsk4QVJ5m4Iknn VSpGH9ylhrRKYl7TfJ8kVEcISQA641Hv5O5MUTx5OG1L5o8UbR373OcjsuGEiuYar3q7 iyYpgpuhGERlm80iASa9TxkTYjCHF+hljbl4rEnuTgMzZqmIBz3KRVeWlpAvksDt4VJL W60jwTwjJ6I3DYB6E7WWsptH5v+cbPhmO3ZKwtUN93VvliBmge0Afc4AwX7KkpjHNy62 9eRA== 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=hs4fzNAtCBY3s69XZn6dxkOk4wxP0L03EbvJSr8AeS4=; b=X/xJS8zRT75dguyFXyoFW1crVL0pqzhCK801zITVkMYWryQJGbI5HIZlK+q6l1haLu +R4XPu4h4YHbmwVCHfXc7V4GJHrGG8iVXFY3a34yv+1n9su+NBuYGM0EE9kA/1c0KhBy Ix15cWpIJc/2rZ5gCF+oh4GZo/9zkgeH7NfX3V87Mqoe5x9CnWLOqkuGHOxqaktpQUMQ eNUL9xLIvysxGUoo7GvpCSuEKJJnY512Q7vly840slrtPHlCvgQwDQh6OdBiYrUVBs+I vI6BNdC1mUm0Lg7K/xFrgGsnse48G4QoGvVD0sTLgwlJD/FSQ4ZgLXBRlldjAzCcHUGV TVWA== 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 d31-20020a056402401f00b00461f44d7bfcsi4756278eda.573.2022.10.26.01.36.44; Wed, 26 Oct 2022 01:37:12 -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 S233317AbiJZI3D (ORCPT <rfc822;pwkd43@gmail.com> + 99 others); Wed, 26 Oct 2022 04:29:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233254AbiJZI3A (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 26 Oct 2022 04:29:00 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3DD4C4DA4 for <linux-kernel@vger.kernel.org>; Wed, 26 Oct 2022 01:28:59 -0700 (PDT) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4My204418DzmVDG; Wed, 26 Oct 2022 16:24:04 +0800 (CST) Received: from dggpemm500007.china.huawei.com (7.185.36.183) by dggpemm500020.china.huawei.com (7.185.36.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 26 Oct 2022 16:28:57 +0800 Received: from huawei.com (10.175.103.91) by dggpemm500007.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 26 Oct 2022 16:28:57 +0800 From: Yang Yingliang <yangyingliang@huawei.com> To: <linux-kernel@vger.kernel.org> CC: <gregkh@linuxfoundation.org>, <rafael@kernel.org> Subject: [PATCH] class: fix possible memory leak in __class_register() Date: Wed, 26 Oct 2022 16:28:03 +0800 Message-ID: <20221026082803.3458760-1-yangyingliang@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500007.china.huawei.com (7.185.36.183) 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747738618833099585?= X-GMAIL-MSGID: =?utf-8?q?1747738618833099585?= |
Series |
class: fix possible memory leak in __class_register()
|
|
Commit Message
Yang Yingliang
Oct. 26, 2022, 8:28 a.m. UTC
If class_add_groups() returns error, the 'cp->subsys' need be
unregister, and the 'cp' need be freed.
We can not call kset_unregister() here, because the 'cls' will
be freed in callback function class_release() and it's also
freed in caller's error path, it will cause double free.
So fix this by calling kobject_del() and kfree_const(name) to
cleanup kobject. Besides, call kfree() to free the 'cp'.
Fault injection test can trigger this:
unreferenced object 0xffff888102fa8190 (size 8):
comm "modprobe", pid 502, jiffies 4294906074 (age 49.296s)
hex dump (first 8 bytes):
70 6b 74 63 64 76 64 00 pktcdvd.
backtrace:
[<00000000e7c7703d>] __kmalloc_track_caller+0x1ae/0x320
[<000000005e4d70bc>] kstrdup+0x3a/0x70
[<00000000c2e5e85a>] kstrdup_const+0x68/0x80
[<000000000049a8c7>] kvasprintf_const+0x10b/0x190
[<0000000029123163>] kobject_set_name_vargs+0x56/0x150
[<00000000747219c9>] kobject_set_name+0xab/0xe0
[<0000000005f1ea4e>] __class_register+0x15c/0x49a
unreferenced object 0xffff888037274000 (size 1024):
comm "modprobe", pid 502, jiffies 4294906074 (age 49.296s)
hex dump (first 32 bytes):
00 40 27 37 80 88 ff ff 00 40 27 37 80 88 ff ff .@'7.....@'7....
00 00 00 00 ad 4e ad de ff ff ff ff 00 00 00 00 .....N..........
backtrace:
[<00000000151f9600>] kmem_cache_alloc_trace+0x17c/0x2f0
[<00000000ecf3dd95>] __class_register+0x86/0x49a
Fixes: ced6473e7486 ("driver core: class: add class_groups support")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
drivers/base/class.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/base/class.c b/drivers/base/class.c index 64f7b9a0970f..8ceafb7d0203 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -192,6 +192,11 @@ int __class_register(struct class *cls, struct lock_class_key *key) } error = class_add_groups(class_get(cls), cls->class_groups); class_put(cls); + if (error) { + kobject_del(&cp->subsys.kobj); + kfree_const(cp->subsys.kobj.name); + kfree(cp); + } return error; } EXPORT_SYMBOL_GPL(__class_register);