History log of /openbsd-current/sys/kern/sys_process.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.98 03-Jun-2024 claudio

Remove the now unsued s argument to SCHED_LOCK and SCHED_UNLOCK.

The SPL level is not tacked by the mutex and we no longer need to track
this in the callers.
OK miod@ mlarkin@ tb@ jca@


# 1.97 02-Apr-2024 deraadt

remove useless whitespace; from Jia Tan


# 1.96 30-Mar-2024 mpi

Prevent a recursion inside wakeup(9) when scheduler tracepoints are enabled.

Tracepoints like "sched:enqueue" and "sched:unsleep" were called from inside
the loop iterating over sleeping threads as part of wakeup_proc(). When such
tracepoints were enabled they could result in another wakeup(9) possibly
corrupting the sleepqueue.

Rewrite wakeup(9) in two stages, first dequeue threads from the sleepqueue then
call setrunnable() and possible tracepoints for each of them.

This requires moving unsleep() outside of setrunnable() because it messes with
the sleepqueue.

ok claudio@


Revision tags: OPENBSD_7_5_BASE
# 1.95 21-Nov-2023 bluhm

Fix kernel build without option PTRACE, but with dt(4).

Since revision 1.26 dt_ioctl_get_auxbase() is calling process_domem().
Build the latter function into kernel if pseudo device dt is enabled.

from Matthias Pitzl; OK claudio@


Revision tags: OPENBSD_7_4_BASE
# 1.94 10-Jun-2023 kettenis

Implement support for pointer authentication (PAC) in userland. With PAC
it is possible to "sign" pointers with a hidden key. The signature is
placed in unused bits of the pointer and can be checked later. This can
be used to provide "tail CFI" that is similar to what retguard provides.

Debuggers need to be aware of the fact that pointers can be signed. For
this purpose a new PT_PACMASK ptrace(2) request is introduced that returns
as mask that indicates the bits used for the signature. Separate masks
are provided for code and data pointers even though the masks are identical
in the current implementation. These masks are also written into a special
note section in the core dump.

ok patrick@


Revision tags: OPENBSD_7_3_BASE
# 1.93 24-Jan-2023 deraadt

ptrace reads/writes memory using uvm_io, which generates an temporary
alias mapping using uvm_map_extract. With xonly now operational, this
alias mapping is created with minprot, which for text will be xonly, and
the kernel cannot read it (unless the architecture has implied read for
exec from kernel pov).
Pass UVM_IO_FIXPROT to create the alias with maxprot instead.
ok kettenis


# 1.92 02-Jan-2023 guenther

Add tfind_user(), for getting a proc* given a user-space TID and
the process* that it should be part of. Use that in clock_get{time,res}(),
thrkill(), and ptrace().

ok jca@ miod@ mpi@ mvs@


# 1.91 21-Dec-2022 claudio

The location of the aux info vector is now cached in ps_auxinfo of struct
process. Use this information to access the vector.
OK mpi@ mbuhl@ deraadt@


# 1.90 05-Dec-2022 deraadt

zap a pile of dangling tabs


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.89 07-Dec-2021 guenther

Continue to delete emulation support: since we're Just ELF, the size
of the auxinfo is fixed: provide ELF_AUX_WORDS in <sys/exec_elf.h>
as a replacement for emul->e_arglen

ok millert@


Revision tags: OPENBSD_7_0_BASE
# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.97 02-Apr-2024 deraadt

remove useless whitespace; from Jia Tan


# 1.96 30-Mar-2024 mpi

Prevent a recursion inside wakeup(9) when scheduler tracepoints are enabled.

Tracepoints like "sched:enqueue" and "sched:unsleep" were called from inside
the loop iterating over sleeping threads as part of wakeup_proc(). When such
tracepoints were enabled they could result in another wakeup(9) possibly
corrupting the sleepqueue.

Rewrite wakeup(9) in two stages, first dequeue threads from the sleepqueue then
call setrunnable() and possible tracepoints for each of them.

