#include <machine/reg.h>
#include <machine/psl.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/errno.h>
#include <sys/ptrace.h>
#include <sys/uio.h>
#include <sys/user.h>
#include <sys/sysctl.h>
#include <sys/wait.h>
#include <sys/mount_internal.h>
#include <sys/sysproto.h>
#include <bsm/audit_kernel.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <mach/machine/thread_status.h>
#define SET(t, f) (t) |= (f)
#define CLR(t, f) (t) &= ~(f)
#define ISSET(t, f) ((t) & (f))
extern thread_t port_name_to_thread(mach_port_name_t port_name);
extern kern_return_t thread_getstatus(thread_t thread, int flavor, thread_state_t tstate, mach_msg_type_number_t *count);
extern thread_t get_firstthread(task_t);
#if defined (ppc)
extern kern_return_t thread_setstatus(thread_t thread, int flavor, thread_state_t tstate, mach_msg_type_number_t count);
#endif
int
ptrace(p, uap, retval)
struct proc *p;
struct ptrace_args *uap;
register_t *retval;
{
struct proc *t = current_proc();
task_t task;
thread_t th_act;
struct uthread *ut;
int *locr0;
#if defined(ppc)
struct ppc_thread_state64 statep;
#elif defined(i386)
struct i386_saved_state statep;
#else
#error architecture not supported
#endif
unsigned long state_count;
int tr_sigexc = 0;
AUDIT_ARG(cmd, uap->req);
AUDIT_ARG(pid, uap->pid);
AUDIT_ARG(addr, uap->addr);
AUDIT_ARG(value, uap->data);
if (uap->req == PT_DENY_ATTACH) {
if (ISSET(p->p_flag, P_TRACED)) {
exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
thread_funnel_set(kernel_flock, FALSE);
thread_exception_return();
}
SET(p->p_flag, P_NOATTACH);
return(0);
}
if (uap->req == PT_FORCEQUOTA) {
if (is_suser()) {
SET(t->p_flag, P_FORCEQUOTA);
return (0);
} else
return (EPERM);
}
if (uap->req == PT_TRACE_ME) {
SET(p->p_flag, P_TRACED);
t->p_oppid = t->p_pptr->p_pid;
return(0);
}
if (uap->req == PT_SIGEXC) {
if (ISSET(p->p_flag, P_TRACED)) {
SET(p->p_flag, P_SIGEXC);
return(0);
} else
return(EINVAL);
}
if ((t = pfind(uap->pid)) == NULL)
return (ESRCH);
AUDIT_ARG(process, t);
if (uap->pid <=2 )
return (EPERM);
task = t->task;
if (uap->req == PT_ATTACHEXC) {
uap->req = PT_ATTACH;
tr_sigexc = 1;
}
if (uap->req == PT_ATTACH) {
int err;
if ( kauth_authorize_process(proc_ucred(p), KAUTH_PROCESS_CANTRACE,
t, (uintptr_t)&err, 0, 0) == 0 ) {
SET(t->p_flag, P_TRACED);
if (tr_sigexc)
SET(t->p_flag, P_SIGEXC);
t->p_oppid = t->p_pptr->p_pid;
if (t->p_pptr != p)
proc_reparent(t, p);
if (get_task_userstop(task) == 0 ) {
t->p_xstat = 0;
psignal(t, SIGSTOP);
} else {
t->p_xstat = SIGSTOP;
task_resume(task);
}
return(0);
}
else {
if (ISSET(t->p_flag, P_NOATTACH)) {
psignal(p, SIGSEGV);
}
return (err);
}
}
if (!ISSET(t->p_flag, P_TRACED))
return (EPERM);
if (t->p_pptr != p)
return (EBUSY);
if (t->p_stat != SSTOP)
return (EBUSY);
switch (uap->req) {
case PT_DETACH:
if (t->p_oppid != t->p_pptr->p_pid) {
struct proc *pp;
pp = pfind(t->p_oppid);
proc_reparent(t, pp ? pp : initproc);
}
t->p_oppid = 0;
CLR(t->p_flag, P_TRACED);
CLR(t->p_flag, P_SIGEXC);
goto resume;
case PT_KILL:
psignal_lock(t, SIGKILL, 0);
goto resume;
case PT_STEP:
case PT_CONTINUE:
th_act = (thread_t)get_firstthread(task);
if (th_act == THREAD_NULL)
goto errorLabel;
ut = (uthread_t)get_bsdthread_info(th_act);
locr0 = ut->uu_ar0;
#if defined(i386)
state_count = i386_NEW_THREAD_STATE_COUNT;
if (thread_getstatus(th_act, i386_NEW_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
goto errorLabel;
}
#elif defined(ppc)
state_count = PPC_THREAD_STATE64_COUNT;
if (thread_getstatus(th_act, PPC_THREAD_STATE64, (thread_state_t)&statep, (mach_msg_type_number_t *)&state_count) != KERN_SUCCESS) {
goto errorLabel;
}
#else
#error architecture not supported
#endif
if (uap->addr != (user_addr_t)1) {
#if defined(i386)
locr0[PC] = (int)uap->addr;
#elif defined(ppc)
#define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
if (!ALIGNED((int)uap->addr, sizeof(int)))
return (ERESTART);
statep.srr0 = uap->addr;
state_count = PPC_THREAD_STATE64_COUNT;
if (thread_setstatus(th_act, PPC_THREAD_STATE64, (thread_state_t)&statep, state_count) != KERN_SUCCESS) {
goto errorLabel;
}
#undef ALIGNED
#else
#error architecture not implemented!
#endif
}
if ((unsigned)uap->data >= NSIG)
goto errorLabel;
if (uap->data != 0) {
psignal_lock(t, uap->data, 0);
}
#if defined(ppc)
state_count = PPC_THREAD_STATE64_COUNT;
if (thread_getstatus(th_act, PPC_THREAD_STATE64, (thread_state_t)&statep, (mach_msg_type_number_t *)&state_count) != KERN_SUCCESS) {
goto errorLabel;
}
#endif
#define MSR_SE_BIT 21
if (uap->req == PT_STEP) {
#if defined(i386)
locr0[PS] |= PSL_T;
#elif defined(ppc)
statep.srr1 |= MASK(MSR_SE);
#else
#error architecture not implemented!
#endif
}
else {
#if defined(i386)
locr0[PS] &= ~PSL_T;
#elif defined(ppc)
statep.srr1 &= ~MASK(MSR_SE);
#endif
}
#if defined (ppc)
state_count = PPC_THREAD_STATE64_COUNT;
if (thread_setstatus(th_act, PPC_THREAD_STATE64, (thread_state_t)&statep, state_count) != KERN_SUCCESS) {
goto errorLabel;
}
#endif
resume:
t->p_xstat = uap->data;
t->p_stat = SRUN;
if (t->sigwait) {
wakeup((caddr_t)&(t->sigwait));
if ((t->p_flag & P_SIGEXC) == 0)
task_release(task);
}
break;
case PT_THUPDATE: {
if ((unsigned)uap->data >= NSIG)
goto errorLabel;
th_act = port_name_to_thread(CAST_DOWN(mach_port_name_t, uap->addr));
if (th_act == THREAD_NULL)
return (ESRCH);
ut = (uthread_t)get_bsdthread_info(th_act);
if (uap->data)
ut->uu_siglist |= sigmask(uap->data);
t->p_xstat = uap->data;
t->p_stat = SRUN;
thread_deallocate(th_act);
return(0);
}
break;
errorLabel:
default:
return(EINVAL);
}
return(0);
}
int
cantrace(proc_t cur_procp, kauth_cred_t creds, proc_t traced_procp, int *errp)
{
int my_err;
if (traced_procp->p_pid == cur_procp->p_pid) {
*errp = EINVAL;
return (0);
}
if (ISSET(traced_procp->p_flag, P_TRACED)) {
*errp = EBUSY;
return (0);
}
if ((creds->cr_ruid != proc_ucred(traced_procp)->cr_ruid ||
ISSET(traced_procp->p_flag, P_SUGID)) &&
(my_err = suser(creds, &cur_procp->p_acflag)) != 0) {
*errp = my_err;
return (0);
}
if ((cur_procp->p_flag & P_TRACED) && isinferior(cur_procp, traced_procp)) {
*errp = EPERM;
return (0);
}
if (ISSET(traced_procp->p_flag, P_NOATTACH)) {
*errp = EBUSY;
return (0);
}
return(1);
}