All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Seongjun Hong" <hsj0512@snu.ac.kr>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH] drm/radeon: check radeon_ttm_tt_pin_userptr() return value in bind
Date: Thu, 27 Aug 2026 16:12:34 +0000	[thread overview]
Message-ID: <20260827161234.5F47C1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260827160124.679309-2-hsj0512@snu.ac.kr>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The error path in `radeon_ttm_tt_pin_userptr()` incorrectly frees `ttm->sg` using `kfree()` without setting the pointer to NULL and without properly freeing the inner scatterlist array if it was allocated. This leads to a memory leak, dangling pointer, use-after-free, double-put of pages, and double-free during the object teardown sequence.
--

commit 4389e2026f1a66251ba9951cbc3ed34d9181fa4f
Author: Seongjun Hong <hsj0512@snu.ac.kr>

drm/radeon: check radeon_ttm_tt_pin_userptr() return value in bind

This commit propagates the error from radeon_ttm_tt_pin_userptr() in
radeon_ttm_backend_bind() to avoid writing stale or uninitialized DMA
addresses to the GPU's GART page table.

> diff --git a/drivers/gpu/drm/radeon/radeon_ttm.c b/drivers/gpu/drm/radeon/radeon_ttm.c
> index e7ab8162ac694..1b72cac9ed6b6 100644
> --- a/drivers/gpu/drm/radeon/radeon_ttm.c
> +++ b/drivers/gpu/drm/radeon/radeon_ttm.c
> @@ -431,7 +431,9 @@ static int radeon_ttm_backend_bind(struct ttm_device *bdev,
>  		return 0;
>  
>  	if (gtt->userptr) {
> -		radeon_ttm_tt_pin_userptr(bdev, ttm);
> +		r = radeon_ttm_tt_pin_userptr(bdev, ttm);
> +		if (r)
> +			return r;

[Severity: Critical]
This is a pre-existing issue, but by returning the error here, we trigger
the TTM object teardown sequence which exposes memory management bugs in
the existing error handling paths.

If radeon_ttm_tt_pin_userptr() fails at dma_map_sgtable() (for instance),
it does this:

drivers/gpu/drm/radeon/radeon_ttm.c:radeon_ttm_tt_pin_userptr() {
    ...
	r = dma_map_sgtable(rdev->dev, ttm->sg, direction, 0);
	if (r)
		goto release_sg;
    ...
release_sg:
	kfree(ttm->sg);
release_pages:
	release_pages(ttm->pages, pinned);
	return r;
}

This frees ttm->sg without setting it to NULL, leaving a dangling pointer.
It also leaks the internal scatterlist nodes instead of using sg_free_table().

Because we now return the error from radeon_ttm_backend_bind(), TTM initiates
teardown via ttm_tt_unpopulate() -> radeon_ttm_tt_unpopulate() ->
radeon_ttm_tt_unbind() -> radeon_ttm_tt_unpin_userptr().

In radeon_ttm_tt_unpin_userptr(), we have a use-after-free read:

drivers/gpu/drm/radeon/radeon_ttm.c:radeon_ttm_tt_unpin_userptr() {
    ...
	/* double check that we don't free the table twice */
	if (!ttm->sg || !ttm->sg->sgl)
		return;
    ...
}

It also does a double-put on the pages, as they were already released in the
initial error path:

drivers/gpu/drm/radeon/radeon_ttm.c:radeon_ttm_tt_unpin_userptr() {
    ...
		put_page(page);
    ...
}

Finally, radeon_ttm_tt_unpopulate() causes a double-free of ttm->sg:

drivers/gpu/drm/radeon/radeon_ttm.c:radeon_ttm_tt_unpopulate() {
    ...
	if (gtt && gtt->userptr) {
		kfree(ttm->sg);
    ...
}

Can we update the error paths in radeon_ttm_tt_pin_userptr() and related
teardown functions to safely handle these failure cases before propagating
the error here?

>  		flags &= ~RADEON_GART_PAGE_WRITE;
>  	}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260827160124.679309-2-hsj0512@snu.ac.kr?part=1

  reply	other threads:[~2026-08-27 16:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-27  9:00 [PATCH] drm/radeon: switch from get_user_pages to pin_user_pages Seongjun Hong
2026-08-27  9:15 ` sashiko-bot
2026-08-27 16:01   ` [PATCH] drm/radeon: fix double-free/UAF of ttm->sg on userptr pin failure Seongjun Hong
2026-08-27 16:01     ` [PATCH] drm/radeon: check radeon_ttm_tt_pin_userptr() return value in bind Seongjun Hong
2026-08-27 16:12       ` sashiko-bot [this message]
2026-09-06 15:27 ` [PATCH v2] drm/radeon: switch from get_user_pages to pin_user_pages Seongjun Hong
2026-09-06 15:37   ` sashiko-bot

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=20260827161234.5F47C1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hsj0512@snu.ac.kr \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.