| 1 | /* $NetBSD: compat_util.c,v 1.46 2014/11/09 17:48:07 maxv Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 1994 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 and Frank van der Linden. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * Copyright (c) 2008, 2009 Matthew R. Green |
| 34 | * All rights reserved. |
| 35 | * |
| 36 | * Redistribution and use in source and binary forms, with or without |
| 37 | * modification, are permitted provided that the following conditions |
| 38 | * are met: |
| 39 | * 1. Redistributions of source code must retain the above copyright |
| 40 | * notice, this list of conditions and the following disclaimer. |
| 41 | * 2. Redistributions in binary form must reproduce the above copyright |
| 42 | * notice, this list of conditions and the following disclaimer in the |
| 43 | * documentation and/or other materials provided with the distribution. |
| 44 | * 3. The name of the author may not be used to endorse or promote products |
| 45 | * derived from this software without specific prior written permission. |
| 46 | * |
| 47 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 48 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 49 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 50 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 51 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 52 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 53 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 54 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 55 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 56 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 57 | * SUCH DAMAGE. |
| 58 | */ |
| 59 | |
| 60 | #include <sys/cdefs.h> |
| 61 | __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.46 2014/11/09 17:48:07 maxv Exp $" ); |
| 62 | |
| 63 | #include <sys/param.h> |
| 64 | #include <sys/systm.h> |
| 65 | #include <sys/namei.h> |
| 66 | #include <sys/proc.h> |
| 67 | #include <sys/file.h> |
| 68 | #include <sys/stat.h> |
| 69 | #include <sys/filedesc.h> |
| 70 | #include <sys/exec.h> |
| 71 | #include <sys/ioctl.h> |
| 72 | #include <sys/kernel.h> |
| 73 | #include <sys/vnode.h> |
| 74 | #include <sys/syslog.h> |
| 75 | #include <sys/mount.h> |
| 76 | |
| 77 | #include <compat/common/compat_util.h> |
| 78 | |
| 79 | void |
| 80 | emul_find_root(struct lwp *l, struct exec_package *epp) |
| 81 | { |
| 82 | struct vnode *vp; |
| 83 | const char *emul_path; |
| 84 | |
| 85 | if (epp->ep_emul_root != NULL) |
| 86 | /* We've already found it */ |
| 87 | return; |
| 88 | |
| 89 | emul_path = epp->ep_esch->es_emul->e_path; |
| 90 | if (emul_path == NULL) |
| 91 | /* Emulation doesn't have a root */ |
| 92 | return; |
| 93 | |
| 94 | if (namei_simple_kernel(emul_path, NSM_FOLLOW_NOEMULROOT, &vp) != 0) |
| 95 | /* emulation root doesn't exist */ |
| 96 | return; |
| 97 | |
| 98 | epp->ep_emul_root = vp; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Search the alternate path for dynamic binary interpreter. If not found |
| 103 | * there, check if the interpreter exists in within 'proper' tree. |
| 104 | */ |
| 105 | int |
| 106 | emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp) |
| 107 | { |
| 108 | int error; |
| 109 | struct pathbuf *pb; |
| 110 | struct nameidata nd; |
| 111 | unsigned int flags; |
| 112 | |
| 113 | pb = pathbuf_create(itp); |
| 114 | if (pb == NULL) { |
| 115 | return ENOMEM; |
| 116 | } |
| 117 | |
| 118 | /* If we haven't found the emulation root already, do so now */ |
| 119 | /* Maybe we should remember failures somehow ? */ |
| 120 | if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL) |
| 121 | emul_find_root(l, epp); |
| 122 | |
| 123 | if (epp->ep_interp != NULL) |
| 124 | vrele(epp->ep_interp); |
| 125 | |
| 126 | /* We need to use the emulation root for the new program, |
| 127 | * not the one for the current process. */ |
| 128 | if (epp->ep_emul_root == NULL) |
| 129 | flags = FOLLOW; |
| 130 | else { |
| 131 | nd.ni_erootdir = epp->ep_emul_root; |
| 132 | /* hack: Pass in the emulation path for ktrace calls */ |
| 133 | nd.ni_next = epp->ep_esch->es_emul->e_path; |
| 134 | flags = FOLLOW | TRYEMULROOT | EMULROOTSET; |
| 135 | } |
| 136 | |
| 137 | NDINIT(&nd, LOOKUP, flags, pb); |
| 138 | error = namei(&nd); |
| 139 | if (error != 0) { |
| 140 | epp->ep_interp = NULL; |
| 141 | pathbuf_destroy(pb); |
| 142 | return error; |
| 143 | } |
| 144 | |
| 145 | /* Save interpreter in case we actually need to load it */ |
| 146 | epp->ep_interp = nd.ni_vp; |
| 147 | |
| 148 | pathbuf_destroy(pb); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Translate one set of flags to another, based on the entries in |
| 155 | * the given table. If 'leftover' is specified, it is filled in |
| 156 | * with any flags which could not be translated. |
| 157 | */ |
| 158 | unsigned long |
| 159 | emul_flags_translate(const struct emul_flags_xtab *tab, |
| 160 | unsigned long in, unsigned long *leftover) |
| 161 | { |
| 162 | unsigned long out; |
| 163 | |
| 164 | for (out = 0; tab->omask != 0; tab++) { |
| 165 | if ((in & tab->omask) == tab->oval) { |
| 166 | in &= ~tab->omask; |
| 167 | out |= tab->nval; |
| 168 | } |
| 169 | } |
| 170 | if (leftover != NULL) |
| 171 | *leftover = in; |
| 172 | return (out); |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | compat_offseterr(struct vnode *vp, const char *msg) |
| 177 | { |
| 178 | struct mount *mp; |
| 179 | |
| 180 | mp = vp->v_mount; |
| 181 | |
| 182 | log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n" , |
| 183 | msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname); |
| 184 | uprintf("%s: dir offset too large for emulated program\n" , msg); |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Look for native NetBSD compatibility libraries, usually interp-ABI. |
| 189 | * It returns 0 if it changed the interpreter, otherwise it returns |
| 190 | * the error from namei(). Callers should not try any more processing |
| 191 | * if this returns 0, and probably should just ignore the return value. |
| 192 | */ |
| 193 | int |
| 194 | compat_elf_check_interp(struct exec_package *epp, |
| 195 | char *interp, |
| 196 | const char *interp_suffix) |
| 197 | { |
| 198 | int error = 0; |
| 199 | |
| 200 | /* |
| 201 | * Don't look for something else, if someone has already found and |
| 202 | * setup the ep_interp already. |
| 203 | */ |
| 204 | if (interp && epp->ep_interp == NULL) { |
| 205 | /* |
| 206 | * If the path is exactly "/usr/libexec/ld.elf_so", first |
| 207 | * try to see if "/usr/libexec/ld.elf_so-<abi>" exists |
| 208 | * and if so, use that instead. |
| 209 | */ |
| 210 | if (strcmp(interp, "/usr/libexec/ld.elf_so" ) == 0 || |
| 211 | strcmp(interp, "/libexec/ld.elf_so" ) == 0) { |
| 212 | struct vnode *vp; |
| 213 | char *path; |
| 214 | |
| 215 | path = PNBUF_GET(); |
| 216 | snprintf(path, MAXPATHLEN, "%s-%s" , interp, interp_suffix); |
| 217 | error = namei_simple_kernel(path, |
| 218 | NSM_FOLLOW_NOEMULROOT, &vp); |
| 219 | /* |
| 220 | * If that worked, replace interpreter in case we |
| 221 | * actually need to load it. |
| 222 | */ |
| 223 | if (error == 0) { |
| 224 | epp->ep_interp = vp; |
| 225 | snprintf(interp, MAXPATHLEN, "%s" , path); |
| 226 | } |
| 227 | PNBUF_PUT(path); |
| 228 | } |
| 229 | } |
| 230 | return error; |
| 231 | } |
| 232 | |