Home | History | Annotate | Line # | Download | only in netbsd32
      1 /*	$NetBSD: netbsd32_compat_30.c,v 1.36 2021/01/19 03:20:13 simonb Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_30.c,v 1.36 2021/01/19 03:20:13 simonb Exp $");
     31 
     32 #if defined(_KERNEL_OPT)
     33 #include <opt_ntp.h>
     34 #endif
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/module.h>
     39 #include <sys/mount.h>
     40 #include <sys/mount.h>
     41 #include <sys/socket.h>
     42 #include <sys/socketvar.h>
     43 #include <sys/stat.h>
     44 #include <sys/time.h>
     45 #include <sys/ktrace.h>
     46 #include <sys/resourcevar.h>
     47 #include <sys/vnode.h>
     48 #include <sys/file.h>
     49 #include <sys/filedesc.h>
     50 #include <sys/namei.h>
     51 #include <sys/statvfs.h>
     52 #include <sys/syscallargs.h>
     53 #include <sys/syscallvar.h>
     54 #include <sys/proc.h>
     55 #include <sys/dirent.h>
     56 #include <sys/kauth.h>
     57 #include <sys/vfs_syscalls.h>
     58 #include <sys/compat_stub.h>
     59 
     60 #include <compat/netbsd32/netbsd32.h>
     61 #include <compat/netbsd32/netbsd32_syscall.h>
     62 #include <compat/netbsd32/netbsd32_syscallargs.h>
     63 #include <compat/netbsd32/netbsd32_conv.h>
     64 #include <compat/sys/mount.h>
     65 
     66 int
     67 compat_30_netbsd32_getdents(struct lwp *l, const struct compat_30_netbsd32_getdents_args *uap, register_t *retval)
     68 {
     69 	/* {
     70 		syscallarg(int) fd;
     71 		syscallarg(netbsd32_charp) buf;
     72 		syscallarg(netbsd32_size_t) count;
     73 	} */
     74 	file_t *fp;
     75 	int error, done;
     76 	char  *buf;
     77 	netbsd32_size_t count;
     78 
     79 	/* Limit the size on any kernel buffers used by VOP_READDIR */
     80 	count = uimin(MAXBSIZE, SCARG(uap, count));
     81 
     82 	/* fd_getvnode() will use the descriptor for us */
     83 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
     84 		return error;
     85 	if ((fp->f_flag & FREAD) == 0) {
     86 		error = EBADF;
     87 		goto out;
     88 	}
     89 	if (count == 0)
     90 		goto out;
     91 
     92 	buf = kmem_alloc(count, KM_SLEEP);
     93 	error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0);
     94 	if (error == 0) {
     95 		*retval = netbsd32_to_dirent12(buf, done);
     96 		error = copyout(buf, SCARG_P32(uap, buf), *retval);
     97 	}
     98 	kmem_free(buf, count);
     99  out:
    100  	fd_putfile(SCARG(uap, fd));
    101 	return error;
    102 }
    103 
    104 int
    105 compat_30_netbsd32___stat13(struct lwp *l, const struct compat_30_netbsd32___stat13_args *uap, register_t *retval)
    106 {
    107 	/* {
    108 		syscallarg(const netbsd32_charp) path;
    109 		syscallarg(netbsd32_stat13p_t) ub;
    110 	} */
    111 	struct netbsd32_stat13 sb32;
    112 	struct stat sb;
    113 	int error;
    114 	const char *path;
    115 
    116 	path = SCARG_P32(uap, path);
    117 
    118 	error = do_sys_stat(path, FOLLOW, &sb);
    119 	if (error)
    120 		return error;
    121 	netbsd32_from___stat13(&sb, &sb32);
    122 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    123 	return error;
    124 }
    125 
    126 int
    127 compat_30_netbsd32___fstat13(struct lwp *l, const struct compat_30_netbsd32___fstat13_args *uap, register_t *retval)
    128 {
    129 	/* {
    130 		syscallarg(int) fd;
    131 		syscallarg(netbsd32_stat13p_t) sb;
    132 	} */
    133 	struct netbsd32_stat13 sb32;
    134 	struct stat ub;
    135 	int error;
    136 
    137 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    138 	if (error == 0) {
    139 		netbsd32_from___stat13(&ub, &sb32);
    140 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    141 	}
    142 	return error;
    143 }
    144 
    145 int
    146 compat_30_netbsd32___lstat13(struct lwp *l, const struct compat_30_netbsd32___lstat13_args *uap, register_t *retval)
    147 {
    148 	/* {
    149 		syscallarg(const netbsd32_charp) path;
    150 		syscallarg(netbsd32_stat13p_t) ub;
    151 	} */
    152 	struct netbsd32_stat13 sb32;
    153 	struct stat sb;
    154 	int error;
    155 	const char *path;
    156 
    157 	path = SCARG_P32(uap, path);
    158 
    159 	error = do_sys_stat(path, NOFOLLOW, &sb);
    160 	if (error)
    161 		return error;
    162 	netbsd32_from___stat13(&sb, &sb32);
    163 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    164 	return error;
    165 }
    166 
    167 int
    168 compat_30_netbsd32_fhstat(struct lwp *l, const struct compat_30_netbsd32_fhstat_args *uap, register_t *retval)
    169 {
    170 	/* {
    171 		syscallarg(const netbsd32_fhandlep_t) fhp;
    172 		syscallarg(netbsd32_stat13p_t) sb;
    173 	} */
    174 	struct stat sb;
    175 	struct netbsd32_stat13 sb32;
    176 	int error;
    177 	struct compat_30_fhandle fh;
    178 	struct mount *mp;
    179 	struct vnode *vp;
    180 
    181 	/*
    182 	 * Must be super user
    183 	 */
    184 	if ((error = kauth_authorize_system(l->l_cred,
    185 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
    186 		return error;
    187 
    188 	if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
    189 		return error;
    190 
    191 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
    192 		return ESTALE;
    193 	if (mp->mnt_op->vfs_fhtovp == NULL)
    194 		return EOPNOTSUPP;
    195 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, LK_EXCLUSIVE, &vp)))
    196 		return error;
    197 	error = vn_stat(vp, &sb);
    198 	vput(vp);
    199 	if (error)
    200 		return error;
    201 	netbsd32_from___stat13(&sb, &sb32);
    202 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    203 	return error;
    204 }
    205 
    206 int
    207 compat_30_netbsd32_fhstatvfs1(struct lwp *l, const struct compat_30_netbsd32_fhstatvfs1_args *uap, register_t *retval)
    208 {
    209 	/* {
    210 		syscallarg(const netbsd32_fhandlep_t) fhp;
    211 		syscallarg(netbsd32_statvfsp_t) buf;
    212 		syscallarg(int) flags;
    213 	} */
    214 	struct statvfs *sbuf;
    215 	struct netbsd32_statvfs *s32;
    216 	int error;
    217 
    218 	sbuf = STATVFSBUF_GET();
    219 	error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
    220 	    SCARG(uap, flags));
    221 
    222 	if (error != 0) {
    223 		s32 = kmem_alloc(sizeof(*s32), KM_SLEEP);
    224 		netbsd32_from_statvfs(sbuf, s32);
    225 		error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
    226 		kmem_free(s32, sizeof(*s32));
    227 	}
    228 	STATVFSBUF_PUT(sbuf);
    229 
    230 	return error;
    231 }
    232 
    233 int
    234 compat_30_netbsd32_socket(struct lwp *l, const struct compat_30_netbsd32_socket_args *uap, register_t *retval)
    235 {
    236 	/* {
    237 		syscallarg(int) domain;
    238 		syscallarg(int) type;
    239 		syscallarg(int) protocol;
    240 	} */
    241 	struct compat_30_sys_socket_args ua;
    242 
    243 	NETBSD32TO64_UAP(domain);
    244 	NETBSD32TO64_UAP(type);
    245 	NETBSD32TO64_UAP(protocol);
    246 	return compat_30_sys_socket(l, &ua, retval);
    247 }
    248 
    249 int
    250 compat_30_netbsd32_getfh(struct lwp *l, const struct compat_30_netbsd32_getfh_args *uap, register_t *retval)
    251 {
    252 	/* {
    253 		syscallarg(const netbsd32_charp) fname;
    254 		syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
    255 	} */
    256 	struct compat_30_sys_getfh_args ua;
    257 
    258 	NETBSD32TOP_UAP(fname, const char);
    259 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
    260 	/* Lucky for us a fhandle_t doesn't change sizes */
    261 	return compat_30_sys_getfh(l, &ua, retval);
    262 }
    263 
    264 
    265 int
    266 compat_30_netbsd32___fhstat30(struct lwp *l, const struct compat_30_netbsd32___fhstat30_args *uap, register_t *retval)
    267 {
    268 	/* {
    269 		syscallarg(const netbsd32_fhandlep_t) fhp;
    270 		syscallarg(netbsd32_statp_t) sb;
    271 	} */
    272 	struct stat sb;
    273 	struct netbsd32_stat50 sb32;
    274 	int error;
    275 
    276 	error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
    277 	if (error)
    278 		return error;
    279 
    280 	netbsd32_from___stat50(&sb, &sb32);
    281 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    282 	return error;
    283 }
    284 
    285 /*
    286  * Open a file given a file handle.
    287  *
    288  * Check permissions, allocate an open file structure,
    289  * and call the device open routine if any.
    290  */
    291 int
    292 compat_30_netbsd32_fhopen(struct lwp *l, const struct compat_30_netbsd32_fhopen_args *uap, register_t *retval)
    293 {
    294 	/* {
    295 		syscallarg(const fhandle_t *) fhp;
    296 		syscallarg(int) flags;
    297 	} */
    298 	struct compat_30_sys_fhopen_args ua;
    299 
    300 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
    301 	NETBSD32TO64_UAP(flags);
    302 	return compat_30_sys_fhopen(l, &ua, retval);
    303 }
    304 
    305 #ifdef NTP
    306 int
    307 compat_30_netbsd32_ntp_gettime(struct lwp *l, const struct compat_30_netbsd32_ntp_gettime_args *uap, register_t *retval)
    308 {
    309 	/* {
    310 		syscallarg(netbsd32_ntptimevalp_t) ntvp;
    311 	} */
    312 	struct netbsd32_ntptimeval30 ntv32;
    313 	struct ntptimeval ntv;
    314 	int error = 0;
    315 
    316 	if (vec_ntp_gettime == NULL)
    317 		return EINVAL;
    318 
    319 	if (SCARG_P32(uap, ntvp)) {
    320 		(*vec_ntp_gettime)(&ntv);
    321 
    322 		memset(&ntv32, 0, sizeof(ntv32));
    323 		ntv32.time.tv_sec = ntv.time.tv_sec;
    324 		ntv32.time.tv_usec = ntv.time.tv_nsec / 1000;
    325 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
    326 		ntv32.esterror = (netbsd32_long)ntv.esterror;
    327 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
    328 	}
    329 	if (!error) {
    330 		*retval = (*vec_ntp_timestatus)();
    331 	}
    332 
    333 	return error;
    334 }
    335 #endif
    336 
    337 static struct syscall_package compat_netbsd32_30_syscalls[] = {
    338 	{ NETBSD32_SYS_compat_30_netbsd32_getdents, 0,
    339 	    (sy_call_t *)compat_30_netbsd32_getdents },
    340 	{ NETBSD32_SYS_compat_30_netbsd32___stat13, 0,
    341 	    (sy_call_t *)compat_30_netbsd32___stat13 },
    342 	{ NETBSD32_SYS_compat_30_netbsd32___fstat13, 0,
    343 	    (sy_call_t *)compat_30_netbsd32___fstat13 },
    344 	{ NETBSD32_SYS_compat_30_netbsd32___lstat13, 0,
    345 	    (sy_call_t *)compat_30_netbsd32___lstat13 },
    346 	{ NETBSD32_SYS_compat_30_netbsd32_fhstat, 0,
    347 	    (sy_call_t *)compat_30_netbsd32_fhstat },
    348 	{ NETBSD32_SYS_compat_30_netbsd32_fhstatvfs1, 0,
    349 	    (sy_call_t *)compat_30_netbsd32_fhstatvfs1 },
    350 	{ NETBSD32_SYS_compat_30_netbsd32_socket, 0,
    351 	    (sy_call_t *)compat_30_netbsd32_socket },
    352 	{ NETBSD32_SYS_compat_30_netbsd32_getfh, 0,
    353 	    (sy_call_t *)compat_30_netbsd32_getfh },
    354 	{ NETBSD32_SYS_compat_30_netbsd32___fhstat30, 0,
    355 	    (sy_call_t *)compat_30_netbsd32___fhstat30 },
    356 	{ NETBSD32_SYS_compat_30_netbsd32_fhopen, 0,
    357 	    (sy_call_t *)compat_30_netbsd32_fhopen },
    358 #ifdef NTP
    359 	{ NETBSD32_SYS_compat_30_netbsd32_ntp_gettime, 0,
    360 	    (sy_call_t *)compat_30_netbsd32_ntp_gettime },
    361 #endif
    362 	{ 0, 0, NULL }
    363 };
    364 
    365 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_30, "compat_netbsd32_40,compat_30");
    366 
    367 static int
    368 compat_netbsd32_30_modcmd(modcmd_t cmd, void *arg)
    369 {
    370 
    371 	switch (cmd) {
    372 	case MODULE_CMD_INIT:
    373 		return syscall_establish(&emul_netbsd32,
    374 		    compat_netbsd32_30_syscalls);
    375 
    376 	case MODULE_CMD_FINI:
    377 		return syscall_disestablish(&emul_netbsd32,
    378 		    compat_netbsd32_30_syscalls);
    379 
    380 	default:
    381 		return ENOTTY;
    382 	}
    383 }
    384