This requires moving unsleep() outside of setrunnable() because it messes with
the sleepqueue.

ok claudio@


Revision tags: OPENBSD_7_5_BASE
# 1.95 21-Nov-2023 bluhm

Fix kernel build without option PTRACE, but with dt(4).

Since revision 1.26 dt_ioctl_get_auxbase() is calling process_domem().
Build the latter function into kernel if pseudo device dt is enabled.

from Matthias Pitzl; OK claudio@


Revision tags: OPENBSD_7_4_BASE
# 1.94 10-Jun-2023 kettenis

Implement support for pointer authentication (PAC) in userland. With PAC
it is possible to "sign" pointers with a hidden key. The signature is
placed in unused bits of the pointer and can be checked later. This can
be used to provide "tail CFI" that is similar to what retguard provides.

Debuggers need to be aware of the fact that pointers can be signed. For
this purpose a new PT_PACMASK ptrace(2) request is introduced that returns
as mask that indicates the bits used for the signature. Separate masks
are provided for code and data pointers even though the masks are identical
in the current implementation. These masks are also written into a special
note section in the core dump.

ok patrick@


Revision tags: OPENBSD_7_3_BASE
# 1.93 24-Jan-2023 deraadt

ptrace reads/writes memory using uvm_io, which generates an temporary
alias mapping using uvm_map_extract. With xonly now operational, this
alias mapping is created with minprot, which for text will be xonly, and
the kernel cannot read it (unless the architecture has implied read for
exec from kernel pov).
Pass UVM_IO_FIXPROT to create the alias with maxprot instead.
ok kettenis


# 1.92 02-Jan-2023 guenther

Add tfind_user(), for getting a proc* given a user-space TID and
the process* that it should be part of. Use that in clock_get{time,res}(),
thrkill(), and ptrace().

ok jca@ miod@ mpi@ mvs@


# 1.91 21-Dec-2022 claudio

The location of the aux info vector is now cached in ps_auxinfo of struct
process. Use this information to access the vector.
OK mpi@ mbuhl@ deraadt@


# 1.90 05-Dec-2022 deraadt

zap a pile of dangling tabs


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.89 07-Dec-2021 guenther

Continue to delete emulation support: since we're Just ELF, the size
of the auxinfo is fixed: provide ELF_AUX_WORDS in <sys/exec_elf.h>
as a replacement for emul->e_arglen

ok millert@


Revision tags: OPENBSD_7_0_BASE
# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.96 30-Mar-2024 mpi

Prevent a recursion inside wakeup(9) when scheduler tracepoints are enabled.

Tracepoints like "sched:enqueue" and "sched:unsleep" were called from inside
the loop iterating over sleeping threads as part of wakeup_proc(). When such
tracepoints were enabled they could result in another wakeup(9) possibly
corrupting the sleepqueue.

Rewrite wakeup(9) in two stages, first dequeue threads from the sleepqueue then
call setrunnable() and possible tracepoints for each of them.

This requires moving unsleep() outside of setrunnable() because it messes with
the sleepqueue.

ok claudio@


Revision tags: OPENBSD_7_5_BASE
# 1.95 21-Nov-2023 bluhm

Fix kernel build without option PTRACE, but with dt(4).

Since revision 1.26 dt_ioctl_get_auxbase() is calling process_domem().
Build the latter function into kernel if pseudo device dt is enabled.

from Matthias Pitzl; OK claudio@


Revision tags: OPENBSD_7_4_BASE
# 1.94 10-Jun-2023 kettenis

Implement support for pointer authentication (PAC) in userland. With PAC
it is possible to "sign" pointers with a hidden key. The signature is
placed in unused bits of the pointer and can be checked later. This can
be used to provide "tail CFI" that is similar to what retguard provides.

Debuggers need to be aware of the fact that pointers can be signed. For
this purpose a new PT_PACMASK ptrace(2) request is introduced that returns
as mask that indicates the bits used for the signature. Separate masks
are provided for code and data pointers even though the masks are identical
in the current implementation. These masks are also written into a special
note section in the core dump.

