linux32_exec_elf32.c revision 1.22 1 /* $NetBSD: linux32_exec_elf32.c,v 1.22 2021/11/25 03:08:04 ryo Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998, 2000, 2001,2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
9 * Emmanuel Dreyfus.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux32_exec_elf32.c,v 1.22 2021/11/25 03:08:04 ryo Exp $");
35
36 #define ELFSIZE 32
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/vnode.h>
42 #include <sys/exec.h>
43 #include <sys/exec_elf.h>
44 #include <sys/kauth.h>
45 #include <sys/kernel.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signal.h>
48 #include <sys/signalvar.h>
49 #include <sys/cprng.h>
50
51 #include <compat/linux/common/linux_exec.h>
52 #include <compat/netbsd32/netbsd32.h>
53 #include <compat/netbsd32/netbsd32_exec.h>
54 #include <compat/linux32/common/linux32_exec.h>
55
56 #ifndef __aarch64__
57 #include <machine/cpuvar.h>
58 #endif
59 #include <machine/frame.h>
60
61 #ifdef DEBUG_LINUX
62 #define DPRINTF(a) uprintf a
63 #else
64 #define DPRINTF(a)
65 #endif
66
67 int linux32_copyinargs(struct exec_package *, struct ps_strings *,
68 void *, size_t, const void *, const void *);
69
70 int
71 ELFNAME2(linux32,probe)(struct lwp *l, struct exec_package *epp,
72 void *eh, char *itp, vaddr_t *pos)
73 {
74 int error;
75
76 if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
77 #ifdef LINUX32_GCC_SIGNATURE
78 ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
79 #endif
80 #ifdef LINUX32_ATEXIT_SIGNATURE
81 ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
82 #endif
83 #ifdef LINUX32_DEBUGLINK_SIGNATURE
84 ((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
85 #endif
86 1)
87 return error;
88
89 if (itp) {
90 if ((error = emul_find_interp(l, epp, itp)))
91 return (error);
92 }
93 #if 0
94 DPRINTF(("linux32_probe: returning 0\n"));
95 #endif
96
97 epp->ep_flags |= EXEC_32 | EXEC_FORCEAUX;
98 epp->ep_vm_minaddr = exec_vm_minaddr(VM_MIN_ADDRESS);
99 epp->ep_vm_maxaddr = USRSTACK32;
100
101 return 0;
102 }
103
104 /*
105 * Copy arguments onto the stack in the normal way, but add some
106 * extra information in case of dynamic binding.
107 */
108 int
109 linux32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
110 struct ps_strings *arginfo, char **stackp, void *argp)
111 {
112 struct linux32_extra_stack_data esd, *esdp;
113 Aux32Info *a, *ai __diagused;
114 struct elf_args *ap;
115 struct vattr *vap;
116 int error;
117
118 if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
119 return error;
120
121 esdp = (struct linux32_extra_stack_data *)(*stackp); /* userspace */
122
123 memset(&esd, 0, sizeof(esd));
124 ai = a = esd.ai;
125
126 /*
127 * Push extra arguments on the stack needed by dynamically
128 * linked binaries and static binaries as well.
129 */
130
131 a->a_type = AT_PAGESZ;
132 a->a_v = PAGE_SIZE;
133 a++;
134
135 if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
136
137 a->a_type = AT_PHDR;
138 a->a_v = ap->arg_phaddr;
139 a++;
140
141 a->a_type = AT_PHENT;
142 a->a_v = ap->arg_phentsize;
143 a++;
144
145 a->a_type = AT_PHNUM;
146 a->a_v = ap->arg_phnum;
147 a++;
148
149 a->a_type = AT_BASE;
150 a->a_v = ap->arg_interp;
151 a++;
152
153 a->a_type = AT_FLAGS;
154 a->a_v = 0;
155 a++;
156
157 a->a_type = AT_ENTRY;
158 a->a_v = ap->arg_entry;
159 a++;
160
161 exec_free_emul_arg(pack);
162 }
163
164 /* Linux-specific items */
165 a->a_type = LINUX_AT_CLKTCK;
166 a->a_v = hz;
167 a++;
168
169 vap = pack->ep_vap;
170
171 a->a_type = LINUX_AT_UID;
172 a->a_v = kauth_cred_getuid(l->l_cred);
173 a++;
174
175 a->a_type = LINUX_AT_EUID;
176 a->a_v = ((vap->va_mode & S_ISUID) ?
177 vap->va_uid : kauth_cred_geteuid(l->l_cred));
178 a++;
179
180 a->a_type = LINUX_AT_GID;
181 a->a_v = kauth_cred_getgid(l->l_cred);
182 a++;
183
184 a->a_type = LINUX_AT_EGID;
185 a->a_v = ((vap->va_mode & S_ISGID) ?
186 vap->va_gid : kauth_cred_getegid(l->l_cred));
187 a++;
188
189 a->a_type = LINUX_AT_SECURE;
190 a->a_v = 0;
191 a++;
192
193 a->a_type = LINUX_AT_RANDOM;
194 a->a_v = NETBSD32PTR32I(&esdp->randbytes[0]);
195 a++;
196 esd.randbytes[0] = cprng_strong32();
197 esd.randbytes[1] = cprng_strong32();
198 esd.randbytes[2] = cprng_strong32();
199 esd.randbytes[3] = cprng_strong32();
200
201 #if 0 /* defined(__amd64__) */
202 /* XXX: broken. vsyscall must be placed in the executable page */
203 a->a_type = LINUX_AT_SYSINFO;
204 a->a_v = NETBSD32PTR32I(&esdp->kernel_vsyscall[0]);
205 a++;
206 memcpy(esd.kernel_vsyscall, linux32_kernel_vsyscall,
207 sizeof(linux32_kernel_vsyscall));
208 #endif
209
210 #if 0 /* notyet */
211 a->a_type = LINUX_AT_SYSINFO_EHDR;
212 a->a_v = NETBSD32PTR32I(&esdp->elfhdr);
213 a++;
214 memcpy(&esd.elfhdr, eh, sizeof(*eh));
215 #endif
216
217 #ifdef LINUX32_CPUCAP
218 a->a_type = LINUX_AT_HWCAP;
219 a->a_v = LINUX32_CPUCAP;
220 a++;
221 #endif
222
223 #ifdef LINUX32_PLATFORM
224 a->a_type = LINUX_AT_PLATFORM;
225 a->a_v = NETBSD32PTR32I(&esdp->hw_platform[0]);
226 a++;
227 strncpy(esd.hw_platform, LINUX32_PLATFORM, sizeof(esd.hw_platform));
228 #endif
229
230 a->a_type = AT_NULL;
231 a->a_v = 0;
232 a++;
233
234 KASSERTMSG(a <= &ai[LINUX32_ELF_AUX_ENTRIES],
235 "increase LINUX32_ELF_AUX_ENTRIES to %lu", a - ai);
236
237 /*
238 * Copy out the ELF auxiliary table and hw platform name
239 */
240 if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
241 return error;
242 *stackp += sizeof(esd);
243
244 return 0;
245 }
246