Subject: CVE-2023-21658: memstick: fix memory leak if card device is never registered

From: Greg Kroah-Hartman <[email-masked]>

Description
===========

memstick: fix memory leak if card device is never registered

When calling dev_set_name() memory is allocated for the name for the
struct device.  Once that structure device is registered, or attempted
to be registerd, with the driver core, the driver core will handle
cleaning up that memory when the device is removed from the system.

Unfortunatly for the memstick code, there is an error path that causes
the struct device to never be registered, and so the memory allocated in
dev_set_name will be leaked.  Fix that leak by manually freeing it right
before the memory for the device is freed.

The Linux kernel CVE team has assigned CVE-2023-21658 to this issue.

Affected and fixed versions
===========================

	Issue introduced in 2.6.29 with commit 0252c3b4f018 and fixed in 4.14.314 with commit 052d22bf5453
	Issue introduced in 2.6.29 with commit 0252c3b4f018 and fixed in 4.19.282 with commit 70ae89da72f3
	Issue introduced in 2.6.29 with commit 0252c3b4f018 and fixed in 5.4.242 with commit 1b8b54fc55a4
	Issue introduced in 2.6.29 with commit 0252c3b4f018 and fixed in 5.10.179 with commit e10a6d88ae9b
	Issue introduced in 2.6.29 with commit 0252c3b4f018 and fixed in 5.15.109 with commit ebab1a86ca3e
	Issue introduced in 2.6.29 with commit 0252c3b4f018 and fixed in 6.1.26 with commit a3a93b46833f
	Issue introduced in 2.6.29 with commit 0252c3b4f018 and fixed in 6.2.13 with commit d299776014ba
	Issue introduced in 2.6.29 with commit 0252c3b4f018 and fixed in 6.3 with commit 4b6d621c9d85

Please see https://www.kernel.org or a full list of currently supported
kernel versions by the kernel community.

Unaffected versions might change over time as fixes are backported to
older supported kernel versions.  The official CVE entry at
	https://cve.org/CVERecord/?id=CVE-2023-21658
will be updated if fixes are backported, please check that for the most
up to date information about this issue.

Affected files
==============

The file(s) affected by this issue are:
	drivers/memstick/core/memstick.c

Mitigation
==========

The Linux kernel CVE team recommends that you update to the latest
stable kernel version for this, and many other bugfixes.  Individual
changes are never tested alone, but rather are part of a larger kernel
release.  Cherry-picking individual commits is not recommended or
supported by the Linux kernel community at all.  If however, updating to
the latest release is impossible, the individual changes to resolve this
issue can be found at these commits:
	https://git.kernel.org/stable/c/052d22bf5453
	https://git.kernel.org/stable/c/70ae89da72f3
	https://git.kernel.org/stable/c/1b8b54fc55a4
	https://git.kernel.org/stable/c/e10a6d88ae9b
	https://git.kernel.org/stable/c/ebab1a86ca3e
	https://git.kernel.org/stable/c/a3a93b46833f
	https://git.kernel.org/stable/c/d299776014ba
	https://git.kernel.org/stable/c/4b6d621c9d85