ok patrick@


Revision tags: OPENBSD_7_3_BASE
# 1.93 24-Jan-2023 deraadt

ptrace reads/writes memory using uvm_io, which generates an temporary
alias mapping using uvm_map_extract. With xonly now operational, this
alias mapping is created with minprot, which for text will be xonly, and
the kernel cannot read it (unless the architecture has implied read for
exec from kernel pov).
Pass UVM_IO_FIXPROT to create the alias with maxprot instead.
ok kettenis


# 1.92 02-Jan-2023 guenther

Add tfind_user(), for getting a proc* given a user-space TID and
the process* that it should be part of. Use that in clock_get{time,res}(),
thrkill(), and ptrace().

ok jca@ miod@ mpi@ mvs@


# 1.91 21-Dec-2022 claudio

The location of the aux info vector is now cached in ps_auxinfo of struct
process. Use this information to access the vector.
OK mpi@ mbuhl@ deraadt@


# 1.90 05-Dec-2022 deraadt

zap a pile of dangling tabs


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.89 07-Dec-2021 guenther

Continue to delete emulation support: since we're Just ELF, the size
of the auxinfo is fixed: provide ELF_AUX_WORDS in <sys/exec_elf.h>
as a replacement for emul->e_arglen

ok millert@


Revision tags: OPENBSD_7_0_BASE
# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.95 21-Nov-2023 bluhm

Fix kernel build without option PTRACE, but with dt(4).

Since revision 1.26 dt_ioctl_get_auxbase() is calling process_domem().
Build the latter function into kernel if pseudo device dt is enabled.

from Matthias Pitzl; OK claudio@


Revision tags: OPENBSD_7_4_BASE
# 1.94 10-Jun-2023 kettenis

Implement support for pointer authentication (PAC) in userland. With PAC
it is possible to "sign" pointers with a hidden key. The signature is
placed in unused bits of the pointer and can be checked later. This can
be used to provide "tail CFI" that is similar to what retguard provides.

Debuggers need to be aware of the fact that pointers can be signed. For
this purpose a new PT_PACMASK ptrace(2) request is introduced that returns
as mask that indicates the bits used for the signature. Separate masks
are provided for code and data pointers even though the masks are identical
in the current implementation. These masks are also written into a special
note section in the core dump.

ok patrick@


Revision tags: OPENBSD_7_3_BASE
# 1.93 24-Jan-2023 deraadt

ptrace reads/writes memory using uvm_io, which generates an temporary
alias mapping using uvm_map_extract. With xonly now operational, this
alias mapping is created with minprot, which for text will be xonly, and
the kernel cannot read it (unless the architecture has implied read for
exec from kernel pov).
Pass UVM_IO_FIXPROT to create the alias with maxprot instead.
ok kettenis


# 1.92 02-Jan-2023 guenther

Add tfind_user(), for getting a proc* given a user-space TID and
the process* that it should be part of. Use that in clock_get{time,res}(),
thrkill(), and ptrace().

ok jca@ miod@ mpi@ mvs@


# 1.91 21-Dec-2022 claudio

The location of the aux info vector is now cached in ps_auxinfo of struct
process. Use this information to access the vector.
OK mpi@ mbuhl@ deraadt@


# 1.90 05-Dec-2022 deraadt

zap a pile of dangling tabs


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.89 07-Dec-2021 guenther

Continue to delete emulation support: since we're Just ELF, the size
of the auxinfo is fixed: provide ELF_AUX_WORDS in <sys/exec_elf.h>
as a replacement for emul->e_arglen

ok millert@


Revision tags: OPENBSD_7_0_BASE
# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.94 10-Jun-2023 kettenis

Implement support for pointer authentication (PAC) in userland. With PAC
it is possible to "sign" pointers with a hidden key. The signature is
placed in unused bits of the pointer and can be checked later. This can
be used to provide "tail CFI" that is similar to what retguard provides.

