1 /* $NetBSD: linux32_exec_elf32.c,v 1.23 2021/11/26 08:56:29 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.23 2021/11/26 08:56:29 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 #ifdef LINUX32_GO_RT0_SIGNATURE 87 ((error = ELFNAME2(linux,go_rt0_signature)(l, epp, eh)) != 0) && 88 #endif 89 1) 90 return error; 91 92 if (itp) { 93 if ((error = emul_find_interp(l, epp, itp))) 94 return (error); 95 } 96 #if 0 97 DPRINTF(("linux32_probe: returning 0\n")); 98 #endif 99 100 epp->ep_flags |= EXEC_32 | EXEC_FORCEAUX; 101 epp->ep_vm_minaddr = exec_vm_minaddr(VM_MIN_ADDRESS); 102 epp->ep_vm_maxaddr = USRSTACK32; 103 104 return 0; 105 } 106 107 /* 108 * Copy arguments onto the stack in the normal way, but add some 109 * extra information in case of dynamic binding. 110 */ 111 int 112 linux32_elf32_copyargs(struct lwp *l, struct exec_package *pack, 113 struct ps_strings *arginfo, char **stackp, void *argp) 114 { 115 struct linux32_extra_stack_data esd, *esdp; 116 Aux32Info *a, *ai __diagused; 117 struct elf_args *ap; 118 struct vattr *vap; 119 int error; 120 121 if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0) 122 return error; 123 124 esdp = (struct linux32_extra_stack_data *)(*stackp); /* userspace */ 125 126 memset(&esd, 0, sizeof(esd)); 127 ai = a = esd.ai; 128 129 /* 130 * Push extra arguments on the stack needed by dynamically 131 * linked binaries and static binaries as well. 132 */ 133 134 a->a_type = AT_PAGESZ; 135 a->a_v = PAGE_SIZE; 136 a++; 137 138 if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 139 140 a->a_type = AT_PHDR; 141 a->a_v = ap->arg_phaddr; 142 a++; 143 144 a->a_type = AT_PHENT; 145 a->a_v = ap->arg_phentsize; 146 a++; 147 148 a->a_type = AT_PHNUM; 149 a->a_v = ap->arg_phnum; 150 a++; 151 152 a->a_type = AT_BASE; 153 a->a_v = ap->arg_interp; 154 a++; 155 156 a->a_type = AT_FLAGS; 157 a->a_v = 0; 158 a++; 159 160 a->a_type = AT_ENTRY; 161 a->a_v = ap->arg_entry; 162 a++; 163 164 exec_free_emul_arg(pack); 165 } 166 167 /* Linux-specific items */ 168 a->a_type = LINUX_AT_CLKTCK; 169 a->a_v = hz; 170 a++; 171 172 vap = pack->ep_vap; 173 174 a->a_type = LINUX_AT_UID; 175 a->a_v = kauth_cred_getuid(l->l_cred); 176 a++; 177 178 a->a_type = LINUX_AT_EUID; 179 a->a_v = ((vap->va_mode & S_ISUID) ? 180 vap->va_uid : kauth_cred_geteuid(l->l_cred)); 181 a++; 182 183 a->a_type = LINUX_AT_GID; 184 a->a_v = kauth_cred_getgid(l->l_cred); 185 a++; 186 187 a->a_type = LINUX_AT_EGID; 188 a->a_v = ((vap->va_mode & S_ISGID) ? 189 vap->va_gid : kauth_cred_getegid(l->l_cred)); 190 a++; 191 192 a->a_type = LINUX_AT_SECURE; 193 a->a_v = 0; 194 a++; 195 196 a->a_type = LINUX_AT_RANDOM; 197 a->a_v = NETBSD32PTR32I(&esdp->randbytes[0]); 198 a++; 199 esd.randbytes[0] = cprng_strong32(); 200 esd.randbytes[1] = cprng_strong32(); 201 esd.randbytes[2] = cprng_strong32(); 202 esd.randbytes[3] = cprng_strong32(); 203 204 #if 0 /* defined(__amd64__) */ 205 /* XXX: broken. vsyscall must be placed in the executable page */ 206 a->a_type = LINUX_AT_SYSINFO; 207 a->a_v = NETBSD32PTR32I(&esdp->kernel_vsyscall[0]); 208 a++; 209 memcpy(esd.kernel_vsyscall, linux32_kernel_vsyscall, 210 sizeof(linux32_kernel_vsyscall)); 211 #endif 212 213 #if 0 /* notyet */ 214 a->a_type = LINUX_AT_SYSINFO_EHDR; 215 a->a_v = NETBSD32PTR32I(&esdp->elfhdr); 216 a++; 217 memcpy(&esd.elfhdr, eh, sizeof(*eh)); 218 #endif 219 220 #ifdef LINUX32_CPUCAP 221 a->a_type = LINUX_AT_HWCAP; 222 a->a_v = LINUX32_CPUCAP; 223 a++; 224 #endif 225 226 #ifdef LINUX32_PLATFORM 227 a->a_type = LINUX_AT_PLATFORM; 228 a->a_v = NETBSD32PTR32I(&esdp->hw_platform[0]); 229 a++; 230 strncpy(esd.hw_platform, LINUX32_PLATFORM, sizeof(esd.hw_platform)); 231 #endif 232 233 a->a_type = AT_NULL; 234 a->a_v = 0; 235 a++; 236 237 KASSERTMSG(a <= &ai[LINUX32_ELF_AUX_ENTRIES], 238 "increase LINUX32_ELF_AUX_ENTRIES to %lu", a - ai); 239 240 /* 241 * Copy out the ELF auxiliary table and hw platform name 242 */ 243 if ((error = copyout(&esd, esdp, sizeof(esd))) != 0) 244 return error; 245 *stackp += sizeof(esd); 246 247 return 0; 248 } 249