Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Yuansheng Yang <0xbeac@gmail.com>
To: Rob Clark <robin.clark@oss.qualcomm.com>,
	Dmitry Baryshkov <lumag@kernel.org>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>
Cc: Sean Paul <sean@poorly.run>,
	Konrad Dybcio <konradybcio@kernel.org>,
	Akhil P Oommen <akhilpo@oss.qualcomm.com>,
	Abhinav Kumar <abhinav.kumar@linux.dev>,
	Jessica Zhang <jesszhan0024@gmail.com>,
	Marijn Suijten <marijn.suijten@somainline.org>,
	linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org,
	freedreno@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH] drm/msm: fix double put of memptrs_bo on msm_gpu_init() failure
Date: Fri,  4 Dec 2026 14:47:42 +0800	[thread overview]
Message-ID: <20261204064742.851564-1-0xbeac@gmail.com> (raw)

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 <gen>_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

                 reply	other threads:[~2026-12-04  6:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204064742.851564-1-0xbeac@gmail.com \
    --to=0xbeac@gmail.com \
    --cc=abhinav.kumar@linux.dev \
    --cc=airlied@gmail.com \
    --cc=akhilpo@oss.qualcomm.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=freedreno@lists.freedesktop.org \
    --cc=jesszhan0024@gmail.com \
    --cc=konradybcio@kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lumag@kernel.org \
    --cc=marijn.suijten@somainline.org \
    --cc=robin.clark@oss.qualcomm.com \
    --cc=sean@poorly.run \
    --cc=simona@ffwll.ch \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox