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 translate_path_major_minor(td, path, &buf); 183 LFREEPATH(path); 184 if (error) 185 return (error); 186 return (newstat_copyout(&buf, args->buf)); 187 } 188 189 int 190 linux_newlstat(struct thread *td, struct linux_newlstat_args *args) 191 { 192 struct stat sb; 193 char *path; 194 int error; 195 196 LCONVPATHEXIST(td, args->path, &path); 197 198 #ifdef DEBUG 199 if (ldebug(newlstat)) 200 printf(ARGS(newlstat, "%s, *"), path); 201 #endif 202 203 error = kern_lstat(td, path, UIO_SYSSPACE, &sb); 204 if (!error) 205 translate_path_major_minor(td, path, &sb); 206 LFREEPATH(path); 207 if (error) 208 return (error); 209 return (newstat_copyout(&sb, args->buf)); 210 } 211 212 int 213 linux_newfstat(struct thread *td, struct linux_newfstat_args *args) 214 { 215 struct stat buf; 216 int error; 217 218 #ifdef DEBUG 219 if (ldebug(newfstat)) 220 printf(ARGS(newfstat, "%d, *"), args->fd); 221 #endif 222 223 error = kern_fstat(td, args->fd, &buf); 224 translate_fd_major_minor(td, args->fd, &buf); 225 if (!error) 226 error = newstat_copyout(&buf, args->buf); 227 228 return (error); 229 } 230 231 static int 232 stat_copyout(struct stat *buf, void *ubuf) 233 { 234 struct l_stat lbuf; 235 236 bzero(&lbuf, sizeof(lbuf)); 237 lbuf.st_dev = buf->st_dev; 238 lbuf.st_ino = buf->st_ino; 239 lbuf.st_mode = buf->st_mode; 240 lbuf.st_nlink = buf->st_nlink; 241 lbuf.st_uid = buf->st_uid; 242 lbuf.st_gid = buf->st_gid; 243 lbuf.st_rdev = buf->st_rdev; 244 if (buf->st_size < (quad_t)1 << 32) 245 lbuf.st_size = buf->st_size; 246 else 247 lbuf.st_size = -2; 248 lbuf.st_atime = buf->st_atime; 249 lbuf.st_mtime = buf->st_mtime; 250 lbuf.st_ctime = buf->st_ctime; 251 lbuf.st_blksize = buf->st_blksize; 252 lbuf.st_blocks = buf->st_blocks; 253 lbuf.st_flags = buf->st_flags; 254 lbuf.st_gen = buf->st_gen; 255 256 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 257 } 258 259 int 260 linux_stat(struct thread *td, struct linux_stat_args *args) 261 { 262 struct stat buf; 263 char *path; 264 int error; 265 266 LCONVPATHEXIST(td, args->path, &path); 267 268 #ifdef DEBUG 269 if (ldebug(stat)) 270 printf(ARGS(stat, "%s, *"), path); 271 #endif 272 error = kern_stat(td, path, UIO_SYSSPACE, &buf); 273 if (error) { 274 LFREEPATH(path); 275 return (error); 276 } 277 translate_path_major_minor(td, path, &buf); 278 LFREEPATH(path); 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 char *path; 287 int error; 288 289 LCONVPATHEXIST(td, args->path, &path); 290 291 #ifdef DEBUG 292 if (ldebug(lstat)) 293 printf(ARGS(lstat, "%s, *"), path); 294 #endif 295 error = kern_lstat(td, path, UIO_SYSSPACE, &buf); 296 if (error) { 297 LFREEPATH(path); 298 return (error); 299 } 300 translate_path_major_minor(td, path, &buf); 301 LFREEPATH(path); 302 return(stat_copyout(&buf, args->up)); 303 } 304 305 /* XXX - All fields of type l_int are defined as l_long on i386 */ 306 struct l_statfs { 307 l_int f_type; 308 l_int f_bsize; 309 l_int f_blocks; 310 l_int f_bfree; 311 l_int f_bavail; 312 l_int f_files; 313 l_int f_ffree; 314 l_fsid_t f_fsid; 315 l_int f_namelen; 316 l_int f_spare[6]; 317 }; 318 319 #define LINUX_CODA_SUPER_MAGIC 0x73757245L 320 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L 321 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L 322 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L 323 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L 324 #define LINUX_NCP_SUPER_MAGIC 0x564cL 325 #define LINUX_NFS_SUPER_MAGIC 0x6969L 326 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL 327 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L 328 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */ 329 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L 330 331 static long 332 bsd_to_linux_ftype(const char *fstypename) 333 { 334 int i; 335 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = { 336 {"ufs", LINUX_UFS_SUPER_MAGIC}, 337 {"cd9660", LINUX_ISOFS_SUPER_MAGIC}, 338 {"nfs", LINUX_NFS_SUPER_MAGIC}, 339 {"ext2fs", LINUX_EXT2_SUPER_MAGIC}, 340 {"procfs", LINUX_PROC_SUPER_MAGIC}, 341 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC}, 342 {"ntfs", LINUX_NTFS_SUPER_MAGIC}, 343 {"nwfs", LINUX_NCP_SUPER_MAGIC}, 344 {"hpfs", LINUX_HPFS_SUPER_MAGIC}, 345 {"coda", LINUX_CODA_SUPER_MAGIC}, 346 {"devfs", LINUX_DEVFS_SUPER_MAGIC}, 347 {NULL, 0L}}; 348 349 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++) 350 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0) 351 return (b2l_tbl[i].linux_type); 352 353 return (0L); 354 } 355 356 static void 357 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs) 358 { 359 360 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 361 linux_statfs->f_bsize = bsd_statfs->f_bsize; 362 linux_statfs->f_blocks = bsd_statfs->f_blocks; 363 linux_statfs->f_bfree = bsd_statfs->f_bfree; 364 linux_statfs->f_bavail = bsd_statfs->f_bavail; 365 linux_statfs->f_ffree = bsd_statfs->f_ffree; 366 linux_statfs->f_files = bsd_statfs->f_files; 367 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 368 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 369 linux_statfs->f_namelen = MAXNAMLEN; 370 } 371 372 int 373 linux_statfs(struct thread *td, struct linux_statfs_args *args) 374 { 375 struct l_statfs linux_statfs; 376 struct statfs bsd_statfs; 377 char *path; 378 int error; 379 380 LCONVPATHEXIST(td, args->path, &path); 381 382 #ifdef DEBUG 383 if (ldebug(statfs)) 384 printf(ARGS(statfs, "%s, *"), path); 385 #endif 386 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 387 LFREEPATH(path); 388 if (error) 389 return (error); 390 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 391 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 392 } 393 394 static void 395 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs) 396 { 397 398 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 399 linux_statfs->f_bsize = bsd_statfs->f_bsize; 400 linux_statfs->f_blocks = bsd_statfs->f_blocks; 401 linux_statfs->f_bfree = bsd_statfs->f_bfree; 402 linux_statfs->f_bavail = bsd_statfs->f_bavail; 403 linux_statfs->f_ffree = bsd_statfs->f_ffree; 404 linux_statfs->f_files = bsd_statfs->f_files; 405 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 406 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 407 linux_statfs->f_namelen = MAXNAMLEN; 408 } 409 410 int 411 linux_statfs64(struct thread *td, struct linux_statfs64_args *args) 412 { 413 struct l_statfs64 linux_statfs; 414 struct statfs bsd_statfs; 415 char *path; 416 int error; 417 418 if (args->bufsize != sizeof(struct l_statfs64)) 419 return EINVAL; 420 421 LCONVPATHEXIST(td, args->path, &path); 422 423 #ifdef DEBUG 424 if (ldebug(statfs64)) 425 printf(ARGS(statfs64, "%s, *"), path); 426 #endif 427 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 428 LFREEPATH(path); 429 if (error) 430 return (error); 431 bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs); 432 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 433 } 434 435 int 436 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args) 437 { 438 struct l_statfs linux_statfs; 439 struct statfs bsd_statfs; 440 int error; 441 442 #ifdef DEBUG 443 if (ldebug(fstatfs)) 444 printf(ARGS(fstatfs, "%d, *"), args->fd); 445 #endif 446 error = kern_fstatfs(td, args->fd, &bsd_statfs); 447 if (error) 448 return error; 449 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs); 450 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 451 } 452 453 struct l_ustat 454 { 455 l_daddr_t f_tfree; 456 l_ino_t f_tinode; 457 char f_fname[6]; 458 char f_fpack[6]; 459 }; 460 461 int 462 linux_ustat(struct thread *td, struct linux_ustat_args *args) 463 { 464 #ifdef DEBUG 465 if (ldebug(ustat)) 466 printf(ARGS(ustat, "%d, *"), args->dev); 467 #endif 468 469 return (EOPNOTSUPP); 470 } 471 472 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 473 474 static int 475 stat64_copyout(struct stat *buf, void *ubuf) 476 { 477 struct l_stat64 lbuf; 478 479 bzero(&lbuf, sizeof(lbuf)); 480 lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 481 lbuf.st_ino = buf->st_ino; 482 lbuf.st_mode = buf->st_mode; 483 lbuf.st_nlink = buf->st_nlink; 484 lbuf.st_uid = buf->st_uid; 485 lbuf.st_gid = buf->st_gid; 486 lbuf.st_rdev = buf->st_rdev; 487 lbuf.st_size = buf->st_size; 488 lbuf.st_atime = buf->st_atime; 489 lbuf.st_mtime = buf->st_mtime; 490 lbuf.st_ctime = buf->st_ctime; 491 lbuf.st_blksize = buf->st_blksize; 492 lbuf.st_blocks = buf->st_blocks; 493 494 /* 495 * The __st_ino field makes all the difference. In the Linux kernel 496 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 497 * but without the assignment to __st_ino the runtime linker refuses 498 * to mmap(2) any shared libraries. I guess it's broken alright :-) 499 */ 500 lbuf.__st_ino = buf->st_ino; 501 502 return (copyout(&lbuf, ubuf, sizeof(lbuf))); 503 } 504 505 int 506 linux_stat64(struct thread *td, struct linux_stat64_args *args) 507 { 508 struct stat buf; 509 char *filename; 510 int error; 511 512 LCONVPATHEXIST(td, args->filename, &filename); 513 514 #ifdef DEBUG 515 if (ldebug(stat64)) 516 printf(ARGS(stat64, "%s, *"), filename); 517 #endif 518 519 error = kern_stat(td, filename, UIO_SYSSPACE, &buf); 520 if (!error) 521 translate_path_major_minor(td, filename, &buf); 522 LFREEPATH(filename); 523 if (error) 524 return (error); 525 return (stat64_copyout(&buf, args->statbuf)); 526 } 527 528 int 529 linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 530 { 531 struct stat sb; 532 char *filename; 533 int error; 534 535 LCONVPATHEXIST(td, args->filename, &filename); 536 537 #ifdef DEBUG 538 if (ldebug(lstat64)) 539 printf(ARGS(lstat64, "%s, *"), args->filename); 540 #endif 541 542 error = kern_lstat(td, filename, UIO_SYSSPACE, &sb); 543 if (!error) 544 translate_path_major_minor(td, filename, &sb); 545 LFREEPATH(filename); 546 if (error) 547 return (error); 548 return (stat64_copyout(&sb, args->statbuf)); 549 } 550 551 int 552 linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 553 { 554 struct stat buf; 555 int error; 556 557 #ifdef DEBUG 558 if (ldebug(fstat64)) 559 printf(ARGS(fstat64, "%d, *"), args->fd); 560 #endif 561 562 error = kern_fstat(td, args->fd, &buf); 563 translate_fd_major_minor(td, args->fd, &buf); 564 if (!error) 565 error = stat64_copyout(&buf, args->statbuf); 566 567 return (error); 568 } 569 570 int 571 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args) 572 { 573 char *path; 574 int error, dfd, flag; 575 struct stat buf; 576 577 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 578 return (EINVAL); 579 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 580 AT_SYMLINK_NOFOLLOW : 0; 581 582 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 583 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 584 585 #ifdef DEBUG 586 if (ldebug(fstatat64)) 587 printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag); 588 #endif 589 590 error = kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf); 591 translate_path_major_minor_at(td, args->pathname, &buf, dfd); 592 if (!error) 593 error = stat64_copyout(&buf, args->statbuf); 594 LFREEPATH(path); 595 596 return (error); 597 } 598 599 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 600