Subject: CVE-2023-52447: bpf: Defer the free of inner map when necessary

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

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

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

bpf: Defer the free of inner map when necessary

When updating or deleting an inner map in map array or map htab, the map
may still be accessed by non-sleepable program or sleepable program.
However bpf_map_fd_put_ptr() decreases the ref-counter of the inner map
directly through bpf_map_put(), if the ref-counter is the last one
(which is true for most cases), the inner map will be freed by
ops->map_free() in a kworker. But for now, most .map_free() callbacks
don't use synchronize_rcu() or its variants to wait for the elapse of a
RCU grace period, so after the invocation of ops->map_free completes,
the bpf program which is accessing the inner map may incur
use-after-free problem.

Fix the free of inner map by invoking bpf_map_free_deferred() after both
one RCU grace period and one tasks trace RCU grace period if the inner
map has been removed from the outer map before. The deferment is
accomplished by using call_rcu() or call_rcu_tasks_trace() when
releasing the last ref-counter of bpf map. The newly-added rcu_head
field in bpf_map shares the same storage space with work field to
reduce the size of bpf_map.

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

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

	Issue introduced in 5.9 with commit bba1dc0b55ac and fixed in 5.10.214 with commit 90c445799fd1
	Issue introduced in 5.9 with commit bba1dc0b55ac and fixed in 5.15.153 with commit 37d98fb9c314
	Issue introduced in 5.9 with commit bba1dc0b55ac and fixed in 6.1.75 with commit 62fca83303d6
	Issue introduced in 5.9 with commit bba1dc0b55ac and fixed in 6.6.14 with commit f91cd728b10c
	Issue introduced in 5.9 with commit bba1dc0b55ac and fixed in 6.7.2 with commit bfd9b20c4862
	Issue introduced in 5.9 with commit bba1dc0b55ac and fixed in 6.8 with commit 876673364161

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-2023-52447
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:
	include/linux/bpf.h
	kernel/bpf/map_in_map.c
	kernel/bpf/syscall.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/90c445799fd1dc214d7c6279c144e33a35e29ef2
	https://git.kernel.org/stable/c/37d98fb9c3144c0fddf7f6e99aece9927ac8dce6
	https://git.kernel.org/stable/c/62fca83303d608ad4fec3f7428c8685680bb01b0
	https://git.kernel.org/stable/c/f91cd728b10c51f6d4a39957ccd56d1e802fc8ee
	https://git.kernel.org/stable/c/bfd9b20c4862f41d4590fde11d70a5eeae53dcc5
	https://git.kernel.org/stable/c/876673364161da50eed6b472d746ef88242b2368