]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_syscall.c
MFC r319873:
[FreeBSD/FreeBSD.git] / sys / kern / subr_syscall.c
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
44 __FBSDID("$FreeBSD$");
45
46 #include <sys/capsicum.h>
47 #include <sys/ktr.h>
48 #ifdef KTRACE
49 #include <sys/uio.h>
50 #include <sys/ktrace.h>
51 #endif
52 #include <security/audit/audit.h>
53
54 static inline int
55 syscallenter(struct thread *td)
56 {
57         struct proc *p;
58         struct syscall_args *sa;
59         int error, traced;
60
61         PCPU_INC(cnt.v_syscall);
62         p = td->td_proc;
63         sa = &td->td_sa;
64
65         td->td_pticks = 0;
66         if (td->td_cowgen != p->p_cowgen)
67                 thread_cow_update(td);
68         traced = (p->p_flag & P_TRACED) != 0;
69         if (traced || td->td_dbgflags & TDB_USERWR) {
70                 PROC_LOCK(p);
71                 td->td_dbgflags &= ~TDB_USERWR;
72                 if (traced)
73                         td->td_dbgflags |= TDB_SCE;
74                 PROC_UNLOCK(p);
75         }
76         error = (p->p_sysent->sv_fetch_syscall_args)(td);
77 #ifdef KTRACE
78         if (KTRPOINT(td, KTR_SYSCALL))
79                 ktrsyscall(sa->code, sa->narg, sa->args);
80 #endif
81         KTR_START4(KTR_SYSC, "syscall", syscallname(p, sa->code),
82             (uintptr_t)td, "pid:%d", td->td_proc->p_pid, "arg0:%p", sa->args[0],
83             "arg1:%p", sa->args[1], "arg2:%p", sa->args[2]);
84
85         if (error == 0) {
86
87                 STOPEVENT(p, S_SCE, sa->narg);
88                 if (p->p_flag & P_TRACED) {
89                         PROC_LOCK(p);
90                         if (p->p_ptevents & PTRACE_SCE)
91                                 ptracestop((td), SIGTRAP, NULL);
92                         PROC_UNLOCK(p);
93                 }
94                 if (td->td_dbgflags & TDB_USERWR) {
95                         /*
96                          * Reread syscall number and arguments if
97                          * debugger modified registers or memory.
98                          */
99                         error = (p->p_sysent->sv_fetch_syscall_args)(td);
100 #ifdef KTRACE
101                         if (KTRPOINT(td, KTR_SYSCALL))
102                                 ktrsyscall(sa->code, sa->narg, sa->args);
103 #endif
104                         if (error != 0)
105                                 goto retval;
106                 }
107
108 #ifdef CAPABILITY_MODE
109                 /*
110                  * In capability mode, we only allow access to system calls
111                  * flagged with SYF_CAPENABLED.
112                  */
113                 if (IN_CAPABILITY_MODE(td) &&
114                     !(sa->callp->sy_flags & SYF_CAPENABLED)) {
115                         error = ECAPMODE;
116                         goto retval;
117                 }
118 #endif
119
120                 error = syscall_thread_enter(td, sa->callp);
121                 if (error != 0)
122                         goto retval;
123
124 #ifdef KDTRACE_HOOKS
125                 /* Give the syscall:::entry DTrace probe a chance to fire. */
126                 if (systrace_probe_func != NULL && sa->callp->sy_entry != 0)
127                         (*systrace_probe_func)(sa, SYSTRACE_ENTRY, 0);
128 #endif
129
130                 AUDIT_SYSCALL_ENTER(sa->code, td);
131                 error = (sa->callp->sy_call)(td, sa->args);
132                 AUDIT_SYSCALL_EXIT(error, td);
133
134                 /* Save the latest error return value. */
135                 if ((td->td_pflags & TDP_NERRNO) == 0)
136                         td->td_errno = error;
137
138 #ifdef KDTRACE_HOOKS
139                 /* Give the syscall:::return DTrace probe a chance to fire. */
140                 if (systrace_probe_func != NULL && sa->callp->sy_return != 0)
141                         (*systrace_probe_func)(sa, SYSTRACE_RETURN,
142                             error ? -1 : td->td_retval[0]);
143 #endif
144                 syscall_thread_exit(td, sa->callp);
145         }
146  retval:
147         KTR_STOP4(KTR_SYSC, "syscall", syscallname(p, sa->code),
148             (uintptr_t)td, "pid:%d", td->td_proc->p_pid, "error:%d", error,
149             "retval0:%#lx", td->td_retval[0], "retval1:%#lx",
150             td->td_retval[1]);
151         if (traced) {
152                 PROC_LOCK(p);
153                 td->td_dbgflags &= ~TDB_SCE;
154                 PROC_UNLOCK(p);
155         }
156         (p->p_sysent->sv_set_syscall_retval)(td, error);
157         return (error);
158 }
159
160 static inline void
161 syscallret(struct thread *td, int error)
162 {
163         struct proc *p, *p2;
164         struct syscall_args *sa;
165         ksiginfo_t ksi;
166         int traced, error1;
167
168         KASSERT((td->td_pflags & TDP_FORKING) == 0,
169             ("fork() did not clear TDP_FORKING upon completion"));
170
171         p = td->td_proc;
172         sa = &td->td_sa;
173         if ((trap_enotcap || (p->p_flag2 & P2_TRAPCAP) != 0) &&
174             IN_CAPABILITY_MODE(td)) {
175                 error1 = (td->td_pflags & TDP_NERRNO) == 0 ? error :
176                     td->td_errno;
177                 if (error1 == ENOTCAPABLE || error1 == ECAPMODE) {
178                         ksiginfo_init_trap(&ksi);
179                         ksi.ksi_signo = SIGTRAP;
180                         ksi.ksi_errno = error1;
181                         ksi.ksi_code = TRAP_CAP;
182                         trapsignal(td, &ksi);
183                 }
184         }
185
186         /*
187          * Handle reschedule and other end-of-syscall issues
188          */
189         userret(td, td->td_frame);
190
191 #ifdef KTRACE
192         if (KTRPOINT(td, KTR_SYSRET)) {
193                 ktrsysret(sa->code, (td->td_pflags & TDP_NERRNO) == 0 ?
194                     error : td->td_errno, td->td_retval[0]);
195         }
196 #endif
197         td->td_pflags &= ~TDP_NERRNO;
198
199         if (p->p_flag & P_TRACED) {
200                 traced = 1;
201                 PROC_LOCK(p);
202                 td->td_dbgflags |= TDB_SCX;
203                 PROC_UNLOCK(p);
204         } else
205                 traced = 0;
206         /*
207          * This works because errno is findable through the
208          * register set.  If we ever support an emulation where this
209          * is not the case, this code will need to be revisited.
210          */
211         STOPEVENT(p, S_SCX, sa->code);
212         if (traced || (td->td_dbgflags & (TDB_EXEC | TDB_FORK)) != 0) {
213                 PROC_LOCK(p);
214                 /*
215                  * If tracing the execed process, trap to the debugger
216                  * so that breakpoints can be set before the program
217                  * executes.  If debugger requested tracing of syscall
218                  * returns, do it now too.
219                  */
220                 if (traced &&
221                     ((td->td_dbgflags & (TDB_FORK | TDB_EXEC)) != 0 ||
222                     (p->p_ptevents & PTRACE_SCX) != 0))
223                         ptracestop(td, SIGTRAP, NULL);
224                 td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC | TDB_FORK);
225                 PROC_UNLOCK(p);
226         }
227
228         if (td->td_pflags & TDP_RFPPWAIT) {
229                 /*
230                  * Preserve synchronization semantics of vfork.  If
231                  * waiting for child to exec or exit, fork set
232                  * P_PPWAIT on child, and there we sleep on our proc
233                  * (in case of exit).
234                  *
235                  * Do it after the ptracestop() above is finished, to
236                  * not block our debugger until child execs or exits
237                  * to finish vfork wait.
238                  */
239                 td->td_pflags &= ~TDP_RFPPWAIT;
240                 p2 = td->td_rfppwait_p;
241 again:
242                 PROC_LOCK(p2);
243                 while (p2->p_flag & P_PPWAIT) {
244                         PROC_LOCK(p);
245                         if (thread_suspend_check_needed()) {
246                                 PROC_UNLOCK(p2);
247                                 thread_suspend_check(0);
248                                 PROC_UNLOCK(p);
249                                 goto again;
250                         } else {
251                                 PROC_UNLOCK(p);
252                         }
253                         cv_timedwait(&p2->p_pwait, &p2->p_mtx, hz);
254                 }
255                 PROC_UNLOCK(p2);
256
257                 if (td->td_dbgflags & TDB_VFORK) {
258                         PROC_LOCK(p);
259                         if (p->p_ptevents & PTRACE_VFORK)
260                                 ptracestop(td, SIGTRAP, NULL);
261                         td->td_dbgflags &= ~TDB_VFORK;
262                         PROC_UNLOCK(p);
263                 }
264         }
265 }