Debuggers need to be aware of the fact that pointers can be signed. For
this purpose a new PT_PACMASK ptrace(2) request is introduced that returns
as mask that indicates the bits used for the signature. Separate masks
are provided for code and data pointers even though the masks are identical
in the current implementation. These masks are also written into a special
note section in the core dump.

ok patrick@


Revision tags: OPENBSD_7_3_BASE
# 1.93 24-Jan-2023 deraadt

ptrace reads/writes memory using uvm_io, which generates an temporary
alias mapping using uvm_map_extract. With xonly now operational, this
alias mapping is created with minprot, which for text will be xonly, and
the kernel cannot read it (unless the architecture has implied read for
exec from kernel pov).
Pass UVM_IO_FIXPROT to create the alias with maxprot instead.
ok kettenis


# 1.92 02-Jan-2023 guenther

Add tfind_user(), for getting a proc* given a user-space TID and
the process* that it should be part of. Use that in clock_get{time,res}(),
thrkill(), and ptrace().

ok jca@ miod@ mpi@ mvs@


# 1.91 21-Dec-2022 claudio

The location of the aux info vector is now cached in ps_auxinfo of struct
process. Use this information to access the vector.
OK mpi@ mbuhl@ deraadt@


# 1.90 05-Dec-2022 deraadt

zap a pile of dangling tabs


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.89 07-Dec-2021 guenther

Continue to delete emulation support: since we're Just ELF, the size
of the auxinfo is fixed: provide ELF_AUX_WORDS in <sys/exec_elf.h>
as a replacement for emul->e_arglen

ok millert@


Revision tags: OPENBSD_7_0_BASE
# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.93 24-Jan-2023 deraadt

ptrace reads/writes memory using uvm_io, which generates an temporary
alias mapping using uvm_map_extract. With xonly now operational, this
alias mapping is created with minprot, which for text will be xonly, and
the kernel cannot read it (unless the architecture has implied read for
exec from kernel pov).
Pass UVM_IO_FIXPROT to create the alias with maxprot instead.
ok kettenis


# 1.92 02-Jan-2023 guenther

Add tfind_user(), for getting a proc* given a user-space TID and
the process* that it should be part of. Use that in clock_get{time,res}(),
thrkill(), and ptrace().

ok jca@ miod@ mpi@ mvs@


# 1.91 21-Dec-2022 claudio

The location of the aux info vector is now cached in ps_auxinfo of struct
process. Use this information to access the vector.
OK mpi@ mbuhl@ deraadt@


# 1.90 05-Dec-2022 deraadt

zap a pile of dangling tabs


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.89 07-Dec-2021 guenther

Continue to delete emulation support: since we're Just ELF, the size
of the auxinfo is fixed: provide ELF_AUX_WORDS in <sys/exec_elf.h>
as a replacement for emul->e_arglen

ok millert@


Revision tags: OPENBSD_7_0_BASE
# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.92 02-Jan-2023 guenther

Add tfind_user(), for getting a proc* given a user-space TID and
the process* that it should be part of. Use that in clock_get{time,res}(),
thrkill(), and ptrace().

ok jca@ miod@ mpi@ mvs@


# 1.91 21-Dec-2022 claudio

The location of the aux info vector is now cached in ps_auxinfo of struct
process. Use this information to access the vector.
OK mpi@ mbuhl@ deraadt@


# 1.90 05-Dec-2022 deraadt

zap a pile of dangling tabs


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.89 07-Dec-2021 guenther

Continue to delete emulation support: since we're Just ELF, the size
of the auxinfo is fixed: provide ELF_AUX_WORDS in <sys/exec_elf.h>
as a replacement for emul->e_arglen

ok millert@


Revision tags: OPENBSD_7_0_BASE
# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.91 21-Dec-2022 claudio

The location of the aux info vector is now cached in ps_auxinfo of struct
process. Use this information to access the vector.
OK mpi@ mbuhl@ deraadt@


# 1.90 05-Dec-2022 deraadt

