Subject: CVE-2024-42104: nilfs2: add missing check for inode numbers on directory entries

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

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

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

nilfs2: add missing check for inode numbers on directory entries

Syzbot reported that mounting and unmounting a specific pattern of
corrupted nilfs2 filesystem images causes a use-after-free of metadata
file inodes, which triggers a kernel bug in lru_add_fn().

As Jan Kara pointed out, this is because the link count of a metadata file
gets corrupted to 0, and nilfs_evict_inode(), which is called from iput(),
tries to delete that inode (ifile inode in this case).

The inconsistency occurs because directories containing the inode numbers
of these metadata files that should not be visible in the namespace are
read without checking.

Fix this issue by treating the inode numbers of these internal files as
errors in the sanity check helper when reading directory folios/pages.

Also thanks to Hillf Danton and Matthew Wilcox for their initial mm-layer
analysis.

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

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

	Fixed in 4.19.318 with commit c33c2b0d92aa
	Fixed in 5.4.280 with commit 07c176e7acc5
	Fixed in 5.10.222 with commit 2f2fa9cf7c35
	Fixed in 5.15.163 with commit b11e8fb93ea5
	Fixed in 6.1.98 with commit 1b7d549ed2c1
	Fixed in 6.6.39 with commit 3ab40870edb8
	Fixed in 6.9.9 with commit 265fff1a01cd
	Fixed in 6.10 with commit bb76c6c27468

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-42104
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/nilfs2/dir.c
	fs/nilfs2/nilfs.h

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/c33c2b0d92aa1c2262d999b2598ad6fbd53bd479
	https://git.kernel.org/stable/c/07c176e7acc5579c133bb923ab21316d192d0a95
	https://git.kernel.org/stable/c/2f2fa9cf7c3537958a82fbe8c8595a5eb0861ad7
	https://git.kernel.org/stable/c/b11e8fb93ea5eefb2e4e719497ea177a58ff6131
	https://git.kernel.org/stable/c/1b7d549ed2c1fa202c751b69423a0d3a6bd5a180
	https://git.kernel.org/stable/c/3ab40870edb883b9633dc5cd55f5a2a11afa618d
	https://git.kernel.org/stable/c/265fff1a01cdc083aeaf0d934c929db5cc64aebf
	https://git.kernel.org/stable/c/bb76c6c274683c8570ad788f79d4b875bde0e458