Home | History | Annotate | Line # | Download | only in kern
subr_emul.c revision 1.2
      1  1.2  pgoyette /*	$NetBSD: subr_emul.c,v 1.2 2019/01/27 02:08:43 pgoyette Exp $	*/
      2  1.2  pgoyette 
      3  1.2  pgoyette /*-
      4  1.2  pgoyette  * Copyright (c) 1994, 2000, 2005, 2015 The NetBSD Foundation, Inc.
      5  1.2  pgoyette  * All rights reserved.
      6  1.2  pgoyette  *
      7  1.2  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  pgoyette  * by Christos Zoulas and Maxime Villard.
      9  1.2  pgoyette  *
     10  1.2  pgoyette  * Redistribution and use in source and binary forms, with or without
     11  1.2  pgoyette  * modification, are permitted provided that the following conditions
     12  1.2  pgoyette  * are met:
     13  1.2  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14  1.2  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15  1.2  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17  1.2  pgoyette  *    documentation and/or other materials provided with the distribution.
     18  1.2  pgoyette  *
     19  1.2  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2  pgoyette  */
     31  1.2  pgoyette 
     32  1.2  pgoyette /*
     33  1.2  pgoyette  * Copyright (c) 1996 Christopher G. Demetriou
     34  1.2  pgoyette  * All rights reserved.
     35  1.2  pgoyette  *
     36  1.2  pgoyette  * Redistribution and use in source and binary forms, with or without
     37  1.2  pgoyette  * modification, are permitted provided that the following conditions
     38  1.2  pgoyette  * are met:
     39  1.2  pgoyette  * 1. Redistributions of source code must retain the above copyright
     40  1.2  pgoyette  *    notice, this list of conditions and the following disclaimer.
     41  1.2  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.2  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     43  1.2  pgoyette  *    documentation and/or other materials provided with the distribution.
     44  1.2  pgoyette  * 3. The name of the author may not be used to endorse or promote products
     45  1.2  pgoyette  *    derived from this software without specific prior written permission
     46  1.2  pgoyette  *
     47  1.2  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  1.2  pgoyette  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  1.2  pgoyette  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  1.2  pgoyette  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  1.2  pgoyette  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  1.2  pgoyette  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  1.2  pgoyette  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  1.2  pgoyette  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  1.2  pgoyette  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  1.2  pgoyette  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  1.2  pgoyette  */
     58  1.2  pgoyette 
     59  1.2  pgoyette #include <sys/cdefs.h>
     60  1.2  pgoyette __KERNEL_RCSID(1, "$NetBSD: subr_emul.c,v 1.2 2019/01/27 02:08:43 pgoyette Exp $");
     61  1.2  pgoyette 
     62  1.2  pgoyette #ifdef _KERNEL_OPT
     63  1.2  pgoyette #include "opt_pax.h"
     64  1.2  pgoyette #endif /* _KERNEL_OPT */
     65  1.2  pgoyette 
     66  1.2  pgoyette #include <sys/param.h>
     67  1.2  pgoyette #include <sys/proc.h>
     68  1.2  pgoyette #include <sys/vnode.h>
     69  1.2  pgoyette #include <sys/namei.h>
     70  1.2  pgoyette #include <sys/exec.h>
     71  1.2  pgoyette 
     72  1.2  pgoyette #include <compat/common/compat_util.h>
     73  1.2  pgoyette 
     74  1.2  pgoyette void
     75  1.2  pgoyette emul_find_root(struct lwp *l, struct exec_package *epp)
     76  1.2  pgoyette {
     77  1.2  pgoyette 	struct vnode *vp;
     78  1.2  pgoyette 	const char *emul_path;
     79  1.2  pgoyette 
     80  1.2  pgoyette 	if (epp->ep_emul_root != NULL)
     81  1.2  pgoyette 		/* We've already found it */
     82  1.2  pgoyette 		return;
     83  1.2  pgoyette 
     84  1.2  pgoyette 	emul_path = epp->ep_esch->es_emul->e_path;
     85  1.2  pgoyette 	if (emul_path == NULL)
     86  1.2  pgoyette 		/* Emulation doesn't have a root */
     87  1.2  pgoyette 		return;
     88  1.2  pgoyette 
     89  1.2  pgoyette 	if (namei_simple_kernel(emul_path, NSM_FOLLOW_NOEMULROOT, &vp) != 0)
     90  1.2  pgoyette 		/* emulation root doesn't exist */
     91  1.2  pgoyette 		return;
     92  1.2  pgoyette 
     93  1.2  pgoyette 	epp->ep_emul_root = vp;
     94  1.2  pgoyette }
     95  1.2  pgoyette 
     96  1.2  pgoyette /*
     97  1.2  pgoyette  * Search the alternate path for dynamic binary interpreter. If not found
     98  1.2  pgoyette  * there, check if the interpreter exists in within 'proper' tree.
     99  1.2  pgoyette  */
    100  1.2  pgoyette int
    101  1.2  pgoyette emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp)
    102  1.2  pgoyette {
    103  1.2  pgoyette 	int error;
    104  1.2  pgoyette 	struct pathbuf *pb;
    105  1.2  pgoyette 	struct nameidata nd;
    106  1.2  pgoyette 	unsigned int flags;
    107  1.2  pgoyette 
    108  1.2  pgoyette 	pb = pathbuf_create(itp);
    109  1.2  pgoyette 	if (pb == NULL) {
    110  1.2  pgoyette 		return ENOMEM;
    111  1.2  pgoyette 	}
    112  1.2  pgoyette 
    113  1.2  pgoyette 	/* If we haven't found the emulation root already, do so now */
    114  1.2  pgoyette 	/* Maybe we should remember failures somehow ? */
    115  1.2  pgoyette 	if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL)
    116  1.2  pgoyette 		emul_find_root(l, epp);
    117  1.2  pgoyette 
    118  1.2  pgoyette 	if (epp->ep_interp != NULL)
    119  1.2  pgoyette 		vrele(epp->ep_interp);
    120  1.2  pgoyette 
    121  1.2  pgoyette 	/* We need to use the emulation root for the new program,
    122  1.2  pgoyette 	 * not the one for the current process. */
    123  1.2  pgoyette 	if (epp->ep_emul_root == NULL)
    124  1.2  pgoyette 		flags = FOLLOW;
    125  1.2  pgoyette 	else {
    126  1.2  pgoyette 		nd.ni_erootdir = epp->ep_emul_root;
    127  1.2  pgoyette 		/* hack: Pass in the emulation path for ktrace calls */
    128  1.2  pgoyette 		nd.ni_next = epp->ep_esch->es_emul->e_path;
    129  1.2  pgoyette 		flags = FOLLOW | TRYEMULROOT | EMULROOTSET;
    130  1.2  pgoyette 	}
    131  1.2  pgoyette 
    132  1.2  pgoyette 	NDINIT(&nd, LOOKUP, flags, pb);
    133  1.2  pgoyette 	error = namei(&nd);
    134  1.2  pgoyette 	if (error != 0) {
    135  1.2  pgoyette 		epp->ep_interp = NULL;
    136  1.2  pgoyette 		pathbuf_destroy(pb);
    137  1.2  pgoyette 		return error;
    138  1.2  pgoyette 	}
    139  1.2  pgoyette 
    140  1.2  pgoyette 	/* Save interpreter in case we actually need to load it */
    141  1.2  pgoyette 	epp->ep_interp = nd.ni_vp;
    142  1.2  pgoyette 
    143  1.2  pgoyette 	pathbuf_destroy(pb);
    144  1.2  pgoyette 
    145  1.2  pgoyette 	return 0;
    146  1.2  pgoyette }
    147