xref: /openbsd/sys/sys/syscall_mi.h (revision 3e38f438)
1 /*	$OpenBSD: syscall_mi.h,v 1.36 2024/10/29 12:40:17 jsg 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/tracepoint.h>
38 #include <sys/syscall.h>
39 #include <sys/signalvar.h>
40 #include <uvm/uvm_extern.h>
41 
42 #ifdef KTRACE
43 #include <sys/ktrace.h>
44 #endif
45 
46 #include "dt.h"
47 #if NDT > 0
48 #include <dev/dt/dtvar.h>
49 #endif
50 
51 /*
52  * Check if a system call is entered from precisely correct location
53  */
54 static inline int
pin_check(struct proc * p,register_t code)55 pin_check(struct proc *p, register_t code)
56 {
57 	extern char sigcodecall[], sigcoderet[], sigcodecall[];
58 	struct pinsyscall *pin = NULL, *ppin, *plibcpin;
59 	struct process *pr = p->p_p;
60 	vaddr_t addr;
61 	int error = 0;
62 
63 	/* point at start of syscall instruction */
64 	addr = (vaddr_t)PROC_PC(p) - (vaddr_t)(sigcoderet - sigcodecall);
65 	ppin = &pr->ps_pin;
66 	plibcpin = &pr->ps_libcpin;
67 
68 	/*
69 	 * System calls come from the following places, checks are ordered
70 	 * by most common case:
71 	 * 1) dynamic binary: syscalls in libc.so (in the ps_libcpin region)
72 	 * 2a) static binary: syscalls in main program (in the ps_pin region)
73 	 * 2b) dynamic binary: syscalls in ld.so (in the ps_pin region)
74 	 * 3) sigtramp, containing only sigreturn(2)
75 	 */
76 	if (plibcpin->pn_pins &&
77 	    addr >= plibcpin->pn_start && addr < plibcpin->pn_end)
78 		pin = plibcpin;
79 	else if (ppin->pn_pins &&
80 	    addr >= ppin->pn_start && addr < ppin->pn_end)
81 		pin = ppin;
82 	else if (PROC_PC(p) == pr->ps_sigcoderet) {
83 		if (code == SYS_sigreturn)
84 			return (0);
85 		error = EPERM;
86 		goto die;
87 	}
88 	if (pin) {
89 		if (code >= pin->pn_npins || pin->pn_pins[code] == 0)
90 			error = ENOSYS;
91 		else if (pin->pn_pins[code] + pin->pn_start == addr)
92 			; /* correct location */
93 		else if (pin->pn_pins[code] == (u_int)-1)
94 			; /* multiple locations, hopefully a boring operation */
95 		else
96 			error = ENOSYS;
97 	} else
98 		error = ENOSYS;
99 	if (error == 0)
100 		return (0);
101 die:
102 #ifdef KTRACE
103 	if (KTRPOINT(p, KTR_PINSYSCALL))
104 		ktrpinsyscall(p, error, code, addr);
105 #endif
106 	KERNEL_LOCK();
107 	/* XXX remove or simplify this uprintf() call after OpenBSD 7.5 release */
108 	uprintf("%s[%d]: pinsyscalls addr %lx code %ld, pinoff 0x%x "
109 	    "(pin%s %d %lx-%lx %lx) (libcpin%s %d %lx-%lx %lx) error %d\n",
110 	    p->p_p->ps_comm, p->p_p->ps_pid, addr, code,
111 	    (pin && code < pin->pn_npins) ? pin->pn_pins[code] : -1,
112 	    pin == ppin ? "(Y)" : "", ppin->pn_npins,
113 	    ppin->pn_start, ppin->pn_end, ppin->pn_end - ppin->pn_start,
114 	    pin == plibcpin ? "(Y)" : "", plibcpin->pn_npins,
115 	    plibcpin->pn_start, plibcpin->pn_end, plibcpin->pn_end - plibcpin->pn_start,
116 	    error);
117         p->p_p->ps_acflag |= APINSYS;
118 
119 	/* Try to stop threads immediately, because this process is suspect */
120 	if (P_HASSIBLING(p))
121 		single_thread_set(p, SINGLE_UNWIND | SINGLE_DEEP);
122 	/* Send uncatchable SIGABRT for coredump */
123 	sigabort(p);
124 	KERNEL_UNLOCK();
125 	return (error);
126 }
127 
128 /*
129  * The MD setup for a system call has been done; here's the MI part.
130  */
131 static inline int
mi_syscall(struct proc * p,register_t code,const struct sysent * callp,register_t * argp,register_t retval[2])132 mi_syscall(struct proc *p, register_t code, const struct sysent *callp,
133     register_t *argp, register_t retval[2])
134 {
135 	uint64_t tval;
136 	int lock = !(callp->sy_flags & SY_NOLOCK);
137 	int error, pledged;
138 
139 	/* refresh the thread's cache of the process's creds */
140 	refreshcreds(p);
141 
142 #ifdef SYSCALL_DEBUG
143 	KERNEL_LOCK();
144 	scdebug_call(p, code, argp);
145 	KERNEL_UNLOCK();
146 #endif
147 	TRACEPOINT(raw_syscalls, sys_enter, code, NULL);
148 #if NDT > 0
149 	DT_ENTER(syscall, code, callp->sy_argsize, argp);
150 #endif
151 #ifdef KTRACE
152 	if (KTRPOINT(p, KTR_SYSCALL)) {
153 		/* convert to mask, then include with code */
154 		KERNEL_LOCK();
155 		ktrsyscall(p, code, callp->sy_argsize, argp);
156 		KERNEL_UNLOCK();
157 	}
158 #endif
159 
160 	/* SP must be within MAP_STACK space */
161 	if (!uvm_map_inentry(p, &p->p_spinentry, PROC_STACK(p),
162 	    "[%s]%d/%d sp=%lx inside %lx-%lx: not MAP_STACK\n",
163 	    uvm_map_inentry_sp, p->p_vmspace->vm_map.sserial))
164 		return (EPERM);
165 
166 	if ((error = pin_check(p, code)))
167 		return (error);
168 
169 	pledged = (p->p_p->ps_flags & PS_PLEDGE);
170 	if (pledged && (error = pledge_syscall(p, code, &tval))) {
171 		KERNEL_LOCK();
172 		error = pledge_fail(p, error, tval);
173 		KERNEL_UNLOCK();
174 		return (error);
175 	}
176 	if (lock)
177 		KERNEL_LOCK();
178 	error = (*callp->sy_call)(p, argp, retval);
179 	if (lock)
180 		KERNEL_UNLOCK();
181 
182 	return (error);
183 }
184 
185 /*
186  * Finish MI stuff on return, after the registers have been set
187  */
188 static inline void
mi_syscall_return(struct proc * p,register_t code,int error,const register_t retval[2])189 mi_syscall_return(struct proc *p, register_t code, int error,
190     const register_t retval[2])
191 {
192 #ifdef SYSCALL_DEBUG
193 	KERNEL_LOCK();
194 	scdebug_ret(p, code, error, retval);
195 	KERNEL_UNLOCK();
196 #endif
197 #if NDT > 0
198 	DT_LEAVE(syscall, code, error, retval[0], retval[1]);
199 #endif
200 	TRACEPOINT(raw_syscalls, sys_exit, code, NULL);
201 
202 	userret(p);
203 
204 #ifdef KTRACE
205 	if (KTRPOINT(p, KTR_SYSRET)) {
206 		KERNEL_LOCK();
207 		ktrsysret(p, code, error, retval);
208 		KERNEL_UNLOCK();
209 	}
210 #endif
211 }
212 
213 /*
214  * Finish MI stuff for a new process/thread to return
215  */
216 static inline void
mi_child_return(struct proc * p)217 mi_child_return(struct proc *p)
218 {
219 #if defined(SYSCALL_DEBUG) || defined(KTRACE) || NDT > 0
220 	int code = (p->p_flag & P_THREAD) ? SYS___tfork :
221 	    (p->p_p->ps_flags & PS_PPWAIT) ? SYS_vfork : SYS_fork;
222 	const register_t child_retval[2] = { 0, 1 };
223 #endif
224 
225 	TRACEPOINT(sched, on__cpu, NULL);
226 
227 #ifdef SYSCALL_DEBUG
228 	KERNEL_LOCK();
229 	scdebug_ret(p, code, 0, child_retval);
230 	KERNEL_UNLOCK();
231 #endif
232 #if NDT > 0
233 	DT_LEAVE(syscall, code, 0, child_retval[0], child_retval[1]);
234 #endif
235 	TRACEPOINT(raw_syscalls, sys_exit, code, NULL);
236 
237 	userret(p);
238 
239 #ifdef KTRACE
240 	if (KTRPOINT(p, KTR_SYSRET)) {
241 		KERNEL_LOCK();
242 		ktrsysret(p, code, 0, child_retval);
243 		KERNEL_UNLOCK();
244 	}
245 #endif
246 }
247 
248 /*
249  * Do the specific processing necessary for an AST
250  */
251 static inline void
mi_ast(struct proc * p,int resched)252 mi_ast(struct proc *p, int resched)
253 {
254 	if (p->p_flag & P_OWEUPC) {
255 		KERNEL_LOCK();
256 		ADDUPROF(p);
257 		KERNEL_UNLOCK();
258 	}
259 	if (resched)
260 		preempt();
261 
262 	/*
263 	 * XXX could move call to userret() here, but
264 	 * hppa calls ast() in syscall return and sh calls
265 	 * it after userret()
266 	 */
267 }
268