Klaus Demo nginx / 86c5513
Disable symlinks: use O_SEARCH|O_DIRECTORY to open path components. Valentin Bartenev 10 years ago
2 changed file(s) with 32 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
564564 u_char *p, *cp, *end;
565565 ngx_fd_t at_fd;
566566 ngx_str_t at_name;
567 ngx_file_info_t fi;
568567
569568 if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_OFF) {
570569 fd = ngx_open_file(name->data, mode, create, access);
585584
586585 if (*p == '/') {
587586 at_fd = ngx_open_file("/",
588 NGX_FILE_RDONLY|NGX_FILE_NONBLOCK,
587 NGX_FILE_SEARCH|NGX_FILE_NONBLOCK,
589588 NGX_FILE_OPEN, 0);
590589
591590 if (at_fd == NGX_INVALID_FILE) {
616615
617616 if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_NOTOWNER) {
618617 fd = ngx_openat_file_owner(at_fd, p,
619 NGX_FILE_RDONLY|NGX_FILE_NONBLOCK,
618 NGX_FILE_SEARCH|NGX_FILE_NONBLOCK,
620619 NGX_FILE_OPEN, 0, log);
621620
622621 } else {
623622 fd = ngx_openat_file(at_fd, p,
624 NGX_FILE_RDONLY|NGX_FILE_NONBLOCK|NGX_FILE_NOFOLLOW,
623 NGX_FILE_SEARCH|NGX_FILE_NONBLOCK|NGX_FILE_NOFOLLOW,
625624 NGX_FILE_OPEN, 0);
626625 }
627626
646645 if (p == end) {
647646
648647 /*
649 * If pathname ends with a trailing slash, check if last path
650 * component is a directory; if not, fail with ENOTDIR as per
651 * POSIX.
648 * If pathname ends with a trailing slash, assume the last path
649 * component is a directory and reopen it with requested flags;
650 * if not, fail with ENOTDIR as per POSIX.
652651 *
653 * We use separate check instead of O_DIRECTORY in the loop above,
654 * as O_DIRECTORY doesn't work on FreeBSD 8.
655 *
656 * Note this returns already opened file descriptor, with different
657 * mode/create/access. This is believed to be safe as we don't
658 * use this codepath to create directories.
652 * We cannot rely on O_DIRECTORY in the loop above to check
653 * that the last path component is a directory because
654 * O_DIRECTORY doesn't work on FreeBSD 8. Fortunately, by
655 * reopening a directory, we don't depend on it at all.
659656 */
660657
661 if (ngx_fd_info(at_fd, &fi) == NGX_FILE_ERROR) {
662 of->err = ngx_errno;
663 of->failed = ngx_fd_info_n;
664 fd = NGX_INVALID_FILE;
665
666 goto failed;
667 }
668
669 if (ngx_is_dir(&fi)) {
670 return at_fd;
671 }
672
673 of->err = ENOTDIR;
674 of->failed = ngx_openat_file_n;
675 fd = NGX_INVALID_FILE;
676
677 goto failed;
658 fd = ngx_openat_file(at_fd, ".", mode, create, access);
659 goto done;
678660 }
679661
680662 if (of->disable_symlinks == NGX_DISABLE_SYMLINKS_NOTOWNER
685667 } else {
686668 fd = ngx_openat_file(at_fd, p, mode|NGX_FILE_NOFOLLOW, create, access);
687669 }
670
671 done:
688672
689673 if (fd == NGX_INVALID_FILE) {
690674 of->err = ngx_errno;
7777
7878 #if (NGX_HAVE_OPENAT)
7979 #define NGX_FILE_NOFOLLOW O_NOFOLLOW
80 #endif
80
81 #if defined(O_DIRECTORY)
82 #define NGX_FILE_DIRECTORY O_DIRECTORY
83 #else
84 #define NGX_FILE_DIRECTORY 0
85 #endif
86
87 #if defined(O_SEARCH)
88 #define NGX_FILE_SEARCH O_SEARCH|NGX_FILE_DIRECTORY
89
90 #elif defined(O_EXEC)
91 #define NGX_FILE_SEARCH O_EXEC|NGX_FILE_DIRECTORY
92
93 #else
94 #define NGX_FILE_SEARCH O_RDONLY|NGX_FILE_DIRECTORY
95 #endif
96
97 #endif /* NGX_HAVE_OPENAT */
8198
8299 #define NGX_FILE_DEFAULT_ACCESS 0644
83100 #define NGX_FILE_OWNER_ACCESS 0600