linux_exec_machdep.c revision 1.24.26.1 1 /* $NetBSD: linux_exec_machdep.c,v 1.24.26.1 2025/08/02 05:56:21 perseant 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.24.26.1 2025/08/02 05:56:21 perseant 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/kmem.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 #include <sys/cprng.h>
53
54 #include <sys/cpu.h>
55 #include <machine/vmparam.h>
56 #include <sys/syscallargs.h>
57
58 #include <compat/linux/common/linux_types.h>
59 #include <compat/linux/common/linux_signal.h>
60 #include <compat/linux/common/linux_machdep.h>
61 #include <compat/linux/common/linux_util.h>
62 #include <compat/linux/common/linux_ioctl.h>
63 #include <compat/linux/common/linux_hdio.h>
64 #include <compat/linux/common/linux_exec.h>
65 #include <compat/linux/common/linux_errno.h>
66 #include <compat/linux/common/linux_prctl.h>
67 #include <compat/linux/common/linux_ipc.h>
68 #include <compat/linux/common/linux_sem.h>
69 #include <compat/linux/linux_syscallargs.h>
70
71 int
72 linux_exec_setup_stack(struct lwp *l, struct exec_package *epp)
73 {
74 u_long max_stack_size;
75 u_long access_linear_min, access_size;
76 u_long noaccess_linear_min, noaccess_size;
77
78 #ifndef USRSTACK32
79 #define USRSTACK32 (0x00000000ffffffffL & ~PGOFSET)
80 #endif
81
82 if (epp->ep_flags & EXEC_32) {
83 epp->ep_minsaddr = USRSTACK32;
84 max_stack_size = MAXSSIZ;
85 if (epp->ep_minsaddr > LINUX_USRSTACK32)
86 epp->ep_minsaddr = LINUX_USRSTACK32;
87 } else {
88 epp->ep_minsaddr = USRSTACK;
89 max_stack_size = MAXSSIZ;
90 if (epp->ep_minsaddr > LINUX_USRSTACK)
91 epp->ep_minsaddr = LINUX_USRSTACK;
92
93 }
94
95 epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
96 max_stack_size);
97 epp->ep_ssize = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
98
99 /*
100 * set up commands for stack. note that this takes *two*, one to
101 * map the part of the stack which we can access, and one to map
102 * the part which we can't.
103 *
104 * arguably, it could be made into one, but that would require the
105 * addition of another mapping proc, which is unnecessary
106 */
107 access_size = epp->ep_ssize;
108 access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
109 noaccess_size = max_stack_size - access_size;
110 noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
111 access_size), noaccess_size);
112 if (noaccess_size > 0) {
113 NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
114 noaccess_linear_min, NULLVP, 0, VM_PROT_NONE, VMCMD_STACK);
115 }
116 KASSERT(access_size > 0);
117 NEW_VMCMD2(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
118 access_linear_min, NULLVP, 0, VM_PROT_READ | VM_PROT_WRITE,
119 VMCMD_STACK);
120
121 return 0;
122 }
123
124 int
125 ELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
126 struct ps_strings *arginfo, char **stackp, void *argp)
127 {
128 struct linux_extra_stack_data64 *esdp, esd;
129 struct elf_args *ap;
130 struct vattr *vap;
131 Elf_Ehdr *eh;
132 Elf_Phdr *ph;
133 u_long phsize;
134 Elf_Addr phdr = 0;
135 int error;
136 int i;
137
138 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
139 return error;
140
141 /*
142 * Push extra arguments on the stack needed by dynamically
143 * linked binaries and static binaries as well.
144 */
145 memset(&esd, 0, sizeof(esd));
146 esdp = (struct linux_extra_stack_data64 *)(*stackp);
147 ap = (struct elf_args *)pack->ep_emul_arg;
148 vap = pack->ep_vap;
149 eh = (Elf_Ehdr *)pack->ep_hdr;
150
151 /*
152 * We forgot this, so we need to reload it now. XXX keep track of it?
153 */
154 if (ap == NULL) {
155 phsize = eh->e_phnum * sizeof(Elf_Phdr);
156 ph = (Elf_Phdr *)kmem_alloc(phsize, KM_SLEEP);
157 error = exec_read(l, pack->ep_vp, eh->e_phoff, ph, phsize, 0);
158 if (error != 0) {
159 for (i = 0; i < eh->e_phnum; i++) {
160 if (ph[i].p_type == PT_PHDR) {
161 phdr = ph[i].p_vaddr;
162 break;
163 }
164 }
165 }
166 kmem_free(ph, phsize);
167 }
168
169
170 /*
171 * The exec_package doesn't have a proc pointer and it's not
172 * exactly trivial to add one since the credentials are
173 * changing. XXX Linux uses curlwp's credentials.
174 * Why can't we use them too?
175 */
176
177 i = 0;
178 esd.ai[i].a_type = LINUX_AT_HWCAP;
179 esd.ai[i++].a_v = rcr4();
180
181 esd.ai[i].a_type = AT_PAGESZ;
182 esd.ai[i++].a_v = PAGE_SIZE;
183
184 esd.ai[i].a_type = LINUX_AT_CLKTCK;
185 esd.ai[i++].a_v = hz;
186
187 esd.ai[i].a_type = AT_PHDR;
188 esd.ai[i++].a_v = (ap ? ap->arg_phaddr: phdr);
189
190 esd.ai[i].a_type = AT_PHENT;
191 esd.ai[i++].a_v = (ap ? ap->arg_phentsize : eh->e_phentsize);
192
193 esd.ai[i].a_type = AT_PHNUM;
194 esd.ai[i++].a_v = (ap ? ap->arg_phnum : eh->e_phnum);
195
196 esd.ai[i].a_type = AT_BASE;
197 esd.ai[i++].a_v = (ap ? ap->arg_interp : 0);
198
199 esd.ai[i].a_type = AT_FLAGS;
200 esd.ai[i++].a_v = 0;
201
202 esd.ai[i].a_type = AT_ENTRY;
203 esd.ai[i++].a_v = (ap ? ap->arg_entry : eh->e_entry);
204
205 esd.ai[i].a_type = LINUX_AT_EGID;
206 esd.ai[i++].a_v = ((vap->va_mode & S_ISGID) ?
207 vap->va_gid : kauth_cred_getegid(l->l_cred));
208
209 esd.ai[i].a_type = LINUX_AT_GID;
210 esd.ai[i++].a_v = kauth_cred_getgid(l->l_cred);
211
212 esd.ai[i].a_type = LINUX_AT_EUID;
213 esd.ai[i++].a_v = ((vap->va_mode & S_ISUID) ?
214 vap->va_uid : kauth_cred_geteuid(l->l_cred));
215
216 esd.ai[i].a_type = LINUX_AT_UID;
217 esd.ai[i++].a_v = kauth_cred_getuid(l->l_cred);
218
219 esd.ai[i].a_type = LINUX_AT_SECURE;
220 esd.ai[i++].a_v = 0;
221
222 esd.ai[i].a_type = LINUX_AT_PLATFORM;
223 esd.ai[i++].a_v = (Elf_Addr)&esdp->hw_platform[0];
224
225 esd.ai[i].a_type = LINUX_AT_RANDOM;
226 esd.ai[i++].a_v = (Elf_Addr)&esdp->randbytes[0];
227 esd.randbytes[0] = cprng_strong32();
228 esd.randbytes[1] = cprng_strong32();
229 esd.randbytes[2] = cprng_strong32();
230 esd.randbytes[3] = cprng_strong32();
231
232 esd.ai[i].a_type = AT_NULL;
233 esd.ai[i++].a_v = 0;
234
235 KASSERT(i == LINUX_ELF_AUX_ENTRIES);
236
237 strcpy(esd.hw_platform, LINUX_PLATFORM);
238
239 exec_free_emul_arg(pack);
240
241 /*
242 * Copy out the ELF auxiliary table and hw platform name
243 */
244 if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
245 return error;
246 *stackp += sizeof(esd);
247
248 return 0;
249 }
250