#include "opt_capsicum.h"
#include "opt_ktrace.h"
#include <sys/capsicum.h>
#include <sys/ktr.h>
#include <sys/vmmeter.h>
#ifdef KTRACE
#include <sys/uio.h>
#include <sys/ktrace.h>
#endif
#include <security/audit/audit.h>
static inline void
syscallenter(struct thread *td)
{
struct proc *p;
struct syscall_args *sa;
struct sysent *se;
int error;
bool sy_thr_static, traced;
VM_CNT_INC(v_syscall);
p = td->td_proc;
sa = &td->td_sa;
td->td_pticks = 0;
if (__predict_false(td->td_cowgen != atomic_load_int(&p->p_cowgen)))
thread_cow_update(td);
traced = (p->p_flag & P_TRACED) != 0;
if (__predict_false(traced || (td->td_dbgflags & (TDB_USERWR |
TDB_SET_SC_RET)) != 0)) {
PROC_LOCK(p);
MPASS((td->td_dbgflags & TDB_BOUNDARY) == 0);
td->td_dbgflags &= ~(TDB_USERWR | TDB_SET_SC_RET);
if (traced)
td->td_dbgflags |= TDB_SCE;
PROC_UNLOCK(p);
}
if ((td->td_pflags2 & TDP2_UEXTERR) != 0)
td->td_pflags2 &= ~TDP2_EXTERR;
error = (p->p_sysent->sv_fetch_syscall_args)(td);
se = sa->callp;
#ifdef KTRACE
if (KTRPOINT(td, KTR_SYSCALL))
ktrsyscall(sa->code, se->sy_narg, sa->args);
#endif
KTR_START4(KTR_SYSC, "syscall", syscallname(p, sa->code),
(uintptr_t)td, "pid:%d", td->td_proc->p_pid, "arg0:%p", sa->args[0],
"arg1:%p", sa->args[1], "arg2:%p", sa->args[2]);
if (__predict_false(error != 0)) {
td->td_errno = error;
goto retval;
}
if (__predict_false(traced)) {
PROC_LOCK(p);
if (p->p_ptevents & PTRACE_SCE)
ptracestop((td), SIGTRAP, NULL);
PROC_UNLOCK(p);
if ((td->td_dbgflags & TDB_SET_SC_RET) != 0) {
error = td->td_errno;
goto retval;
} else if ((td->td_dbgflags & TDB_USERWR) != 0) {
error = (p->p_sysent->sv_fetch_syscall_args)(td);
se = sa->callp;
#ifdef KTRACE
if (KTRPOINT(td, KTR_SYSCALL))
ktrsyscall(sa->code, se->sy_narg, sa->args);
#endif
if (error != 0) {
td->td_errno = error;
goto retval;
}
}
}
#ifdef CAPABILITY_MODE
if ((se->sy_flags & SYF_CAPENABLED) == 0) {
if (CAP_TRACING(td))
ktrcapfail(CAPFAIL_SYSCALL, NULL);
if (IN_CAPABILITY_MODE(td)) {
td->td_errno = error = ECAPMODE;
goto retval;
}
}
#endif
if (__predict_false(sigfastblock_fetch_always))
(void)sigfastblock_fetch(td);
KASSERT((td->td_pflags & TDP_NERRNO) == 0,
("%s: TDP_NERRNO set", __func__));
sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
if (__predict_false(AUDIT_SYSCALL_ENABLED() ||
SYSTRACE_ENABLED() || !sy_thr_static)) {
if (!sy_thr_static) {
syscall_thread_enter(td, &se);
sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
}
#ifdef KDTRACE_HOOKS
if (__predict_false(se->sy_entry != 0))
(*systrace_probe_func)(sa, SYSTRACE_ENTRY, 0);
#endif
AUDIT_SYSCALL_ENTER(sa->code, td);
error = (se->sy_call)(td, sa->args);
if (__predict_false((td->td_pflags & TDP_NERRNO) != 0))
td->td_pflags &= ~TDP_NERRNO;
else
td->td_errno = error;
AUDIT_SYSCALL_EXIT(error, td);
#ifdef KDTRACE_HOOKS
if (__predict_false(se->sy_return != 0))
(*systrace_probe_func)(sa, SYSTRACE_RETURN,
error ? -1 : td->td_retval[0]);
#endif
if (!sy_thr_static)
syscall_thread_exit(td, se);
} else {
error = (se->sy_call)(td, sa->args);
if (__predict_false((td->td_pflags & TDP_NERRNO) != 0))
td->td_pflags &= ~TDP_NERRNO;
else
td->td_errno = error;
}
retval:
KTR_STOP4(KTR_SYSC, "syscall", syscallname(p, sa->code),
(uintptr_t)td, "pid:%d", td->td_proc->p_pid, "error:%d", error,
"retval0:%#lx", td->td_retval[0], "retval1:%#lx",
td->td_retval[1]);
if (__predict_false(traced)) {
PROC_LOCK(p);
td->td_dbgflags &= ~(TDB_SCE | TDB_BOUNDARY | TDB_SET_SC_RET);
PROC_UNLOCK(p);
}
(p->p_sysent->sv_set_syscall_retval)(td, error);
if (error != 0 && (td->td_pflags2 & TDP2_UEXTERR) != 0)
exterr_copyout(td);
}
static inline void
syscallret(struct thread *td)
{
struct proc *p;
struct syscall_args *sa;
ksiginfo_t ksi;
bool traced;
KASSERT(td->td_errno != ERELOOKUP,
("ERELOOKUP not consumed syscall %d", td->td_sa.code));
p = td->td_proc;
sa = &td->td_sa;
if (__predict_false(td->td_errno == ENOTCAPABLE ||
td->td_errno == ECAPMODE)) {
if ((trap_enotcap ||
(p->p_flag2 & P2_TRAPCAP) != 0) && IN_CAPABILITY_MODE(td)) {
ksiginfo_init_trap(&ksi);
ksi.ksi_signo = SIGTRAP;
ksi.ksi_errno = td->td_errno;
ksi.ksi_code = TRAP_CAP;
ksi.ksi_info.si_syscall = sa->original_code;
trapsignal(td, &ksi);
}
}
userret(td, td->td_frame);
#ifdef KTRACE
if (KTRPOINT(td, KTR_SYSRET)) {
ktrsysret(sa->code, td->td_errno, td->td_retval[0]);
}
#endif
traced = false;
if (__predict_false(p->p_flag & P_TRACED)) {
traced = true;
PROC_LOCK(p);
td->td_dbgflags |= TDB_SCX;
PROC_UNLOCK(p);
}
if (__predict_false(traced ||
(td->td_dbgflags & (TDB_EXEC | TDB_FORK)) != 0)) {
PROC_LOCK(p);
if (traced && (td->td_dbgflags & TDB_EXEC) != 0 &&
SV_PROC_ABI(p->p_pptr) == SV_ABI_LINUX) {
ptracestop(td, SIGTRAP, NULL);
td->td_dbgflags &= ~TDB_EXEC;
}
if (traced &&
((td->td_dbgflags & (TDB_FORK | TDB_EXEC)) != 0 ||
(p->p_ptevents & PTRACE_SCX) != 0)) {
MPASS((td->td_dbgflags & TDB_BOUNDARY) == 0);
td->td_dbgflags |= TDB_BOUNDARY;
ptracestop(td, SIGTRAP, NULL);
}
td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC | TDB_FORK |
TDB_BOUNDARY);
PROC_UNLOCK(p);
}
}