AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jesse Zhang" <Jesse.Zhang@amd.com>, amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails
Date: Wed, 18 Nov 2026 06:07:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-220206@kernel.org> (raw)
In-Reply-To: <20261118055009.3005630-3-Jesse.Zhang@amd.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/amdkfd: TOCTOU race on debugger_process in
  kfd_ioctl_set_debug_trap()
--

commit 7ecd0279fcac9bb56764baeea1894a732f3b3ff3
Author: Jesse Zhang <Jesse.Zhang@amd.com>

drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails

This commit restores target->debugger_process when kfd_dbg_trap_enable()
fails and returns -EALREADY before modifying target->debugger_process if
debugging is already enabled.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> index 5a7f4f0cd0ce7..9969d48ca6c7f 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> @@ -3226,7 +3226,19 @@ static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, v
>  	}
>  
>  	switch (args->op) {
> -	case KFD_IOC_DBG_TRAP_ENABLE:
> +	case KFD_IOC_DBG_TRAP_ENABLE: {
> +		struct kfd_process *old_debugger = target->debugger_process;
> +
> +		/*
> +		 * Don't touch the debugger of a session that is already enabled,
> +		 * not even temporarily: its exit cleanup looks the pointer up
> +		 * without target->mutex.
> +		 */
> +		if (target->debug_trap_enabled) {
> +			r = -EALREADY;
> +			break;
> +		}
> +
>  		if (target != p)
>  			target->debugger_process = p;
>  
> @@ -3236,8 +3248,12 @@ static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, v
>  					&args->enable.rinfo_size);
>  		if (!r)
>  			target->exception_enable_mask = args->enable.exception_mask;
> +		else
> +			/* the debugger check above trusts this pointer */
> +			target->debugger_process = old_debugger;
>  
>  		break;
> +	}
>  	case KFD_IOC_DBG_TRAP_DISABLE:
>  		r = kfd_dbg_trap_disable(target, true);
>  		break;

[Severity: Medium]
Can a second thread in the debugger process still bypass the ptrace check
by reading target->debugger_process before kfd_dbg_trap_enable() rolls it
back?

kfd_ioctl_set_debug_trap() checks target->debugger_process before
acquiring target->mutex and does not re-check it after locking:

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c:kfd_ioctl_set_debug_trap() {
	...
	rcu_read_lock();
	if (target != p && ptrace_parent(target->lead_thread) != current
			&& target->debugger_process != p) {
		pr_err("PID %i is not PTRACED and cannot be debugged\n", args->pid);
		r = -EPERM;
	}
	rcu_read_unlock();

	if (r)
		goto out;

	mutex_lock(&target->mutex);
	...
}

Because target->debugger_process is set to p while kfd_dbg_trap_enable()
sleeps in copy_to_user(), another thread in p can pass the unlocked
permission check and wait on target->mutex:

CPU0 (ptrace thread in p)         CPU1 (other thread in p)
kfd_ioctl_set_debug_trap(ENABLE)
  mutex_lock(&target->mutex)
  target->debugger_process = p
  kfd_dbg_trap_enable()
    copy_to_user(...)
                                  kfd_ioctl_set_debug_trap(ENABLE)
                                    /* sees target->debugger_process == p */
                                    mutex_lock(&target->mutex)
    /* fails with -EFAULT */
  target->debugger_process = NULL
  mutex_unlock(&target->mutex)
/* detaches ptrace */
                                    target->debugger_process = p
                                    kfd_dbg_trap_enable()  <-- succeeds without ptrace

The same window exists during KFD_IOC_DBG_TRAP_DISABLE while
kfd_dbg_trap_disable() sleeps in cancel_work_sync() under target->mutex
before clearing target->debugger_process.  Performing the permission check
inside target->mutex and only setting target->debugger_process after
kfd_dbg_trap_enable() succeeds (passing p into kfd_dbg_trap_enable() or
updating debugged_process_count afterwards) would close this race.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118055009.3005630-1-Jesse.Zhang@amd.com?part=3

      reply	other threads:[~2026-11-18  6:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  5:49 [PATCH 1/3] drm/amdkfd: only flush the TLB of devices whose queues got suspended Jesse Zhang
2026-11-18  5:49 ` [PATCH 2/3] drm/amdkfd: return the memory violation data to the debugger Jesse Zhang
2026-11-18  6:06   ` sashiko-bot
2026-11-18  5:50 ` [PATCH 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails Jesse Zhang
2026-11-18  6:07   ` sashiko-bot [this message]

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=sashiko-outbox-220206@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Jesse.Zhang@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox