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 <[email protected]>

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/[email protected]?part=3

Reply via email to