Home | History | Annotate | Line # | Download | only in common
compat_util.c revision 1.20.2.3
      1 /* 	$NetBSD: compat_util.c,v 1.20.2.3 2002/04/01 07:43:49 nathanw 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.20.2.3 2002/04/01 07:43:49 nathanw Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/namei.h>
     45 #include <sys/lwp.h>
     46 #include <sys/proc.h>
     47 #include <sys/file.h>
     48 #include <sys/stat.h>
     49 #include <sys/filedesc.h>
     50 #include <sys/exec.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/kernel.h>
     53 #include <sys/malloc.h>
     54 #include <sys/vnode.h>
     55 #include <sys/syslog.h>
     56 #include <sys/mount.h>
     57 
     58 
     59 #include <compat/common/compat_util.h>
     60 
     61 /*
     62  * Search an alternate path before passing pathname arguments on
     63  * to system calls. Useful for keeping a separate 'emulation tree'.
     64  *
     65  * According to sflag, we either check for existance of the file or if
     66  * it can be created or if the file is symlink.
     67  *
     68  * In case of success, emul_find returns 0:
     69  * 	If sgp is provided, the path is in user space, and pbuf gets
     70  *	allocated in user space (in the stackgap). Otherwise the path
     71  *	is already in kernel space and a kernel buffer gets allocated
     72  *	and returned in pbuf, that must be freed by the user.
     73  * In case of error, the error number is returned and *pbuf = path.
     74  */
     75 int
     76 emul_find(p, sgp, prefix, path, pbuf, sflag)
     77 	struct proc	 *p;
     78 	caddr_t		 *sgp;		/* Pointer to stackgap memory */
     79 	const char	 *prefix;
     80 	const char	 *path;
     81 	const char	**pbuf;
     82 	int		  sflag;
     83 {
     84 	struct nameidata	 nd;
     85 	struct nameidata	 ndroot;
     86 	struct vattr		 vat;
     87 	struct vattr		 vatroot;
     88 	int			 error;
     89 	char			*ptr, *buf, *cp;
     90 	const char		*pr;
     91 	size_t			 sz, len;
     92 
     93 	buf = (char *)malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
     94 	*pbuf = path;
     95 
     96 	for (ptr = buf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++)
     97 		continue;
     98 
     99 	sz = MAXPATHLEN - (ptr - buf);
    100 
    101 	/*
    102 	 * If sgp is not given then the path is already in kernel space
    103 	 */
    104 	if (sgp == NULL)
    105 		error = copystr(path, ptr, sz, &len);
    106 	else
    107 		error = copyinstr(path, ptr, sz, &len);
    108 
    109 	if (error)
    110 		goto bad;
    111 
    112 	if (*ptr != '/') {
    113 		error = EINVAL;
    114 		goto bad;
    115 	}
    116 
    117 	/*
    118 	 * We provide an escape method, so that the user can
    119 	 * always specify the real root. If the path is prefixed
    120 	 * by /../ we kill the alternate search
    121 	 */
    122 	if (ptr[1] == '.' && ptr[2] == '.' && ptr[3] == '/') {
    123 		len -= 3;
    124 		(void)memcpy(buf, &ptr[3], len);
    125 		ptr = buf;
    126 		goto good;
    127 	}
    128 
    129 	/*
    130 	 * We know that there is a / somewhere in this pathname.
    131 	 * Search backwards for it, to find the file's parent dir
    132 	 * to see if it exists in the alternate tree. If it does,
    133 	 * and we want to create a file (sflag is set). We don't
    134 	 * need to worry about the root comparison in this case.
    135 	 */
    136 
    137 	switch (sflag) {
    138 	case CHECK_ALT_FL_CREAT:
    139 		for (cp = &ptr[len] - 1; *cp != '/'; cp--)
    140 			;
    141 		*cp = '\0';
    142 
    143 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
    144 
    145 		if ((error = namei(&nd)) != 0)
    146 			goto bad;
    147 
    148 		*cp = '/';
    149 		break;
    150 	case CHECK_ALT_FL_EXISTS:
    151 	case CHECK_ALT_FL_SYMLINK:
    152 		NDINIT(&nd, LOOKUP,
    153 			(sflag == CHECK_ALT_FL_SYMLINK) ? NOFOLLOW : FOLLOW,
    154 			UIO_SYSSPACE, buf, p);
    155 
    156 		if ((error = namei(&nd)) != 0)
    157 			goto bad;
    158 
    159 		/*
    160 		 * We now compare the vnode of the emulation root to the one
    161 		 * vnode asked. If they resolve to be the same, then we
    162 		 * ignore the match so that the real root gets used.
    163 		 * This avoids the problem of traversing "../.." to find the
    164 		 * root directory and never finding it, because "/" resolves
    165 		 * to the emulation root directory. This is expensive :-(
    166 		 */
    167 		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, p);
    168 
    169 		if ((error = namei(&ndroot)) != 0)
    170 			goto bad2;
    171 
    172 		if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0)
    173 			goto bad3;
    174 
    175 		if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
    176 		    != 0)
    177 			goto bad3;
    178 
    179 		if (vat.va_fsid == vatroot.va_fsid &&
    180 		    vat.va_fileid == vatroot.va_fileid) {
    181 			error = ENOENT;
    182 			goto bad3;
    183 		}
    184 
    185 		break;
    186 	}
    187 
    188 	vrele(nd.ni_vp);
    189 	if (sflag == CHECK_ALT_FL_EXISTS)
    190 		vrele(ndroot.ni_vp);
    191 
    192 good:
    193 	if (sgp == NULL)
    194 		*pbuf = buf;
    195 	else {
    196 		sz = &ptr[len] - buf;
    197 		*pbuf = stackgap_alloc(p, sgp, sz + 1);
    198 		if (*pbuf == NULL) {
    199 			error = ENAMETOOLONG;
    200 			goto bad;
    201 		}
    202 		if ((error = copyout(buf, (void *)*pbuf, sz)) != 0) {
    203 			*pbuf = path;
    204 			goto bad;
    205 		}
    206 		free(buf, M_TEMP);
    207 	}
    208 	return 0;
    209 
    210 bad3:
    211 	vrele(ndroot.ni_vp);
    212 bad2:
    213 	vrele(nd.ni_vp);
    214 bad:
    215 	free(buf, M_TEMP);
    216 	return error;
    217 }
    218 
    219 /*
    220  * Translate one set of flags to another, based on the entries in
    221  * the given table.  If 'leftover' is specified, it is filled in
    222  * with any flags which could not be translated.
    223  */
    224 unsigned long
    225 emul_flags_translate(const struct emul_flags_xtab *tab,
    226 		     unsigned long in, unsigned long *leftover)
    227 {
    228 	unsigned long out;
    229 
    230 	for (out = 0; tab->omask != 0; tab++) {
    231 		if ((in & tab->omask) == tab->oval) {
    232 			in &= ~tab->omask;
    233 			out |= tab->nval;
    234 		}
    235 	}
    236 	if (leftover != NULL)
    237 		*leftover = in;
    238 	return (out);
    239 }
    240 
    241 caddr_t
    242 stackgap_init(p, sz)
    243 	const struct proc *p;
    244 	size_t sz;
    245 {
    246 	if (sz == 0)
    247 		sz = STACKGAPLEN;
    248 	if (sz > STACKGAPLEN)
    249 		panic("size %lu > STACKGAPLEN", (unsigned long)sz);
    250 #define szsigcode ((caddr_t)(p->p_emul->e_esigcode - p->p_emul->e_sigcode))
    251 	return (caddr_t)(((unsigned long)p->p_psstr - (unsigned long)szsigcode
    252 		- sz) & ~ALIGNBYTES);
    253 #undef szsigcode
    254 }
    255 
    256 
    257 void *
    258 stackgap_alloc(p, sgp, sz)
    259 	const struct proc *p;
    260 	caddr_t *sgp;
    261 	size_t sz;
    262 {
    263 	void *n = (void *) *sgp;
    264 	caddr_t nsgp;
    265 	const struct emul *e = p->p_emul;
    266 	int sigsize = e->e_esigcode - e->e_sigcode;
    267 
    268 	sz = ALIGN(sz);
    269 	nsgp = *sgp + sz;
    270 	if (nsgp > (((const caddr_t)p->p_psstr) - sigsize))
    271 		return NULL;
    272 	*sgp = nsgp;
    273 	return n;
    274 }
    275 
    276 void
    277 compat_offseterr(vp, msg)
    278 	struct vnode *vp;
    279 	char *msg;
    280 {
    281 	struct mount *mp;
    282 
    283 	mp = vp->v_mount;
    284 
    285 	log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
    286 	    msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
    287 	uprintf("%s: dir offset too large for emulated program\n", msg);
    288 }
    289