1 /*	$NetBSD: linux_exec_machdep.c,v 1.18 2010/07/07 01:30:33 chs Exp $ */
2 
3 /*-
4  * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Emmanuel Dreyfus
17  * 4. The name of the author may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: linux_exec_machdep.c,v 1.18 2010/07/07 01:30:33 chs Exp $");
36 
37 #define ELFSIZE 64
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/resource.h>
43 #include <sys/proc.h>
44 #include <sys/conf.h>
45 #include <sys/malloc.h>
46 #include <sys/exec_elf.h>
47 #include <sys/vnode.h>
48 #include <sys/lwp.h>
49 #include <sys/exec.h>
50 #include <sys/stat.h>
51 #include <sys/kauth.h>
52 
53 #include <sys/cpu.h>
54 #include <machine/vmparam.h>
55 #include <sys/syscallargs.h>
56 
57 #include <uvm/uvm.h>
58 
59 #include <compat/linux/common/linux_types.h>
60 #include <compat/linux/common/linux_signal.h>
61 #include <compat/linux/common/linux_machdep.h>
62 #include <compat/linux/common/linux_util.h>
63 #include <compat/linux/common/linux_ioctl.h>
64 #include <compat/linux/common/linux_hdio.h>
65 #include <compat/linux/common/linux_exec.h>
66 #include <compat/linux/common/linux_errno.h>
67 #include <compat/linux/common/linux_prctl.h>
68 #include <compat/linux/common/linux_ipc.h>
69 #include <compat/linux/common/linux_sem.h>
70 #include <compat/linux/linux_syscallargs.h>
71 
72 int
73 linux_exec_setup_stack(struct lwp *l, struct exec_package *epp)
74 {
75 	u_long max_stack_size;
76 	u_long access_linear_min, access_size;
77 	u_long noaccess_linear_min, noaccess_size;
78 
79 #ifndef USRSTACK32
80 #define USRSTACK32      (0x00000000ffffffffL & ~PGOFSET)
81 #endif
82 
83 	if (epp->ep_flags & EXEC_32) {
84 		epp->ep_minsaddr = USRSTACK32;
85 		max_stack_size = MAXSSIZ;
86 		if (epp->ep_minsaddr > LINUX_USRSTACK32)
87 			epp->ep_minsaddr = LINUX_USRSTACK32;
88 	} else {
89 		epp->ep_minsaddr = USRSTACK;
90 		max_stack_size = MAXSSIZ;
91 		if (epp->ep_minsaddr > LINUX_USRSTACK)
92 			epp->ep_minsaddr = LINUX_USRSTACK;
93 
94 	}
95 
96 	epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
97 		max_stack_size);
98 	epp->ep_ssize = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
99 
100 	/*
101 	 * set up commands for stack.  note that this takes *two*, one to
102 	 * map the part of the stack which we can access, and one to map
103 	 * the part which we can't.
104 	 *
105 	 * arguably, it could be made into one, but that would require the
106 	 * addition of another mapping proc, which is unnecessary
107 	 */
108 	access_size = epp->ep_ssize;
109 	access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
110 	noaccess_size = max_stack_size - access_size;
111 	noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
112 	    access_size), noaccess_size);
113 	if (noaccess_size > 0) {
114 		NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
115 		    noaccess_linear_min, NULLVP, 0, VM_PROT_NONE, VMCMD_STACK);
116 	}
117 	KASSERT(access_size > 0);
118 	NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
119 	    access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE,
120 	    VMCMD_STACK);
121 
122 	return 0;
123 }
124 
125 int
126 ELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
127 	struct ps_strings *arginfo, char **stackp, void *argp)
128 {
129 	struct linux_extra_stack_data64 *esdp, esd;
130 	struct elf_args *ap;
131 	struct vattr *vap;
132 	Elf_Ehdr *eh;
133 	Elf_Phdr *ph;
134 	u_long phsize;
135 	Elf_Addr phdr = 0;
136 	int error;
137 	int i;
138 
139 	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
140 		return error;
141 
142 	/*
143 	 * Push extra arguments on the stack needed by dynamically
144 	 * linked binaries and static binaries as well.
145 	 */
146 	memset(&esd, 0, sizeof(esd));
147 	esdp = (struct linux_extra_stack_data64 *)(*stackp);
148 	ap = (struct elf_args *)pack->ep_emul_arg;
149 	vap = pack->ep_vap;
150 	eh = (Elf_Ehdr *)pack->ep_hdr;
151 
152 	/*
153 	 * We forgot this, so we need to reload it now. XXX keep track of it?
154 	 */
155 	if (ap == NULL) {
156 		phsize = eh->e_phnum * sizeof(Elf_Phdr);
157 		ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
158 		error = exec_read_from(l, pack->ep_vp, eh->e_phoff, ph, phsize);
159 		if (error != 0) {
160 			for (i = 0; i < eh->e_phnum; i++) {
161 				if (ph[i].p_type == PT_PHDR) {
162 					phdr = ph[i].p_vaddr;
163 					break;
164 				}
165 			}
166 		}
167 		free(ph, M_TEMP);
168 	}
169 
170 
171 	/*
172 	 * The exec_package doesn't have a proc pointer and it's not
173 	 * exactly trivial to add one since the credentials are
174 	 * changing. XXX Linux uses curlwp's credentials.
175 	 * Why can't we use them too?
176 	 */
177 
178 	i = 0;
179 	esd.ai[i].a_type = LINUX_AT_HWCAP;
180 	esd.ai[i++].a_v = rcr4();
181 
182 	esd.ai[i].a_type = AT_PAGESZ;
183 	esd.ai[i++].a_v = PAGE_SIZE;
184 
185 	esd.ai[i].a_type = LINUX_AT_CLKTCK;
186 	esd.ai[i++].a_v = hz;
187 
188 	esd.ai[i].a_type = AT_PHDR;
189 	esd.ai[i++].a_v = (ap ? ap->arg_phaddr: phdr);
190 
191 	esd.ai[i].a_type = AT_PHENT;
192 	esd.ai[i++].a_v = (ap ? ap->arg_phentsize : eh->e_phentsize);
193 
194 	esd.ai[i].a_type = AT_PHNUM;
195 	esd.ai[i++].a_v = (ap ? ap->arg_phnum : eh->e_phnum);
196 
197 	esd.ai[i].a_type = AT_BASE;
198 	esd.ai[i++].a_v = (ap ? ap->arg_interp : 0);
199 
200 	esd.ai[i].a_type = AT_FLAGS;
201 	esd.ai[i++].a_v = 0;
202 
203 	esd.ai[i].a_type = AT_ENTRY;
204 	esd.ai[i++].a_v = (ap ? ap->arg_entry : eh->e_entry);
205 
206 	esd.ai[i].a_type = LINUX_AT_EGID;
207 	esd.ai[i++].a_v = ((vap->va_mode & S_ISGID) ?
208 	    vap->va_gid : kauth_cred_getegid(l->l_cred));
209 
210 	esd.ai[i].a_type = LINUX_AT_GID;
211 	esd.ai[i++].a_v = kauth_cred_getgid(l->l_cred);
212 
213 	esd.ai[i].a_type = LINUX_AT_EUID;
214 	esd.ai[i++].a_v = ((vap->va_mode & S_ISUID) ?
215 	    vap->va_uid : kauth_cred_geteuid(l->l_cred));
216 
217 	esd.ai[i].a_type = LINUX_AT_UID;
218 	esd.ai[i++].a_v = kauth_cred_getuid(l->l_cred);
219 
220 	esd.ai[i].a_type = LINUX_AT_SECURE;
221 	esd.ai[i++].a_v = 0;
222 
223 	esd.ai[i].a_type = LINUX_AT_PLATFORM;
224 	esd.ai[i++].a_v = (Elf_Addr)&esdp->hw_platform[0];
225 
226 	esd.ai[i].a_type = AT_NULL;
227 	esd.ai[i++].a_v = 0;
228 
229 #ifdef DEBUG_LINUX
230 	if (i != LINUX_ELF_AUX_ENTRIES) {
231 		printf("linux_elf64_copyargs: %d Aux entries\n", i);
232 		return EINVAL;
233 	}
234 #endif
235 
236 	strcpy(esd.hw_platform, LINUX_PLATFORM);
237 
238 	if (ap) {
239 		free((char *)ap, M_TEMP);
240 		pack->ep_emul_arg = NULL;
241 	}
242 
243 	/*
244 	 * Copy out the ELF auxiliary table and hw platform name
245 	 */
246 	if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
247 		return error;
248 	*stackp += sizeof(esd);
249 
250 	return 0;
251 }
252