xref: /openbsd/sys/sys/syscall_mi.h (revision 01f24c16)
1 /*	$OpenBSD: syscall_mi.h,v 1.34 2024/06/02 15:31:57 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)kern_xxx.c	8.2 (Berkeley) 11/14/93
32  */
33 
34 #include <sys/param.h>
35 #include <sys/pledge.h>
36 #include <sys/acct.h>
37 #include <sys/syslog.h>
38 #include <sys/tracepoint.h>
39 #include <sys/syscall.h>
40 #include <sys/signalvar.h>
41 #include <uvm/uvm_extern.h>
42 
43 #ifdef KTRACE
44 #include <sys/ktrace.h>
45 #endif
46 
47 #include "dt.h"
48 #if NDT > 0
49 #include <dev/dt/dtvar.h>
50 #endif
51 
52 /*
53  * Check if a system call is entered from precisely correct location
54  */
55 static inline int
pin_check(struct proc * p,register_t code)56 pin_check(struct proc *p, register_t code)
57 {
58 	extern char sigcodecall[], sigcoderet[], sigcodecall[];
59 	struct pinsyscall *pin = NULL, *ppin, *plibcpin;
60 	struct process *pr = p->p_p;
61 	vaddr_t addr;
62 	int error = 0;
63 
64 	/* point at start of syscall instruction */
65 	addr = (vaddr_t)PROC_PC(p) - (vaddr_t)(sigcoderet - sigcodecall);
66 	ppin = &pr->ps_pin;
67 	plibcpin = &pr->ps_libcpin;
68 
69 	/*
70 	 * System calls come from the following places, checks are ordered
71 	 * by most common case:
72 	 * 1) dynamic binary: syscalls in libc.so (in the ps_libcpin region)
73 	 * 2a) static binary: syscalls in main program (in the ps_pin region)
74 	 * 2b) dynamic binary: sysalls in ld.so (in the ps_pin region)
75 	 * 3) sigtramp, containing only sigreturn(2)
76 	 */
77 	if (plibcpin->pn_pins &&
78 	    addr >= plibcpin->pn_start && addr < plibcpin->pn_end)
79 		pin = plibcpin;
80 	else if (ppin->pn_pins &&
81 	    addr >= ppin->pn_start && addr < ppin->pn_end)
82 		pin = ppin;
83 	else if (PROC_PC(p) == pr->ps_sigcoderet) {
84 		if (code == SYS_sigreturn)
85 			return (0);
86 		error = EPERM;
87 		goto die;
88 	}
89 	if (pin) {
90 		if (code >= pin->pn_npins || pin->pn_pins[code] == 0)
91 			error = ENOSYS;
92 		else if (pin->pn_pins[code] + pin->pn_start == addr)
93 			; /* correct location */
94 		else if (pin->pn_pins[code] == (u_int)-1)
95 			; /* multiple locations, hopefully a boring operation */
96 		else
97 			error = ENOSYS;
98 	} else
99 		error = ENOSYS;
100 	if (error == 0)
101 		return (0);
102 die:
103 #ifdef KTRACE
104 	if (KTRPOINT(p, KTR_PINSYSCALL))
105 		ktrpinsyscall(p, error, code, addr);
106 #endif
107 	KERNEL_LOCK();
108 	/* XXX remove or simplify this uprintf() call after OpenBSD 7.5 release */
109 	uprintf("%s[%d]: pinsyscalls addr %lx code %ld, pinoff 0x%x "
110 	    "(pin%s %d %lx-%lx %lx) (libcpin%s %d %lx-%lx %lx) error %d\n",
111 	    p->p_p->ps_comm, p->p_p->ps_pid, addr, code,
112 	    (pin && code < pin->pn_npins) ? pin->pn_pins[code] : -1,
113 	    pin == ppin ? "(Y)" : "", ppin->pn_npins,
114 	    ppin->pn_start, ppin->pn_end, ppin->pn_end - ppin->pn_start,
115 	    pin == plibcpin ? "(Y)" : "", plibcpin->pn_npins,
116 	    plibcpin->pn_start, plibcpin->pn_end, plibcpin->pn_end - plibcpin->pn_start,
117 	    error);
118         p->p_p->ps_acflag |= APINSYS;
119 
120 	/* Try to stop threads immediately, because this process is suspect */
121 	if (P_HASSIBLING(p))
122 		single_thread_set(p, SINGLE_UNWIND | SINGLE_DEEP);
123 	/* Send uncatchable SIGABRT for coredump */
124 	sigabort(p);
125 	KERNEL_UNLOCK();
126 	return (error);
127 }
128 
129 /*
130  * The MD setup for a system call has been done; here's the MI part.
131  */
132 static inline int
mi_syscall(struct proc * p,register_t code,const struct sysent * callp,register_t * argp,register_t retval[2])133 mi_syscall(struct proc *p, register_t code, const struct sysent *callp,
134     register_t *argp, register_t retval[2])
135 {
136 	uint64_t tval;
137 	int lock = !(callp->sy_flags & SY_NOLOCK);
138 	int error, pledged;
139 
140 	/* refresh the thread's cache of the process's creds */
141 	refreshcreds(p);
142 
143 #ifdef SYSCALL_DEBUG
144 	KERNEL_LOCK();
145 	scdebug_call(p, code, argp);
146 	KERNEL_UNLOCK();
147 #endif
148 	TRACEPOINT(raw_syscalls, sys_enter, code, NULL);
149 #if NDT > 0
150 	DT_ENTER(syscall, code, callp->sy_argsize, argp);
151 #endif
152 #ifdef KTRACE
153 	if (KTRPOINT(p, KTR_SYSCALL)) {
154 		/* convert to mask, then include with code */
155 		KERNEL_LOCK();
156 		ktrsyscall(p, code, callp->sy_argsize, argp);
157 		KERNEL_UNLOCK();
158 	}
159 #endif
160 
161 	/* SP must be within MAP_STACK space */
162 	if (!uvm_map_inentry(p, &p->p_spinentry, PROC_STACK(p),
163 	    "[%s]%d/%d sp=%lx inside %lx-%lx: not MAP_STACK\n",
164 	    uvm_map_inentry_sp, p->p_vmspace->vm_map.sserial))
165 		return (EPERM);
166 
167 	if ((error = pin_check(p, code)))
168 		return (error);
169 
170 	pledged = (p->p_p->ps_flags & PS_PLEDGE);
171 	if (pledged && (error = pledge_syscall(p, code, &tval))) {
172 		KERNEL_LOCK();
173 		error = pledge_fail(p, error, tval);
174 		KERNEL_UNLOCK();
175 		return (error);
176 	}
177 	if (lock)
178 		KERNEL_LOCK();
179 	error = (*callp->sy_call)(p, argp, retval);
180 	if (lock)
181 		KERNEL_UNLOCK();
182 
183 	return (error);
184 }
185 
186 /*
187  * Finish MI stuff on return, after the registers have been set
188  */
189 static inline void
mi_syscall_return(struct proc * p,register_t code,int error,const register_t retval[2])190 mi_syscall_return(struct proc *p, register_t code, int error,
191     const register_t retval[2])
192 {
193 #ifdef SYSCALL_DEBUG
194 	KERNEL_LOCK();
195 	scdebug_ret(p, code, error, retval);
196 	KERNEL_UNLOCK();
197 #endif
198 #if NDT > 0
199 	DT_LEAVE(syscall, code, error, retval[0], retval[1]);
200 #endif
201 	TRACEPOINT(raw_syscalls, sys_exit, code, NULL);
202 
203 	userret(p);
204 
205 #ifdef KTRACE
206 	if (KTRPOINT(p, KTR_SYSRET)) {
207 		KERNEL_LOCK();
208 		ktrsysret(p, code, error, retval);
209 		KERNEL_UNLOCK();
210 	}
211 #endif
212 }
213 
214 /*
215  * Finish MI stuff for a new process/thread to return
216  */
217 static inline void
mi_child_return(struct proc * p)218 mi_child_return(struct proc *p)
219 {
220 #if defined(SYSCALL_DEBUG) || defined(KTRACE) || NDT > 0
221 	int code = (p->p_flag & P_THREAD) ? SYS___tfork :
222 	    (p->p_p->ps_flags & PS_PPWAIT) ? SYS_vfork : SYS_fork;
223 	const register_t child_retval[2] = { 0, 1 };
224 #endif
225 
226 	TRACEPOINT(sched, on__cpu, NULL);
227 
228 #ifdef SYSCALL_DEBUG
229 	KERNEL_LOCK();
230 	scdebug_ret(p, code, 0, child_retval);
231 	KERNEL_UNLOCK();
232 #endif
233 #if NDT > 0
234 	DT_LEAVE(syscall, code, 0, child_retval[0], child_retval[1]);
235 #endif
236 	TRACEPOINT(raw_syscalls, sys_exit, code, NULL);
237 
238 	userret(p);
239 
240 #ifdef KTRACE
241 	if (KTRPOINT(p, KTR_SYSRET)) {
242 		KERNEL_LOCK();
243 		ktrsysret(p, code, 0, child_retval);
244 		KERNEL_UNLOCK();
245 	}
246 #endif
247 }
248 
249 /*
250  * Do the specific processing necessary for an AST
251  */
252 static inline void
mi_ast(struct proc * p,int resched)253 mi_ast(struct proc *p, int resched)
254 {
255 	if (p->p_flag & P_OWEUPC) {
256 		KERNEL_LOCK();
257 		ADDUPROF(p);
258 		KERNEL_UNLOCK();
259 	}
260 	if (resched)
261 		preempt();
262 
263 	/*
264 	 * XXX could move call to userret() here, but
265 	 * hppa calls ast() in syscall return and sh calls
266 	 * it after userret()
267 	 */
268 }
269