subr_syscall.c revision 226893
1/*-
2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 * Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the University of Utah, and William Jolitz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	from: @(#)trap.c	7.4 (Berkeley) 5/13/91
39 */
40
41#include "opt_capsicum.h"
42#include "opt_ktrace.h"
43#include "opt_kdtrace.h"
44
45__FBSDID("$FreeBSD: head/sys/kern/subr_syscall.c 226893 2011-10-29 01:26:36Z marcel $");
46
47#include <sys/capability.h>
48#include <sys/ktr.h>
49#ifdef KTRACE
50#include <sys/uio.h>
51#include <sys/ktrace.h>
52#endif
53#include <security/audit/audit.h>
54
55#ifdef KDTRACE_HOOKS
56/*
57 * This is a hook which is initialised by the systrace module
58 * when it is loaded. This keeps the DTrace syscall provider
59 * implementation opaque.
60 */
61systrace_probe_func_t systrace_probe_func;
62#endif
63
64static inline int
65syscallenter(struct thread *td, struct syscall_args *sa)
66{
67	struct proc *p;
68	int error, traced;
69
70	PCPU_INC(cnt.v_syscall);
71	p = td->td_proc;
72
73	td->td_pticks = 0;
74	if (td->td_ucred != p->p_ucred)
75		cred_update_thread(td);
76	if (p->p_flag & P_TRACED) {
77		traced = 1;
78		PROC_LOCK(p);
79		td->td_dbgflags &= ~TDB_USERWR;
80		td->td_dbgflags |= TDB_SCE;
81		PROC_UNLOCK(p);
82	} else
83		traced = 0;
84	error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
85#ifdef KTRACE
86	if (KTRPOINT(td, KTR_SYSCALL))
87		ktrsyscall(sa->code, sa->narg, sa->args);
88#endif
89
90	CTR6(KTR_SYSC,
91"syscall: td=%p pid %d %s (%#lx, %#lx, %#lx)",
92	    td, td->td_proc->p_pid, syscallname(p, sa->code),
93	    sa->args[0], sa->args[1], sa->args[2]);
94
95	if (error == 0) {
96		STOPEVENT(p, S_SCE, sa->narg);
97		PTRACESTOP_SC(p, td, S_PT_SCE);
98		if (td->td_dbgflags & TDB_USERWR) {
99			/*
100			 * Reread syscall number and arguments if
101			 * debugger modified registers or memory.
102			 */
103			error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
104#ifdef KTRACE
105			if (KTRPOINT(td, KTR_SYSCALL))
106				ktrsyscall(sa->code, sa->narg, sa->args);
107#endif
108			if (error != 0)
109				goto retval;
110		}
111
112#ifdef CAPABILITY_MODE
113		/*
114		 * In capability mode, we only allow access to system calls
115		 * flagged with SYF_CAPENABLED.
116		 */
117		if (IN_CAPABILITY_MODE(td) &&
118		    !(sa->callp->sy_flags & SYF_CAPENABLED)) {
119			error = ECAPMODE;
120			goto retval;
121		}
122#endif
123
124		error = syscall_thread_enter(td, sa->callp);
125		if (error != 0)
126			goto retval;
127
128#ifdef KDTRACE_HOOKS
129		/*
130		 * If the systrace module has registered it's probe
131		 * callback and if there is a probe active for the
132		 * syscall 'entry', process the probe.
133		 */
134		if (systrace_probe_func != NULL && sa->callp->sy_entry != 0)
135			(*systrace_probe_func)(sa->callp->sy_entry, sa->code,
136			    sa->callp, sa->args, 0);
137#endif
138
139		AUDIT_SYSCALL_ENTER(sa->code, td);
140		error = (sa->callp->sy_call)(td, sa->args);
141		AUDIT_SYSCALL_EXIT(error, td);
142
143		/* Save the latest error return value. */
144		td->td_errno = error;
145
146#ifdef KDTRACE_HOOKS
147		/*
148		 * If the systrace module has registered it's probe
149		 * callback and if there is a probe active for the
150		 * syscall 'return', process the probe.
151		 */
152		if (systrace_probe_func != NULL && sa->callp->sy_return != 0)
153			(*systrace_probe_func)(sa->callp->sy_return, sa->code,
154			    sa->callp, NULL, (error) ? -1 : td->td_retval[0]);
155#endif
156		syscall_thread_exit(td, sa->callp);
157		CTR4(KTR_SYSC, "syscall: p=%p error=%d return %#lx %#lx",
158		    p, error, td->td_retval[0], td->td_retval[1]);
159	}
160 retval:
161	if (traced) {
162		PROC_LOCK(p);
163		td->td_dbgflags &= ~TDB_SCE;
164		PROC_UNLOCK(p);
165	}
166	(p->p_sysent->sv_set_syscall_retval)(td, error);
167	return (error);
168}
169
170static inline void
171syscallret(struct thread *td, int error, struct syscall_args *sa __unused)
172{
173	struct proc *p;
174	int traced;
175
176	p = td->td_proc;
177
178	/*
179	 * Check for misbehavior.
180	 */
181	WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
182	    syscallname(p, sa->code));
183	KASSERT(td->td_critnest == 0,
184	    ("System call %s returning in a critical section",
185	    syscallname(p, sa->code)));
186	KASSERT(td->td_locks == 0,
187	    ("System call %s returning with %d locks held",
188	     syscallname(p, sa->code), td->td_locks));
189
190	/*
191	 * Handle reschedule and other end-of-syscall issues
192	 */
193	userret(td, td->td_frame);
194
195	CTR4(KTR_SYSC, "syscall %s exit thread %p pid %d proc %s",
196	    syscallname(p, sa->code), td, td->td_proc->p_pid, td->td_name);
197
198#ifdef KTRACE
199	if (KTRPOINT(td, KTR_SYSRET))
200		ktrsysret(sa->code, error, td->td_retval[0]);
201#endif
202
203	if (p->p_flag & P_TRACED) {
204		traced = 1;
205		PROC_LOCK(p);
206		td->td_dbgflags |= TDB_SCX;
207		PROC_UNLOCK(p);
208	} else
209		traced = 0;
210	/*
211	 * This works because errno is findable through the
212	 * register set.  If we ever support an emulation where this
213	 * is not the case, this code will need to be revisited.
214	 */
215	STOPEVENT(p, S_SCX, sa->code);
216	if (traced || (td->td_dbgflags & (TDB_EXEC | TDB_FORK)) != 0) {
217		PROC_LOCK(p);
218		/*
219		 * If tracing the execed process, trap to the debugger
220		 * so that breakpoints can be set before the program
221		 * executes.  If debugger requested tracing of syscall
222		 * returns, do it now too.
223		 */
224		if (traced && ((td->td_dbgflags & TDB_EXEC) != 0 ||
225		    (p->p_stops & S_PT_SCX) != 0))
226			ptracestop(td, SIGTRAP);
227		td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC | TDB_FORK);
228		PROC_UNLOCK(p);
229	}
230}
231