diff options
author | jilles <jilles@FreeBSD.org> | 2014-04-13 19:48:28 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2014-04-13 19:48:28 +0000 |
commit | 448f99bff387c7a251e9f111ab0173d362a871d5 (patch) | |
tree | 6684c15d02279ca1e36b837a2c278c819bbeac50 /lib | |
parent | b4545ba27b92f6e63b2dc0d22151ab056207f3fb (diff) | |
download | FreeBSD-src-448f99bff387c7a251e9f111ab0173d362a871d5.zip FreeBSD-src-448f99bff387c7a251e9f111ab0173d362a871d5.tar.gz |
realpath(): Properly fail "." or ".." components after non-directories.
If realpath() is called on pathnames like "/dev/null/." or "/dev/null/..",
it should fail with [ENOTDIR]. Pathnames like "/dev/null/" already failed as
they should.
Also, put the check for non-directories after lstatting the previous
component instead of when the empty component (consecutive or trailing
slashes) is detected, saving an lstat() call and some lines of code.
PR: kern/82980
MFC after: 2 weeks
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/stdlib/realpath.c | 26 |
1 files changed, 6 insertions, 20 deletions
diff --git a/lib/libc/stdlib/realpath.c b/lib/libc/stdlib/realpath.c index a2a9329..c4bd953 100644 --- a/lib/libc/stdlib/realpath.c +++ b/lib/libc/stdlib/realpath.c @@ -132,26 +132,7 @@ realpath(const char * __restrict path, char * __restrict resolved) resolved[resolved_len] = '\0'; } if (next_token[0] == '\0') { - /* - * Handle consequential slashes. The path - * before slash shall point to a directory. - * - * Only the trailing slashes are not covered - * by other checks in the loop, but we verify - * the prefix for any (rare) "//" or "/\0" - * occurrence to not implement lookahead. - */ - if (lstat(resolved, &sb) != 0) { - if (m) - free(resolved); - return (NULL); - } - if (!S_ISDIR(sb.st_mode)) { - if (m) - free(resolved); - errno = ENOTDIR; - return (NULL); - } + /* Handle consequential slashes. */ continue; } else if (strcmp(next_token, ".") == 0) @@ -236,6 +217,11 @@ realpath(const char * __restrict path, char * __restrict resolved) } } left_len = strlcpy(left, symlink, sizeof(left)); + } else if (!S_ISDIR(sb.st_mode) && p != NULL) { + if (m) + free(resolved); + errno = ENOTDIR; + return (NULL); } } |