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