-
- /*
- * You can't attach to a process if:
- * (1) it's the process that's doing the attaching,
- */
- if (t->p_pid == p->p_pid)
- return (EINVAL);
-
- /*
- * (2) it's already being traced, or
- */
- if (ISSET(t->p_flag, P_TRACED))
- return (EBUSY);
-
- /*
- * (3) it's not owned by you, or is set-id on exec
- * (unless you're root).
- */
- if ((t->p_cred->p_ruid != p->p_cred->p_ruid ||
- ISSET(t->p_flag, P_SUGID)) &&
- (error = suser(p->p_ucred, &p->p_acflag)) != 0)
- return (error);
-
- if ((p->p_flag & P_TRACED) && isinferior(p, t))
- return(EPERM);
-
- if (ISSET(t->p_flag, P_NOATTACH)) {
- psignal(p, SIGSEGV);
- return (EBUSY);
- }
- 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 ) {
+ int err;
+
+ if ( kauth_authorize_process(proc_ucred(p), KAUTH_PROCESS_CANTRACE,
+ t, (uintptr_t)&err, 0, 0) == 0 ) {
+ /* it's OK to attach */
+ proc_lock(t);
+ SET(t->p_lflag, P_LTRACED);
+ if (tr_sigexc)
+ SET(t->p_lflag, P_LSIGEXC);
+
+ t->p_oppid = t->p_ppid;
+ proc_unlock(t);
+ if (t->p_pptr != p)
+ proc_reparentlocked(t, p, 1, 0);
+
+ proc_lock(t);
+ if (get_task_userstop(task) > 0 ) {
+ stopped = 1;
+ }