xref: /netbsd/sys/compat/common/compat_util.c (revision c4a72b64)
1 /* 	$NetBSD: compat_util.c,v 1.25 2002/11/29 17:08:06 jdolecek 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.25 2002/11/29 17:08:06 jdolecek 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(p, sgp, prefix, path, pbuf, sflag)
76 	struct proc	 *p;
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 nameidata	 nd;
84 	struct nameidata	 ndroot;
85 	struct vattr		 vat;
86 	struct vattr		 vatroot;
87 	int			 error;
88 	char			*ptr, *buf, *cp;
89 	const char		*pr;
90 	size_t			 sz, len;
91 
92 	buf = (char *)malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
93 	*pbuf = path;
94 
95 	for (ptr = buf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++)
96 		continue;
97 
98 	sz = MAXPATHLEN - (ptr - buf);
99 
100 	/*
101 	 * If sgp is not given then the path is already in kernel space
102 	 */
103 	if (sgp == NULL)
104 		error = copystr(path, ptr, sz, &len);
105 	else
106 		error = copyinstr(path, ptr, sz, &len);
107 
108 	if (error)
109 		goto bad;
110 
111 	if (*ptr != '/') {
112 		error = EINVAL;
113 		goto bad;
114 	}
115 
116 	/*
117 	 * We provide an escape method, so that the user can
118 	 * always specify the real root. If the path is prefixed
119 	 * by /../ we kill the alternate search
120 	 */
121 	if (ptr[1] == '.' && ptr[2] == '.' && ptr[3] == '/') {
122 		len -= 3;
123 		(void)memcpy(buf, &ptr[3], len);
124 		ptr = buf;
125 		goto good;
126 	}
127 
128 	/*
129 	 * We know that there is a / somewhere in this pathname.
130 	 * Search backwards for it, to find the file's parent dir
131 	 * to see if it exists in the alternate tree. If it does,
132 	 * and we want to create a file (sflag is set). We don't
133 	 * need to worry about the root comparison in this case.
134 	 */
135 
136 	switch (sflag) {
137 	case CHECK_ALT_FL_CREAT:
138 		for (cp = &ptr[len] - 1; *cp != '/'; cp--)
139 			;
140 		*cp = '\0';
141 
142 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
143 
144 		if ((error = namei(&nd)) != 0)
145 			goto bad;
146 
147 		*cp = '/';
148 		break;
149 	case CHECK_ALT_FL_EXISTS:
150 	case CHECK_ALT_FL_SYMLINK:
151 		NDINIT(&nd, LOOKUP,
152 			(sflag == CHECK_ALT_FL_SYMLINK) ? NOFOLLOW : FOLLOW,
153 			UIO_SYSSPACE, buf, p);
154 
155 		if ((error = namei(&nd)) != 0)
156 			goto bad;
157 
158 		/*
159 		 * We now compare the vnode of the emulation root to the one
160 		 * vnode asked. If they resolve to be the same, then we
161 		 * ignore the match so that the real root gets used.
162 		 * This avoids the problem of traversing "../.." to find the
163 		 * root directory and never finding it, because "/" resolves
164 		 * to the emulation root directory. This is expensive :-(
165 		 */
166 		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, p);
167 
168 		if ((error = namei(&ndroot)) != 0)
169 			goto bad2;
170 
171 		if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0)
172 			goto bad3;
173 
174 		if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
175 		    != 0)
176 			goto bad3;
177 
178 		if (vat.va_fsid == vatroot.va_fsid &&
179 		    vat.va_fileid == vatroot.va_fileid) {
180 			error = ENOENT;
181 			goto bad3;
182 		}
183 
184 		break;
185 	}
186 
187 	vrele(nd.ni_vp);
188 	if (sflag == CHECK_ALT_FL_EXISTS)
189 		vrele(ndroot.ni_vp);
190 
191 good:
192 	if (sgp == NULL)
193 		*pbuf = buf;
194 	else {
195 		sz = &ptr[len] - buf;
196 		*pbuf = stackgap_alloc(p, sgp, sz + 1);
197 		if (*pbuf == NULL) {
198 			error = ENAMETOOLONG;
199 			goto bad;
200 		}
201 		if ((error = copyout(buf, (void *)*pbuf, sz)) != 0) {
202 			*pbuf = path;
203 			goto bad;
204 		}
205 		free(buf, M_TEMP);
206 	}
207 	return 0;
208 
209 bad3:
210 	vrele(ndroot.ni_vp);
211 bad2:
212 	vrele(nd.ni_vp);
213 bad:
214 	free(buf, M_TEMP);
215 	return error;
216 }
217 
218 /*
219  * Search the alternate path for dynamic binary interpreter. If not found
220  * there, check if the interpreter exists in within 'proper' tree.
221  */
222 int
223 emul_find_interp(struct proc *p, const char *prefix, char *itp)
224 {
225 	const char *bp;
226 	int error;
227 
228 	if (emul_find(p, NULL, prefix, itp, &bp, CHECK_ALT_FL_EXISTS) == 0) {
229 		size_t len;
230 
231 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
232 			return error;
233 		free((void *)bp, M_TEMP);
234 	} else {
235 		/* check filename without the emul prefix */
236 		struct nameidata nd;
237 
238 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, itp, p);
239 
240 		if ((error = namei(&nd)))
241 			return error;
242 
243 		vrele(nd.ni_vp);
244 	}
245 
246 	return (0);
247 }
248 
249 /*
250  * Translate one set of flags to another, based on the entries in
251  * the given table.  If 'leftover' is specified, it is filled in
252  * with any flags which could not be translated.
253  */
254 unsigned long
255 emul_flags_translate(const struct emul_flags_xtab *tab,
256 		     unsigned long in, unsigned long *leftover)
257 {
258 	unsigned long out;
259 
260 	for (out = 0; tab->omask != 0; tab++) {
261 		if ((in & tab->omask) == tab->oval) {
262 			in &= ~tab->omask;
263 			out |= tab->nval;
264 		}
265 	}
266 	if (leftover != NULL)
267 		*leftover = in;
268 	return (out);
269 }
270 
271 caddr_t
272 stackgap_init(p, sz)
273 	const struct proc *p;
274 	size_t sz;
275 {
276 	if (sz == 0)
277 		sz = STACKGAPLEN;
278 	if (sz > STACKGAPLEN)
279 		panic("size %lu > STACKGAPLEN", (unsigned long)sz);
280 #define szsigcode ((caddr_t)(p->p_emul->e_esigcode - p->p_emul->e_sigcode))
281 	return (caddr_t)(((unsigned long)p->p_psstr - (unsigned long)szsigcode
282 		- sz) & ~ALIGNBYTES);
283 #undef szsigcode
284 }
285 
286 
287 void *
288 stackgap_alloc(p, sgp, sz)
289 	const struct proc *p;
290 	caddr_t *sgp;
291 	size_t sz;
292 {
293 	void *n = (void *) *sgp;
294 	caddr_t nsgp;
295 	const struct emul *e = p->p_emul;
296 	int sigsize = e->e_esigcode - e->e_sigcode;
297 
298 	sz = ALIGN(sz);
299 	nsgp = *sgp + sz;
300 	if (nsgp > (((caddr_t)p->p_psstr) - sigsize))
301 		return NULL;
302 	*sgp = nsgp;
303 	return n;
304 }
305 
306 void
307 compat_offseterr(vp, msg)
308 	struct vnode *vp;
309 	char *msg;
310 {
311 	struct mount *mp;
312 
313 	mp = vp->v_mount;
314 
315 	log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
316 	    msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
317 	uprintf("%s: dir offset too large for emulated program\n", msg);
318 }
319