{"id":"CVE-2026-31584","aliases":["CVE-2026-31584"],"related":[],"kind":"vulnerability","state":"published","title":"media: mediatek: vcodec: fix use-after-free in encoder release path","summary":"In the Linux kernel, the following vulnerability has been resolved:\n\nmedia: mediatek: vcodec: fix use-after-free in encoder release path\n\nThe fops_vcodec_release() function frees the context structure (ctx)\nwithout first cancelling any pending or running work in ctx->encode_work.\nThis creates a race window where the workqueue handler (mtk_venc_worker)\nmay still be accessing the context memory after it has been freed.\n\nRace condition:\n\n    CPU 0 (release path)               CPU 1 (workqueue)\n    ---------------------               ------------------\n    fops_vcodec_release()\n      v4l2_m2m_ctx_release()\n        v4l2_m2m_cancel_job()\n        // waits for m2m job \"done\"\n                                        mtk_venc_worker()\n                                          v4l2_m2m_job_finish()\n                                          // m2m job \"done\"\n                                          // BUT worker still running!\n                                          // post-job_finish access:\n                                        other ctx dereferences\n                                          // UAF if ctx already freed\n        // returns (job \"done\")\n      kfree(ctx)  // ctx freed\n\nRoot cause: The v4l2_m2m_ctx_release() only waits for the m2m job\nlifecycle (via TRANS_RUNNING flag), not the workqueue lifecycle.\nAfter v4l2_m2m_job_finish() is called, the m2m framework considers\nthe job complete and v4l2_m2m_ctx_release() returns, but the worker\nfunction continues executing and may still access ctx.\n\nThe work is queued during encode operations via:\n  queue_work(ctx->dev->encode_workqueue, &ctx->encode_work)\nThe worker function accesses ctx->m2m_ctx, ctx->dev, and other ctx\nfields even after calling v4l2_m2m_job_finish().\n\nThis vulnerability was confirmed with KASAN by running an instrumented\ntest module that widens the post-job_finish race window. KASAN detected:\n\n  BUG: KASAN: slab-use-after-free in mtk_venc_worker+0x159/0x180\n  Read of size 4 at addr ffff88800326e000 by task kworker/u8:0/12\n\n  Workqueue: mtk_vcodec_enc_wq mtk_venc_worker\n\n  Allocated by task 47:\n    __kasan_kmalloc+0x7f/0x90\n    fops_vcodec_open+0x85/0x1a0\n\n  Freed by task 47:\n    __kasan_slab_free+0x43/0x70\n    kfree+0xee/0x3a0\n    fops_vcodec_release+0xb7/0x190\n\nFix this by calling cancel_work_sync(&ctx->encode_work) before kfree(ctx).\nThis ensures the workqueue handler is both cancelled (if pending) and\nsynchronized (waits for any running handler to complete) before the\ncontext is freed.\n\nPlacement rationale: The fix is placed after v4l2_ctrl_handler_free()\nand before list_del_init(&ctx->list). At this point, all m2m operations\nare done (v4l2_m2m_ctx_release() has returned), and we need to ensure\nthe workqueue is synchronized before removing ctx from the list and\nfreeing it.\n\nNote: The open error path does NOT need cancel_work_sync() because\nINIT_WORK() only initializes the work structure - it does not schedule\nit. Work is only scheduled later during device_run() operations.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"In the Linux kernel, the following vulnerability has been resolved:\n\nmedia: mediatek: vcodec: fix use-after-free in encoder release path\n\nThe fops_vcodec_release() function frees the context structure (ctx)\nwithout first cancelling any pending or running work in ctx->encode_work.\nThis creates a race window where the workqueue handler (mtk_venc_worker)\nmay still be accessing the context memory after it has been freed.\n\nRace condition:\n\n    CPU 0 (release path)               CPU 1 (workqueue)\n    ---------------------               ------------------\n    fops_vcodec_release()\n      v4l2_m2m_ctx_release()\n        v4l2_m2m_cancel_job()\n        // waits for m2m job \"done\"\n                                        mtk_venc_worker()\n                                          v4l2_m2m_job_finish()\n                                          // m2m job \"done\"\n                                          // BUT worker still running!\n                                          // post-job_finish access:\n                                        other ctx dereferences\n                                          // UAF if ctx already freed\n        // returns (job \"done\")\n      kfree(ctx)  // ctx freed\n\nRoot cause: The v4l2_m2m_ctx_release() only waits for the m2m job\nlifecycle (via TRANS_RUNNING flag), not the workqueue lifecycle.\nAfter v4l2_m2m_job_finish() is called, the m2m framework considers\nthe job complete and v4l2_m2m_ctx_release() returns, but the worker\nfunction continues executing and may still access ctx.\n\nThe work is queued during encode operations via:\n  queue_work(ctx->dev->encode_workqueue, &ctx->encode_work)\nThe worker function accesses ctx->m2m_ctx, ctx->dev, and other ctx\nfields even after calling v4l2_m2m_job_finish().\n\nThis vulnerability was confirmed with KASAN by running an instrumented\ntest module that widens the post-job_finish race window. KASAN detected:\n\n  BUG: KASAN: slab-use-after-free in mtk_venc_worker+0x159/0x180\n  Read of size 4 at addr ffff88800326e000 by task kworker/u8:0/12\n\n  Workqueue: mtk_vcodec_enc_wq mtk_venc_worker\n\n  Allocated by task 47:\n    __kasan_kmalloc+0x7f/0x90\n    fops_vcodec_open+0x85/0x1a0\n\n  Freed by task 47:\n    __kasan_slab_free+0x43/0x70\n    kfree+0xee/0x3a0\n    fops_vcodec_release+0xb7/0x190\n\nFix this by calling cancel_work_sync(&ctx->encode_work) before kfree(ctx).\nThis ensures the workqueue handler is both cancelled (if pending) and\nsynchronized (waits for any running handler to complete) before the\ncontext is freed.\n\nPlacement rationale: The fix is placed after v4l2_ctrl_handler_free()\nand before list_del_init(&ctx->list). At this point, all m2m operations\nare done (v4l2_m2m_ctx_release() has returned), and we need to ensure\nthe workqueue is synchronized before removing ctx from the list and\nfreeing it.\n\nNote: The open error path does NOT need cancel_work_sync() because\nINIT_WORK() only initializes the work structure - it does not schedule\nit. Work is only scheduled later during device_run() operations.","source":"cvelist"},{"lang":"es","text":"En el kernel de Linux, la siguiente vulnerabilidad ha sido resuelta:\n\nmedia: mediatek: vcodec: corrección de uso después de liberación en la ruta de liberación del codificador\n\nLa función fops_vcodec_release() libera la estructura de contexto (ctx) sin cancelar primero ningún trabajo pendiente o en ejecución en ctx  ->  encode_work. Esto crea una ventana de carrera donde el gestor de la cola de trabajo (mtk_venc_worker) aún puede estar accediendo a la memoria de contexto después de que esta haya sido liberada.\n\nCondición de carrera:\n\n    CPU 0 (ruta de liberación)               CPU 1 (cola de trabajo)\n    - - - - - - - - - - - - - - - - - - - - -               - - - - - - - - - - - - - - - - - -\n    fops_vcodec_release()\n      v4l2_m2m_ctx_release()\n        v4l2_m2m_cancel_job()\n        // espera a que el trabajo m2m ’terminado’\n                                        mtk_venc_worker()\n                                          v4l2_m2m_job_finish()\n                                          // trabajo m2m ’terminado’\n                                          // ¡PERO el worker sigue en ejecución!\n                                          // acceso post-job_finish:\n                                        otras desreferencias de ctx\n                                          // UAF si ctx ya ha sido liberado\n        // retorna (trabajo ’terminado’)\n      kfree(ctx)  // ctx liberado\n\nCausa raíz: La función v4l2_m2m_ctx_release() solo espera por el ciclo de vida del trabajo m2m (a través del flag TRANS_RUNNING), no por el ciclo de vida de la cola de trabajo. Después de que se llama a v4l2_m2m_job_finish(), el framework m2m considera el trabajo completo y v4l2_m2m_ctx_release() retorna, pero la función worker continúa ejecutándose y aún puede acceder a ctx.\n\nEl trabajo se encola durante las operaciones de codificación a través de:\n  queue_work(ctx  ->  dev  ->  encode_workqueue, ref:ctx  ->  encode_work)\nLa función worker accede a ctx  ->  m2m_ctx, ctx  ->  dev y otros campos de ctx incluso después de llamar a v4l2_m2m_job_finish().\n\nEsta vulnerabilidad fue confirmada con KASAN ejecutando un módulo de prueba instrumentado que amplía la ventana de carrera post-job_finish. KASAN detectó:\n\n  BUG: KASAN: slab-use-after-free en mtk_venc_worker+0x159/0x180\n  Lectura de tamaño 4 en la dirección ffff88800326e000 por la tarea kworker/u8:0/12\n\n  Cola de trabajo: mtk_vcodec_enc_wq mtk_venc_worker\n\n  Asignado por la tarea 47:\n    __kasan_kmalloc+0x7f/0x90\n    fops_vcodec_open+0x85/0x1a0\n\n  Liberado por la tarea 47:\n    __kasan_slab_free+0x43/0x70\n    kfree+0xee/0x3a0\n    fops_vcodec_release+0xb7/0x190\n\nSolucione esto llamando a cancel_work_sync(ref:ctx  ->  encode_work) antes de kfree(ctx). Esto asegura que el gestor de la cola de trabajo sea tanto cancelado (si está pendiente) como sincronizado (espera a que cualquier gestor en ejecución complete) antes de que el contexto sea liberado.\n\nJustificación de la ubicación: La corrección se ubica después de v4l2_ctrl_handler_free() y antes de list_del_init(ref:ctx  ->  list). En este punto, todas las operaciones m2m están hechas (v4l2_m2m_ctx_release() ha retornado), y necesitamos asegurar que la cola de trabajo esté sincronizada antes de eliminar ctx de la lista y liberarlo.\n\nNota: La ruta de error de apertura NO necesita cancel_work_sync() porque INIT_WORK() solo inicializa la estructura de trabajo, no la programa. El trabajo solo se programa más tarde durante las operaciones de device_run().","source":"nvd"}],"published":"2026-04-24T14:42:13.586Z","modified":"2026-11-11T23:10:00.263Z","severity":{"scorer":"Linux","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H","score":7.8,"rating":"HIGH"},"severities":[{"scorer":"Linux","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H","score":7.8,"rating":"HIGH"}],"cwes":["CWE-416"],"affected":[{"ranges":[{"type":"git","introduced":"0934d37596151edce115c6d0843a9ad7d5e5d232","fixed":"9a9bdaf9dc42ccca50e53f82165292f74a365c11","status":"affected"},{"type":"git","introduced":"0934d37596151edce115c6d0843a9ad7d5e5d232","fixed":"a8a55913552aed45108525d1851c65e1db0cc25b","status":"affected"},{"type":"git","introduced":"0934d37596151edce115c6d0843a9ad7d5e5d232","fixed":"f99353cd0e9f58bf17889049137b8d65fb44ebf1","status":"affected"},{"type":"git","introduced":"0934d37596151edce115c6d0843a9ad7d5e5d232","fixed":"93d9a58961a9e09306857e999b3ee76aa4be67f0","status":"affected"},{"type":"git","introduced":"0934d37596151edce115c6d0843a9ad7d5e5d232","fixed":"f1692337c6fa26e04f89b22a4d84bf5b7ada50d1","status":"affected"},{"type":"git","introduced":"0934d37596151edce115c6d0843a9ad7d5e5d232","fixed":"76e35091ffc722ba39b303e48bc5d08abb59dd56","status":"affected"}],"vendor":"Linux","product":"Linux","repo":"https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git","source":"cvelist"},{"ranges":[{"version":"6.6","status":"affected"},{"type":"semver","introduced":"0","fixed":"6.6","status":"unaffected"},{"type":"semver","introduced":"6.6.136","lastAffected":"6.6.*","status":"unaffected"},{"type":"semver","introduced":"6.12.83","lastAffected":"6.12.*","status":"unaffected"},{"type":"semver","introduced":"6.18.24","lastAffected":"6.18.*","status":"unaffected"},{"type":"semver","introduced":"6.19.14","lastAffected":"6.19.*","status":"unaffected"},{"type":"semver","introduced":"7.0.1","lastAffected":"7.0.*","status":"unaffected"},{"type":"original_commit_for_fix","introduced":"7.1","lastAffected":"*","status":"unaffected"}],"vendor":"Linux","product":"Linux","repo":"https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git","source":"cvelist"},{"ranges":[{"status":"affected","fixed":"6.6.136"},{"status":"affected","introduced":"6.12","fixed":"6.12.83"},{"status":"affected","introduced":"6.13","fixed":"6.18.24"},{"status":"affected","introduced":"6.19","fixed":"6.19.14"},{"status":"affected","introduced":"7.0","fixed":"7.0.1"}],"vendor":"linux","product":"linux_kernel","cpe":"cpe:2.3:o:linux:linux_kernel:*:*:*:*:*:*:*:*","source":"nvd"}],"references":[{"url":"https://git.kernel.org/stable/c/9a9bdaf9dc42ccca50e53f82165292f74a365c11","tags":["patch"],"sources":["cvelist","nvd"]},{"url":"https://git.kernel.org/stable/c/a8a55913552aed45108525d1851c65e1db0cc25b","tags":["patch"],"sources":["cvelist","nvd"]},{"url":"https://git.kernel.org/stable/c/f99353cd0e9f58bf17889049137b8d65fb44ebf1","tags":["patch"],"sources":["cvelist","nvd"]},{"url":"https://git.kernel.org/stable/c/93d9a58961a9e09306857e999b3ee76aa4be67f0","tags":["patch"],"sources":["cvelist","nvd"]},{"url":"https://git.kernel.org/stable/c/f1692337c6fa26e04f89b22a4d84bf5b7ada50d1","tags":["patch"],"sources":["cvelist","nvd"]},{"url":"https://git.kernel.org/stable/c/76e35091ffc722ba39b303e48bc5d08abb59dd56","tags":["patch"],"sources":["cvelist","nvd"]}],"credits":[],"assigner":"Linux","sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2026-31584","url":"https://www.cve.org/CVERecord?id=CVE-2026-31584","licence":"CVE Terms of Use","modified":"2026-08-05T12:23:36.030Z"},{"source":"nvd","name":"NVD","id":"CVE-2026-31584","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-31584","licence":"Public domain","modified":"2026-11-11T23:10:00.263Z"}],"updatedAt":"2026-11-25T22:59:19.496Z","epss":{"score":0.0013,"percentile":0.02871,"date":"2026-12-01T12:00:22.000Z"}}