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 v3 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails
Date: Mon, 23 Nov 2026 03:11:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-228581@kernel.org> (raw)
In-Reply-To: <20261123024737.1475714-3-Jesse.Zhang@amd.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/amdkfd: premature debugger_process store in
  kfd_dbg_trap_enable()
--

commit aae136214c876252feb4eb6f8aa225bc102db225
Author: Jesse Zhang <Jesse.Zhang@amd.com>

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

This commit moves the assignment of target->debugger_process into
kfd_dbg_trap_enable() and clears it if copy_to_user() fails, preventing a
failed KFD_IOC_DBG_TRAP_ENABLE call from leaving target->debugger_process
set.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> index a025c3189753b..cf62aaffc2600 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> @@ -793,8 +793,8 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
>  	return r;
>  }
>  
> -int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
> -			void __user *runtime_info, uint32_t *runtime_size)
> +int kfd_dbg_trap_enable(struct kfd_process *target, struct kfd_process *debugger,
> +			uint32_t fd, void __user *runtime_info, uint32_t *runtime_size)
>  {
>  	struct file *f;
>  	uint32_t copy_size;
> @@ -835,6 +835,8 @@ int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
>  	kref_get(&target->ref);
>  	target->debug_trap_enabled = true;
>  
> +	if (target != debugger)
> +		target->debugger_process = debugger;
>  	if (target->debugger_process)
>  		atomic_inc(&target->debugger_process->debugged_process_count);
>  
> @@ -844,6 +846,7 @@ int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
>  		target->dbg_ev_file = NULL;
>  		if (target->debugger_process)
>  			atomic_dec(&target->debugger_process->debugged_process_count);
> +		target->debugger_process = NULL;
>  		target->debug_trap_enabled = false;
>  		kfd_unref_process(target);
>  		r = -EFAULT;

[Severity: Medium]
Can setting target->debugger_process before copy_to_user() still allow the
caller to bypass the ptrace check in kfd_ioctl_set_debug_trap()?

kfd_ioctl_set_debug_trap() checks target->debugger_process before taking
target->mutex:

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);
    ...
}

If copy_to_user() sleeps (for example via userfaultfd) and then fails with
-EFAULT, a second thread in the debugger process can pass the unlocked
permission check after ptrace is detached and wait on target->mutex:

CPU0                              CPU1
kfd_ioctl_set_debug_trap()
  mutex_lock(&target->mutex)
  kfd_dbg_trap_enable()
    target->debugger_process = p
    copy_to_user() [sleeps]
                                  [ptrace detached]
                                  kfd_ioctl_set_debug_trap()
                                    target->debugger_process == p
                                    mutex_lock(&target->mutex)
    /* copy_to_user() fails */
    target->debugger_process = NULL
    target->debug_trap_enabled = false
  mutex_unlock(&target->mutex)
                                    kfd_dbg_trap_enable()  <-- succeeds without ptrace

Could copy_to_user() be called before setting target->debugger_process and
activating the debug session?

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

  reply	other threads:[~2026-11-23  3:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  2:47 [PATCH v3 1/3] drm/amdkfd: don't flush the TLB of a device the process has no VM on Jesse Zhang
2026-11-23  2:47 ` [PATCH v3 2/3] drm/amdkfd: return the memory violation data to the debugger Jesse Zhang
2026-11-23  2:47 ` [PATCH v3 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails Jesse Zhang
2026-11-23  3:11   ` sashiko-bot [this message]
2026-11-23 15:08   ` Kim, Jonathan
2026-11-23  3:12 ` [PATCH v3 1/3] drm/amdkfd: don't flush the TLB of a device the process has no VM on 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=sashiko-outbox-228581@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