KFD_IOC_DBG_TRAP_ENABLE stores the caller in target->debugger_process
before calling kfd_dbg_trap_enable(), and nothing resets it when that
fails (-EALREADY, -ENODEV, -EBUSY, -EBADF for a bad dbg_fd, -EFAULT).
The permission check at the top of kfd_ioctl_set_debug_trap() accepts
any caller equal to target->debugger_process, so the caller keeps
debug access after it stopped ptracing the target. The pointer is also
not reference counted, so it dangles once the caller exits, and a later
successful ENABLE by the target itself increments
debugged_process_count of the freed process.
On navi31, a parent that ptraces its child, tries to enable debugging
with dbg_fd = -1 (-EBADF) and then detaches can still enable debugging
on the child with a valid fd:
1. ptraced, ENABLE with dbg_fd=-1 -> -9 (Bad file descriptor)
2. ptrace detached
3. not ptraced, ENABLE with valid fd -> 0 (success)
Restore the previous debugger when enabling fails. Step 3 now fails with
-EPERM.
Return -EALREADY for an already enabled session before touching the
pointer at all: the exit cleanup of the current debugger looks for its
targets through target->debugger_process without holding
target->mutex, so even a temporary change of the pointer could make it
skip the target and leave a dangling pointer behind.
Fixes: 0ab2d7532b05 ("drm/amdkfd: prepare per-process debug enable and disable")
Signed-off-by: Jesse Zhang <[email protected]>
---
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 3d2136adb62b..c42c30b459a3 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;
--
2.50.1