git: 730405499ecb - stable/14 - PT_CONTINUE: undo transparent attach consequences

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Thu, 05 Jun 2025 00:35:21 UTC
The branch stable/14 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=730405499ecb63f84b0f0aa9a866dbfd1250e75b

commit 730405499ecb63f84b0f0aa9a866dbfd1250e75b
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-05-27 21:09:18 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-06-05 00:35:03 +0000

    PT_CONTINUE: undo transparent attach consequences
    
    PR:     287050
    
    (cherry picked from commit 99976934274de6fa19f049a0b6eac10856710f96)
---
 sys/kern/sys_process.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/sys/kern/sys_process.c b/sys/kern/sys_process.c
index 5f647961d676..4d1d0ef21a15 100644
--- a/sys/kern/sys_process.c
+++ b/sys/kern/sys_process.c
@@ -40,6 +40,7 @@
 #include <sys/mman.h>
 #include <sys/mutex.h>
 #include <sys/reg.h>
+#include <sys/sleepqueue.h>
 #include <sys/syscallsubr.h>
 #include <sys/sysent.h>
 #include <sys/sysproto.h>
@@ -1352,6 +1353,27 @@ kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
 		if (data == SIGKILL)
 			proc_wkilled(p);
 
+		/*
+		 * If the PT_CONTINUE-like operation is attempted on
+		 * the thread on sleepq, this is possible only after
+		 * the transparent PT_ATTACH.  In this case, if the
+		 * caller modified the thread state, e.g. by writing
+		 * register file or specifying the pc, make the thread
+		 * xstopped by waking it up.
+		 */
+		if ((td2->td_dbgflags & TDB_USERWR) != 0) {
+			if (pt_attach_transparent) {
+				thread_lock(td2);
+				if (TD_ON_SLEEPQ(td2) &&
+				    (td2->td_flags & TDF_SINTR) != 0) {
+					sleepq_abort(td2, EINTR);
+				} else {
+					thread_unlock(td2);
+				}
+			}
+			td2->td_dbgflags &= ~TDB_USERWR;
+		}
+
 		/*
 		 * Unsuspend all threads.  To leave a thread
 		 * suspended, use PT_SUSPEND to suspend it before