CVE-2022-49236

Source
https://nvd.nist.gov/vuln/detail/CVE-2022-49236
Import Source
https://storage.googleapis.com/osv-test-cve-osv-conversion/osv-output/CVE-2022-49236.json
JSON Data
https://api.test.osv.dev/v1/vulns/CVE-2022-49236
Downstream
Related
Published
2025-02-26T01:56:00Z
Modified
2025-10-15T20:19:55.222958Z
Severity
  • 7.8 (High) CVSS_V3 - CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H CVSS Calculator
Summary
bpf: Fix UAF due to race between btf_try_get_module and load_module
Details

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

bpf: Fix UAF due to race between btftrygetmodule and loadmodule

While working on code to populate kfunc BTF ID sets for module BTF from its initcall, I noticed that by the time the initcall is invoked, the module BTF can already be seen by userspace (and the BPF verifier). The existing btftrygetmodule calls trymoduleget which only fails if mod->state == MODULESTATE_GOING, i.e. it can increment module reference when module initcall is happening in parallel.

Currently, BTF parsing happens from MODULESTATECOMING notifier callback. At this point, the module initcalls have not been invoked. The notifier callback parses and prepares the module BTF, allocates an ID, which publishes it to userspace, and then adds it to the btfmodules list allowing the kernel to invoke btftrygetmodule for the BTF.

However, at this point, the module has not been fully initialized (i.e. its initcalls have not finished). The code in module.c can still fail and free the module, without caring for other users. However, nothing stops btftrygetmodule from succeeding between the state transition from MODULESTATECOMING to MODULESTATE_LIVE.

This leads to a use-after-free issue when BPF program loads successfully in the state transition, loadmodule's doinitmodule call fails and frees the module, and BPF program fd on close calls moduleput for the freed module. Future patch has test case to verify we don't regress in this area in future.

There are multiple points after preparecomingmodule (in loadmodule) where failure can occur and module loading can return error. We illustrate and test for the race using the last point where it can practically occur (in module _init function).

An illustration of the race:

CPU 0 CPU 1 loadmodule notifiercall(MODULESTATECOMING) btfparsemodule btfallocid // Published to userspace listadd(&btfmod->list, btfmodules) mod->init(...) ... ^ bpfcheck | checkpseudobtfid | btftrygetmodule | returns true | ... ... | module _init in progress return progfd | ... ... V if (ret < 0) freemodule(mod) ... close(progfd) ... bpfprogfreedeferred moduleput(used_btf.mod) // use-after-free

We fix this issue by setting a flag BTFMODULEFLIVE, from the notifier callback when MODULESTATELIVE state is reached for the module, so that we return NULL from btftrygetmodule for modules that are not fully formed. Since trymoduleget already checks that module is not in MODULESTATEGOING state, and that is the only transition a live module can make before being removed from btf_modules list, this is enough to close the race and prevent the bug.

A later selftest patch crafts the race condition artifically to verify that it has been fixed, and that verifier fails to load program (with ENXIO).

Lastly, a couple of comments:

  1. Even if this race didn't exist, it seems more appropriate to only access resources (ksyms and kfuncs) of a fully formed module which has been initialized completely.

  2. This patch was born out of need for synchronization against module initcall for the next patch, so it is needed for correctness even without the aforementioned race condition. The BTF resources initialized by module initcall are set up once and then only looked up, so just waiting until the initcall has finished ensures correct behavior.

References

Affected packages

Git / git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git

Affected ranges

Type
GIT
Repo
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
Events
Introduced
541c3bad8dc51b253ba8686d0cd7628e6b9b5f4c
Fixed
51b82141fffa454abf937a8ff0b8af89e4fd0c8f
Type
GIT
Repo
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
Events
Introduced
541c3bad8dc51b253ba8686d0cd7628e6b9b5f4c
Fixed
d7fccf264b1a785525b366a5b7f8113c756187ad
Type
GIT
Repo
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
Events
Introduced
541c3bad8dc51b253ba8686d0cd7628e6b9b5f4c
Fixed
0481baa2318cb1ab13277715da6cdbb657807b3f
Type
GIT
Repo
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
Events
Introduced
541c3bad8dc51b253ba8686d0cd7628e6b9b5f4c
Fixed
18688de203b47e5d8d9d0953385bf30b5949324f

Affected versions

v5.*

v5.11
v5.11-rc3
v5.11-rc4
v5.11-rc5
v5.11-rc6
v5.11-rc7
v5.12
v5.12-rc1
v5.12-rc1-dontuse
v5.12-rc2
v5.12-rc3
v5.12-rc4
v5.12-rc5
v5.12-rc6
v5.12-rc7
v5.12-rc8
v5.13
v5.13-rc1
v5.13-rc2
v5.13-rc3
v5.13-rc4
v5.13-rc5
v5.13-rc6
v5.13-rc7
v5.14
v5.14-rc1
v5.14-rc2
v5.14-rc3
v5.14-rc4
v5.14-rc5
v5.14-rc6
v5.14-rc7
v5.15
v5.15-rc1
v5.15-rc2
v5.15-rc3
v5.15-rc4
v5.15-rc5
v5.15-rc6
v5.15-rc7
v5.15.1
v5.15.10
v5.15.11
v5.15.12
v5.15.13
v5.15.14
v5.15.15
v5.15.16
v5.15.17
v5.15.18
v5.15.19
v5.15.2
v5.15.20
v5.15.21
v5.15.22
v5.15.23
v5.15.24
v5.15.25
v5.15.26
v5.15.27
v5.15.28
v5.15.29
v5.15.3
v5.15.30
v5.15.31
v5.15.32
v5.15.4
v5.15.5
v5.15.6
v5.15.7
v5.15.8
v5.15.9
v5.16
v5.16-rc1
v5.16-rc2
v5.16-rc3
v5.16-rc4
v5.16-rc5
v5.16-rc6
v5.16-rc7
v5.16-rc8
v5.16.1
v5.16.10
v5.16.11
v5.16.12
v5.16.13
v5.16.14
v5.16.15
v5.16.16
v5.16.17
v5.16.18
v5.16.2
v5.16.3
v5.16.4
v5.16.5
v5.16.6
v5.16.7
v5.16.8
v5.16.9
v5.17
v5.17-rc1
v5.17-rc2
v5.17-rc3
v5.17-rc4
v5.17-rc5
v5.17-rc6
v5.17-rc7
v5.17-rc8
v5.17.1

Linux / Kernel

Package

Name
Kernel

Affected ranges

Type
ECOSYSTEM
Events
Introduced
5.12.0
Fixed
5.15.33
Type
ECOSYSTEM
Events
Introduced
5.16.0
Fixed
5.16.19
Type
ECOSYSTEM
Events
Introduced
5.17.0
Fixed
5.17.2