1/*	$NetBSD: subr_emul.c,v 1.2 2019/01/27 02:08:43 pgoyette 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.2 2019/01/27 02:08:43 pgoyette Exp $");
61
62#ifdef _KERNEL_OPT
63#include "opt_pax.h"
64#endif /* _KERNEL_OPT */
65
66#include <sys/param.h>
67#include <sys/proc.h>
68#include <sys/vnode.h>
69#include <sys/namei.h>
70#include <sys/exec.h>
71
72#include <compat/common/compat_util.h>
73
74void
75emul_find_root(struct lwp *l, struct exec_package *epp)
76{
77	struct vnode *vp;
78	const char *emul_path;
79
80	if (epp->ep_emul_root != NULL)
81		/* We've already found it */
82		return;
83
84	emul_path = epp->ep_esch->es_emul->e_path;
85	if (emul_path == NULL)
86		/* Emulation doesn't have a root */
87		return;
88
89	if (namei_simple_kernel(emul_path, NSM_FOLLOW_NOEMULROOT, &vp) != 0)
90		/* emulation root doesn't exist */
91		return;
92
93	epp->ep_emul_root = vp;
94}
95
96/*
97 * Search the alternate path for dynamic binary interpreter. If not found
98 * there, check if the interpreter exists in within 'proper' tree.
99 */
100int
101emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp)
102{
103	int error;
104	struct pathbuf *pb;
105	struct nameidata nd;
106	unsigned int flags;
107
108	pb = pathbuf_create(itp);
109	if (pb == NULL) {
110		return ENOMEM;
111	}
112
113	/* If we haven't found the emulation root already, do so now */
114	/* Maybe we should remember failures somehow ? */
115	if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL)
116		emul_find_root(l, epp);
117
118	if (epp->ep_interp != NULL)
119		vrele(epp->ep_interp);
120
121	/* We need to use the emulation root for the new program,
122	 * not the one for the current process. */
123	if (epp->ep_emul_root == NULL)
124		flags = FOLLOW;
125	else {
126		nd.ni_erootdir = epp->ep_emul_root;
127		/* hack: Pass in the emulation path for ktrace calls */
128		nd.ni_next = epp->ep_esch->es_emul->e_path;
129		flags = FOLLOW | TRYEMULROOT | EMULROOTSET;
130	}
131
132	NDINIT(&nd, LOOKUP, flags, pb);
133	error = namei(&nd);
134	if (error != 0) {
135		epp->ep_interp = NULL;
136		pathbuf_destroy(pb);
137		return error;
138	}
139
140	/* Save interpreter in case we actually need to load it */
141	epp->ep_interp = nd.ni_vp;
142
143	pathbuf_destroy(pb);
144
145	return 0;
146}
147