1/* $NetBSD: linux32_exec_elf32.c,v 1.19 2016/08/06 15:13:13 maxv 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.19 2016/08/06 15:13:13 maxv 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#include <machine/cpuvar.h>
57#include <machine/frame.h>
58
59#ifdef DEBUG_LINUX
60#define DPRINTF(a) uprintf a
61#else
62#define DPRINTF(a)
63#endif
64
65int linux32_copyinargs(struct exec_package *, struct ps_strings *,
66 void *, size_t, const void *, const void *);
67
68int
69ELFNAME2(linux32,probe)(struct lwp *l, struct exec_package *epp,
70 void *eh, char *itp, vaddr_t *pos)
71{
72 int error;
73
74 if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
75#ifdef LINUX32_GCC_SIGNATURE
76 ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
77#endif
78#ifdef LINUX32_ATEXIT_SIGNATURE
79 ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
80#endif
81#ifdef LINUX32_DEBUGLINK_SIGNATURE
82 ((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
83#endif
84 1)
85 return error;
86
87 if (itp) {
88 if ((error = emul_find_interp(l, epp, itp)))
89 return (error);
90 }
91#if 0
92 DPRINTF(("linux32_probe: returning 0\n"));
93#endif
94
95 epp->ep_flags |= EXEC_32 | EXEC_FORCEAUX;
96 epp->ep_vm_minaddr = exec_vm_minaddr(VM_MIN_ADDRESS);
97 epp->ep_vm_maxaddr = USRSTACK32;
98
99 return 0;
100}
101
102/*
103 * Copy arguments onto the stack in the normal way, but add some
104 * extra information in case of dynamic binding.
105 */
106int
107linux32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
108 struct ps_strings *arginfo, char **stackp, void *argp)
109{
110 Aux32Info ai[LINUX32_ELF_AUX_ENTRIES], *a;
111 uint32_t randbytes[4];
112 struct elf_args *ap;
113 struct vattr *vap;
114 size_t len;
115 int error;
116
117 if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
118 return error;
119
120 a = ai;
121
122 memset(ai, 0, sizeof(ai));
123
124 /*
125 * Push extra arguments on the stack needed by dynamically
126 * linked binaries and static binaries as well.
127 */
128
129 a->a_type = AT_PAGESZ;
130 a->a_v = PAGE_SIZE;
131 a++;
132
133 if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
134
135 a->a_type = AT_PHDR;
136 a->a_v = ap->arg_phaddr;
137 a++;
138
139 a->a_type = AT_PHENT;
140 a->a_v = ap->arg_phentsize;
141 a++;
142
143 a->a_type = AT_PHNUM;
144 a->a_v = ap->arg_phnum;
145 a++;
146
147 a->a_type = AT_BASE;
148 a->a_v = ap->arg_interp;
149 a++;
150
151 a->a_type = AT_FLAGS;
152 a->a_v = 0;
153 a++;
154
155 a->a_type = AT_ENTRY;
156 a->a_v = ap->arg_entry;
157 a++;
158
159 exec_free_emul_arg(pack);
160 }
161
162 /* Linux-specific items */
163 a->a_type = LINUX_AT_CLKTCK;
164 a->a_v = hz;
165 a++;
166
167 vap = pack->ep_vap;
168
169 a->a_type = LINUX_AT_UID;
170 a->a_v = kauth_cred_getuid(l->l_cred);
171 a++;
172
173 a->a_type = LINUX_AT_EUID;
174 a->a_v = ((vap->va_mode & S_ISUID) ?
175 vap->va_uid : kauth_cred_geteuid(l->l_cred));
176 a++;
177
178 a->a_type = LINUX_AT_GID;
179 a->a_v = kauth_cred_getgid(l->l_cred);
180 a++;
181
182 a->a_type = LINUX_AT_EGID;
183 a->a_v = ((vap->va_mode & S_ISGID) ?
184 vap->va_gid : kauth_cred_getegid(l->l_cred));
185 a++;
186
187 a->a_type = LINUX_AT_SECURE;
188 a->a_v = 0;
189 a++;
190
191 a->a_type = LINUX_AT_RANDOM;
192 a->a_v = NETBSD32PTR32I(*stackp);
193 a++;
194
195#if 0
196 /* XXX: increase LINUX32_ELF_AUX_ENTRIES if we enable those things */
197
198 a->a_type = LINUX_AT_SYSINFO;
199 a->a_v = NETBSD32PTR32I(&esdp->kernel_vsyscall[0]);
200 a++;
201
202 a->a_type = LINUX_AT_SYSINFO_EHDR;
203 a->a_v = NETBSD32PTR32I(&esdp->elfhdr);
204 a++;
205
206 a->a_type = LINUX_AT_HWCAP;
207 a->a_v = LINUX32_CPUCAP;
208 a++;
209
210 a->a_type = LINUX_AT_PLATFORM;
211 a->a_v = NETBSD32PTR32I(&esdp->hw_platform[0]);
212 a++;
213#endif
214
215 a->a_type = AT_NULL;
216 a->a_v = 0;
217 a++;
218
219 randbytes[0] = cprng_strong32();
220 randbytes[1] = cprng_strong32();
221 randbytes[2] = cprng_strong32();
222 randbytes[3] = cprng_strong32();
223
224 len = sizeof(randbytes);
225 if ((error = copyout(randbytes, *stackp, len)) != 0)
226 return error;
227 *stackp += len;
228
229#if 0
230 memcpy(esd.kernel_vsyscall, linux32_kernel_vsyscall,
231 sizeof(linux32_kernel_vsyscall));
232
233 memcpy(&esd.elfhdr, eh, sizeof(*eh));
234
235 strcpy(esd.hw_platform, LINUX32_PLATFORM);
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#endif
244
245 len = (a - ai) * sizeof(Aux32Info);
246 KASSERT(len <= LINUX32_ELF_AUX_ENTRIES * sizeof(Aux32Info));
247 if ((error = copyout(ai, *stackp, len)) != 0)
248 return error;
249 *stackp += len;
250
251 return 0;
252}
253