1 /*-
2  * Copyright (c) 2013 Dmitry Chagin
3  * Copyright (c) 2004 Tim J. Robbins
4  * Copyright (c) 2002 Doug Rabson
5  * Copyright (c) 2000 Marcel Moolenaar
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer
13  *    in this position and unchanged.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/clock.h>
38 #include <sys/dirent.h>
39 #include <sys/fcntl.h>
40 #include <sys/file.h>
41 #include <sys/filedesc.h>
42 #include <sys/imgact.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/limits.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mman.h>
49 #include <sys/mutex.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/resource.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysproto.h>
57 #include <sys/systm.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <sys/wait.h>
61 
62 #include <security/mac/mac_framework.h>
63 
64 #include <ufs/ufs/extattr.h>
65 #include <ufs/ufs/quota.h>
66 #include <ufs/ufs/ufsmount.h>
67 
68 #include <machine/frame.h>
69 #include <machine/md_var.h>
70 #include <machine/pcb.h>
71 #include <machine/psl.h>
72 #include <machine/segments.h>
73 #include <machine/specialreg.h>
74 
75 #include <vm/pmap.h>
76 #include <vm/vm.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
80 
81 #include <x86/ifunc.h>
82 #include <x86/sysarch.h>
83 
84 #include <security/audit/audit.h>
85 
86 #include <amd64/linux/linux.h>
87 #include <amd64/linux/linux_proto.h>
88 #include <compat/linux/linux_emul.h>
89 #include <compat/linux/linux_file.h>
90 #include <compat/linux/linux_ipc.h>
91 #include <compat/linux/linux_misc.h>
92 #include <compat/linux/linux_mmap.h>
93 #include <compat/linux/linux_signal.h>
94 #include <compat/linux/linux_util.h>
95 
96 int
linux_execve(struct thread * td,struct linux_execve_args * args)97 linux_execve(struct thread *td, struct linux_execve_args *args)
98 {
99 	struct image_args eargs;
100 	char *path;
101 	int error;
102 
103 	LCONVPATHEXIST(td, args->path, &path);
104 
105 	LINUX_CTR(execve);
106 
107 	error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
108 	    args->envp);
109 	free(path, M_TEMP);
110 	if (error == 0)
111 		error = linux_common_execve(td, &eargs);
112 	AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
113 	return (error);
114 }
115 
116 int
linux_set_upcall_kse(struct thread * td,register_t stack)117 linux_set_upcall_kse(struct thread *td, register_t stack)
118 {
119 
120 	if (stack)
121 		td->td_frame->tf_rsp = stack;
122 
123 	/*
124 	 * The newly created Linux thread returns
125 	 * to the user space by the same path that a parent do.
126 	 */
127 	td->td_frame->tf_rax = 0;
128 	return (0);
129 }
130 
131 int
linux_mmap2(struct thread * td,struct linux_mmap2_args * args)132 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
133 {
134 
135 	return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
136 		args->flags, args->fd, args->pgoff));
137 }
138 
139 int
linux_mprotect(struct thread * td,struct linux_mprotect_args * uap)140 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
141 {
142 
143 	return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
144 }
145 
146 int
linux_iopl(struct thread * td,struct linux_iopl_args * args)147 linux_iopl(struct thread *td, struct linux_iopl_args *args)
148 {
149 	int error;
150 
151 	LINUX_CTR(iopl);
152 
153 	if (args->level > 3)
154 		return (EINVAL);
155 	if ((error = priv_check(td, PRIV_IO)) != 0)
156 		return (error);
157 	if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
158 		return (error);
159 	td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
160 	    (args->level * (PSL_IOPL / 3));
161 
162 	return (0);
163 }
164 
165 int
linux_rt_sigsuspend(struct thread * td,struct linux_rt_sigsuspend_args * uap)166 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
167 {
168 	l_sigset_t lmask;
169 	sigset_t sigmask;
170 	int error;
171 
172 	LINUX_CTR2(rt_sigsuspend, "%p, %ld",
173 	    uap->newset, uap->sigsetsize);
174 
175 	if (uap->sigsetsize != sizeof(l_sigset_t))
176 		return (EINVAL);
177 
178 	error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
179 	if (error)
180 		return (error);
181 
182 	linux_to_bsd_sigset(&lmask, &sigmask);
183 	return (kern_sigsuspend(td, sigmask));
184 }
185 
186 int
linux_pause(struct thread * td,struct linux_pause_args * args)187 linux_pause(struct thread *td, struct linux_pause_args *args)
188 {
189 	struct proc *p = td->td_proc;
190 	sigset_t sigmask;
191 
192 	LINUX_CTR(pause);
193 
194 	PROC_LOCK(p);
195 	sigmask = td->td_sigmask;
196 	PROC_UNLOCK(p);
197 	return (kern_sigsuspend(td, sigmask));
198 }
199 
200 int
linux_sigaltstack(struct thread * td,struct linux_sigaltstack_args * uap)201 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
202 {
203 	stack_t ss, oss;
204 	l_stack_t lss;
205 	int error;
206 
207 	memset(&lss, 0, sizeof(lss));
208 	LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss);
209 
210 	if (uap->uss != NULL) {
211 		error = copyin(uap->uss, &lss, sizeof(l_stack_t));
212 		if (error)
213 			return (error);
214 
215 		ss.ss_sp = PTRIN(lss.ss_sp);
216 		ss.ss_size = lss.ss_size;
217 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
218 	}
219 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
220 	    (uap->uoss != NULL) ? &oss : NULL);
221 	if (!error && uap->uoss != NULL) {
222 		lss.ss_sp = PTROUT(oss.ss_sp);
223 		lss.ss_size = oss.ss_size;
224 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
225 		error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
226 	}
227 
228 	return (error);
229 }
230 
231 int
linux_arch_prctl(struct thread * td,struct linux_arch_prctl_args * args)232 linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args)
233 {
234 	struct pcb *pcb;
235 	int error;
236 
237 	pcb = td->td_pcb;
238 	LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr);
239 
240 	switch (args->code) {
241 	case LINUX_ARCH_SET_GS:
242 		if (args->addr < VM_MAXUSER_ADDRESS) {
243 			set_pcb_flags(pcb, PCB_FULL_IRET);
244 			pcb->pcb_gsbase = args->addr;
245 			td->td_frame->tf_gs = _ugssel;
246 			error = 0;
247 		} else
248 			error = EPERM;
249 		break;
250 	case LINUX_ARCH_SET_FS:
251 		if (args->addr < VM_MAXUSER_ADDRESS) {
252 			set_pcb_flags(pcb, PCB_FULL_IRET);
253 			pcb->pcb_fsbase = args->addr;
254 			td->td_frame->tf_fs = _ufssel;
255 			error = 0;
256 		} else
257 			error = EPERM;
258 		break;
259 	case LINUX_ARCH_GET_FS:
260 		error = copyout(&pcb->pcb_fsbase, PTRIN(args->addr),
261 		    sizeof(args->addr));
262 		break;
263 	case LINUX_ARCH_GET_GS:
264 		error = copyout(&pcb->pcb_gsbase, PTRIN(args->addr),
265 		    sizeof(args->addr));
266 		break;
267 	default:
268 		error = EINVAL;
269 	}
270 	return (error);
271 }
272 
273 int
linux_set_cloned_tls(struct thread * td,void * desc)274 linux_set_cloned_tls(struct thread *td, void *desc)
275 {
276 	struct pcb *pcb;
277 
278 	if ((uint64_t)desc >= VM_MAXUSER_ADDRESS)
279 		return (EPERM);
280 
281 	pcb = td->td_pcb;
282 	pcb->pcb_fsbase = (register_t)desc;
283 	td->td_frame->tf_fs = _ufssel;
284 
285 	return (0);
286 }
287 
288 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
289 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval);
290 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *), static)
291 {
292 
293 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
294 	    futex_xchgl_smap : futex_xchgl_nosmap);
295 }
296 
297 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
298 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval);
299 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *), static)
300 {
301 
302 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
303 	    futex_addl_smap : futex_addl_nosmap);
304 }
305 
306 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
307 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval);
308 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *), static)
309 {
310 
311 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
312 	    futex_orl_smap : futex_orl_nosmap);
313 }
314 
315 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
316 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval);
317 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *), static)
318 {
319 
320 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
321 	    futex_andl_smap : futex_andl_nosmap);
322 }
323 
324 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
325 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval);
326 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *), static)
327 {
328 
329 	return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
330 	    futex_xorl_smap : futex_xorl_nosmap);
331 }
332