Subject: CVE-2024-39496: btrfs: zoned: fix use-after-free due to race with dev replace

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

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

In the Linux kernel, the following vulnerability has been resolved:

btrfs: zoned: fix use-after-free due to race with dev replace

While loading a zone's info during creation of a block group, we can race
with a device replace operation and then trigger a use-after-free on the
device that was just replaced (source device of the replace operation).

This happens because at btrfs_load_zone_info() we extract a device from
the chunk map into a local variable and then use the device while not
under the protection of the device replace rwsem. So if there's a device
replace operation happening when we extract the device and that device
is the source of the replace operation, we will trigger a use-after-free
if before we finish using the device the replace operation finishes and
frees the device.

Fix this by enlarging the critical section under the protection of the
device replace rwsem so that all uses of the device are done inside the
critical section.

The Linux kernel CVE team has assigned CVE-2024-39496 to this issue.

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

	Fixed in 6.1.95 with commit 17765964703b
	Fixed in 6.6.35 with commit 092571ef9a81
	Fixed in 6.9.6 with commit a0cc006f4214
	Fixed in 6.10 with commit 0090d6e1b210

Please see https://www.kernel.org for 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-2024-39496
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:
	fs/btrfs/zoned.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/17765964703b88d8befd899f8501150bb7e07e43
	https://git.kernel.org/stable/c/092571ef9a812566c8f2c9038d9c2a64c49788d6
	https://git.kernel.org/stable/c/a0cc006f4214b87e70983c692e05bb36c59b5752
	https://git.kernel.org/stable/c/0090d6e1b210551e63cf43958dc7a1ec942cdde9