Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: subr_emul.c,v 1.4 2026/01/04 03:17:08 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1994, 2000, 2005, 2015 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 Maxime Villard.
      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) 1996 Christopher G. Demetriou
     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, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  */
     58 
     59 #include <sys/cdefs.h>
     60 __KERNEL_RCSID(1, "$NetBSD: subr_emul.c,v 1.4 2026/01/04 03:17:08 riastradh Exp $");
     61 
     62 #ifdef _KERNEL_OPT
     63 #include "opt_pax.h"
     64 #endif /* _KERNEL_OPT */
     65 
     66 #include <sys/param.h>
     67 
     68 #include <sys/exec.h>
     69 #include <sys/namei.h>
     70 #include <sys/proc.h>
     71 #include <sys/sdt.h>
     72 #include <sys/vnode.h>
     73 
     74 #include <compat/common/compat_util.h>
     75 
     76 void
     77 emul_find_root(struct lwp *l, struct exec_package *epp)
     78 {
     79 	struct vnode *vp;
     80 	const char *emul_path;
     81 
     82 	if (epp->ep_emul_root != NULL)
     83 		/* We've already found it */
     84 		return;
     85 
     86 	emul_path = epp->ep_esch->es_emul->e_path;
     87 	if (emul_path == NULL)
     88 		/* Emulation doesn't have a root */
     89 		return;
     90 
     91 	if (namei_simple_kernel(emul_path, NSM_FOLLOW_NOEMULROOT, &vp) != 0)
     92 		/* emulation root doesn't exist */
     93 		return;
     94 
     95 	epp->ep_emul_root = vp;
     96 }
     97 
     98 /*
     99  * Search the alternate path for dynamic binary interpreter. If not found
    100  * there, check if the interpreter exists in within 'proper' tree.
    101  */
    102 int
    103 emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp)
    104 {
    105 	int error;
    106 	struct pathbuf *pb;
    107 	struct nameidata nd;
    108 	unsigned int flags;
    109 
    110 	pb = pathbuf_create(itp);
    111 	if (pb == NULL) {
    112 		return SET_ERROR(ENOMEM);
    113 	}
    114 
    115 	/* If we haven't found the emulation root already, do so now */
    116 	/* Maybe we should remember failures somehow ? */
    117 	if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL)
    118 		emul_find_root(l, epp);
    119 
    120 	if (epp->ep_interp != NULL)
    121 		vrele(epp->ep_interp);
    122 
    123 	/* We need to use the emulation root for the new program,
    124 	 * not the one for the current process. */
    125 	if (epp->ep_emul_root == NULL)
    126 		flags = FOLLOW;
    127 	else {
    128 		nd.ni_erootdir = epp->ep_emul_root;
    129 		/* hack: Pass in the emulation path for ktrace calls */
    130 		nd.ni_next = epp->ep_esch->es_emul->e_path;
    131 		flags = FOLLOW | TRYEMULROOT | EMULROOTSET;
    132 	}
    133 
    134 	NDINIT(&nd, LOOKUP, flags, pb);
    135 	error = namei(&nd);
    136 	if (error != 0) {
    137 		epp->ep_interp = NULL;
    138 		pathbuf_destroy(pb);
    139 		return error;
    140 	}
    141 
    142 	/* Save interpreter in case we actually need to load it */
    143 	epp->ep_interp = nd.ni_vp;
    144 
    145 	pathbuf_destroy(pb);
    146 
    147 	return 0;
    148 }
    149