Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 | /* * linux/fs/namei.c * * Copyright (C) 1991, 1992 Linus Torvalds */ /* * Some corrections by tytso. */ #include <asm/segment.h> #include <linux/errno.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/string.h> #include <linux/fcntl.h> #include <linux/stat.h> #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE]) /* * How long a filename can we get from user space? * -EFAULT if invalid area * 0 if ok (ENAMETOOLONG before EFAULT) * >0 EFAULT after xx bytes */ static inline int get_max_filename(unsigned long address) { struct vm_area_struct * vma; if (get_fs() == KERNEL_DS) return 0; for (vma = current->mm->mmap ; ; vma = vma->vm_next) { if (!vma) return -EFAULT; if (vma->vm_end > address) break; } if (vma->vm_start > address || !(vma->vm_page_prot & PAGE_USER)) return -EFAULT; address = vma->vm_end - address; if (address > PAGE_SIZE) return 0; if (vma->vm_next && vma->vm_next->vm_start == vma->vm_end && (vma->vm_next->vm_page_prot & PAGE_USER)) return 0; return address; } /* * In order to reduce some races, while at the same time doing additional * checking and hopefully speeding things up, we copy filenames to the * kernel data space before using them.. * * POSIX.1 2.4: an empty pathname is invalid (ENOENT). */ int getname(const char * filename, char **result) { int i, error; unsigned long page; char * tmp, c; i = get_max_filename((unsigned long) filename); if (i < 0) return i; error = -EFAULT; if (!i) { error = -ENAMETOOLONG; i = PAGE_SIZE; } c = get_fs_byte(filename++); if (!c) return -ENOENT; if(!(page = __get_free_page(GFP_KERNEL))) return -ENOMEM; *result = tmp = (char *) page; while (--i) { *(tmp++) = c; c = get_fs_byte(filename++); if (!c) { *tmp = '\0'; return 0; } } free_page(page); return error; } void putname(char * name) { free_page((unsigned long) name); } /* * permission() * * is used to check for read/write/execute permissions on a file. * We use "fsuid" for this, letting us set arbitrary permissions * for filesystem access without changing the "normal" uids which * are used for other things.. */ int permission(struct inode * inode,int mask) { int mode = inode->i_mode; if (inode->i_op && inode->i_op->permission) return inode->i_op->permission(inode, mask); else if ((mask & S_IWOTH) && IS_IMMUTABLE(inode)) return 0; /* Nobody gets write access to an immutable file */ else if (current->fsuid == inode->i_uid) mode >>= 6; else if (in_group_p(inode->i_gid)) mode >>= 3; if (((mode & mask & 0007) == mask) || fsuser()) return 1; return 0; } /* * lookup() looks up one part of a pathname, using the fs-dependent * routines (currently minix_lookup) for it. It also checks for * fathers (pseudo-roots, mount-points) */ int lookup(struct inode * dir,const char * name, int len, struct inode ** result) { struct super_block * sb; int perm; *result = NULL; if (!dir) return -ENOENT; /* check permissions before traversing mount-points */ perm = permission(dir,MAY_EXEC); if (len==2 && name[0] == '.' && name[1] == '.') { if (dir == current->fs->root) { *result = dir; return 0; } else if ((sb = dir->i_sb) && (dir == sb->s_mounted)) { sb = dir->i_sb; iput(dir); dir = sb->s_covered; if (!dir) return -ENOENT; dir->i_count++; } } if (!dir->i_op || !dir->i_op->lookup) { iput(dir); return -ENOTDIR; } if (!perm) { iput(dir); return -EACCES; } if (!len) { *result = dir; return 0; } return dir->i_op->lookup(dir,name,len,result); } int follow_link(struct inode * dir, struct inode * inode, int flag, int mode, struct inode ** res_inode) { if (!dir || !inode) { iput(dir); iput(inode); *res_inode = NULL; return -ENOENT; } if (!inode->i_op || !inode->i_op->follow_link) { iput(dir); *res_inode = inode; return 0; } return inode->i_op->follow_link(dir,inode,flag,mode,res_inode); } /* * dir_namei() * * dir_namei() returns the inode of the directory of the * specified name, and the name within that directory. */ static int dir_namei(const char * pathname, int * namelen, const char ** name, struct inode * base, struct inode ** res_inode) { char c; const char * thisname; int len,error; struct inode * inode; *res_inode = NULL; if (!base) { base = current->fs->pwd; base->i_count++; } if ((c = *pathname) == '/') { iput(base); base = current->fs->root; pathname++; base->i_count++; } while (1) { thisname = pathname; for(len=0;(c = *(pathname++))&&(c != '/');len++) /* nothing */ ; if (!c) break; base->i_count++; error = lookup(base,thisname,len,&inode); if (error) { iput(base); return error; } error = follow_link(base,inode,0,0,&base); if (error) return error; } if (!base->i_op || !base->i_op->lookup) { iput(base); return -ENOTDIR; } *name = thisname; *namelen = len; *res_inode = base; return 0; } static int _namei(const char * pathname, struct inode * base, int follow_links, struct inode ** res_inode) { const char * basename; int namelen,error; struct inode * inode; *res_inode = NULL; error = dir_namei(pathname,&namelen,&basename,base,&base); if (error) return error; base->i_count++; /* lookup uses up base */ error = lookup(base,basename,namelen,&inode); if (error) { iput(base); return error; } if (follow_links) { error = follow_link(base,inode,0,0,&inode); if (error) return error; } else iput(base); *res_inode = inode; return 0; } int lnamei(const char * pathname, struct inode ** res_inode) { int error; char * tmp; error = getname(pathname,&tmp); if (!error) { error = _namei(tmp,NULL,0,res_inode); putname(tmp); } return error; } /* * namei() * * is used by most simple commands to get the inode of a specified name. * Open, link etc use their own routines, but this is enough for things * like 'chmod' etc. */ int namei(const char * pathname, struct inode ** res_inode) { int error; char * tmp; error = getname(pathname,&tmp); if (!error) { error = _namei(tmp,NULL,1,res_inode); putname(tmp); } return error; } /* * open_namei() * * namei for open - this is in fact almost the whole open-routine. * * Note that the low bits of "flag" aren't the same as in the open * system call - they are 00 - no permissions needed * 01 - read permission needed * 10 - write permission needed * 11 - read/write permissions needed * which is a lot more logical, and also allows the "no perm" needed * for symlinks (where the permissions are checked later). */ int open_namei(const char * pathname, int flag, int mode, struct inode ** res_inode, struct inode * base) { const char * basename; int namelen,error; struct inode * dir, *inode; struct task_struct ** p; mode &= S_IALLUGO & ~current->fs->umask; mode |= S_IFREG; error = dir_namei(pathname,&namelen,&basename,base,&dir); if (error) return error; if (!namelen) { /* special case: '/usr/' etc */ if (flag & 2) { iput(dir); return -EISDIR; } /* thanks to Paul Pluzhnikov for noticing this was missing.. */ if (!permission(dir,ACC_MODE(flag))) { iput(dir); return -EACCES; } *res_inode=dir; return 0; } dir->i_count++; /* lookup eats the dir */ if (flag & O_CREAT) { down(&dir->i_sem); error = lookup(dir,basename,namelen,&inode); if (!error) { if (flag & O_EXCL) { iput(inode); error = -EEXIST; } } else if (!permission(dir,MAY_WRITE | MAY_EXEC)) error = -EACCES; else if (!dir->i_op || !dir->i_op->create) error = -EACCES; else if (IS_RDONLY(dir)) error = -EROFS; else { dir->i_count++; /* create eats the dir */ error = dir->i_op->create(dir,basename,namelen,mode,res_inode); up(&dir->i_sem); iput(dir); return error; } up(&dir->i_sem); } else error = lookup(dir,basename,namelen,&inode); if (error) { iput(dir); return error; } error = follow_link(dir,inode,flag,mode,&inode); if (error) return error; if (S_ISDIR(inode->i_mode) && (flag & 2)) { iput(inode); return -EISDIR; } if (!permission(inode,ACC_MODE(flag))) { iput(inode); return -EACCES; } if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) { if (IS_NODEV(inode)) { iput(inode); return -EACCES; } } else { if (IS_RDONLY(inode) && (flag & 2)) { iput(inode); return -EROFS; } } if ((inode->i_count > 1) && (flag & 2)) { for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) { struct vm_area_struct * mpnt; if (!*p) continue; for(mpnt = (*p)->mm->mmap; mpnt; mpnt = mpnt->vm_next) { if (inode != mpnt->vm_inode) continue; if (mpnt->vm_page_prot & PAGE_RW) continue; if (mpnt->vm_flags & VM_DENYWRITE) { iput(inode); return -ETXTBSY; } } } } /* * An append-only file must be opened in append mode for writing */ if (IS_APPEND(inode) && ((flag & 2) && !(flag & O_APPEND))) { iput(inode); return -EPERM; } if (flag & O_TRUNC) { inode->i_size = 0; if (inode->i_op && inode->i_op->truncate) inode->i_op->truncate(inode); if ((error = notify_change(NOTIFY_SIZE, inode))) { iput(inode); return error; } inode->i_dirt = 1; } *res_inode = inode; return 0; } int do_mknod(const char * filename, int mode, dev_t dev) { const char * basename; int namelen, error; struct inode * dir; mode &= ~current->fs->umask; error = dir_namei(filename,&namelen,&basename, NULL, &dir); if (error) return error; if (!namelen) { iput(dir); return -ENOENT; } if (IS_RDONLY(dir)) { iput(dir); return -EROFS; } if (!permission(dir,MAY_WRITE | MAY_EXEC)) { iput(dir); return -EACCES; } if (!dir->i_op || !dir->i_op->mknod) { iput(dir); return -EPERM; } dir->i_count++; down(&dir->i_sem); error = dir->i_op->mknod(dir,basename,namelen,mode,dev); up(&dir->i_sem); iput(dir); return error; } asmlinkage int sys_mknod(const char * filename, int mode, dev_t dev) { int error; char * tmp; if (S_ISDIR(mode) || (!S_ISFIFO(mode) && !fsuser())) return -EPERM; switch (mode & S_IFMT) { case 0: mode |= S_IFREG; break; case S_IFREG: case S_IFCHR: case S_IFBLK: case S_IFIFO: break; default: return -EINVAL; } error = getname(filename,&tmp); if (!error) { error = do_mknod(tmp,mode,dev); putname(tmp); } return error; } static int do_mkdir(const char * pathname, int mode) { const char * basename; int namelen, error; struct inode * dir; error = dir_namei(pathname,&namelen,&basename,NULL,&dir); if (error) return error; if (!namelen) { iput(dir); return -ENOENT; } if (IS_RDONLY(dir)) { iput(dir); return -EROFS; } if (!permission(dir,MAY_WRITE | MAY_EXEC)) { iput(dir); return -EACCES; } if (!dir->i_op || !dir->i_op->mkdir) { iput(dir); return -EPERM; } dir->i_count++; down(&dir->i_sem); error = dir->i_op->mkdir(dir,basename,namelen,mode); up(&dir->i_sem); iput(dir); return error; } asmlinkage int sys_mkdir(const char * pathname, int mode) { int error; char * tmp; error = getname(pathname,&tmp); if (!error) { error = do_mkdir(tmp,mode); putname(tmp); } return error; } static int do_rmdir(const char * name) { const char * basename; int namelen, error; struct inode * dir; error = dir_namei(name,&namelen,&basename,NULL,&dir); if (error) return error; if (!namelen) { iput(dir); return -ENOENT; } if (IS_RDONLY(dir)) { iput(dir); return -EROFS; } if (!permission(dir,MAY_WRITE | MAY_EXEC)) { iput(dir); return -EACCES; } /* * A subdirectory cannot be removed from an append-only directory */ if (IS_APPEND(dir)) { iput(dir); return -EPERM; } if (!dir->i_op || !dir->i_op->rmdir) { iput(dir); return -EPERM; } return dir->i_op->rmdir(dir,basename,namelen); } asmlinkage int sys_rmdir(const char * pathname) { int error; char * tmp; error = getname(pathname,&tmp); if (!error) { error = do_rmdir(tmp); putname(tmp); } return error; } static int do_unlink(const char * name) { const char * basename; int namelen, error; struct inode * dir; error = dir_namei(name,&namelen,&basename,NULL,&dir); if (error) return error; if (!namelen) { iput(dir); return -EPERM; } if (IS_RDONLY(dir)) { iput(dir); return -EROFS; } if (!permission(dir,MAY_WRITE | MAY_EXEC)) { iput(dir); return -EACCES; } /* * A file cannot be removed from an append-only directory */ if (IS_APPEND(dir)) { iput(dir); return -EPERM; } if (!dir->i_op || !dir->i_op->unlink) { iput(dir); return -EPERM; } return dir->i_op->unlink(dir,basename,namelen); } asmlinkage int sys_unlink(const char * pathname) { int error; char * tmp; error = getname(pathname,&tmp); if (!error) { error = do_unlink(tmp); putname(tmp); } return error; } static int do_symlink(const char * oldname, const char * newname) { struct inode * dir; const char * basename; int namelen, error; error = dir_namei(newname,&namelen,&basename,NULL,&dir); if (error) return error; if (!namelen) { iput(dir); return -ENOENT; } if (IS_RDONLY(dir)) { iput(dir); return -EROFS; } if (!permission(dir,MAY_WRITE | MAY_EXEC)) { iput(dir); return -EACCES; } if (!dir->i_op || !dir->i_op->symlink) { iput(dir); return -EPERM; } dir->i_count++; down(&dir->i_sem); error = dir->i_op->symlink(dir,basename,namelen,oldname); up(&dir->i_sem); iput(dir); return error; } asmlinkage int sys_symlink(const char * oldname, const char * newname) { int error; char * from, * to; error = getname(oldname,&from); if (!error) { error = getname(newname,&to); if (!error) { error = do_symlink(from,to); putname(to); } putname(from); } return error; } static int do_link(struct inode * oldinode, const char * newname) { struct inode * dir; const char * basename; int namelen, error; error = dir_namei(newname,&namelen,&basename,NULL,&dir); if (error) { iput(oldinode); return error; } if (!namelen) { iput(oldinode); iput(dir); return -EPERM; } if (IS_RDONLY(dir)) { iput(oldinode); iput(dir); return -EROFS; } if (dir->i_dev != oldinode->i_dev) { iput(dir); iput(oldinode); return -EXDEV; } if (!permission(dir,MAY_WRITE | MAY_EXEC)) { iput(dir); iput(oldinode); return -EACCES; } /* * A link to an append-only or immutable file cannot be created */ if (IS_APPEND(oldinode) || IS_IMMUTABLE(oldinode)) { iput(dir); iput(oldinode); return -EPERM; } if (!dir->i_op || !dir->i_op->link) { iput(dir); iput(oldinode); return -EPERM; } dir->i_count++; down(&dir->i_sem); error = dir->i_op->link(oldinode, dir, basename, namelen); up(&dir->i_sem); iput(dir); return error; } asmlinkage int sys_link(const char * oldname, const char * newname) { int error; char * to; struct inode * oldinode; error = namei(oldname, &oldinode); if (error) return error; error = getname(newname,&to); if (error) { iput(oldinode); return error; } error = do_link(oldinode,to); putname(to); return error; } static int do_rename(const char * oldname, const char * newname) { struct inode * old_dir, * new_dir; const char * old_base, * new_base; int old_len, new_len, error; error = dir_namei(oldname,&old_len,&old_base,NULL,&old_dir); if (error) return error; if (!permission(old_dir,MAY_WRITE | MAY_EXEC)) { iput(old_dir); return -EACCES; } if (!old_len || (old_base[0] == '.' && (old_len == 1 || (old_base[1] == '.' && old_len == 2)))) { iput(old_dir); return -EPERM; } error = dir_namei(newname,&new_len,&new_base,NULL,&new_dir); if (error) { iput(old_dir); return error; } if (!permission(new_dir,MAY_WRITE | MAY_EXEC)) { iput(old_dir); iput(new_dir); return -EACCES; } if (!new_len || (new_base[0] == '.' && (new_len == 1 || (new_base[1] == '.' && new_len == 2)))) { iput(old_dir); iput(new_dir); return -EPERM; } if (new_dir->i_dev != old_dir->i_dev) { iput(old_dir); iput(new_dir); return -EXDEV; } if (IS_RDONLY(new_dir) || IS_RDONLY(old_dir)) { iput(old_dir); iput(new_dir); return -EROFS; } /* * A file cannot be removed from an append-only directory */ if (IS_APPEND(old_dir)) { iput(old_dir); iput(new_dir); return -EPERM; } if (!old_dir->i_op || !old_dir->i_op->rename) { iput(old_dir); iput(new_dir); return -EPERM; } new_dir->i_count++; down(&new_dir->i_sem); error = old_dir->i_op->rename(old_dir, old_base, old_len, new_dir, new_base, new_len); up(&new_dir->i_sem); iput(new_dir); return error; } asmlinkage int sys_rename(const char * oldname, const char * newname) { int error; char * from, * to; error = getname(oldname,&from); if (!error) { error = getname(newname,&to); if (!error) { error = do_rename(from,to); putname(to); } putname(from); } return error; } |