In msm_gpu_init(), gpu->memptrs_bo is allocated with msm_gem_kernel_new(), which maps vaddr, pins the IOVA, and returns a GEM object with a single reference (refcount == 1). If creating a ringbuffer (msm_ringbuffer_new()) or initializing performance counters (msm_perfcntr_init()) subsequently fails and jumps to `fail:`, msm_gpu_init() destroys gpu->rb[i] and clears each slot to NULL, then calls msm_gem_kernel_put(gpu->memptrs_bo, gpu->vm) -- dropping the sole reference and freeing the GEM object -- without clearing gpu->memptrs_bo to NULL. All Adreno callers (a2xx_gpu_init(), a3xx_gpu_init(), a4xx_gpu_init(), a5xx_gpu_init(), and a6xx_gpu_init()) respond to a non-zero return from msm_gpu_init() by calling _destroy() -> adreno_gpu_cleanup() -> msm_gpu_cleanup(), which unconditionally calls msm_gem_kernel_put(gpu->memptrs_bo, gpu->vm) a second time on the already-freed object. Exercising msm_gpu_init() followed by msm_gpu_cleanup() in an x86_64 KASAN QEMU guest when msm_ringbuffer_new() fails with -ENOSPC triggers a use-after-free in msm_gem_kernel_put(): [drm:msm_gpu_init] *ERROR* could not create ringbuffer 0: -28 BUG: KASAN: slab-use-after-free in msm_gem_kernel_put Call Trace: msm_gem_kernel_put msm_gpu_cleanup Set gpu->memptrs_bo = NULL after msm_gem_kernel_put() in msm_gpu_init()'s `fail:` path so the subsequent msm_gem_kernel_put(gpu->memptrs_bo, ...) in msm_gpu_cleanup() returns early on IS_ERR_OR_NULL(bo). Fixes: cd414f3d9316 ("drm/msm: Move memptrs to msm_gpu") Cc: stable@vger.kernel.org Signed-off-by: Yuansheng Yang <0xbeac@gmail.com> --- Testing: - Compiled drivers/gpu/drm/msm/msm_gpu.o with W=1 on x86_64 (CONFIG_DRM_MSM=y, CONFIG_COMPILE_TEST=y, CONFIG_KASAN=y) with 0 warnings and 0 errors. - Verified in an x86_64 KASAN QEMU guest using a reproducer module that triggers msm_ringbuffer_new() failure (-ENOSPC) in msm_gpu_init() followed by msm_gpu_cleanup(): before this fix it reliably triggered KASAN slab-use-after-free in msm_gem_kernel_put() and a fatal wild memory access panic; with this fix, 10 consecutive iterations returned -ENOSPC cleanly with gpu->memptrs_bo == NULL and 0 KASAN, BUG, or WARNING messages on unload. drivers/gpu/drm/msm/msm_gpu.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/gpu/drm/msm/msm_gpu.c b/drivers/gpu/drm/msm/msm_gpu.c index 831911904..6d95d8cfd 100644 --- a/drivers/gpu/drm/msm/msm_gpu.c +++ b/drivers/gpu/drm/msm/msm_gpu.c @@ -1034,6 +1034,7 @@ int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev, } msm_gem_kernel_put(gpu->memptrs_bo, gpu->vm); + gpu->memptrs_bo = NULL; platform_set_drvdata(pdev, NULL); return ret; -- 2.53.0