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