zap a pile of dangling tabs


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.89 07-Dec-2021 guenther

Continue to delete emulation support: since we're Just ELF, the size
of the auxinfo is fixed: provide ELF_AUX_WORDS in <sys/exec_elf.h>
as a replacement for emul->e_arglen

ok millert@


Revision tags: OPENBSD_7_0_BASE
# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.90 05-Dec-2022 deraadt

zap a pile of dangling tabs


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.89 07-Dec-2021 guenther

Continue to delete emulation support: since we're Just ELF, the size
of the auxinfo is fixed: provide ELF_AUX_WORDS in <sys/exec_elf.h>
as a replacement for emul->e_arglen

ok millert@


Revision tags: OPENBSD_7_0_BASE
# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.89 07-Dec-2021 guenther

Continue to delete emulation support: since we're Just ELF, the size
of the auxinfo is fixed: provide ELF_AUX_WORDS in <sys/exec_elf.h>
as a replacement for emul->e_arglen

ok millert@


Revision tags: OPENBSD_7_0_BASE
# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.88 10-May-2021 mpi

Revert previous, it introduced a regression with breakpoints in gdb.


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.87 06-May-2021 mpi

Refactor routines to stop/unstop processes and save the corresponding signal.

- Move the "hack" involving P_SINTR to avoid grabbing the SCHED_LOCK()
recursively closer to where it is necessary, in proc_stop()

- Introduce proc_unstop(), the symmetric routine to proc_stop(), which
manipulates `ps_xsig' and use it whenever a SSTOPed thread needs to be
awaken.

- Manipulate `ps_xsig' only in proc_stop/unstop()

ok kettenis@


Revision tags: OPENBSD_6_9_BASE
# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.86 08-Feb-2021 mpi

Revert the convertion of per-process thread into a SMR_TAILQ.

We did not reach a consensus about using SMR to unlock single_thread_set()
so there's no point in keeping this change.


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.85 07-Dec-2020 mpi

Convert the per-process thread list into a SMR_TAILQ.

Currently all iterations are done under KERNEL_LOCK() and therefor use
the *_LOCKED() variant.

From and ok claudio@


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.84 19-Oct-2020 mpi

Serialize accesses to "struct vmspace" and document its refcounting.

