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 #include <compat/linux/linux_file.h> 61 62 #include <security/mac/mac_framework.h> 63 64 /* 65 * XXX: This was removed from newstat_copyout(), and almost identical 66 * XXX: code was in stat64_copyout(). findcdev() needs to be replaced 67 * XXX: with something that does lookup and locking properly. 68 * XXX: When somebody fixes this: please try to avoid duplicating it. 69 */ 70 #if 0 71 static void 72 disk_foo(struct somestat *tbuf) 73 { 74 struct cdevsw *cdevsw; 75 struct cdev *dev; 76 77 /* Lie about disk drives which are character devices 78 * in FreeBSD but block devices under Linux. 79 */ 80 if (S_ISCHR(tbuf.st_mode) && 81 (dev = findcdev(buf->st_rdev)) != NULL) { 82 cdevsw = dev_refthread(dev); 83 if (cdevsw != NULL) { 84 if (cdevsw->d_flags & D_DISK) { 85 tbuf.st_mode &= ~S_IFMT; 86 tbuf.st_mode |= S_IFBLK; 87 88 /* XXX this may not be quite right */ 89 /* Map major number to 0 */ 90 tbuf.st_dev = uminor(buf->st_dev) & 0xf; 91 tbuf.st_rdev = buf->st_rdev & 0xff; 92 } 93 dev_relthread(dev); 94 } 95 } 96 97 } 98 #endif 99 100 static void 101 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf) 102 { 103 struct file *fp; 104 int major, minor; 105 106 if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) || 107 fget(td, fd, &fp) != 0) 108 return; 109 if (fp->f_vnode != NULL && 110 fp->f_vnode->v_un.vu_cdev != NULL && 111 linux_driver_get_major_minor(fp->f_vnode->v_un.vu_cdev->si_name, 112 &major, &minor) == 0) 113 buf->st_rdev = (major << 8 | minor); 114 fdrop(fp, td); 115 } 116 117 static void 118 translate_path_major_minor_at(struct thread *td, char *path, 119 struct stat *buf, int dfd) 120 { 121 struct proc *p = td->td_proc; 122 struct filedesc *fdp = p->p_fd; 123 int fd; 124 int temp; 125 126 if (!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) 127 return; 128 temp = td->td_retval[0]; 129 if (kern_openat(td, dfd, path, UIO_SYSSPACE, O_RDONLY, 0) != 0) 130 return; 131 fd = td->td_retval[0]; 132 td->td_retval[0] = temp; 133 translate_fd_major_minor(td, fd, buf); 134 fdclose(fdp, fdp->fd_ofiles[fd], fd, td); 135 } 136 137 static inline void 138 translate_path_major_minor(struct thread *td, char *path, struct stat *buf) 139 { 140 translate_path_major_minor_at(td, path, buf, AT_FDCWD); 141 } 142 143 static int 144 newstat_copyout(struct stat *buf, void *ubuf) 145 { 146 struct l_newstat tbuf; 147 148 bzero(&tbuf, sizeof(tbuf)); 149 tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 150 tbuf.st_ino = buf->st_ino; 151 tbuf.st_mode = buf->st_mode; 152 tbuf.st_nlink = buf->st_nlink; 153 tbuf.st_uid = buf->st_uid; 154 tbuf.st_gid = buf->st_gid; 155 tbuf.st_rdev = buf->st_rdev; 156 tbuf.st_size = buf->st_size; 157 tbuf.st_atime = buf->st_atime; 158 tbuf.st_mtime = buf->st_mtime; 159 tbuf.st_ctime = buf->st_ctime; 160 tbuf.st_blksize = buf->st_blksize; 161 tbuf.st_blocks = buf->st_blocks; 162 163 return (copyout(&tbuf, ubuf, sizeof(tbuf))); 164 } 165 166 int 167 linux_newstat(struct thread *td, struct linux_newstat_args *args) 168 { 169 struct stat buf; 170 char *path; 171 int error; 172 173 LCONVPATHEXIST(td, args->path, &path); 174 175 #ifdef DEBUG 176 if (ldebug(newstat)) 177 printf(ARGS(newstat, "%s, *"), path); 178 #endif 179 180 error = kern_stat(td, path, UIO_SYSSPACE, &buf); 181 if (!error) { 182 if (strlen(path) > strlen("/dev/pts/") && 183 !strncmp(path, "/dev/pts/", strlen("/dev/pts/")) && 184 path[9] >= '0' && path[9] <= '9') { 185 /* 186 * Linux checks major and minors of the slave device 187 * to make sure it's a pty device, so let's make him 188 * believe it is. 189 */ 190 buf.st_rdev = (136 << 8); 191 } else 192 translate_path_major_minor(td, path, &buf); 193 } 194 LFREEPATH(path); 195 if (error) 196 return (error); 197 return (newstat_copyout(&buf, args->buf)); 198 } 199 200 int 201 linux_newlstat(struct thread *td, struct linux_newlstat_args *args) 202 { 203 struct stat sb; 204 char *path; 205 int error; 206 207 LCONVPATHEXIST(td, args->path, &path); 208 209 #ifdef DEBUG 210 if (ldebug(newlstat)) 211 printf(ARGS(newlstat, "%s, *"), path); 212 #endif 213 214 error = kern_lstat(td, path, UIO_SYSSPACE, &sb); 215 if (!error) 216 translate_path_major_minor(td, path, &sb); 217 LFREEPATH(path); 218 if (error) 219 return (error); 220 return (newstat_copyout(&sb, args->buf)); 221 } 222 223 int 224 linux_newfstat(struct thread *td, struct linux_newfstat_args *args) 225 { 226 struct stat buf; 227 int error; 228 229 #ifdef DEBUG 230 if (ldebug(newfstat)) 231 printf(ARGS(newfstat, "%d, *"), args->fd); 232 #endif 233 234 error = kern_fstat(td, args->fd, &buf); 235 translate_fd_major_minor(td, args->fd, &buf); 236 if (!error) 237 error = newstat_copyout(&buf, args->buf); 238 239 return (error); 240 } 241 242 static int 243 stat_copyout(struct stat *buf, void *ubuf) 244 { 245 struct l_stat lbuf; 246 247 bzero(&lbuf, sizeof(lbuf)); 248 lbuf.st_dev = buf->st_dev; 249 lbuf.st_ino = buf->st_ino; 250 lbuf.st_mode = buf->st_mode; 251 lbuf.st_nlink = buf->st_nlink; 252 lbuf.st_uid = buf->st_uid; 253 lbuf.st_gid = buf->st_gid; 254 lbuf.st_rdev = buf->st_rdev; 255 if (buf->st_size < (quad_t)1 << 32) 256 lbuf.st_size = buf->st_size; 257 else 258 lbuf.st_size = -2; 259 lbuf.st_atime = buf->st_atime; 260 lbuf.st_mtime = buf->st_mtime; 261 lbuf.st_ctime = buf->st_ctime; 262 lbuf.st_blksize = buf->st_blksize; 263 lbuf.st_blocks = buf->st_blocks; 264 lbuf.st_flags = buf->st_flags; 265 lbuf.st_gen = buf->st_gen; 266 267 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 268 } 269 270 int 271 linux_stat(struct thread *td, struct linux_stat_args *args) 272 { 273 struct stat buf; 274 char *path; 275 int error; 276 277 LCONVPATHEXIST(td, args->path, &path); 278 279 #ifdef DEBUG 280 if (ldebug(stat)) 281 printf(ARGS(stat, "%s, *"), path); 282 #endif 283 error = kern_stat(td, path, UIO_SYSSPACE, &buf); 284 if (error) { 285 LFREEPATH(path); 286 return (error); 287 } 288 translate_path_major_minor(td, path, &buf); 289 LFREEPATH(path); 290 return(stat_copyout(&buf, args->up)); 291 } 292 293 int 294 linux_lstat(struct thread *td, struct linux_lstat_args *args) 295 { 296 struct stat buf; 297 char *path; 298 int error; 299 300 LCONVPATHEXIST(td, args->path, &path); 301 302 #ifdef DEBUG 303 if (ldebug(lstat)) 304 printf(ARGS(lstat, "%s, *"), path); 305 #endif 306 error = kern_lstat(td, path, UIO_SYSSPACE, &buf); 307 if (error) { 308 LFREEPATH(path); 309 return (error); 310 } 311 translate_path_major_minor(td, path, &buf); 312 LFREEPATH(path); 313 return(stat_copyout(&buf, args->up)); 314 } 315 316 /* XXX - All fields of type l_int are defined as l_long on i386 */ 317 struct l_statfs { 318 l_int f_type; 319 l_int f_bsize; 320 l_int f_blocks; 321 l_int f_bfree; 322 l_int f_bavail; 323 l_int f_files; 324 l_int f_ffree; 325 l_fsid_t f_fsid; 326 l_int f_namelen; 327 l_int f_spare[6]; 328 }; 329 330 #define LINUX_CODA_SUPER_MAGIC 0x73757245L 331 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L 332 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L 333 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L 334 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L 335 #define LINUX_NCP_SUPER_MAGIC 0x564cL 336 #define LINUX_NFS_SUPER_MAGIC 0x6969L 337 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL 338 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L 339 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */ 340 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L 341 342 static long 343 bsd_to_linux_ftype(const char *fstypename) 344 { 345 int i; 346 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = { 347 {"ufs", LINUX_UFS_SUPER_MAGIC}, 348 {"cd9660", LINUX_ISOFS_SUPER_MAGIC}, 349 {"nfs", LINUX_NFS_SUPER_MAGIC}, 350 {"ext2fs", LINUX_EXT2_SUPER_MAGIC}, 351 {"procfs", LINUX_PROC_SUPER_MAGIC}, 352 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC}, 353 {"ntfs", LINUX_NTFS_SUPER_MAGIC}, 354 {"nwfs", LINUX_NCP_SUPER_MAGIC}, 355 {"hpfs", LINUX_HPFS_SUPER_MAGIC}, 356 {"coda", LINUX_CODA_SUPER_MAGIC}, 357 {"devfs", LINUX_DEVFS_SUPER_MAGIC}, 358 {NULL, 0L}}; 359 360 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++) 361 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0) 362 return (b2l_tbl[i].linux_type); 363 364 return (0L); 365 } 366 367 static void 368 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs) 369 { 370 371 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 372 linux_statfs->f_bsize = bsd_statfs->f_bsize; 373 linux_statfs->f_blocks = bsd_statfs->f_blocks; 374 linux_statfs->f_bfree = bsd_statfs->f_bfree; 375 linux_statfs->f_bavail = bsd_statfs->f_bavail; 376 linux_statfs->f_ffree = bsd_statfs->f_ffree; 377 linux_statfs->f_files = bsd_statfs->f_files; 378 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 379 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 380 linux_statfs->f_namelen = MAXNAMLEN; 381 } 382 383 int 384 linux_statfs(struct thread *td, struct linux_statfs_args *args) 385 { 386 struct l_statfs linux_statfs; 387 struct statfs bsd_statfs; 388 char *path; 389 int error; 390 391 LCONVPATHEXIST(td, args->path, &path); 392 393 #ifdef DEBUG 394 if (ldebug(statfs)) 395 printf(ARGS(statfs, "%s, *"), path); 396 #endif 397 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 398 LFREEPATH(path); 399 if (error) 400 return (error); 401 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 402 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 403 } 404 405 static void 406 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs) 407 { 408 409 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 410 linux_statfs->f_bsize = bsd_statfs->f_bsize; 411 linux_statfs->f_blocks = bsd_statfs->f_blocks; 412 linux_statfs->f_bfree = bsd_statfs->f_bfree; 413 linux_statfs->f_bavail = bsd_statfs->f_bavail; 414 linux_statfs->f_ffree = bsd_statfs->f_ffree; 415 linux_statfs->f_files = bsd_statfs->f_files; 416 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 417 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 418 linux_statfs->f_namelen = MAXNAMLEN; 419 } 420 421 int 422 linux_statfs64(struct thread *td, struct linux_statfs64_args *args) 423 { 424 struct l_statfs64 linux_statfs; 425 struct statfs bsd_statfs; 426 char *path; 427 int error; 428 429 if (args->bufsize != sizeof(struct l_statfs64)) 430 return EINVAL; 431 432 LCONVPATHEXIST(td, args->path, &path); 433 434 #ifdef DEBUG 435 if (ldebug(statfs64)) 436 printf(ARGS(statfs64, "%s, *"), path); 437 #endif 438 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 439 LFREEPATH(path); 440 if (error) 441 return (error); 442 bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs); 443 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 444 } 445 446 int 447 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args) 448 { 449 struct l_statfs linux_statfs; 450 struct statfs bsd_statfs; 451 int error; 452 453 #ifdef DEBUG 454 if (ldebug(fstatfs)) 455 printf(ARGS(fstatfs, "%d, *"), args->fd); 456 #endif 457 error = kern_fstatfs(td, args->fd, &bsd_statfs); 458 if (error) 459 return error; 460 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 461 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 462 } 463 464 struct l_ustat 465 { 466 l_daddr_t f_tfree; 467 l_ino_t f_tinode; 468 char f_fname[6]; 469 char f_fpack[6]; 470 }; 471 472 int 473 linux_ustat(struct thread *td, struct linux_ustat_args *args) 474 { 475 #ifdef DEBUG 476 if (ldebug(ustat)) 477 printf(ARGS(ustat, "%d, *"), args->dev); 478 #endif 479 480 return (EOPNOTSUPP); 481 } 482 483 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 484 485 static int 486 stat64_copyout(struct stat *buf, void *ubuf) 487 { 488 struct l_stat64 lbuf; 489 490 bzero(&lbuf, sizeof(lbuf)); 491 lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 492 lbuf.st_ino = buf->st_ino; 493 lbuf.st_mode = buf->st_mode; 494 lbuf.st_nlink = buf->st_nlink; 495 lbuf.st_uid = buf->st_uid; 496 lbuf.st_gid = buf->st_gid; 497 lbuf.st_rdev = buf->st_rdev; 498 lbuf.st_size = buf->st_size; 499 lbuf.st_atime = buf->st_atime; 500 lbuf.st_mtime = buf->st_mtime; 501 lbuf.st_ctime = buf->st_ctime; 502 lbuf.st_blksize = buf->st_blksize; 503 lbuf.st_blocks = buf->st_blocks; 504 505 /* 506 * The __st_ino field makes all the difference. In the Linux kernel 507 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 508 * but without the assignment to __st_ino the runtime linker refuses 509 * to mmap(2) any shared libraries. I guess it's broken alright :-) 510 */ 511 lbuf.__st_ino = buf->st_ino; 512 513 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 514 } 515 516 int 517 linux_stat64(struct thread *td, struct linux_stat64_args *args) 518 { 519 struct stat buf; 520 char *filename; 521 int error; 522 523 LCONVPATHEXIST(td, args->filename, &filename); 524 525 #ifdef DEBUG 526 if (ldebug(stat64)) 527 printf(ARGS(stat64, "%s, *"), filename); 528 #endif 529 530 error = kern_stat(td, filename, UIO_SYSSPACE, &buf); 531 if (!error) { 532 if (strlen(filename) > strlen("/dev/pts/") && 533 !strncmp(filename, "/dev/pts/", strlen("/dev/pts/")) && 534 filename[9] >= '0' && filename[9] <= '9') { 535 /* 536 * Linux checks major and minors of the slave device 537 * to make sure it's a pty deivce, so let's make him 538 * believe it is. 539 */ 540 buf.st_rdev = (136 << 8); 541 } else 542 translate_path_major_minor(td, filename, &buf); 543 } 544 LFREEPATH(filename); 545 if (error) 546 return (error); 547 return (stat64_copyout(&buf, args->statbuf)); 548 } 549 550 int 551 linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 552 { 553 struct stat sb; 554 char *filename; 555 int error; 556 557 LCONVPATHEXIST(td, args->filename, &filename); 558 559 #ifdef DEBUG 560 if (ldebug(lstat64)) 561 printf(ARGS(lstat64, "%s, *"), args->filename); 562 #endif 563 564 error = kern_lstat(td, filename, UIO_SYSSPACE, &sb); 565 if (!error) 566 translate_path_major_minor(td, filename, &sb); 567 LFREEPATH(filename); 568 if (error) 569 return (error); 570 return (stat64_copyout(&sb, args->statbuf)); 571 } 572 573 int 574 linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 575 { 576 struct stat buf; 577 int error; 578 579 #ifdef DEBUG 580 if (ldebug(fstat64)) 581 printf(ARGS(fstat64, "%d, *"), args->fd); 582 #endif 583 584 error = kern_fstat(td, args->fd, &buf); 585 translate_fd_major_minor(td, args->fd, &buf); 586 if (!error) 587 error = stat64_copyout(&buf, args->statbuf); 588 589 return (error); 590 } 591 592 int 593 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args) 594 { 595 char *path; 596 int error, dfd, flag; 597 struct stat buf; 598 599 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 600 return (EINVAL); 601 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 602 AT_SYMLINK_NOFOLLOW : 0; 603 604 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 605 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 606 607 #ifdef DEBUG 608 if (ldebug(fstatat64)) 609 printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag); 610 #endif 611 612 error = kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf); 613 translate_path_major_minor_at(td, args->pathname, &buf, dfd); 614 if (!error) 615 error = stat64_copyout(&buf, args->statbuf); 616 LFREEPATH(path); 617 618 return (error); 619 } 620 621 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 622