Skip to main content

starnix_core/ptrace/
ptrace.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::arch::execution::new_syscall_from_state;
6use crate::mm::{IOVecPtr, MemoryAccessor, MemoryAccessorExt};
7use crate::ptrace::StopState;
8use crate::security;
9use crate::signals::syscalls::WaitingOptions;
10use crate::signals::{
11    SignalDetail, SignalInfo, UncheckedSignalInfo, send_signal_first, send_standard_signal,
12};
13use crate::task::{
14    CurrentTask, PidTable, ProcessSelector, Task, TaskMutableState, ThreadGroup, ThreadState,
15    WaitQueue, ZombieProcess,
16};
17use bitflags::bitflags;
18use starnix_logging::track_stub;
19use starnix_registers::HeapRegs;
20use starnix_sync::{LockBefore, Locked, MmDumpable, ThreadGroupLimits, Unlocked};
21use starnix_syscalls::SyscallResult;
22use starnix_syscalls::decls::SyscallDecl;
23use starnix_types::ownership::{OwnedRef, Releasable, ReleaseGuard};
24use starnix_uapi::auth::PTRACE_MODE_ATTACH_REALCREDS;
25use starnix_uapi::elf::ElfNoteType;
26use starnix_uapi::errors::Errno;
27use starnix_uapi::signals::{SIGKILL, SIGSTOP, SIGTRAP, SigSet, Signal, UncheckedSignal};
28#[allow(unused_imports)]
29use starnix_uapi::user_address::ArchSpecific;
30use starnix_uapi::user_address::{LongPtr, MultiArchUserRef, UserAddress, UserRef};
31use starnix_uapi::{
32    PTRACE_CONT, PTRACE_DETACH, PTRACE_EVENT_CLONE, PTRACE_EVENT_EXEC, PTRACE_EVENT_EXIT,
33    PTRACE_EVENT_FORK, PTRACE_EVENT_SECCOMP, PTRACE_EVENT_STOP, PTRACE_EVENT_VFORK,
34    PTRACE_EVENT_VFORK_DONE, PTRACE_GET_SYSCALL_INFO, PTRACE_GETEVENTMSG, PTRACE_GETREGSET,
35    PTRACE_GETSIGINFO, PTRACE_GETSIGMASK, PTRACE_INTERRUPT, PTRACE_KILL, PTRACE_LISTEN,
36    PTRACE_O_EXITKILL, PTRACE_O_TRACECLONE, PTRACE_O_TRACEEXEC, PTRACE_O_TRACEEXIT,
37    PTRACE_O_TRACEFORK, PTRACE_O_TRACESYSGOOD, PTRACE_O_TRACEVFORK, PTRACE_O_TRACEVFORKDONE,
38    PTRACE_PEEKDATA, PTRACE_PEEKTEXT, PTRACE_PEEKUSR, PTRACE_POKEDATA, PTRACE_POKETEXT,
39    PTRACE_POKEUSR, PTRACE_SETOPTIONS, PTRACE_SETREGSET, PTRACE_SETSIGINFO, PTRACE_SETSIGMASK,
40    PTRACE_SYSCALL, PTRACE_SYSCALL_INFO_ENTRY, PTRACE_SYSCALL_INFO_EXIT, PTRACE_SYSCALL_INFO_NONE,
41    clone_args, errno, error, pid_t, ptrace_syscall_info, tid_t, uapi,
42};
43use zerocopy::IntoBytes;
44
45use std::collections::BTreeMap;
46use std::sync::atomic::Ordering;
47use std::sync::{Arc, Weak};
48
49#[cfg(target_arch = "x86_64")]
50use starnix_uapi::{PTRACE_GETREGS, user};
51
52#[cfg(all(target_arch = "aarch64"))]
53use starnix_uapi::arch32::PTRACE_GETREGS;
54
55type UserRegsStructPtr =
56    MultiArchUserRef<starnix_uapi::user_regs_struct, starnix_uapi::arch32::user_regs_struct>;
57
58uapi::check_arch_independent_layout! {
59    ptrace_syscall_info {
60        op,
61        arch,
62        instruction_pointer,
63        stack_pointer,
64        __bindgen_anon_1,
65    }
66
67    ptrace_syscall_info__bindgen_ty_1 {
68        entry,
69        exit,
70        seccomp,
71    }
72
73    ptrace_syscall_info__bindgen_ty_1__bindgen_ty_1 {
74        nr,
75        args,
76    }
77
78    ptrace_syscall_info__bindgen_ty_1__bindgen_ty_2 {
79        rval,
80        is_error,
81    }
82
83    ptrace_syscall_info__bindgen_ty_1__bindgen_ty_3 {
84        nr,
85        args,
86        ret_data,
87    }
88}
89
90/// For most of the time, for the purposes of ptrace, a tracee is either "going"
91/// or "stopped".  However, after certain ptrace calls, there are special rules
92/// to be followed.
93#[derive(Clone, Default, PartialEq)]
94pub enum PtraceStatus {
95    /// Proceed as otherwise indicated by the task's stop status.
96    #[default]
97    Default,
98    /// Resuming after a ptrace_cont with a signal, so do not stop for signal-delivery-stop
99    Continuing,
100    /// "The state of the tracee after PTRACE_LISTEN is somewhat of a
101    /// gray area: it is not in any ptrace-stop (ptrace commands won't work on it,
102    /// and it will deliver waitpid(2) notifications), but it also may be considered
103    /// "stopped" because it is not executing instructions (is not scheduled), and
104    /// if it was in group-stop before PTRACE_LISTEN, it will not respond to signals
105    /// until SIGCONT is received."
106    Listening,
107}
108
109impl PtraceStatus {
110    pub fn is_continuing(&self) -> bool {
111        *self == PtraceStatus::Continuing
112    }
113}
114
115/// Indicates the way that ptrace attached to the task.
116#[derive(Copy, Clone, PartialEq)]
117pub enum PtraceAttachType {
118    /// Attached with PTRACE_ATTACH
119    Attach,
120    /// Attached with PTRACE_SEIZE
121    Seize,
122}
123
124bitflags! {
125    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
126    #[repr(transparent)]
127    pub struct PtraceOptions: u32 {
128        const EXITKILL = starnix_uapi::PTRACE_O_EXITKILL;
129        const TRACECLONE = starnix_uapi::PTRACE_O_TRACECLONE;
130        const TRACEEXEC = starnix_uapi::PTRACE_O_TRACEEXEC;
131        const TRACEEXIT = starnix_uapi::PTRACE_O_TRACEEXIT;
132        const TRACEFORK = starnix_uapi::PTRACE_O_TRACEFORK;
133        const TRACESYSGOOD = starnix_uapi::PTRACE_O_TRACESYSGOOD;
134        const TRACEVFORK = starnix_uapi::PTRACE_O_TRACEVFORK;
135        const TRACEVFORKDONE = starnix_uapi::PTRACE_O_TRACEVFORKDONE;
136        const TRACESECCOMP = starnix_uapi::PTRACE_O_TRACESECCOMP;
137        const SUSPEND_SECCOMP = starnix_uapi::PTRACE_O_SUSPEND_SECCOMP;
138    }
139}
140
141#[repr(u32)]
142#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
143pub enum PtraceEvent {
144    #[default]
145    None = 0,
146    Stop = PTRACE_EVENT_STOP,
147    Clone = PTRACE_EVENT_CLONE,
148    Fork = PTRACE_EVENT_FORK,
149    Vfork = PTRACE_EVENT_VFORK,
150    VforkDone = PTRACE_EVENT_VFORK_DONE,
151    Exec = PTRACE_EVENT_EXEC,
152    Exit = PTRACE_EVENT_EXIT,
153    Seccomp = PTRACE_EVENT_SECCOMP,
154}
155
156impl PtraceEvent {
157    pub fn from_option(option: &PtraceOptions) -> Self {
158        match *option {
159            PtraceOptions::TRACECLONE => PtraceEvent::Clone,
160            PtraceOptions::TRACEFORK => PtraceEvent::Fork,
161            PtraceOptions::TRACEVFORK => PtraceEvent::Vfork,
162            PtraceOptions::TRACEVFORKDONE => PtraceEvent::VforkDone,
163            PtraceOptions::TRACEEXEC => PtraceEvent::Exec,
164            PtraceOptions::TRACEEXIT => PtraceEvent::Exit,
165            PtraceOptions::TRACESECCOMP => PtraceEvent::Seccomp,
166            _ => unreachable!("Bad ptrace event specified"),
167        }
168    }
169}
170
171/// Information about what caused a ptrace-event-stop.
172pub struct PtraceEventData {
173    /// The event that caused the task to stop (e.g., PTRACE_EVENT_TRACEFORK or PTRACE_EVENT_EXIT).
174    pub event: PtraceEvent,
175
176    /// The message associated with the event (e.g., tid, exit status)..
177    pub msg: u64,
178}
179
180impl PtraceEventData {
181    pub fn new(option: PtraceOptions, msg: u64) -> Self {
182        Self { event: PtraceEvent::from_option(&option), msg }
183    }
184    pub fn new_from_event(event: PtraceEvent, msg: u64) -> Self {
185        Self { event, msg }
186    }
187}
188
189/// The ptrace state that a new task needs to connect to the same tracer as the
190/// task that clones it.
191#[derive(Clone)]
192pub struct PtraceCoreState {
193    /// The pid of the tracer
194    pub pid: pid_t,
195
196    /// The thread group of the tracer
197    pub thread_group: Weak<ThreadGroup>,
198
199    /// Whether the attach was a seize or an attach.  There are a few subtle
200    /// differences in behavior of the different attach types - see ptrace(2).
201    pub attach_type: PtraceAttachType,
202
203    /// The options set by PTRACE_SETOPTIONS
204    pub options: PtraceOptions,
205
206    /// The tracer waits on this WaitQueue to find out if the tracee has done
207    /// something worth being notified about.
208    pub tracer_waiters: Arc<WaitQueue>,
209}
210
211impl PtraceCoreState {
212    pub fn has_option(&self, option: PtraceOptions) -> bool {
213        self.options.contains(option)
214    }
215}
216
217/// Per-task ptrace-related state
218pub struct PtraceState {
219    /// The core state of the tracer, which can be shared between processes
220    pub core_state: PtraceCoreState,
221
222    /// The tracee waits on this WaitQueue to find out when it should stop or wake
223    /// for ptrace-related shenanigans.
224    pub tracee_waiters: WaitQueue,
225
226    /// The signal that caused the task to enter the given state (for
227    /// signal-delivery-stop)
228    pub last_signal: Option<SignalInfo>,
229
230    /// Whether waitpid() will return the last signal.  The presence of last_signal
231    /// can't be used for that, because that needs to be saved for GETSIGINFO.
232    pub last_signal_waitable: bool,
233
234    /// Data about the PTRACE_EVENT that caused the most recent stop (if any).
235    pub event_data: Option<PtraceEventData>,
236
237    /// Indicates whether the last ptrace call put this thread into a state with
238    /// special semantics for stopping behavior.
239    pub stop_status: PtraceStatus,
240
241    /// For SYSCALL_INFO_EXIT
242    pub last_syscall_was_error: bool,
243}
244
245impl PtraceState {
246    pub fn new(
247        pid: pid_t,
248        thread_group: Weak<ThreadGroup>,
249        attach_type: PtraceAttachType,
250        options: PtraceOptions,
251    ) -> Box<Self> {
252        Box::new(PtraceState {
253            core_state: PtraceCoreState {
254                pid,
255                thread_group,
256                attach_type,
257                options,
258                tracer_waiters: Arc::new(WaitQueue::default()),
259            },
260            tracee_waiters: WaitQueue::default(),
261            last_signal: None,
262            last_signal_waitable: false,
263            event_data: None,
264            stop_status: PtraceStatus::default(),
265            last_syscall_was_error: false,
266        })
267    }
268
269    pub fn get_pid(&self) -> pid_t {
270        self.core_state.pid
271    }
272
273    pub fn set_pid(&mut self, pid: pid_t) {
274        self.core_state.pid = pid;
275    }
276
277    pub fn is_seized(&self) -> bool {
278        self.core_state.attach_type == PtraceAttachType::Seize
279    }
280
281    pub fn get_attach_type(&self) -> PtraceAttachType {
282        self.core_state.attach_type
283    }
284
285    pub fn is_waitable(&self, stop: StopState, options: &WaitingOptions) -> bool {
286        if self.stop_status == PtraceStatus::Listening {
287            // Waiting for any change of state
288            return self.last_signal_waitable;
289        }
290        if !options.wait_for_continued && !stop.is_stopping_or_stopped() {
291            // Only waiting for stops, but is not stopped.
292            return false;
293        }
294        self.last_signal_waitable && !stop.is_in_progress()
295    }
296
297    pub fn set_last_signal(&mut self, mut signal: Option<SignalInfo>) {
298        if let Some(ref mut siginfo) = signal {
299            // We don't want waiters to think the process was unstopped because
300            // of a sigkill. They will get woken when the process dies.
301            if siginfo.signal == SIGKILL {
302                return;
303            }
304            self.last_signal_waitable = true;
305            self.last_signal = signal;
306        }
307    }
308
309    pub fn set_last_event(&mut self, event: Option<PtraceEventData>) {
310        if event.is_some() {
311            self.event_data = event;
312        }
313    }
314
315    pub fn get_last_signal_ref(&self) -> Option<&SignalInfo> {
316        self.last_signal.as_ref()
317    }
318
319    // Gets the last signal, and optionally clears the wait state of the ptrace.
320    pub fn get_last_signal(&mut self, keep_signal_waitable: bool) -> Option<SignalInfo> {
321        self.last_signal_waitable = keep_signal_waitable;
322        self.last_signal.clone()
323    }
324
325    pub fn has_option(&self, option: PtraceOptions) -> bool {
326        self.core_state.has_option(option)
327    }
328
329    pub fn set_options_from_bits(&mut self, option: u32) -> Result<(), Errno> {
330        if let Some(options) = PtraceOptions::from_bits(option) {
331            self.core_state.options = options;
332            Ok(())
333        } else {
334            error!(EINVAL)
335        }
336    }
337
338    pub fn get_options(&self) -> PtraceOptions {
339        self.core_state.options
340    }
341
342    /// Returns enough of the ptrace state to propagate it to a fork / clone / vforked task.
343    pub fn get_core_state(&self) -> PtraceCoreState {
344        self.core_state.clone()
345    }
346
347    pub fn tracer_waiters(&self) -> &Arc<WaitQueue> {
348        &self.core_state.tracer_waiters
349    }
350
351    /// Returns an (i32, ptrace_syscall_info) pair.  The ptrace_syscall_info is
352    /// the info associated with the syscall that the target task is currently
353    /// blocked on, The i32 is (per ptrace(2)) "the number of bytes available to
354    /// be written by the kernel.  If the size of the data to be written by the
355    /// kernel exceeds the size specified by the addr argument, the output data
356    /// is truncated."; ptrace(PTRACE_GET_SYSCALL_INFO) returns that value"
357    pub fn get_target_syscall(
358        &self,
359        target: &Task,
360        state: &TaskMutableState,
361    ) -> Result<(i32, ptrace_syscall_info), Errno> {
362        #[cfg(target_arch = "x86_64")]
363        let arch = starnix_uapi::AUDIT_ARCH_X86_64;
364        #[cfg(target_arch = "aarch64")]
365        let arch = starnix_uapi::AUDIT_ARCH_AARCH64;
366        #[cfg(target_arch = "riscv64")]
367        let arch = starnix_uapi::AUDIT_ARCH_RISCV64;
368
369        let mut info = ptrace_syscall_info { arch, ..Default::default() };
370        let mut info_len = memoffset::offset_of!(ptrace_syscall_info, __bindgen_anon_1);
371
372        match &state.captured_thread_state {
373            Some(captured) => {
374                let registers = captured.thread_state.registers.clone();
375                info.instruction_pointer = registers.instruction_pointer_register();
376                info.stack_pointer = registers.stack_pointer_register();
377                #[cfg(target_arch = "aarch64")]
378                if captured.thread_state.is_arch32() {
379                    // If any additional arch32 archs are added, just use a cfg
380                    // macro here.
381                    info.arch = starnix_uapi::AUDIT_ARCH_ARM;
382                }
383                match target.load_stopped() {
384                    StopState::SyscallEnterStopped => {
385                        let syscall_decl = SyscallDecl::from_number(
386                            registers.syscall_register(),
387                            captured.thread_state.arch_width(),
388                        );
389                        let syscall = new_syscall_from_state(syscall_decl, &captured.thread_state);
390                        info.op = PTRACE_SYSCALL_INFO_ENTRY as u8;
391                        let entry = linux_uapi::ptrace_syscall_info__bindgen_ty_1__bindgen_ty_1 {
392                            nr: syscall.decl.number,
393                            args: [
394                                syscall.arg0.raw(),
395                                syscall.arg1.raw(),
396                                syscall.arg2.raw(),
397                                syscall.arg3.raw(),
398                                syscall.arg4.raw(),
399                                syscall.arg5.raw(),
400                            ],
401                        };
402                        info_len += memoffset::offset_of!(
403                            linux_uapi::ptrace_syscall_info__bindgen_ty_1__bindgen_ty_1,
404                            args
405                        ) + std::mem::size_of_val(&entry.args);
406                        info.__bindgen_anon_1.entry = entry;
407                    }
408                    StopState::SyscallExitStopped => {
409                        info.op = PTRACE_SYSCALL_INFO_EXIT as u8;
410                        let exit = linux_uapi::ptrace_syscall_info__bindgen_ty_1__bindgen_ty_2 {
411                            rval: registers.return_register() as i64,
412                            is_error: state
413                                .ptrace
414                                .as_ref()
415                                .map_or(0, |ptrace| ptrace.last_syscall_was_error as u8),
416                            ..Default::default()
417                        };
418                        info_len += memoffset::offset_of!(
419                            linux_uapi::ptrace_syscall_info__bindgen_ty_1__bindgen_ty_2,
420                            is_error
421                        ) + std::mem::size_of_val(&exit.is_error);
422                        info.__bindgen_anon_1.exit = exit;
423                    }
424                    _ => {
425                        info.op = PTRACE_SYSCALL_INFO_NONE as u8;
426                    }
427                };
428            }
429            _ => (),
430        }
431        Ok((info_len as i32, info))
432    }
433
434    /// Gets the core state for this ptrace if the options set on this ptrace
435    /// match |trace_kind|.  Returns a pair: the trace option you *should* use
436    /// (sometimes this is different from the one that the caller thinks it
437    /// should use), and the core state.
438    pub fn get_core_state_for_clone(
439        &self,
440        clone_args: &clone_args,
441    ) -> (PtraceOptions, Option<PtraceCoreState>) {
442        // ptrace(2): If the tracee calls clone(2) with the CLONE_VFORK flag,
443        // PTRACE_EVENT_VFORK will be delivered instead if PTRACE_O_TRACEVFORK
444        // is set, otherwise if the tracee calls clone(2) with the exit signal
445        // set to SIGCHLD, PTRACE_EVENT_FORK will be delivered if
446        // PTRACE_O_TRACEFORK is set.
447        let trace_type = if clone_args.flags & (starnix_uapi::CLONE_UNTRACED as u64) != 0 {
448            PtraceOptions::empty()
449        } else {
450            if clone_args.flags & (starnix_uapi::CLONE_VFORK as u64) != 0 {
451                PtraceOptions::TRACEVFORK
452            } else if clone_args.exit_signal != (starnix_uapi::SIGCHLD as u64) {
453                PtraceOptions::TRACECLONE
454            } else {
455                PtraceOptions::TRACEFORK
456            }
457        };
458
459        if !self.has_option(trace_type)
460            && (clone_args.flags & (starnix_uapi::CLONE_PTRACE as u64) == 0)
461        {
462            return (PtraceOptions::empty(), None);
463        }
464
465        (trace_type, Some(self.get_core_state()))
466    }
467}
468
469/// A zombie that must delivered to a tracer process for a traced process.
470struct TracedZombie {
471    /// An artificial zombie that must be delivered to the tracer program.
472    artificial_zombie: ZombieProcess,
473
474    /// An optional real zombie to be sent to the given ThreadGroup after the zomboe has been
475    /// delivered to the tracer.
476    delegate: Option<(Weak<ThreadGroup>, OwnedRef<ZombieProcess>)>,
477}
478
479impl Releasable for TracedZombie {
480    type Context<'a> = &'a mut PidTable;
481
482    fn release<'a>(self, pids: &'a mut PidTable) {
483        self.artificial_zombie.release(pids);
484        if let Some((_, z)) = self.delegate {
485            z.release(pids);
486        }
487    }
488}
489
490impl TracedZombie {
491    fn new(artificial_zombie: ZombieProcess) -> ReleaseGuard<Self> {
492        ReleaseGuard::from(Self { artificial_zombie, delegate: None })
493    }
494
495    fn new_with_delegate(
496        artificial_zombie: ZombieProcess,
497        delegate: (Weak<ThreadGroup>, OwnedRef<ZombieProcess>),
498    ) -> ReleaseGuard<Self> {
499        ReleaseGuard::from(Self { artificial_zombie, delegate: Some(delegate) })
500    }
501
502    fn set_parent(
503        &mut self,
504        new_zombie: Option<OwnedRef<ZombieProcess>>,
505        new_parent: &ThreadGroup,
506    ) {
507        if let Some(new_zombie) = new_zombie {
508            self.delegate = Some((new_parent.weak_self.clone(), new_zombie));
509        } else {
510            self.delegate = self.delegate.take().map(|(_, z)| (new_parent.weak_self.clone(), z));
511        }
512    }
513}
514
515/// A list of zombie processes that were traced by a given tracer, but which
516/// have not yet notified that tracer of their exit.  Once the tracer is
517/// notified, the original parent will be notified.
518#[derive(Default)]
519pub struct ZombiePtracees {
520    /// A list of zombies that have to be delivered to the ptracer.  The key is
521    /// the tid of the traced process.
522    zombies: BTreeMap<tid_t, ReleaseGuard<TracedZombie>>,
523}
524
525impl ZombiePtracees {
526    pub fn new() -> Self {
527        Self::default()
528    }
529
530    /// Adds a zombie tracee to the list, but does not provide a parent task to
531    /// notify when the tracer is done.
532    pub fn add(&mut self, pids: &mut PidTable, tid: tid_t, zombie: ZombieProcess) {
533        if let std::collections::btree_map::Entry::Vacant(entry) = self.zombies.entry(tid) {
534            entry.insert(TracedZombie::new(zombie));
535        } else {
536            zombie.release(pids);
537        }
538    }
539
540    /// Delete any zombie ptracees for the given tid.
541    pub fn remove(&mut self, pids: &mut PidTable, tid: tid_t) {
542        self.zombies.remove(&tid).release(pids);
543    }
544
545    pub fn is_empty(&self) -> bool {
546        self.zombies.is_empty()
547    }
548
549    /// Provide a parent task and a zombie to notify when the tracer has been
550    /// notified.
551    pub fn set_parent_of(
552        &mut self,
553        tracee: tid_t,
554        new_zombie: Option<OwnedRef<ZombieProcess>>,
555        new_parent: &ThreadGroup,
556    ) {
557        match self.zombies.entry(tracee) {
558            std::collections::btree_map::Entry::Vacant(entry) => {
559                if let Some(new_zombie) = new_zombie {
560                    entry.insert(TracedZombie::new_with_delegate(
561                        new_zombie.as_artificial(),
562                        (new_parent.weak_self.clone(), new_zombie),
563                    ));
564                }
565            }
566            std::collections::btree_map::Entry::Occupied(mut entry) => {
567                entry.get_mut().set_parent(new_zombie, new_parent);
568            }
569        }
570    }
571
572    /// When a parent dies without having been notified, replace it with a given
573    /// new parent.
574    pub fn reparent(old_parent: &ThreadGroup, new_parent: &ThreadGroup) {
575        let mut lockless_list = old_parent.read().deferred_zombie_ptracers.clone();
576
577        for deferred_zombie_ptracer in &lockless_list {
578            if let Some(tg) = deferred_zombie_ptracer.tracer_thread_group_key.upgrade() {
579                tg.write().zombie_ptracees.set_parent_of(
580                    deferred_zombie_ptracer.tracee_tid,
581                    None,
582                    new_parent,
583                );
584            }
585        }
586        let mut new_state = new_parent.write();
587        new_state.deferred_zombie_ptracers.append(&mut lockless_list);
588    }
589
590    /// Empty the table and notify all of the remaining parents.  Used if the
591    /// tracer terminates or detaches without acknowledging all pending tracees.
592    pub fn release(&mut self, pids: &mut PidTable) {
593        let mut entry = self.zombies.pop_first();
594        while let Some((_, mut zombie)) = entry {
595            if let Some((tg, z)) = zombie.delegate.take() {
596                if let Some(tg) = tg.upgrade() {
597                    tg.do_zombie_notifications(z);
598                }
599            }
600            zombie.release(pids);
601
602            entry = self.zombies.pop_first();
603        }
604    }
605
606    /// Returns true iff there is a zombie waiting to be delivered to the tracers matching the
607    /// given selector.
608    pub fn has_zombie_matching(&self, selector: &ProcessSelector) -> bool {
609        self.zombies.values().any(|z| z.artificial_zombie.matches_selector(selector))
610    }
611
612    /// Returns true iff the given `tid` is a traced thread that needs to deliver a zombie to the
613    /// tracer.
614    pub fn has_tracee(&self, tid: tid_t) -> bool {
615        self.zombies.contains_key(&tid)
616    }
617
618    /// Returns a zombie matching the given selector and options, and
619    /// (optionally) a thread group to notify after the caller has consumed that
620    /// zombie.
621    pub fn get_waitable_entry(
622        &mut self,
623        selector: &ProcessSelector,
624        options: &WaitingOptions,
625    ) -> Option<(ZombieProcess, Option<(Weak<ThreadGroup>, OwnedRef<ZombieProcess>)>)> {
626        // We look for the last zombie in the vector that matches pid
627        // selector and waiting options
628        let Some((t, found_zombie)) = self
629            .zombies
630            .iter()
631            .map(|(t, z)| (*t, &z.artificial_zombie))
632            .rfind(|(_, zombie)| zombie.matches_selector_and_waiting_option(selector, options))
633        else {
634            return None;
635        };
636
637        let result;
638        if !options.keep_waitable_state {
639            // Maybe notify child waiters.
640            result = self.zombies.remove(&t).map(|traced_zombie| {
641                let traced_zombie = ReleaseGuard::take(traced_zombie);
642                (traced_zombie.artificial_zombie, traced_zombie.delegate)
643            });
644        } else {
645            result = Some((found_zombie.as_artificial(), None));
646        }
647
648        result
649    }
650}
651
652// PR_SET_PTRACER_ANY is defined as ((unsigned long) -1),
653// which is not understood by bindgen.
654pub const PR_SET_PTRACER_ANY: i32 = -1;
655
656/// Indicates processes specifically allowed to trace a given process if using
657/// SCOPE_RESTRICTED.  Used by prctl(PR_SET_PTRACER).
658#[derive(Copy, Clone, Default, PartialEq)]
659pub enum PtraceAllowedPtracers {
660    #[default]
661    None,
662    Some(pid_t),
663    Any,
664}
665
666/// Continues the target thread, optionally detaching from it.
667/// |data| is treated as it is in PTRACE_CONT.
668/// |new_status| is the PtraceStatus to set for this trace.
669/// |detach| will cause the tracer to detach from the tracee.
670fn ptrace_cont<L>(
671    locked: &mut Locked<L>,
672    tracee: &Task,
673    data: &UserAddress,
674    detach: bool,
675) -> Result<(), Errno>
676where
677    L: LockBefore<ThreadGroupLimits>,
678{
679    let data = data.ptr() as u64;
680    let new_state;
681    let mut siginfo = if data != 0 {
682        let signal = Signal::try_from(UncheckedSignal::new(data))?;
683        Some(SignalInfo::kernel(signal))
684    } else {
685        None
686    };
687
688    let mut state = tracee.write();
689    let is_listen = state.is_ptrace_listening();
690
691    if tracee.load_stopped().is_waking_or_awake() && !is_listen {
692        if detach {
693            state.set_ptrace(None)?;
694        }
695        return error!(EIO);
696    }
697
698    if !state.can_accept_ptrace_commands() && !detach {
699        return error!(ESRCH);
700    }
701
702    if let Some(ptrace) = &mut state.ptrace {
703        if data != 0 {
704            new_state = PtraceStatus::Continuing;
705            if let Some(last_signal) = &mut ptrace.last_signal {
706                if let Some(si) = siginfo {
707                    let new_signal = si.signal;
708                    last_signal.signal = new_signal;
709                }
710                siginfo = Some(last_signal.clone());
711            }
712        } else {
713            new_state = PtraceStatus::Default;
714            ptrace.last_signal = None;
715            ptrace.event_data = None;
716        }
717        ptrace.stop_status = new_state;
718
719        if is_listen {
720            state.notify_ptracees();
721        }
722    }
723
724    if let Some(siginfo) = siginfo {
725        // This will wake up the task for us, and also release state
726        send_signal_first(locked, &tracee, state, siginfo);
727    } else {
728        state.set_stopped(StopState::Waking, None, None, None);
729        drop(state);
730        tracee.thread_group().set_stopped(StopState::Waking, None, false);
731    }
732    if detach {
733        tracee.write().set_ptrace(None)?;
734    }
735    Ok(())
736}
737
738fn ptrace_interrupt(tracee: &Task) -> Result<(), Errno> {
739    let mut state = tracee.write();
740    if let Some(ptrace) = &mut state.ptrace {
741        if !ptrace.is_seized() {
742            return error!(EIO);
743        }
744        let status = ptrace.stop_status.clone();
745        ptrace.stop_status = PtraceStatus::Default;
746        let event_data = Some(PtraceEventData::new_from_event(PtraceEvent::Stop, 0));
747        if status == PtraceStatus::Listening {
748            let signal = ptrace.last_signal.clone();
749            // "If the tracee was already stopped by a signal and PTRACE_LISTEN
750            // was sent to it, the tracee stops with PTRACE_EVENT_STOP and
751            // WSTOPSIG(status) returns the stop signal"
752            state.set_stopped(StopState::PtraceEventStopped, signal, None, event_data);
753        } else {
754            state.set_stopped(
755                StopState::PtraceEventStopping,
756                Some(SignalInfo::kernel(SIGTRAP)),
757                None,
758                event_data,
759            );
760            drop(state);
761            tracee.interrupt();
762        }
763    }
764    Ok(())
765}
766
767fn ptrace_listen(tracee: &Task) -> Result<(), Errno> {
768    let mut state = tracee.write();
769    if let Some(ptrace) = &mut state.ptrace {
770        if !ptrace.is_seized()
771            || (ptrace.last_signal_waitable
772                && ptrace
773                    .event_data
774                    .as_ref()
775                    .is_some_and(|event_data| event_data.event != PtraceEvent::Stop))
776        {
777            return error!(EIO);
778        }
779        ptrace.stop_status = PtraceStatus::Listening;
780    }
781    Ok(())
782}
783
784pub fn ptrace_detach<L>(
785    locked: &mut Locked<L>,
786    pids: &mut PidTable,
787    thread_group: &ThreadGroup,
788    tracee: &Task,
789    data: &UserAddress,
790) -> Result<(), Errno>
791where
792    L: LockBefore<ThreadGroupLimits>,
793{
794    if let Err(x) = ptrace_cont(locked, &tracee, &data, true) {
795        return Err(x);
796    }
797    let tid = tracee.get_tid();
798    thread_group.ptracees.lock().remove(&tid);
799    thread_group.write().zombie_ptracees.remove(pids, tid);
800    Ok(())
801}
802
803/// For all ptrace requests that require an attached tracee
804
805pub fn ptrace_dispatch<L>(
806    locked: &mut Locked<L>,
807    current_task: &mut CurrentTask,
808    request: u32,
809    pid: pid_t,
810    addr: UserAddress,
811    data: UserAddress,
812) -> Result<SyscallResult, Errno>
813where
814    L: LockBefore<ThreadGroupLimits>,
815{
816    let mut pids = current_task.kernel().pids.write();
817    let tracee = pids.get_task(pid)?;
818
819    if let Some(ptrace) = &tracee.read().ptrace {
820        if ptrace.get_pid() != current_task.get_pid() {
821            return error!(ESRCH);
822        }
823    }
824
825    // These requests may be run without the thread in a stop state, or
826    // check the stop state themselves.
827    match request {
828        PTRACE_KILL => {
829            let siginfo = SignalInfo::with_detail(
830                SIGKILL,
831                (SIGTRAP.number() | PTRACE_KILL << 8) as i32,
832                SignalDetail::None,
833            );
834            send_standard_signal(locked, &tracee, siginfo);
835            return Ok(starnix_syscalls::SUCCESS);
836        }
837        PTRACE_INTERRUPT => {
838            ptrace_interrupt(tracee.as_ref())?;
839            return Ok(starnix_syscalls::SUCCESS);
840        }
841        PTRACE_LISTEN => {
842            ptrace_listen(&tracee)?;
843            return Ok(starnix_syscalls::SUCCESS);
844        }
845        PTRACE_CONT => {
846            ptrace_cont(locked, &tracee, &data, false)?;
847            return Ok(starnix_syscalls::SUCCESS);
848        }
849        PTRACE_SYSCALL => {
850            tracee.trace_syscalls.store(true, std::sync::atomic::Ordering::Relaxed);
851            ptrace_cont(locked, &tracee, &data, false)?;
852            return Ok(starnix_syscalls::SUCCESS);
853        }
854        PTRACE_DETACH => {
855            ptrace_detach(locked, &mut pids, current_task.thread_group(), tracee.as_ref(), &data)?;
856            return Ok(starnix_syscalls::SUCCESS);
857        }
858        _ => {}
859    }
860
861    // The remaining requests (to be added) require the thread to be stopped.
862    let mut state = tracee.write();
863    if !state.can_accept_ptrace_commands() {
864        return error!(ESRCH);
865    }
866
867    match request {
868        PTRACE_PEEKDATA | PTRACE_PEEKTEXT => {
869            let Some(captured) = &mut state.captured_thread_state else {
870                return error!(ESRCH);
871            };
872
873            // NB: The behavior of the syscall is different from the behavior in ptrace(2),
874            // which is provided by libc.
875            let src = LongPtr::new(captured.as_ref(), addr);
876            let val = tracee.read_multi_arch_object(src)?;
877
878            let dst = LongPtr::new(&src, data);
879            current_task.write_multi_arch_object(dst, val)?;
880            Ok(starnix_syscalls::SUCCESS)
881        }
882        PTRACE_POKEDATA | PTRACE_POKETEXT => {
883            let Some(captured) = &mut state.captured_thread_state else {
884                return error!(ESRCH);
885            };
886
887            let bytes = if captured.is_arch32() {
888                u32::try_from(data.ptr()).map_err(|_| errno!(EINVAL))?.to_ne_bytes().to_vec()
889            } else {
890                data.ptr().to_ne_bytes().to_vec()
891            };
892
893            tracee.mm()?.force_write_memory(addr, &bytes)?;
894
895            Ok(starnix_syscalls::SUCCESS)
896        }
897        PTRACE_PEEKUSR => {
898            let Some(captured) = &mut state.captured_thread_state else {
899                return error!(ESRCH);
900            };
901
902            let dst = LongPtr::new(captured.as_ref(), data);
903            let val = ptrace_peekuser(&mut captured.thread_state, addr.ptr() as usize)?;
904            current_task.write_multi_arch_object(dst, val as u64)?;
905            return Ok(starnix_syscalls::SUCCESS);
906        }
907        PTRACE_POKEUSR => {
908            ptrace_pokeuser(&mut *state, data.ptr() as usize, addr.ptr() as usize)?;
909            return Ok(starnix_syscalls::SUCCESS);
910        }
911        PTRACE_GETREGSET => {
912            if let Some(ref mut captured) = state.captured_thread_state {
913                let uiv = IOVecPtr::new(current_task, data);
914                let mut iv = current_task.read_multi_arch_object(uiv)?;
915                let base = iv.iov_base.addr;
916                let mut len = iv.iov_len as usize;
917                ptrace_getregset(
918                    current_task,
919                    &captured.thread_state,
920                    ElfNoteType::try_from(addr.ptr() as usize)?,
921                    base,
922                    &mut len,
923                )?;
924                iv.iov_len = len as u64;
925                current_task.write_multi_arch_object(uiv, iv)?;
926                return Ok(starnix_syscalls::SUCCESS);
927            }
928            error!(ESRCH)
929        }
930        PTRACE_SETREGSET => {
931            if let Some(ref mut captured) = state.captured_thread_state {
932                captured.dirty = true;
933                let uiv = IOVecPtr::new(current_task, data);
934                let iv = current_task.read_multi_arch_object(uiv)?;
935                let base = iv.iov_base.addr;
936                let len = iv.iov_len as usize;
937                ptrace_setregset(
938                    current_task,
939                    &mut captured.thread_state,
940                    ElfNoteType::try_from(addr.ptr() as usize)?,
941                    base,
942                    len,
943                )?;
944                return Ok(starnix_syscalls::SUCCESS);
945            }
946            error!(ESRCH)
947        }
948        #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
949        PTRACE_GETREGS => {
950            if let Some(captured) = &mut state.captured_thread_state {
951                let mut len = usize::MAX;
952                ptrace_getregset(
953                    current_task,
954                    &captured.thread_state,
955                    ElfNoteType::PrStatus,
956                    data.ptr() as u64,
957                    &mut len,
958                )?;
959                return Ok(starnix_syscalls::SUCCESS);
960            }
961            error!(ESRCH)
962        }
963        PTRACE_SETSIGMASK => {
964            // addr is the size of the buffer pointed to
965            // by data, but has to be sizeof(sigset_t).
966            if addr.ptr() != std::mem::size_of::<SigSet>() {
967                return error!(EINVAL);
968            }
969            // sigset comes from *data.
970            let src: UserRef<SigSet> = UserRef::from(data);
971            let val = current_task.read_object(src)?;
972            state.set_signal_mask(val);
973
974            Ok(starnix_syscalls::SUCCESS)
975        }
976        PTRACE_GETSIGMASK => {
977            // addr is the size of the buffer pointed to
978            // by data, but has to be sizeof(sigset_t).
979            if addr.ptr() != std::mem::size_of::<SigSet>() {
980                return error!(EINVAL);
981            }
982            // sigset goes in *data.
983            let dst: UserRef<SigSet> = UserRef::from(data);
984            let val = state.signal_mask();
985            current_task.write_object(dst, &val)?;
986            Ok(starnix_syscalls::SUCCESS)
987        }
988        PTRACE_GETSIGINFO => {
989            if let Some(ptrace) = &state.ptrace {
990                if let Some(signal) = ptrace.last_signal.as_ref() {
991                    let dst = MultiArchUserRef::<uapi::siginfo_t, uapi::arch32::siginfo_t>::new(
992                        current_task,
993                        data,
994                    );
995                    signal.write(current_task, dst)?;
996                } else {
997                    return error!(EINVAL);
998                }
999            }
1000            Ok(starnix_syscalls::SUCCESS)
1001        }
1002        PTRACE_SETSIGINFO => {
1003            let siginfo = UncheckedSignalInfo::read_from_siginfo(current_task, data)?.try_into()?;
1004            if let Some(ptrace) = &mut state.ptrace {
1005                ptrace.last_signal = Some(siginfo);
1006            }
1007            Ok(starnix_syscalls::SUCCESS)
1008        }
1009        PTRACE_GET_SYSCALL_INFO => {
1010            if let Some(ptrace) = &state.ptrace {
1011                let (size, info) = ptrace.get_target_syscall(&tracee, &state)?;
1012                let dst: UserRef<ptrace_syscall_info> = UserRef::from(data);
1013                let len = std::cmp::min(std::mem::size_of::<ptrace_syscall_info>(), addr.ptr());
1014                // SAFETY: ptrace_syscall_info does not implement FromBytes/IntoBytes,
1015                // so this has to happen manually.
1016                let src = unsafe {
1017                    std::slice::from_raw_parts(
1018                        &info as *const ptrace_syscall_info as *const u8,
1019                        len as usize,
1020                    )
1021                };
1022                current_task.write_memory(dst.addr(), src)?;
1023                Ok(size.into())
1024            } else {
1025                error!(ESRCH)
1026            }
1027        }
1028        PTRACE_SETOPTIONS => {
1029            let mask = data.ptr() as u32;
1030            // This is what we currently support.
1031            if mask != 0
1032                && (mask
1033                    & !(PTRACE_O_TRACESYSGOOD
1034                        | PTRACE_O_TRACECLONE
1035                        | PTRACE_O_TRACEFORK
1036                        | PTRACE_O_TRACEVFORK
1037                        | PTRACE_O_TRACEVFORKDONE
1038                        | PTRACE_O_TRACEEXEC
1039                        | PTRACE_O_TRACEEXIT
1040                        | PTRACE_O_EXITKILL)
1041                    != 0)
1042            {
1043                track_stub!(TODO("https://fxbug.dev/322874463"), "ptrace(PTRACE_SETOPTIONS)", mask);
1044                return error!(ENOSYS);
1045            }
1046            if let Some(ptrace) = &mut state.ptrace {
1047                ptrace.set_options_from_bits(mask)?;
1048            }
1049            Ok(starnix_syscalls::SUCCESS)
1050        }
1051        PTRACE_GETEVENTMSG => {
1052            if let Some(ptrace) = &state.ptrace {
1053                if let Some(event_data) = &ptrace.event_data {
1054                    let dst = LongPtr::new(current_task, data);
1055                    current_task.write_multi_arch_object(dst, event_data.msg)?;
1056                    return Ok(starnix_syscalls::SUCCESS);
1057                }
1058            }
1059            error!(EIO)
1060        }
1061        _ => {
1062            track_stub!(TODO("https://fxbug.dev/322874463"), "ptrace", request);
1063            error!(ENOSYS)
1064        }
1065    }
1066}
1067
1068/// Makes the given thread group trace the given task.
1069fn do_attach(
1070    thread_group: &ThreadGroup,
1071    task: &Arc<Task>,
1072    attach_type: PtraceAttachType,
1073    options: PtraceOptions,
1074) -> Result<(), Errno> {
1075    thread_group.ptracees.lock().insert(task.get_tid(), task.into());
1076
1077    let process_state = &mut task.thread_group().write();
1078    let mut state = task.write();
1079    state.set_ptrace(Some(PtraceState::new(
1080        thread_group.leader,
1081        thread_group.weak_self.clone(),
1082        attach_type,
1083        options,
1084    )))?;
1085
1086    // If the tracee is already stopped, make sure that the tracer can
1087    // identify that right away.
1088    if process_state.is_waitable()
1089        && process_state.base.load_stopped() == StopState::GroupStopped
1090        && task.load_stopped() == StopState::GroupStopped
1091    {
1092        if let Some(ptrace) = &mut state.ptrace {
1093            ptrace.last_signal_waitable = true;
1094        }
1095    }
1096
1097    Ok(())
1098}
1099
1100/// Uses the given core ptrace state (including tracer, attach type, etc) to
1101/// attach to another task, given by `tracee_task`.  Also sends a signal to stop
1102/// tracee_task.  Typical for when inheriting ptrace state from another task.
1103pub fn ptrace_attach_from_state<L>(
1104    locked: &mut Locked<L>,
1105    tracee_task: &Arc<Task>,
1106    ptrace_state: PtraceCoreState,
1107) -> Result<(), Errno>
1108where
1109    L: LockBefore<ThreadGroupLimits>,
1110{
1111    {
1112        let tracer_tg =
1113            tracee_task.thread_group().kernel.pids.read().get_thread_group(ptrace_state.pid);
1114        let tracer_tg = tracer_tg.ok_or_else(|| errno!(ESRCH))?;
1115        do_attach(&tracer_tg, tracee_task, ptrace_state.attach_type, ptrace_state.options)?;
1116    }
1117    let mut state = tracee_task.write();
1118    if let Some(ptrace) = &mut state.ptrace {
1119        ptrace.core_state.tracer_waiters = Arc::clone(&ptrace_state.tracer_waiters);
1120    }
1121
1122    // The newly started tracee starts with a signal that depends on the attach type.
1123    let signal = if ptrace_state.attach_type == PtraceAttachType::Seize {
1124        if let Some(ptrace) = &mut state.ptrace {
1125            ptrace.set_last_event(Some(PtraceEventData::new_from_event(PtraceEvent::Stop, 0)));
1126        }
1127        // Ptrace-emitted SIGTRAP signal cannot be blocked.
1128        SignalInfo::forced(SIGTRAP)
1129    } else {
1130        // Note, SIGSTOP can never be blocked, but we use `forced` anyway to be consistent.
1131        SignalInfo::forced(SIGSTOP)
1132    };
1133    send_signal_first(locked, tracee_task, state, signal);
1134
1135    Ok(())
1136}
1137
1138pub fn ptrace_traceme(current_task: &mut CurrentTask) -> Result<SyscallResult, Errno> {
1139    let parent = current_task.thread_group().read().parent.clone();
1140    if let Some(parent) = parent {
1141        let parent = parent.upgrade();
1142        // TODO: Move this check into `do_attach()` so that there is a single `ptrace_access_check(tracer, tracee)`?
1143        {
1144            let pids = current_task.kernel().pids.read();
1145            let parent_task = pids.get_task(parent.leader).map_err(|_| errno!(EINVAL))?;
1146            security::ptrace_traceme(current_task, &parent_task)?;
1147        }
1148
1149        do_attach(&parent, &current_task.task, PtraceAttachType::Attach, PtraceOptions::empty())?;
1150        Ok(starnix_syscalls::SUCCESS)
1151    } else {
1152        error!(EPERM)
1153    }
1154}
1155
1156pub fn ptrace_attach<L>(
1157    locked: &mut Locked<L>,
1158    current_task: &mut CurrentTask,
1159    pid: pid_t,
1160    attach_type: PtraceAttachType,
1161    data: UserAddress,
1162) -> Result<SyscallResult, Errno>
1163where
1164    L: LockBefore<MmDumpable>,
1165{
1166    let tracee = current_task.kernel().pids.read().get_task(pid)?;
1167
1168    if tracee.thread_group == current_task.thread_group {
1169        return error!(EPERM);
1170    }
1171
1172    current_task.check_ptrace_access_mode(locked, PTRACE_MODE_ATTACH_REALCREDS, &tracee)?;
1173    do_attach(current_task.thread_group(), &tracee, attach_type, PtraceOptions::empty())?;
1174    if attach_type == PtraceAttachType::Attach {
1175        send_standard_signal(
1176            locked.cast_locked::<MmDumpable>(),
1177            &tracee,
1178            SignalInfo::kernel(SIGSTOP),
1179        );
1180    } else if attach_type == PtraceAttachType::Seize {
1181        // When seizing, |data| should be used as the options bitmask.
1182        let mut state = tracee.write();
1183        if let Some(ptrace) = &mut state.ptrace {
1184            ptrace.set_options_from_bits(data.ptr() as u32)?;
1185        }
1186    }
1187    Ok(starnix_syscalls::SUCCESS)
1188}
1189
1190/// Implementation of ptrace(PTRACE_PEEKUSER).  The user struct holds the
1191/// registers and other information about the process.  See ptrace(2) and
1192/// sys/user.h for full details.
1193pub fn ptrace_peekuser(
1194    thread_state: &mut ThreadState<HeapRegs>,
1195    offset: usize,
1196) -> Result<usize, Errno> {
1197    #[cfg(any(target_arch = "x86_64"))]
1198    if offset >= std::mem::size_of::<user>() {
1199        return error!(EIO);
1200    }
1201    if offset < UserRegsStructPtr::size_of_object_for(thread_state) {
1202        let result = thread_state.get_user_register(offset)?;
1203        return Ok(result);
1204    }
1205    error!(EIO)
1206}
1207
1208pub fn ptrace_pokeuser(
1209    state: &mut TaskMutableState,
1210    value: usize,
1211    offset: usize,
1212) -> Result<(), Errno> {
1213    if let Some(ref mut thread_state) = state.captured_thread_state {
1214        thread_state.dirty = true;
1215
1216        #[cfg(any(target_arch = "x86_64"))]
1217        if offset >= std::mem::size_of::<user>() {
1218            return error!(EIO);
1219        }
1220        if offset < UserRegsStructPtr::size_of_object_for(thread_state.as_ref()) {
1221            return thread_state.thread_state.set_user_register(offset, value);
1222        }
1223    }
1224    error!(EIO)
1225}
1226
1227pub fn ptrace_getregset(
1228    current_task: &CurrentTask,
1229    thread_state: &ThreadState<HeapRegs>,
1230    regset_type: ElfNoteType,
1231    base: u64,
1232    len: &mut usize,
1233) -> Result<(), Errno> {
1234    match regset_type {
1235        ElfNoteType::PrStatus => {
1236            let user_regs_struct_len = UserRegsStructPtr::size_of_object_for(thread_state);
1237            *len = std::cmp::min(*len, user_regs_struct_len);
1238
1239            if thread_state.is_arch32() {
1240                let regs = thread_state.registers.to_user_regs_struct_arch32();
1241                current_task.write_memory(UserAddress::from(base), &regs.as_bytes()[..*len])?;
1242            } else {
1243                let regs = thread_state.registers.to_user_regs_struct();
1244                current_task.write_memory(UserAddress::from(base), &regs.as_bytes()[..*len])?;
1245            }
1246            Ok(())
1247        }
1248        _ => {
1249            error!(EINVAL)
1250        }
1251    }
1252}
1253
1254pub fn ptrace_setregset(
1255    current_task: &CurrentTask,
1256    thread_state: &mut ThreadState<HeapRegs>,
1257    regset_type: ElfNoteType,
1258    base: u64,
1259    len: usize,
1260) -> Result<(), Errno> {
1261    match regset_type {
1262        ElfNoteType::PrStatus => {
1263            let user_regs_struct_len = UserRegsStructPtr::size_of_object_for(thread_state);
1264            if len < user_regs_struct_len {
1265                return error!(EINVAL);
1266            }
1267
1268            if thread_state.is_arch32() {
1269                let mut regs = starnix_uapi::arch32::user_regs_struct::default();
1270                current_task.read_memory_to_slice(UserAddress::from(base), regs.as_mut_bytes())?;
1271                thread_state.registers.from_user_regs_struct_arch32(&regs);
1272            } else {
1273                let mut regs = starnix_uapi::user_regs_struct::default();
1274                current_task.read_memory_to_slice(UserAddress::from(base), regs.as_mut_bytes())?;
1275                thread_state.registers.from_user_regs_struct(&regs);
1276            }
1277            Ok(())
1278        }
1279        _ => error!(EINVAL),
1280    }
1281}
1282
1283#[inline(never)]
1284pub fn ptrace_syscall_enter(locked: &mut Locked<Unlocked>, current_task: &mut CurrentTask) {
1285    let block = {
1286        let mut state = current_task.write();
1287        if state.ptrace.is_some() {
1288            current_task.trace_syscalls.store(false, Ordering::Relaxed);
1289            let mut sig = SignalInfo::with_detail(
1290                SIGTRAP,
1291                (linux_uapi::SIGTRAP | 0x80) as i32,
1292                SignalDetail::None,
1293            );
1294            if state
1295                .ptrace
1296                .as_ref()
1297                .is_some_and(|ptrace| ptrace.has_option(PtraceOptions::TRACESYSGOOD))
1298            {
1299                sig.signal.set_ptrace_syscall_bit();
1300            }
1301            state.set_stopped(StopState::SyscallEnterStopping, Some(sig), None, None);
1302            true
1303        } else {
1304            false
1305        }
1306    };
1307    if block {
1308        current_task.block_while_stopped(locked);
1309    }
1310}
1311
1312#[inline(never)]
1313pub fn ptrace_syscall_exit(
1314    locked: &mut Locked<Unlocked>,
1315    current_task: &mut CurrentTask,
1316    is_error: bool,
1317) {
1318    let block = {
1319        let mut state = current_task.write();
1320        current_task.trace_syscalls.store(false, Ordering::Relaxed);
1321        if state.ptrace.is_some() {
1322            let mut sig = SignalInfo::with_detail(
1323                SIGTRAP,
1324                (linux_uapi::SIGTRAP | 0x80) as i32,
1325                SignalDetail::None,
1326            );
1327            if state
1328                .ptrace
1329                .as_ref()
1330                .is_some_and(|ptrace| ptrace.has_option(PtraceOptions::TRACESYSGOOD))
1331            {
1332                sig.signal.set_ptrace_syscall_bit();
1333            }
1334
1335            state.set_stopped(StopState::SyscallExitStopping, Some(sig), None, None);
1336            if let Some(ptrace) = &mut state.ptrace {
1337                ptrace.last_syscall_was_error = is_error;
1338            }
1339            true
1340        } else {
1341            false
1342        }
1343    };
1344    if block {
1345        current_task.block_while_stopped(locked);
1346    }
1347}
1348
1349#[cfg(test)]
1350mod tests {
1351    use super::*;
1352    use crate::task::syscalls::sys_prctl;
1353    use crate::testing::{create_task, spawn_kernel_and_run};
1354    use starnix_uapi::PR_SET_PTRACER;
1355    use starnix_uapi::auth::CAP_SYS_PTRACE;
1356
1357    #[::fuchsia::test]
1358    async fn test_set_ptracer() {
1359        spawn_kernel_and_run(async |locked, current_task| {
1360            let kernel = current_task.kernel().clone();
1361            let mut tracee = create_task(locked, &kernel, "tracee");
1362            let mut tracer = create_task(locked, &kernel, "tracer");
1363
1364            let mut creds = tracer.real_creds().clone();
1365            creds.cap_effective &= !CAP_SYS_PTRACE;
1366            tracer.set_creds(creds);
1367
1368            kernel.ptrace_scope.store(security::yama::SCOPE_RESTRICTED, Ordering::Relaxed);
1369            assert_eq!(
1370                sys_prctl(locked, &mut tracee, PR_SET_PTRACER, 0xFFF, 0, 0, 0),
1371                error!(EINVAL)
1372            );
1373
1374            assert_eq!(
1375                ptrace_attach(
1376                    locked,
1377                    &mut tracer,
1378                    tracee.as_ref().task.tid,
1379                    PtraceAttachType::Attach,
1380                    UserAddress::NULL,
1381                ),
1382                error!(EPERM)
1383            );
1384
1385            assert!(
1386                sys_prctl(
1387                    locked,
1388                    &mut tracee,
1389                    PR_SET_PTRACER,
1390                    tracer.thread_group().leader as u64,
1391                    0,
1392                    0,
1393                    0
1394                )
1395                .is_ok()
1396            );
1397
1398            let mut not_tracer = create_task(locked, &kernel, "not-tracer");
1399            not_tracer.set_creds(tracer.real_creds().clone());
1400            assert_eq!(
1401                ptrace_attach(
1402                    locked,
1403                    &mut not_tracer,
1404                    tracee.as_ref().task.tid,
1405                    PtraceAttachType::Attach,
1406                    UserAddress::NULL,
1407                ),
1408                error!(EPERM)
1409            );
1410
1411            assert!(
1412                ptrace_attach(
1413                    locked,
1414                    &mut tracer,
1415                    tracee.as_ref().task.tid,
1416                    PtraceAttachType::Attach,
1417                    UserAddress::NULL,
1418                )
1419                .is_ok()
1420            );
1421        })
1422        .await;
1423    }
1424
1425    #[::fuchsia::test]
1426    async fn test_set_ptracer_any() {
1427        spawn_kernel_and_run(async |locked, current_task| {
1428            let kernel = current_task.kernel().clone();
1429            let mut tracee = create_task(locked, &kernel, "tracee");
1430            let mut tracer = create_task(locked, &kernel, "tracer");
1431
1432            let mut creds = tracer.real_creds().clone();
1433            creds.cap_effective &= !CAP_SYS_PTRACE;
1434            tracer.set_creds(creds);
1435
1436            kernel.ptrace_scope.store(security::yama::SCOPE_RESTRICTED, Ordering::Relaxed);
1437            assert_eq!(
1438                sys_prctl(locked, &mut tracee, PR_SET_PTRACER, 0xFFF, 0, 0, 0),
1439                error!(EINVAL)
1440            );
1441
1442            assert_eq!(
1443                ptrace_attach(
1444                    locked,
1445                    &mut tracer,
1446                    tracee.as_ref().task.tid,
1447                    PtraceAttachType::Attach,
1448                    UserAddress::NULL,
1449                ),
1450                error!(EPERM)
1451            );
1452
1453            assert!(
1454                sys_prctl(locked, &mut tracee, PR_SET_PTRACER, PR_SET_PTRACER_ANY as u64, 0, 0, 0)
1455                    .is_ok()
1456            );
1457
1458            assert!(
1459                ptrace_attach(
1460                    locked,
1461                    &mut tracer,
1462                    tracee.as_ref().task.tid,
1463                    PtraceAttachType::Attach,
1464                    UserAddress::NULL,
1465                )
1466                .is_ok()
1467            );
1468        })
1469        .await;
1470    }
1471}