CVE-2022-49236: bpf: Fix UAF due to race between btf_try_get_module and load_module

Published Feb 26, 2025
·
Updated

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 == MODULESTATEGOING, 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 MODULESTATELIVE.

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(usedbtf.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 btfmodules 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.

Affected Software

4 affected components
Linux Linux kernel
Linux Linux kernel>=5.12<5.15.33
Linux Linux kernel>=5.16<5.16.19
Linux Linux kernel>=5.17<5.17.2

Event History

Feb 26, 2025
CVE Published
via MITRE·01:56 AM
Data Sourced
via MITRE·01:56 AM
Description

Frequently Asked Questions

1

What is the severity of CVE-2022-49236?

CVE-2022-49236 is considered a high-severity vulnerability affecting certain versions of the Linux kernel.

2

How do I fix CVE-2022-49236?

To remediate CVE-2022-49236, update your Linux kernel to version 5.15.34 or later, or apply the relevant security patch.

3

What versions of the Linux kernel are affected by CVE-2022-49236?

CVE-2022-49236 affects Linux kernel versions between 5.12 and 5.15.33, as well as versions between 5.16 and 5.17.2.

4

What type of attack does CVE-2022-49236 enable?

CVE-2022-49236 can potentially allow an attacker to exploit a use-after-free condition, leading to unauthorized code execution.

5

Who is at risk from CVE-2022-49236?

Users and administrators running vulnerable versions of the Linux kernel on their systems are at risk from CVE-2022-49236.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203