xref: /netbsd-src/sys/compat/common/compat_util.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /* 	$NetBSD: compat_util.c,v 1.30 2005/12/11 12:19:56 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994 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 Frank van der Linden.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.30 2005/12/11 12:19:56 christos Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
45 #include <sys/proc.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/filedesc.h>
49 #include <sys/exec.h>
50 #include <sys/ioctl.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/vnode.h>
54 #include <sys/syslog.h>
55 #include <sys/mount.h>
56 
57 
58 #include <compat/common/compat_util.h>
59 
60 /*
61  * Search an alternate path before passing pathname arguments on
62  * to system calls. Useful for keeping a separate 'emulation tree'.
63  *
64  * According to sflag, we either check for existance of the file or if
65  * it can be created or if the file is symlink.
66  *
67  * In case of success, emul_find returns 0:
68  * 	If sgp is provided, the path is in user space, and pbuf gets
69  *	allocated in user space (in the stackgap). Otherwise the path
70  *	is already in kernel space and a kernel buffer gets allocated
71  *	and returned in pbuf, that must be freed by the user.
72  * In case of error, the error number is returned and *pbuf = path.
73  */
74 int
75 emul_find(l, sgp, prefix, path, pbuf, sflag)
76 	struct lwp *l;
77 	caddr_t		 *sgp;		/* Pointer to stackgap memory */
78 	const char	 *prefix;
79 	const char	 *path;
80 	const char	**pbuf;
81 	int		  sflag;
82 {
83 	struct proc *p;
84 	struct nameidata	 nd;
85 	struct nameidata	 ndroot;
86 	struct vattr		 vat;
87 	struct vattr		 vatroot;
88 	int			 error;
89 	char			*ptr, *tbuf, *cp;
90 	const char		*pr;
91 	size_t			 sz, len;
92 
93 	p = l->l_proc;
94 	tbuf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
95 	*pbuf = path;
96 
97 	for (ptr = tbuf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++)
98 		continue;
99 
100 	sz = MAXPATHLEN - (ptr - tbuf);
101 
102 	/*
103 	 * If sgp is not given then the path is already in kernel space
104 	 */
105 	if (sgp == NULL)
106 		error = copystr(path, ptr, sz, &len);
107 	else
108 		error = copyinstr(path, ptr, sz, &len);
109 
110 	if (error)
111 		goto bad;
112 
113 	if (*ptr != '/') {
114 		error = EINVAL;
115 		goto bad;
116 	}
117 
118 	/*
119 	 * We provide an escape method, so that the user can
120 	 * always specify the real root. If the path is prefixed
121 	 * by /../ we kill the alternate search
122 	 */
123 	if (ptr[1] == '.' && ptr[2] == '.' && ptr[3] == '/') {
124 		len -= 3;
125 		(void)memcpy(tbuf, &ptr[3], len);
126 		ptr = tbuf;
127 		goto good;
128 	}
129 
130 	/*
131 	 * We know that there is a / somewhere in this pathname.
132 	 * Search backwards for it, to find the file's parent dir
133 	 * to see if it exists in the alternate tree. If it does,
134 	 * and we want to create a file (sflag is set). We don't
135 	 * need to worry about the root comparison in this case.
136 	 */
137 
138 	switch (sflag) {
139 	case CHECK_ALT_FL_CREAT:
140 		for (cp = &ptr[len] - 1; *cp != '/'; cp--)
141 			;
142 		*cp = '\0';
143 
144 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, tbuf, l);
145 
146 		if ((error = namei(&nd)) != 0)
147 			goto bad;
148 
149 		*cp = '/';
150 		break;
151 	case CHECK_ALT_FL_EXISTS:
152 	case CHECK_ALT_FL_SYMLINK:
153 		NDINIT(&nd, LOOKUP,
154 			(sflag == CHECK_ALT_FL_SYMLINK) ? NOFOLLOW : FOLLOW,
155 			UIO_SYSSPACE, tbuf, l);
156 
157 		if ((error = namei(&nd)) != 0)
158 			goto bad;
159 
160 		/*
161 		 * We now compare the vnode of the emulation root to the one
162 		 * vnode asked. If they resolve to be the same, then we
163 		 * ignore the match so that the real root gets used.
164 		 * This avoids the problem of traversing "../.." to find the
165 		 * root directory and never finding it, because "/" resolves
166 		 * to the emulation root directory. This is expensive :-(
167 		 */
168 		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, l);
169 
170 		if ((error = namei(&ndroot)) != 0)
171 			goto bad2;
172 
173 		if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, l)) != 0)
174 			goto bad3;
175 
176 		if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, l))
177 		    != 0)
178 			goto bad3;
179 
180 		if (vat.va_fsid == vatroot.va_fsid &&
181 		    vat.va_fileid == vatroot.va_fileid) {
182 			error = ENOENT;
183 			goto bad3;
184 		}
185 
186 		break;
187 	}
188 
189 	vrele(nd.ni_vp);
190 	if (sflag == CHECK_ALT_FL_EXISTS)
191 		vrele(ndroot.ni_vp);
192 
193 good:
194 	if (sgp == NULL)
195 		*pbuf = tbuf;
196 	else {
197 		sz = &ptr[len] - tbuf;
198 		*pbuf = stackgap_alloc(p, sgp, sz + 1);
199 		if (*pbuf == NULL) {
200 			error = ENAMETOOLONG;
201 			goto bad;
202 		}
203 		/*XXXUNCONST*/
204 		if ((error = copyout(tbuf, __UNCONST(*pbuf), sz)) != 0) {
205 			*pbuf = path;
206 			goto bad;
207 		}
208 		free(tbuf, M_TEMP);
209 	}
210 	return 0;
211 
212 bad3:
213 	vrele(ndroot.ni_vp);
214 bad2:
215 	vrele(nd.ni_vp);
216 bad:
217 	free(tbuf, M_TEMP);
218 	return error;
219 }
220 
221 /*
222  * Search the alternate path for dynamic binary interpreter. If not found
223  * there, check if the interpreter exists in within 'proper' tree.
224  */
225 int
226 emul_find_interp(struct lwp *l, const char *prefix, char *itp)
227 {
228 	const char *bp;
229 	int error;
230 
231 	if (emul_find(l, NULL, prefix, itp, &bp, CHECK_ALT_FL_EXISTS) == 0) {
232 		size_t len;
233 
234 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
235 			return error;
236 		/*XXXUNCONST*/
237 		free(__UNCONST(bp), M_TEMP);
238 	} else {
239 		/* check filename without the emul prefix */
240 		struct nameidata nd;
241 
242 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, itp, l);
243 
244 		if ((error = namei(&nd)))
245 			return error;
246 
247 		vrele(nd.ni_vp);
248 	}
249 
250 	return (0);
251 }
252 
253 /*
254  * Translate one set of flags to another, based on the entries in
255  * the given table.  If 'leftover' is specified, it is filled in
256  * with any flags which could not be translated.
257  */
258 unsigned long
259 emul_flags_translate(const struct emul_flags_xtab *tab,
260 		     unsigned long in, unsigned long *leftover)
261 {
262 	unsigned long out;
263 
264 	for (out = 0; tab->omask != 0; tab++) {
265 		if ((in & tab->omask) == tab->oval) {
266 			in &= ~tab->omask;
267 			out |= tab->nval;
268 		}
269 	}
270 	if (leftover != NULL)
271 		*leftover = in;
272 	return (out);
273 }
274 
275 caddr_t
276 stackgap_init(p, sz)
277 	const struct proc *p;
278 	size_t sz;
279 {
280 	if (sz == 0)
281 		sz = STACKGAPLEN;
282 	if (sz > STACKGAPLEN)
283 		panic("size %lu > STACKGAPLEN", (unsigned long)sz);
284 #define szsigcode ((caddr_t)(p->p_emul->e_esigcode - p->p_emul->e_sigcode))
285 	return (caddr_t)(((unsigned long)p->p_psstr - (unsigned long)szsigcode
286 		- sz) & ~ALIGNBYTES);
287 #undef szsigcode
288 }
289 
290 
291 void *
292 stackgap_alloc(p, sgp, sz)
293 	const struct proc *p;
294 	caddr_t *sgp;
295 	size_t sz;
296 {
297 	void *n = (void *) *sgp;
298 	caddr_t nsgp;
299 	const struct emul *e = p->p_emul;
300 	int sigsize = e->e_esigcode - e->e_sigcode;
301 
302 	sz = ALIGN(sz);
303 	nsgp = *sgp + sz;
304 	if (nsgp > (((caddr_t)p->p_psstr) - sigsize))
305 		return NULL;
306 	*sgp = nsgp;
307 	return n;
308 }
309 
310 void
311 compat_offseterr(vp, msg)
312 	struct vnode *vp;
313 	const char *msg;
314 {
315 	struct mount *mp;
316 
317 	mp = vp->v_mount;
318 
319 	log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
320 	    msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
321 	uprintf("%s: dir offset too large for emulated program\n", msg);
322 }
323