Date: Tue, 26 Feb 2002 04:20:09 -0800 (PST) From: "Jose M. Alcaide" <jose@we.lc.ehu.es> To: freebsd-bugs@FreeBSD.org Subject: Re: bin/12244: realpath() fails when there is no permission to read the WD Message-ID: <200202261220.g1QCK9C67924@freefall.freebsd.org>
next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/12244; it has been noted by GNATS. From: "Jose M. Alcaide" <jose@we.lc.ehu.es> To: Sheldon Hearn <sheldonh@starjuice.net> Cc: iedowse@FreeBSD.org, bug-followup@FreeBSD.org Subject: Re: bin/12244: realpath() fails when there is no permission to read the WD Date: Tue, 26 Feb 2002 13:19:09 +0100 --UlVJffcvxoiEqYs2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Jan 31, 2002 at 05:13:11PM +0200, Sheldon Hearn wrote: > On Mon, 21 Jan 2002 02:15:34 +0100, "Jose M. Alcaide" wrote: > > Yes, it is. Realpath(3) continues using open()/fchdir() for > > saving/restoring the current directory. I think that the getwd()/chdir() > > method should be used as fallback. > > Could you submit a patch for that? I have written a patch (attached). I did not tested it, but it is pretty trivial. It is somewhat ugly, too ;-) -- ****** Jose M. Alcaide // jose@we.lc.ehu.es // jmas@FreeBSD.org ****** ** "Beware of Programmers who carry screwdrivers" -- Leonard Brandwein ** --UlVJffcvxoiEqYs2 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="realpath.c.patch" --- lib/libc/stdlib/realpath.c.orig Fri Jan 28 00:06:50 2000 +++ lib/libc/stdlib/realpath.c Tue Feb 26 13:16:31 2002 @@ -63,14 +63,19 @@ { struct stat sb; int fd, n, rootd, serrno; - char *p, *q, wbuf[MAXPATHLEN]; + char *p, *q, wbuf[MAXPATHLEN], scwd[MAXPATHLEN]; int symlinks = 0; - /* Save the starting point. */ - if ((fd = _open(".", O_RDONLY)) < 0) { - (void)strcpy(resolved, "."); - return (NULL); - } + /* + * Save the starting point: + * First, try open()/fchdir() method and, if this fails, + * then try getcwd()/chdir(). + */ + if ((fd = _open(".", O_RDONLY)) < 0) + if (getcwd(scwd, MAXPATHLEN) == 0) { + (void)strcpy(resolved, "."); + return (NULL); + } /* * Find the dirname and basename from the path to be resolved. @@ -96,7 +101,7 @@ q = resolved; } if (chdir(q) < 0) - goto err1; + goto err; } else p = resolved; @@ -105,17 +110,17 @@ if (S_ISLNK(sb.st_mode)) { if (++symlinks > MAXSYMLINKS) { errno = ELOOP; - goto err1; + goto err; } n = readlink(p, resolved, MAXPATHLEN - 1); if (n < 0) - goto err1; + goto err; resolved[n] = '\0'; goto loop; } if (S_ISDIR(sb.st_mode)) { if (chdir(p) < 0) - goto err1; + goto err; p = ""; } } @@ -126,7 +131,7 @@ */ (void)strcpy(wbuf, p); if (getcwd(resolved, MAXPATHLEN) == 0) - goto err1; + goto err; /* * Join the two strings together, ensuring that the right thing @@ -140,26 +145,38 @@ if (*wbuf) { if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) { errno = ENAMETOOLONG; - goto err1; + goto err; } if (rootd == 0) (void)strcat(resolved, "/"); (void)strcat(resolved, wbuf); } - /* Go back to where we came from. */ - if (fchdir(fd) < 0) { - serrno = errno; - goto err2; - } + /* + * Go back to where we came from. If fd < 0, we are using the + * getcwd()/chdir() method; else, we are using open()/fchdir(). + */ + if (fd < 0) { + if (chdir(scwd) < 0) + return (NULL); + } else if (fchdir(fd) < 0) { + serrno = errno; + (void)_close(fd); + errno = serrno; + return (NULL); + } /* It's okay if the close fails, what's an fd more or less? */ (void)_close(fd); return (resolved); -err1: serrno = errno; - (void)fchdir(fd); -err2: (void)_close(fd); +err: serrno = errno; + if (fd < 0) + (void)chdir(scwd); + else { + (void)fchdir(fd); + (void)_close(fd); + } errno = serrno; return (NULL); } --UlVJffcvxoiEqYs2-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200202261220.g1QCK9C67924>