aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Khon <fjoe@FreeBSD.org>2001-10-10 19:49:30 +0000
committerMax Khon <fjoe@FreeBSD.org>2001-10-10 19:49:30 +0000
commitef935a1fae8887f91be48a73c2466f07702499c4 (patch)
tree0274a264f2ff0148ceea692ddc5ee989bc1e2ca2
parentcc6aa31940653b11f2879010d0a941575c770c07 (diff)
downloadsrc-ef935a1fae8887f91be48a73c2466f07702499c4.tar.gz
src-ef935a1fae8887f91be48a73c2466f07702499c4.zip
MFC rev 1.18: Protect the remaining instance of chdir() using
fts_safe_changedir().
Notes
Notes: svn path=/stable/3/; revision=84775
-rw-r--r--lib/libc/gen/fts.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/lib/libc/gen/fts.c b/lib/libc/gen/fts.c
index 14137a3bd1e5..0a4853e7c1ca 100644
--- a/lib/libc/gen/fts.c
+++ b/lib/libc/gen/fts.c
@@ -61,7 +61,7 @@ static void fts_padjust __P((FTS *, FTSENT *));
static int fts_palloc __P((FTS *, size_t));
static FTSENT *fts_sort __P((FTS *, FTSENT *, int));
static u_short fts_stat __P((FTS *, FTSENT *, int));
-static int fts_safe_changedir __P((FTS *, FTSENT *, int));
+static int fts_safe_changedir __P((FTS *, FTSENT *, int, char *));
#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
@@ -69,7 +69,6 @@ static int fts_safe_changedir __P((FTS *, FTSENT *, int));
#define ISSET(opt) (sp->fts_options & (opt))
#define SET(opt) (sp->fts_options |= (opt))
-#define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path))
#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
/* fts_build flags */
@@ -273,6 +272,7 @@ FTSENT *
fts_read(sp)
register FTS *sp;
{
+ struct stat sb;
register FTSENT *p, *tmp;
register int instr;
register char *t;
@@ -349,7 +349,7 @@ fts_read(sp)
* FTS_STOP or the fts_info field of the node.
*/
if (sp->fts_child) {
- if (fts_safe_changedir(sp, p, -1)) {
+ if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
p->fts_errno = errno;
p->fts_flags |= FTS_DONTCHDIR;
for (p = sp->fts_child; p; p = p->fts_link)
@@ -446,11 +446,10 @@ name: t = sp->fts_path + NAPPEND(p->fts_parent);
return (NULL);
}
(void)close(p->fts_symfd);
- } else if (!(p->fts_flags & FTS_DONTCHDIR)) {
- if (CHDIR(sp, "..")) {
- SET(FTS_STOP);
- return (NULL);
- }
+ } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
+ fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
+ SET(FTS_STOP);
+ return (NULL);
}
p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
return (sp->fts_cur = p);
@@ -637,7 +636,7 @@ fts_build(sp, type)
*/
cderrno = 0;
if (nlinks || type == BREAD) {
- if (fts_safe_changedir(sp, cur, dirfd(dirp))) {
+ if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
if (nlinks && type == BREAD)
cur->fts_errno = errno;
cur->fts_flags |= FTS_DONTCHDIR;
@@ -803,7 +802,8 @@ mem1: saved_errno = errno;
*/
if (descend && (type == BCHILD || !nitems) &&
(cur->fts_level == FTS_ROOTLEVEL ?
- FCHDIR(sp, sp->fts_rfd) : CHDIR(sp, ".."))) {
+ FCHDIR(sp, sp->fts_rfd) :
+ fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
cur->fts_info = FTS_ERR;
SET(FTS_STOP);
return (NULL);
@@ -1075,10 +1075,11 @@ fts_maxarglen(argv)
* Assumes p->fts_dev and p->fts_ino are filled in.
*/
static int
-fts_safe_changedir(sp, p, fd)
+fts_safe_changedir(sp, p, fd, path)
FTS *sp;
FTSENT *p;
int fd;
+ char *path;
{
int ret, oerrno, newfd;
struct stat sb;
@@ -1086,7 +1087,7 @@ fts_safe_changedir(sp, p, fd)
newfd = fd;
if (ISSET(FTS_NOCHDIR))
return (0);
- if (fd < 0 && (newfd = open(p->fts_accpath, O_RDONLY, 0)) < 0)
+ if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0)
return (-1);
if (fstat(newfd, &sb)) {
ret = -1;