From nobody Sat May 18 08:46:58 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1666615134; cv=none; d=zohomail.com; s=zohoarc; b=fDIC5cVZyhGXtDDI4ZLs3a9XCm9mNhNeG+FzPU0kbfBkAeN5LYgM5BqX2irmYypOf9xbxa1qeNDDa6Mh43jpY5+S2kwCP5W+GcH6/JNx+XlW1drQfbsuPc/PVTcq8tFPmvcp0hewC3e+ZZEVgLTeuXYzfdpxBZRgdT4BtXWF6A8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666615134; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=h7exAZDWjbRIFFeWx1JcKusldiDakMz3AN/pwrODSNE=; b=bntu1GVo5HCfL6+vrtMWO0eMkiWnZsd4jiOscsKUn0qdJLlzUfkNbAtcpch//TB2treSo4do+fPgmZcuYo5+RvczVei+38ILIAedtU2nkdjp3X0kj6AxFfGREc//dt1pMGT0ZjGo8Wyy5KanKWP4SEYJyFlw7JE7YIHu77mooD4= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1666615134426265.8182378468274; Mon, 24 Oct 2022 05:38:54 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1omwTT-0002A0-CL; Mon, 24 Oct 2022 08:22:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1omwRZ-0001G0-80 for qemu-devel@nongnu.org; Mon, 24 Oct 2022 08:20:37 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1omwRV-0005Kz-IX for qemu-devel@nongnu.org; Mon, 24 Oct 2022 08:20:36 -0400 Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MwvD90QZGzVhmf; Mon, 24 Oct 2022 20:15:37 +0800 (CST) Received: from dggpemm500007.china.huawei.com (7.185.36.183) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 24 Oct 2022 20:20:21 +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; Mon, 24 Oct 2022 20:20:20 +0800 To: , , , , , , CC: , , , , , , , , , , , , , , , Subject: [PATCH v2] kset: fix memory leak when kset_register() returns error Date: Mon, 24 Oct 2022 20:19:10 +0800 Message-ID: <20221024121910.1169801-1-yangyingliang@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=45.249.212.188; envelope-from=yangyingliang@huawei.com; helo=szxga02-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Qemu-devel" Reply-to: Yang Yingliang From: Yang Yingliang via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1666615136632100003 Content-Type: text/plain; charset="utf-8" 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. */ int kset_register(struct kset *k) { @@ -844,8 +847,11 @@ int kset_register(struct kset *k) =20 kset_init(k); err =3D kobject_add_internal(&k->kobj); - if (err) + if (err) { + kfree_const(k->kobj.name); + k->kobj.name =3D NULL; return err; + } kobject_uevent(&k->kobj, KOBJ_ADD); return 0; } --=20 2.25.1