CVE-2022-49236

In the Linux kernel, the following vulnerability has been resolved: bpf: Fix UAF due to race between btf_try_get_module and load_module 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 btf_try_get_module calls try_module_get which only fails if mod->state == MODULE_STATE_GOING, i.e. it can increment module reference when module initcall is happening in parallel. Currently, BTF parsing happens from MODULE_STATE_COMING 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 btf_modules list allowing the kernel to invoke btf_try_get_module 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 btf_try_get_module from succeeding between the state transition from MODULE_STATE_COMING to MODULE_STATE_LIVE. This leads to a use-after-free issue when BPF program loads successfully in the state transition, load_module's do_init_module call fails and frees the module, and BPF program fd on close calls module_put 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 prepare_coming_module (in load_module) 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 load_module notifier_call(MODULE_STATE_COMING) btf_parse_module btf_alloc_id // Published to userspace list_add(&btf_mod->list, btf_modules) mod->init(...) ... ^ bpf_check | check_pseudo_btf_id | btf_try_get_module | returns true | ... ... | module __init in progress return prog_fd | ... ... V if (ret < 0) free_module(mod) ... close(prog_fd) ... bpf_prog_free_deferred module_put(used_btf.mod) // use-after-free We fix this issue by setting a flag BTF_MODULE_F_LIVE, from the notifier callback when MODULE_STATE_LIVE state is reached for the module, so that we return NULL from btf_try_get_module for modules that are not fully formed. Since try_module_get already checks that module is not in MODULE_STATE_GOING 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.
Configurations

Configuration 1 (hide)

OR cpe:2.3:o:linux:linux_kernel:*:*:*:*:*:*:*:*
cpe:2.3:o:linux:linux_kernel:*:*:*:*:*:*:*:*
cpe:2.3:o:linux:linux_kernel:*:*:*:*:*:*:*:*

History

25 Mar 2025, 15:08

Type Values Removed Values Added
References () https://git.kernel.org/stable/c/0481baa2318cb1ab13277715da6cdbb657807b3f - () https://git.kernel.org/stable/c/0481baa2318cb1ab13277715da6cdbb657807b3f - Patch
References () https://git.kernel.org/stable/c/18688de203b47e5d8d9d0953385bf30b5949324f - () https://git.kernel.org/stable/c/18688de203b47e5d8d9d0953385bf30b5949324f - Patch
References () https://git.kernel.org/stable/c/51b82141fffa454abf937a8ff0b8af89e4fd0c8f - () https://git.kernel.org/stable/c/51b82141fffa454abf937a8ff0b8af89e4fd0c8f - Patch
References () https://git.kernel.org/stable/c/d7fccf264b1a785525b366a5b7f8113c756187ad - () https://git.kernel.org/stable/c/d7fccf264b1a785525b366a5b7f8113c756187ad - Patch
First Time Linux
Linux linux Kernel
Summary
  • (es) En el kernel de Linux, se ha resuelto la siguiente vulnerabilidad: bpf: Arreglar UAF debido a la ejecución entre btf_try_get_module y load_module Mientras trabajaba en el código para rellenar los conjuntos de ID de BTF de kfunc para el módulo BTF desde su initcall, noté que para el momento en que se invoca la initcall, el espacio de usuario (y el verificador BPF) ya puede ver el módulo BTF. El btf_try_get_module existente llama a try_module_get, que solo falla si mod-&gt;state == MODULE_STATE_GOING, es decir, puede incrementar la referencia del módulo cuando la initcall del módulo está sucediendo en paralelo. Actualmente, el análisis de BTF ocurre desde la devolución de llamada del notificador MODULE_STATE_COMING. En este punto, las initcalls del módulo no han sido invocadas. La devolución de llamada del notificador analiza y prepara el módulo BTF, asigna un ID, que lo publica en el espacio de usuario y luego lo agrega a la lista btf_modules, lo que permite que el núcleo invoque btf_try_get_module para el BTF. Sin embargo, en este punto, el módulo no se ha inicializado por completo (es decir, sus llamadas de inicio no han finalizado). El código en module.c aún puede fallar y liberar el módulo, sin preocuparse por otros usuarios. Sin embargo, nada impide que btf_try_get_module tenga éxito entre la transición de estado de MODULE_STATE_COMING a MODULE_STATE_LIVE. Esto conduce a un problema de use-after-free cuando el programa BPF se carga correctamente en la transición de estado, la llamada do_init_module de load_module falla y libera el módulo, y el programa BPF fd al cerrar llama a module_put para el módulo liberado. El parche futuro tiene un caso de prueba para verificar que no retrocedamos en esta área en el futuro. Hay varios puntos después de prepare_coming_module (en load_module) donde puede ocurrir un fallo y la carga del módulo puede devolver un error. Ilustramos y probamos la ejecución usando el último punto donde puede ocurrir prácticamente (en la función __init del módulo). Una ilustración de la ejecución: CPU 0 CPU 1 load_module notifier_call(MODULE_STATE_COMING) btf_parse_module btf_alloc_id // Publicado en el espacio de usuario list_add(&amp;btf_mod-&gt;list, btf_modules) mod-&gt;init(...) ... ^ bpf_check | check_pseudo_btf_id | btf_try_get_module | devuelve verdadero | ... ... | módulo __init en progreso devuelve prog_fd | ... ... V if (ret &lt; 0) free_module(mod) ... close(prog_fd) ... bpf_prog_free_deferred module_put(used_btf.mod) // use-after-free Solucionamos este problema estableciendo un indicador BTF_MODULE_F_LIVE, desde la devolución de llamada del notificador cuando se alcanza el estado MODULE_STATE_LIVE para el módulo, de modo que devolvamos NULL desde btf_try_get_module para los módulos que no están completamente formados. Dado que try_module_get ya verifica que el módulo no esté en el estado MODULE_STATE_GOING, y esa es la única transición que un módulo activo puede hacer antes de ser eliminado de la lista btf_modules, esto es suficiente para cerrar la ejecución y evitar el error. Un parche de autoprueba posterior crea la condición de ejecución artificialmente para verificar que se ha solucionado, y que el verificador no puede cargar el programa (con ENXIO). Por último, un par de comentarios: 1. Incluso si esta ejecución no existiera, parece más apropiado acceder solo a los recursos (ksyms y kfuncs) de un módulo completamente formado que se haya inicializado por completo. 2. Este parche nació de la necesidad de sincronización con el módulo initcall para el próximo parche, por lo que es necesario para la corrección incluso sin la condición de ejecución mencionada anteriormente. Los recursos BTF inicializados por el módulo initcall se configuran una vez y luego solo se buscan, por lo que simplemente esperar hasta que el initcall haya terminado garantiza un comportamiento correcto.
CPE cpe:2.3:o:linux:linux_kernel:*:*:*:*:*:*:*:*

27 Feb 2025, 18:15

Type Values Removed Values Added
CVSS v2 : unknown
v3 : unknown
v2 : unknown
v3 : 7.8
CWE CWE-416

26 Feb 2025, 07:01

Type Values Removed Values Added
New CVE

Information

Published : 2025-02-26 07:01

Updated : 2025-03-25 15:08


NVD link : CVE-2022-49236

Mitre link : CVE-2022-49236

CVE.ORG link : CVE-2022-49236


JSON object : View

Products Affected

linux

  • linux_kernel
CWE
CWE-416

Use After Free