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