xref: /freebsd/sys/kern/subr_syscall.c (revision f78fe930854cac6eed55859b45e0a7b5d87189d6)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (C) 1994, David Greenman
5  * Copyright (c) 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the University of Utah, and William Jolitz.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #include "opt_capsicum.h"
42 #include "opt_ktrace.h"
43 #include <sys/capsicum.h>
44 #include <sys/ktr.h>
45 #include <sys/vmmeter.h>
46 #ifdef KTRACE
47 #include <sys/uio.h>
48 #include <sys/ktrace.h>
49 #endif
50 #include <security/audit/audit.h>
51 
52 static inline void
syscallenter(struct thread * td)53 syscallenter(struct thread *td)
54 {
55 	struct proc *p;
56 	struct syscall_args *sa;
57 	struct sysent *se;
58 	int error, traced;
59 	bool sy_thr_static;
60 
61 	VM_CNT_INC(v_syscall);
62 	p = td->td_proc;
63 	sa = &td->td_sa;
64 
65 	td->td_pticks = 0;
66 	if (__predict_false(td->td_cowgen != atomic_load_int(&p->p_cowgen)))
67 		thread_cow_update(td);
68 	traced = (p->p_flag & P_TRACED) != 0;
69 	if (__predict_false(traced || td->td_dbgflags & TDB_USERWR)) {
70 		PROC_LOCK(p);
71 		MPASS((td->td_dbgflags & TDB_BOUNDARY) == 0);
72 		td->td_dbgflags &= ~TDB_USERWR;
73 		if (traced)
74 			td->td_dbgflags |= TDB_SCE;
75 		PROC_UNLOCK(p);
76 	}
77 	error = (p->p_sysent->sv_fetch_syscall_args)(td);
78 	se = sa->callp;
79 #ifdef KTRACE
80 	if (KTRPOINT(td, KTR_SYSCALL))
81 		ktrsyscall(sa->code, se->sy_narg, sa->args);
82 #endif
83 	KTR_START4(KTR_SYSC, "syscall", syscallname(p, sa->code),
84 	    (uintptr_t)td, "pid:%d", td->td_proc->p_pid, "arg0:%p", sa->args[0],
85 	    "arg1:%p", sa->args[1], "arg2:%p", sa->args[2]);
86 
87 	if (__predict_false(error != 0)) {
88 		td->td_errno = error;
89 		goto retval;
90 	}
91 
92 	if (__predict_false(traced)) {
93 		PROC_LOCK(p);
94 		if (p->p_ptevents & PTRACE_SCE)
95 			ptracestop((td), SIGTRAP, NULL);
96 		PROC_UNLOCK(p);
97 
98 		if ((td->td_dbgflags & TDB_USERWR) != 0) {
99 			/*
100 			 * Reread syscall number and arguments if debugger
101 			 * modified registers or memory.
102 			 */
103 			error = (p->p_sysent->sv_fetch_syscall_args)(td);
104 			se = sa->callp;
105 #ifdef KTRACE
106 			if (KTRPOINT(td, KTR_SYSCALL))
107 				ktrsyscall(sa->code, se->sy_narg, sa->args);
108 #endif
109 			if (error != 0) {
110 				td->td_errno = error;
111 				goto retval;
112 			}
113 		}
114 	}
115 
116 #ifdef CAPABILITY_MODE
117 	/*
118 	 * In capability mode, we only allow access to system calls
119 	 * flagged with SYF_CAPENABLED.
120 	 */
121 	if ((se->sy_flags & SYF_CAPENABLED) == 0) {
122 		if (CAP_TRACING(td))
123 			ktrcapfail(CAPFAIL_SYSCALL, NULL);
124 		if (IN_CAPABILITY_MODE(td)) {
125 			td->td_errno = error = ECAPMODE;
126 			goto retval;
127 		}
128 	}
129 #endif
130 
131 	/*
132 	 * Fetch fast sigblock value at the time of syscall entry to
133 	 * handle sleepqueue primitives which might call cursig().
134 	 */
135 	if (__predict_false(sigfastblock_fetch_always))
136 		(void)sigfastblock_fetch(td);
137 
138 	/* Let system calls set td_errno directly. */
139 	KASSERT((td->td_pflags & TDP_NERRNO) == 0,
140 	    ("%s: TDP_NERRNO set", __func__));
141 
142 	sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
143 
144 	if (__predict_false(AUDIT_SYSCALL_ENABLED() ||
145 	    SYSTRACE_ENABLED() || !sy_thr_static)) {
146 		if (!sy_thr_static) {
147 			error = syscall_thread_enter(td, &se);
148 			sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
149 			if (error != 0) {
150 				td->td_errno = error;
151 				goto retval;
152 			}
153 		}
154 
155 #ifdef KDTRACE_HOOKS
156 		/* Give the syscall:::entry DTrace probe a chance to fire. */
157 		if (__predict_false(se->sy_entry != 0))
158 			(*systrace_probe_func)(sa, SYSTRACE_ENTRY, 0);
159 #endif
160 
161 		AUDIT_SYSCALL_ENTER(sa->code, td);
162 
163 		error = (se->sy_call)(td, sa->args);
164 		/* Save the latest error return value. */
165 		if (__predict_false((td->td_pflags & TDP_NERRNO) != 0))
166 			td->td_pflags &= ~TDP_NERRNO;
167 		else
168 			td->td_errno = error;
169 
170 		/*
171 		 * Note that some syscall implementations (e.g., sys_execve)
172 		 * will commit the audit record just before their final return.
173 		 * These were done under the assumption that nothing of interest
174 		 * would happen between their return and here, where we would
175 		 * normally commit the audit record.  These assumptions will
176 		 * need to be revisited should any substantial logic be added
177 		 * above.
178 		 */
179 		AUDIT_SYSCALL_EXIT(error, td);
180 
181 #ifdef KDTRACE_HOOKS
182 		/* Give the syscall:::return DTrace probe a chance to fire. */
183 		if (__predict_false(se->sy_return != 0))
184 			(*systrace_probe_func)(sa, SYSTRACE_RETURN,
185 			    error ? -1 : td->td_retval[0]);
186 #endif
187 
188 		if (!sy_thr_static)
189 			syscall_thread_exit(td, se);
190 	} else {
191 		error = (se->sy_call)(td, sa->args);
192 		/* Save the latest error return value. */
193 		if (__predict_false((td->td_pflags & TDP_NERRNO) != 0))
194 			td->td_pflags &= ~TDP_NERRNO;
195 		else
196 			td->td_errno = error;
197 	}
198 
199  retval:
200 	KTR_STOP4(KTR_SYSC, "syscall", syscallname(p, sa->code),
201 	    (uintptr_t)td, "pid:%d", td->td_proc->p_pid, "error:%d", error,
202 	    "retval0:%#lx", td->td_retval[0], "retval1:%#lx",
203 	    td->td_retval[1]);
204 	if (__predict_false(traced)) {
205 		PROC_LOCK(p);
206 		td->td_dbgflags &= ~(TDB_SCE | TDB_BOUNDARY);
207 		PROC_UNLOCK(p);
208 	}
209 	(p->p_sysent->sv_set_syscall_retval)(td, error);
210 }
211 
212 static inline void
syscallret(struct thread * td)213 syscallret(struct thread *td)
214 {
215 	struct proc *p;
216 	struct syscall_args *sa;
217 	ksiginfo_t ksi;
218 	int traced;
219 
220 	KASSERT(td->td_errno != ERELOOKUP,
221 	    ("ERELOOKUP not consumed syscall %d", td->td_sa.code));
222 
223 	p = td->td_proc;
224 	sa = &td->td_sa;
225 	if (__predict_false(td->td_errno == ENOTCAPABLE ||
226 	    td->td_errno == ECAPMODE)) {
227 		if ((trap_enotcap ||
228 		    (p->p_flag2 & P2_TRAPCAP) != 0) && IN_CAPABILITY_MODE(td)) {
229 			ksiginfo_init_trap(&ksi);
230 			ksi.ksi_signo = SIGTRAP;
231 			ksi.ksi_errno = td->td_errno;
232 			ksi.ksi_code = TRAP_CAP;
233 			ksi.ksi_info.si_syscall = sa->original_code;
234 			trapsignal(td, &ksi);
235 		}
236 	}
237 
238 	/*
239 	 * Handle reschedule and other end-of-syscall issues
240 	 */
241 	userret(td, td->td_frame);
242 
243 #ifdef KTRACE
244 	if (KTRPOINT(td, KTR_SYSRET)) {
245 		ktrsysret(sa->code, td->td_errno, td->td_retval[0]);
246 	}
247 #endif
248 
249 	traced = 0;
250 	if (__predict_false(p->p_flag & P_TRACED)) {
251 		traced = 1;
252 		PROC_LOCK(p);
253 		td->td_dbgflags |= TDB_SCX;
254 		PROC_UNLOCK(p);
255 	}
256 	if (__predict_false(traced ||
257 	    (td->td_dbgflags & (TDB_EXEC | TDB_FORK)) != 0)) {
258 		PROC_LOCK(p);
259 		/*
260 		 * Linux debuggers expect an additional stop for exec,
261 		 * between the usual syscall entry and exit.  Raise
262 		 * the exec event now and then clear TDB_EXEC so that
263 		 * the next stop is reported as a syscall exit by
264 		 * linux_ptrace_status().
265 		 *
266 		 * We are accessing p->p_pptr without any additional
267 		 * locks here: it cannot change while p is kept locked;
268 		 * while the debugger could in theory change its ABI
269 		 * while tracing another process, the outcome of such
270 		 * a race wouln't be deterministic anyway.
271 		 */
272 		if (traced && (td->td_dbgflags & TDB_EXEC) != 0 &&
273 		    SV_PROC_ABI(p->p_pptr) == SV_ABI_LINUX) {
274 			ptracestop(td, SIGTRAP, NULL);
275 			td->td_dbgflags &= ~TDB_EXEC;
276 		}
277 		/*
278 		 * If tracing the execed process, trap to the debugger
279 		 * so that breakpoints can be set before the program
280 		 * executes.  If debugger requested tracing of syscall
281 		 * returns, do it now too.
282 		 */
283 		if (traced &&
284 		    ((td->td_dbgflags & (TDB_FORK | TDB_EXEC)) != 0 ||
285 		    (p->p_ptevents & PTRACE_SCX) != 0)) {
286 			MPASS((td->td_dbgflags & TDB_BOUNDARY) == 0);
287 			td->td_dbgflags |= TDB_BOUNDARY;
288 			ptracestop(td, SIGTRAP, NULL);
289 		}
290 		td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC | TDB_FORK |
291 		    TDB_BOUNDARY);
292 		PROC_UNLOCK(p);
293 	}
294 }
295