1 /*- 2 * Copyright (c) 1994-1995 S�ren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_compat.h" 33 #include "opt_mac.h" 34 35 #include <sys/param.h> 36 #include <sys/dirent.h> 37 #include <sys/file.h> 38 #include <sys/filedesc.h> 39 #include <sys/proc.h> 40 #include <sys/jail.h> 41 #include <sys/malloc.h> 42 #include <sys/mount.h> 43 #include <sys/namei.h> 44 #include <sys/stat.h> 45 #include <sys/syscallsubr.h> 46 #include <sys/systm.h> 47 #include <sys/vnode.h> 48 #include <sys/conf.h> 49 #include <sys/fcntl.h> 50 51 #ifdef COMPAT_LINUX32 52 #include <machine/../linux32/linux.h> 53 #include <machine/../linux32/linux32_proto.h> 54 #else 55 #include <machine/../linux/linux.h> 56 #include <machine/../linux/linux_proto.h> 57 #endif 58 59 #include <compat/linux/linux_util.h> 60 61 #include <security/mac/mac_framework.h> 62 63 /* 64 * XXX: This was removed from newstat_copyout(), and almost identical 65 * XXX: code was in stat64_copyout(). findcdev() needs to be replaced 66 * XXX: with something that does lookup and locking properly. 67 * XXX: When somebody fixes this: please try to avoid duplicating it. 68 */ 69 #if 0 70 static void 71 disk_foo(struct somestat *tbuf) 72 { 73 struct cdevsw *cdevsw; 74 struct cdev *dev; 75 76 /* Lie about disk drives which are character devices 77 * in FreeBSD but block devices under Linux. 78 */ 79 if (S_ISCHR(tbuf.st_mode) && 80 (dev = findcdev(buf->st_rdev)) != NULL) { 81 cdevsw = dev_refthread(dev); 82 if (cdevsw != NULL) { 83 if (cdevsw->d_flags & D_DISK) { 84 tbuf.st_mode &= ~S_IFMT; 85 tbuf.st_mode |= S_IFBLK; 86 87 /* XXX this may not be quite right */ 88 /* Map major number to 0 */ 89 tbuf.st_dev = uminor(buf->st_dev) & 0xf; 90 tbuf.st_rdev = buf->st_rdev & 0xff; 91 } 92 dev_relthread(dev); 93 } 94 } 95 96 } 97 #endif 98 99 static void 100 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf) 101 { 102 struct file *fp; 103 int major, minor; 104 105 if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) || 106 fget(td, fd, &fp) != 0) 107 return; 108 if (fp->f_vnode != NULL && 109 fp->f_vnode->v_un.vu_cdev != NULL && 110 linux_driver_get_major_minor(fp->f_vnode->v_un.vu_cdev->si_name, 111 &major, &minor) == 0) 112 buf->st_rdev = (major << 8 | minor); 113 fdrop(fp, td); 114 } 115 116 static void 117 translate_path_major_minor(struct thread *td, char *path, struct stat *buf) 118 { 119 struct proc *p = td->td_proc; 120 struct filedesc *fdp = p->p_fd; 121 struct file *fp; 122 int fd; 123 int temp; 124 125 if (!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) 126 return; 127 temp = td->td_retval[0]; 128 if (kern_open(td, path, UIO_SYSSPACE, O_RDONLY, 0) != 0) 129 return; 130 fd = td->td_retval[0]; 131 td->td_retval[0] = temp; 132 translate_fd_major_minor(td, fd, buf); 133 FILEDESC_LOCK(fdp); 134 fp = fdp->fd_ofiles[fd]; 135 FILEDESC_UNLOCK(fdp); 136 fdclose(fdp, fdp->fd_ofiles[fd], fd, td); 137 } 138 139 static int 140 newstat_copyout(struct stat *buf, void *ubuf) 141 { 142 struct l_newstat tbuf; 143 144 bzero(&tbuf, sizeof(tbuf)); 145 tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 146 tbuf.st_ino = buf->st_ino; 147 tbuf.st_mode = buf->st_mode; 148 tbuf.st_nlink = buf->st_nlink; 149 tbuf.st_uid = buf->st_uid; 150 tbuf.st_gid = buf->st_gid; 151 tbuf.st_rdev = buf->st_rdev; 152 tbuf.st_size = buf->st_size; 153 tbuf.st_atime = buf->st_atime; 154 tbuf.st_mtime = buf->st_mtime; 155 tbuf.st_ctime = buf->st_ctime; 156 tbuf.st_blksize = buf->st_blksize; 157 tbuf.st_blocks = buf->st_blocks; 158 159 return (copyout(&tbuf, ubuf, sizeof(tbuf))); 160 } 161 162 int 163 linux_newstat(struct thread *td, struct linux_newstat_args *args) 164 { 165 struct stat buf; 166 char *path; 167 int error; 168 169 LCONVPATHEXIST(td, args->path, &path); 170 171 #ifdef DEBUG 172 if (ldebug(newstat)) 173 printf(ARGS(newstat, "%s, *"), path); 174 #endif 175 176 error = kern_stat(td, path, UIO_SYSSPACE, &buf); 177 if (!error) { 178 if (strlen(path) > strlen("/dev/pts/") && 179 !strncmp(path, "/dev/pts/", strlen("/dev/pts/")) && 180 path[9] >= '0' && path[9] <= '9') { 181 /* 182 * Linux checks major and minors of the slave device 183 * to make sure it's a pty device, so let's make him 184 * believe it is. 185 */ 186 buf.st_rdev = (136 << 8); 187 } else 188 translate_path_major_minor(td, path, &buf); 189 } 190 LFREEPATH(path); 191 if (error) 192 return (error); 193 return (newstat_copyout(&buf, args->buf)); 194 } 195 196 int 197 linux_newlstat(struct thread *td, struct linux_newlstat_args *args) 198 { 199 struct stat sb; 200 char *path; 201 int error; 202 203 LCONVPATHEXIST(td, args->path, &path); 204 205 #ifdef DEBUG 206 if (ldebug(newlstat)) 207 printf(ARGS(newlstat, "%s, *"), path); 208 #endif 209 210 error = kern_lstat(td, path, UIO_SYSSPACE, &sb); 211 if (!error) 212 translate_path_major_minor(td, path, &sb); 213 LFREEPATH(path); 214 if (error) 215 return (error); 216 return (newstat_copyout(&sb, args->buf)); 217 } 218 219 int 220 linux_newfstat(struct thread *td, struct linux_newfstat_args *args) 221 { 222 struct stat buf; 223 int error; 224 225 #ifdef DEBUG 226 if (ldebug(newfstat)) 227 printf(ARGS(newfstat, "%d, *"), args->fd); 228 #endif 229 230 error = kern_fstat(td, args->fd, &buf); 231 translate_fd_major_minor(td, args->fd, &buf); 232 if (!error) 233 error = newstat_copyout(&buf, args->buf); 234 235 return (error); 236 } 237 238 static int 239 stat_copyout(struct stat *buf, void *ubuf) 240 { 241 struct l_stat lbuf; 242 243 bzero(&lbuf, sizeof(lbuf)); 244 lbuf.st_dev = buf->st_dev; 245 lbuf.st_ino = buf->st_ino; 246 lbuf.st_mode = buf->st_mode; 247 lbuf.st_nlink = buf->st_nlink; 248 lbuf.st_uid = buf->st_uid; 249 lbuf.st_gid = buf->st_gid; 250 lbuf.st_rdev = buf->st_rdev; 251 if (buf->st_size < (quad_t)1 << 32) 252 lbuf.st_size = buf->st_size; 253 else 254 lbuf.st_size = -2; 255 lbuf.st_atime = buf->st_atime; 256 lbuf.st_mtime = buf->st_mtime; 257 lbuf.st_ctime = buf->st_ctime; 258 lbuf.st_blksize = buf->st_blksize; 259 lbuf.st_blocks = buf->st_blocks; 260 lbuf.st_flags = buf->st_flags; 261 lbuf.st_gen = buf->st_gen; 262 263 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 264 } 265 266 int 267 linux_stat(struct thread *td, struct linux_stat_args *args) 268 { 269 struct stat buf; 270 int error; 271 #ifdef DEBUG 272 if (ldebug(stat)) 273 printf(ARGS(stat, "%s, *"), args->path); 274 #endif 275 error = kern_stat(td, args->path, UIO_SYSSPACE, &buf); 276 if (error) 277 return (error); 278 translate_path_major_minor(td, args->path, &buf); 279 return(stat_copyout(&buf, args->up)); 280 } 281 282 int 283 linux_lstat(struct thread *td, struct linux_lstat_args *args) 284 { 285 struct stat buf; 286 int error; 287 288 #ifdef DEBUG 289 if (ldebug(lstat)) 290 printf(ARGS(lstat, "%s, *"), args->path); 291 #endif 292 error = kern_lstat(td, args->path, UIO_SYSSPACE, &buf); 293 if (error) 294 return (error); 295 translate_path_major_minor(td, args->path, &buf); 296 return(stat_copyout(&buf, args->up)); 297 } 298 299 /* XXX - All fields of type l_int are defined as l_long on i386 */ 300 struct l_statfs { 301 l_int f_type; 302 l_int f_bsize; 303 l_int f_blocks; 304 l_int f_bfree; 305 l_int f_bavail; 306 l_int f_files; 307 l_int f_ffree; 308 l_fsid_t f_fsid; 309 l_int f_namelen; 310 l_int f_spare[6]; 311 }; 312 313 struct l_statfs64 { 314 l_int f_type; 315 l_int f_bsize; 316 uint64_t f_blocks; 317 uint64_t f_bfree; 318 uint64_t f_bavail; 319 uint64_t f_files; 320 uint64_t f_ffree; 321 l_fsid_t f_fsid; 322 l_int f_namelen; 323 l_int f_spare[6]; 324 }; 325 326 #define LINUX_CODA_SUPER_MAGIC 0x73757245L 327 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L 328 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L 329 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L 330 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L 331 #define LINUX_NCP_SUPER_MAGIC 0x564cL 332 #define LINUX_NFS_SUPER_MAGIC 0x6969L 333 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL 334 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L 335 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */ 336 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L 337 338 static long 339 bsd_to_linux_ftype(const char *fstypename) 340 { 341 int i; 342 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = { 343 {"ufs", LINUX_UFS_SUPER_MAGIC}, 344 {"cd9660", LINUX_ISOFS_SUPER_MAGIC}, 345 {"nfs", LINUX_NFS_SUPER_MAGIC}, 346 {"ext2fs", LINUX_EXT2_SUPER_MAGIC}, 347 {"procfs", LINUX_PROC_SUPER_MAGIC}, 348 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC}, 349 {"ntfs", LINUX_NTFS_SUPER_MAGIC}, 350 {"nwfs", LINUX_NCP_SUPER_MAGIC}, 351 {"hpfs", LINUX_HPFS_SUPER_MAGIC}, 352 {"coda", LINUX_CODA_SUPER_MAGIC}, 353 {"devfs", LINUX_DEVFS_SUPER_MAGIC}, 354 {NULL, 0L}}; 355 356 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++) 357 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0) 358 return (b2l_tbl[i].linux_type); 359 360 return (0L); 361 } 362 363 static void 364 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs) 365 { 366 367 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 368 linux_statfs->f_bsize = bsd_statfs->f_bsize; 369 linux_statfs->f_blocks = bsd_statfs->f_blocks; 370 linux_statfs->f_bfree = bsd_statfs->f_bfree; 371 linux_statfs->f_bavail = bsd_statfs->f_bavail; 372 linux_statfs->f_ffree = bsd_statfs->f_ffree; 373 linux_statfs->f_files = bsd_statfs->f_files; 374 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 375 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 376 linux_statfs->f_namelen = MAXNAMLEN; 377 } 378 379 int 380 linux_statfs(struct thread *td, struct linux_statfs_args *args) 381 { 382 struct l_statfs linux_statfs; 383 struct statfs bsd_statfs; 384 char *path; 385 int error; 386 387 LCONVPATHEXIST(td, args->path, &path); 388 389 #ifdef DEBUG 390 if (ldebug(statfs)) 391 printf(ARGS(statfs, "%s, *"), path); 392 #endif 393 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 394 LFREEPATH(path); 395 if (error) 396 return (error); 397 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 398 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 399 } 400 401 static void 402 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs) 403 { 404 405 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 406 linux_statfs->f_bsize = bsd_statfs->f_bsize; 407 linux_statfs->f_blocks = bsd_statfs->f_blocks; 408 linux_statfs->f_bfree = bsd_statfs->f_bfree; 409 linux_statfs->f_bavail = bsd_statfs->f_bavail; 410 linux_statfs->f_ffree = bsd_statfs->f_ffree; 411 linux_statfs->f_files = bsd_statfs->f_files; 412 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 413 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 414 linux_statfs->f_namelen = MAXNAMLEN; 415 } 416 417 int 418 linux_statfs64(struct thread *td, struct linux_statfs64_args *args) 419 { 420 struct l_statfs64 linux_statfs; 421 struct statfs bsd_statfs; 422 char *path; 423 int error; 424 425 LCONVPATHEXIST(td, args->path, &path); 426 427 #ifdef DEBUG 428 if (ldebug(statfs64)) 429 printf(ARGS(statfs64, "%s, *"), path); 430 #endif 431 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 432 LFREEPATH(path); 433 if (error) 434 return (error); 435 bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs); 436 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 437 } 438 439 int 440 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args) 441 { 442 struct l_statfs linux_statfs; 443 struct statfs bsd_statfs; 444 int error; 445 446 #ifdef DEBUG 447 if (ldebug(fstatfs)) 448 printf(ARGS(fstatfs, "%d, *"), args->fd); 449 #endif 450 error = kern_fstatfs(td, args->fd, &bsd_statfs); 451 if (error) 452 return error; 453 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 454 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 455 } 456 457 struct l_ustat 458 { 459 l_daddr_t f_tfree; 460 l_ino_t f_tinode; 461 char f_fname[6]; 462 char f_fpack[6]; 463 }; 464 465 int 466 linux_ustat(struct thread *td, struct linux_ustat_args *args) 467 { 468 #ifdef DEBUG 469 if (ldebug(ustat)) 470 printf(ARGS(ustat, "%d, *"), args->dev); 471 #endif 472 473 return (EOPNOTSUPP); 474 } 475 476 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 477 478 static int 479 stat64_copyout(struct stat *buf, void *ubuf) 480 { 481 struct l_stat64 lbuf; 482 483 bzero(&lbuf, sizeof(lbuf)); 484 lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 485 lbuf.st_ino = buf->st_ino; 486 lbuf.st_mode = buf->st_mode; 487 lbuf.st_nlink = buf->st_nlink; 488 lbuf.st_uid = buf->st_uid; 489 lbuf.st_gid = buf->st_gid; 490 lbuf.st_rdev = buf->st_rdev; 491 lbuf.st_size = buf->st_size; 492 lbuf.st_atime = buf->st_atime; 493 lbuf.st_mtime = buf->st_mtime; 494 lbuf.st_ctime = buf->st_ctime; 495 lbuf.st_blksize = buf->st_blksize; 496 lbuf.st_blocks = buf->st_blocks; 497 498 /* 499 * The __st_ino field makes all the difference. In the Linux kernel 500 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 501 * but without the assignment to __st_ino the runtime linker refuses 502 * to mmap(2) any shared libraries. I guess it's broken alright :-) 503 */ 504 lbuf.__st_ino = buf->st_ino; 505 506 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 507 } 508 509 int 510 linux_stat64(struct thread *td, struct linux_stat64_args *args) 511 { 512 struct stat buf; 513 char *filename; 514 int error; 515 516 LCONVPATHEXIST(td, args->filename, &filename); 517 518 #ifdef DEBUG 519 if (ldebug(stat64)) 520 printf(ARGS(stat64, "%s, *"), filename); 521 #endif 522 523 error = kern_stat(td, filename, UIO_SYSSPACE, &buf); 524 if (!error) { 525 if (strlen(filename) > strlen("/dev/pts/") && 526 !strncmp(filename, "/dev/pts/", strlen("/dev/pts/")) && 527 filename[9] >= '0' && filename[9] <= '9') { 528 /* 529 * Linux checks major and minors of the slave device 530 * to make sure it's a pty deivce, so let's make him 531 * believe it is. 532 */ 533 buf.st_rdev = (136 << 8); 534 } else 535 translate_path_major_minor(td, filename, &buf); 536 } 537 LFREEPATH(filename); 538 if (error) 539 return (error); 540 return (stat64_copyout(&buf, args->statbuf)); 541 } 542 543 int 544 linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 545 { 546 struct stat sb; 547 char *filename; 548 int error; 549 550 LCONVPATHEXIST(td, args->filename, &filename); 551 552 #ifdef DEBUG 553 if (ldebug(lstat64)) 554 printf(ARGS(lstat64, "%s, *"), args->filename); 555 #endif 556 557 error = kern_lstat(td, filename, UIO_SYSSPACE, &sb); 558 if (!error) 559 translate_path_major_minor(td, filename, &sb); 560 LFREEPATH(filename); 561 if (error) 562 return (error); 563 return (stat64_copyout(&sb, args->statbuf)); 564 } 565 566 int 567 linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 568 { 569 struct stat buf; 570 int error; 571 572 #ifdef DEBUG 573 if (ldebug(fstat64)) 574 printf(ARGS(fstat64, "%d, *"), args->fd); 575 #endif 576 577 error = kern_fstat(td, args->fd, &buf); 578 translate_fd_major_minor(td, args->fd, &buf); 579 if (!error) 580 error = stat64_copyout(&buf, args->statbuf); 581 582 return (error); 583 } 584 585 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 586