All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH AUTOSEL 6.0 01/18] kset: fix memory leak when kset_register() returns error
@ 2022-12-24  1:30 Sasha Levin
  2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 02/18] USB: core: Change configuration warnings to notices Sasha Levin
                   ` (16 more replies)
  0 siblings, 17 replies; 20+ messages in thread
From: Sasha Levin @ 2022-12-24  1:30 UTC (permalink / raw)
  To: linux-kernel, stable
  Cc: Yang Yingliang, Luben Tuikov, Greg Kroah-Hartman, Sasha Levin

From: Yang Yingliang <yangyingliang@huawei.com>

[ Upstream commit 1662cea4623f75d8251adf07370bbaa958f0355d ]

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 <luben.tuikov@amd.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Reviewed-by: <luben.tuikov@amd.com>
Link: https://lore.kernel.org/r/20221025071549.1280528-1-yangyingliang@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 lib/kobject.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lib/kobject.c b/lib/kobject.c
index 5f0e71ab292c..0f9cc0b93d99 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()
+ * is freed, it can not be used any more.
  */
 int kset_register(struct kset *k)
 {
@@ -844,8 +847,12 @@ int kset_register(struct kset *k)
 
 	kset_init(k);
 	err = kobject_add_internal(&k->kobj);
-	if (err)
+	if (err) {
+		kfree_const(k->kobj.name);
+		/* Set it to NULL to avoid accessing bad pointer in callers. */
+		k->kobj.name = NULL;
 		return err;
+	}
 	kobject_uevent(&k->kobj, KOBJ_ADD);
 	return 0;
 }
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2022-12-24  5:48 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-24  1:30 [PATCH AUTOSEL 6.0 01/18] kset: fix memory leak when kset_register() returns error Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 02/18] USB: core: Change configuration warnings to notices Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 03/18] usb: core: stop USB enumeration if too many retries Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 04/18] usb: gadget: aspeed: fix buffer overflow Sasha Levin
2022-12-24  1:30   ` Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 05/18] usb: gadget: u_ether: Do not make UDC parent of the net device Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 06/18] usb: gadget: f_ecm: Always set current gadget in ecm_bind() Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 07/18] chardev: Fix potential memory leak when cdev_add() failed Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 08/18] usb/usbip: Fix v_recv_cmd_submit() to use PIPE_BULK define Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 09/18] char: xillybus: Prevent use-after-free due to race condition Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 10/18] habanalabs: zero ts registration buff when allocated Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 11/18] char: xillybus: Fix trivial bug with mutex Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 12/18] iio: filter: admv8818: close potential out-of-bounds read in __admv8818_read_[h|l]pf_freq() Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 13/18] xhci: disable U3 suspended ports in S4 hibernate poweroff_late stage Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 14/18] ACPICA: Fix operand resolution Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 15/18] ksmbd: Fix resource leak in smb2_lock() Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 16/18] writeback: Add asserts for adding freed inode to lists Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 17/18] exfat: fix overflow in sector and cluster conversion Sasha Levin
2022-12-24  1:30 ` [PATCH AUTOSEL 6.0 18/18] fbdev: smscufx: fix error handling code in ufx_usb_probe Sasha Levin
2022-12-24  1:30   ` Sasha Levin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.