The underlying vm_space lock is used as a substitute to the KERNEL_LOCK()
in uvm_grow() to make sure `vm_ssize' is not corrupted.

ok anton@, kettenis@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.83 16-Mar-2020 mpi

Keep track of traced child under a list of orphans while they are being
reparented to a debugger process.

Also re-parent exiting traced processes to their original parent, if it
is still alive, after the debugger has seen the exit status.

Logic comes from FreeBSD pointed out by guenther@.

While here rename proc_reparent() into process_reparent() and get rid of
superfluous checks.

ok visa@


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.82 11-Dec-2019 guenther

Replace p_xstat with ps_xexit and ps_xsig
Convert those to a consolidated status when needed in wait4(), kevent(),
and sysctl()
Pass exit code and signal separately to exit1()
(This also serves as prep for adding waitid(2))

ok mpi@


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.81 29-Nov-2019 mpi

Return EBUSY for successive PT_TRACE_ME calls.

Match FreeBSD and NetBSD.

ok bluhm@, deraadt@, kettenis@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.80 19-Feb-2018 mpi

Change some returns into gotos, will help keeping the unlocking path
simpler. No functional change.

Extracted from a larger diff from guenther@, ok kettenis@


# 1.79 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.78 14-Oct-2017 guenther

Split sys_ptrace() by request type:
- control operations: trace_me, attach, detach, step, kill, continue.
Manipulate process relation/state or send a signal
- kernel-state get/set: thread list, event mask, trace state.
About the process and don't require target to be stopped, need copyin/out
- user-state get/set: memory, register, window cookie.
Often thread-specific, require target to be stopped, need copyin/out

sys_ptrace() changes to handle request checking, copyin/out to
kernel buffers with size check and zeroing, and dispatching to the
routines above for the real work. This simplfies the permission checks
and copyin/out handling and will simplify lock handling in the future.

Inspired in part by FreeBSD.
ok mpi@ visa@


Revision tags: OPENBSD_6_2_BASE
# 1.77 19-Jul-2017 deraadt

Uninitialized variable can leak kernel memory.
Found by Ilja Van Sprundel
ok kettenis


Revision tags: OPENBSD_6_1_BASE
# 1.76 25-Jan-2017 guenther

branches: 1.76.4;
deSCARGize sys_ptrace()

ok mpi@


# 1.75 24-Jan-2017 mpi

Rename pfind(9) into tfind(9) to reflect that it deals with threads.

While here document prfind(9.

with and ok guenther@


# 1.74 07-Nov-2016 guenther

Split PID from TID, giving processes a PID unrelated to the TID of their
initial thread

ok jsing@ kettenis@


# 1.73 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.72 19-Oct-2016 guenther

Change pmap_proc_iflush() to take a process instead of a proc
powerpc: rename second argument of pmap_proc_iflush() to match other archs

ok kettenis@


# 1.71 09-Oct-2016 guenther

With systrace and procfs gone, process_checkioperm() and process_domem()
are for option PTRACE only

ok kettenis@


# 1.70 01-Sep-2016 akfaew

Get rid of 'relebad:'.

OK natano@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.69 31-May-2016 jca

branches: 1.69.2;
Fix ptrace PT_WRITE_D that returned EFAULT.

Broken in r.1.33. After discussion from kettenis@, don't attempt to
make PT_WRITE_D and PT_WRITE_I equivalent again.

From Mathieu (naabed at poolp dot org)


Revision tags: OPENBSD_5_9_BASE
# 1.68 24-Sep-2015 tedu

buglet: there's no way for req to be STEP in the DETACH case.
also fix the confusing comment. ok guethner.


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.67 20-Jan-2015 kettenis

Move ps_strings "after" the random stackgap. This makes its location a
per-process value, and therefpore turns the VM_PSSTRINGS sysctl into a
per-process one as well. This gets rid of a pointer to the bottom of the
stack at a fixed location. Also clears the road for unmapping the stackgap.

ok deraadt@


# 1.66 12-Dec-2014 tedu

sysctl kern.global_ptrace.
controls whether you can ptrace any process with appropriate privileges
or only one own's children.
ok deraadt


# 1.65 08-Sep-2014 guenther

Delete procfs; it's always had races and is now unused: no one noticed for
months that I broke it before the 5.5 release.

confirmed as not being required by ports by sthen@, ajacoutot@, dcoppa@


Revision tags: OPENBSD_5_6_BASE
# 1.64 13-Jul-2014 tedu

pass the size to free in some of the obvious cases


# 1.63 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.62 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.61 04-May-2014 guenther

Add PS_SYSTEM, the process-level mirror of the thread-level P_SYSTEM,
and FORK_SYSTEM as a flag to set them. This eliminates needing to
peek into other processes threads in various places. Inspired by NetBSD

ok miod@ matthew@


# 1.60 30-Mar-2014 guenther

Eliminates struct pcred by moving the real and saved ugids into
struct ucred; struct process then directly links to the ucred

Based on a discussion at c2k10 or so before noting that FreeBSD and
NetBSD did this too.

ok matthew@


# 1.59 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_5_BASE
# 1.58 21-Jan-2014 tedu

bzero -> memset


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.57 13-Apr-2012 kettenis

For now, direct the kill signal sent by PT_KILL to the thread that made us
stop, just like we do for PT_CONTINUE/PT_STEP. The current code isn't
ready for directing signals to other threads yet.


# 1.56 13-Apr-2012 kettenis

First stab at making ptrace(2) usable for debugging multi-threaded programs.
It implements a full-stop model where all threads are stopped before handing
over control to the debugger. Events are reported as before through wait(2);
you will have to call ptrace(PT_GET_PROCESS_STATE, ...) to find out which
thread hit the event. Since this changes the size of struct ptrace_state,
you will have to recompile gdb.

ok guenther@


# 1.55 12-Apr-2012 kettenis

If the "main" thread exits it stays around but unlinks itself from the
threads list. Calling TAILQ_NEXT on them is a bad idea and will panic
the kernel. So check the P_WEXIT flag and pretend the thread doesn't
exist if it is set. Also make PT_GET_THREAD_FIRST return the first
thread on the threads list instead of the "main" thread, such that you
can actually keep enumerating the threads in this case.

ok guenther@, miod@


# 1.54 12-Apr-2012 kettenis

PT_GETXMMREGS and PT_SETXMMREGS can take a TID.


# 1.53 11-Apr-2012 kettenis

Move the P_WAITED flag from struct proc to struct process.

ok guenther@


# 1.52 06-Apr-2012 kettenis

Implement PT_GET_THREAD_FIRS and PT_GET_THREAD_NEXT.

ok miod@


# 1.51 10-Mar-2012 guenther

Add PS_EXITING to better differentiate between the process exiting and
the main thread exiting. c.f. regress/sys/kern/main-thread-exited/


# 1.50 25-Feb-2012 miod

Allow this to build on platforms lacking PT_GETFPREGS or PT_SETFPREGS; broken
in previous rev.


# 1.49 20-Feb-2012 guenther

First steps for making ptrace work with rthreads:
- move the P_TRACED and P_INEXEC flags, and p_oppid, p_ptmask, and
p_ptstat member from struct proc to struct process
- sort the PT_* requests into those that take a PID vs those that
can also take a TID
- stub in PT_GET_THREAD_FIRST and PT_GET_THREAD_NEXT

ok kettenis@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.48 02-Apr-2011 guenther

Move P_SUGID and P_SUGIDEXEC from struct proc to struct process, so
that you can't evade the checks by doing the dirty work in an rthread

ok blambert@, deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.47 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


# 1.46 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.45 02-May-2010 kettenis

Use intermediate vaddr_t cast when casting a pointer to off_t. Prevents
gcc4 from complaining about casting a pointer to an integer type of different
size.

ok guenther@, jsg@


Revision tags: OPENBSD_4_7_BASE
# 1.44 28-Jan-2010 guenther

Make sure the process tree is is loop-free by forbidding ptrace()
of a direct ancestor, closing a localhost DoS. As an exception,
do permit ptrace() of pid 1 and have inferiors() stop climbing if
it hits that.

ok tedu@ hpux_compat suggestion from miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.43 31-Oct-2008 deraadt

branches: 1.43.2; 1.43.6;
Do not assume that a pointer to another process will live over a set of
sleeping calls. Since we are simply operating on another process'
vmspace, grab a (refcounted) copy of that pointer and use that instead.
Similar to the bug just fixed in sysctl_proc_args.
discussed with art


# 1.42 31-Oct-2008 deraadt

accidental commit ... backout


# 1.41 31-Oct-2008 deraadt

kern_sysctl.c


# 1.40 16-Sep-2008 kettenis

Add PIOD_READ_AUXV, a way to get the ELF auxilliary vector through ptrace(2).

ok miod@


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.39 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


# 1.38 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.37 29-Nov-2006 miod

Kernel stack can be swapped. This means that stuff that's on the stack
should never be referenced outside the context of the process to which
this stack belongs unless we do the PHOLD/PRELE dance. Loads of code
doesn't follow the rules here. Instead of trying to track down all
offenders and fix this hairy situation, it makes much more sense
to not swap kernel stacks.

From art@, tested by many some time ago.


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jul-2006 grunk

make kernels w/o PTRACE compile again.

help from mickey@, "commit it" miod@


# 1.35 18-May-2006 miod

paramter -> parameter


Revision tags: OPENBSD_3_9_BASE
# 1.34 13-Dec-2005 jsg

ansi/deregister. No binary change.


# 1.33 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.32 14-Sep-2005 kettenis

ptrace(2) following fork(2)
ok miod@


Revision tags: OPENBSD_3_8_BASE
# 1.31 02-Aug-2005 kettenis

Reduce stack usage.


# 1.30 16-Apr-2005 kettenis

Remove regs and fpregs pseudo-files from procfs.
ok deraadt@, miod@


# 1.29 03-Apr-2005 kettenis

Implemente PT_[GS]ETXMMREGS ptrace(2) requests on i386.
ok deraadt@


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.28 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.27 08-Feb-2004 deraadt

restored & repaired wcookie support; kettenis@chello.nl


Revision tags: OPENBSD_3_4_BASE
# 1.26 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.25 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.24 09-Mar-2003 millert

Disallow ptrace if P_SUGIDEXEC flag is set (we already disallow if P_SUGID
is set). deraadt@ and tholo@ OK.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.23 27-Jun-2002 deraadt

KNF


Revision tags: OPENBSD_3_1_BASE
# 1.22 10-Apr-2002 fgsch

don't attach to system processes.
art@ niklas@ markus@ millert@ deraadt@ ok.


# 1.21 12-Mar-2002 art

Change the PT_IO interfaces like discussed on
the bsd-api-discuss list.


# 1.20 12-Mar-2002 art

In the PT_STEP case, first set the new pc, then arrange
for the single-step. This can slightly break the error handling when
setting the sstep fails, but allows us to emulate single stepping in
software on arch that don't have support for that in hardware.


# 1.19 11-Mar-2002 art

Since all archs implement PT_GETREGS and PT_SETREGS, make then unoptional.
They still stay in MD code for backwards compatibility, but a check in
ptrace.h checks if they are defined.

Note - the same thing will be done with PT_{GET,SET}FPREGS once vax implements
them and with PT_STEP when it's implemented by sparc, sparc64 and alpha.


# 1.18 11-Mar-2002 art

Add a more sane API for reading/writing traced process memory
with ptrace - PT_IO.
Man page update in a few.


# 1.17 30-Jan-2002 nordin

Move SET/CLR/ISSET macros to param.h. fgsch@ and millert@ ok


# 1.16 20-Jan-2002 art

When a process is exec:ing mark it with a flag. Check that flag in ptrace
and procfs (and possibly more places in the future) and simply refuse to
fiddle with the execing process. This is an ugly hack, but this far we
haven't been successful in creating a race-free exec.


# 1.15 02-Jan-2002 art

register_t is not an int, so don't use it that way.
Fixes PT_{READ,WRITE}_{I,D} on sparc64.


Revision tags: UBC_BASE
# 1.14 06-Nov-2001 miod

branches: 1.14.2;
Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.13 27-Jun-2001 art

branches: 1.13.2;
remove old vm


# 1.12 18-Jun-2001 art

trace_req is not used anymore (was a nop).


# 1.11 18-Jun-2001 deraadt

split PT_DETACH handling, so that pc cannot be set by it; art ok


Revision tags: OPENBSD_2_9_BASE
# 1.10 09-Apr-2001 tholo

branches: 1.10.2;
Add emulation of Linux features to procfs; mostly from NetBSD. ok deraadt@


# 1.9 10-Nov-2000 provos

seperate -> separate, okay aaron@


Revision tags: OPENBSD_2_8_BASE
# 1.8 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE SMP_BASE kame_19991208
# 1.7 26-Feb-1999 art

branches: 1.7.6;
kmem allocation changes for uvm


Revision tags: OPENBSD_2_4_BASE
# 1.6 27-Jun-1998 deraadt

securelevels do NOT protect running binaries; only filesystem activity


# 1.5 09-Jun-1998 deraadt

do not permit ptrace attach to immutable executable


Revision tags: OPENBSD_2_0_BASE OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.4 29-Jul-1996 deraadt

comment on ptrace & P_SUGID


# 1.3 22-May-1996 deraadt

fix obscure problem involving ptrace of init


# 1.2 03-Mar-1996 niklas

From NetBSD: 960217 merge


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision