defaults: ["libc_defaults"],
srcs: [
"upstream-netbsd/common/lib/libc/stdlib/random.c",
- "upstream-netbsd/lib/libc/gen/ftw.c",
- "upstream-netbsd/lib/libc/gen/nftw.c",
"upstream-netbsd/lib/libc/gen/nice.c",
"upstream-netbsd/lib/libc/gen/popen.c",
"upstream-netbsd/lib/libc/gen/psignal.c",
"bionic/fpclassify.cpp",
"bionic/fsetxattr.cpp",
"bionic/ftruncate.cpp",
+ "bionic/ftw.cpp",
"bionic/futimens.cpp",
"bionic/getcwd.cpp",
"bionic/getdomainname.cpp",
bionic/fpclassify.cpp \
bionic/fsetxattr.cpp \
bionic/ftruncate.cpp \
+ bionic/ftw.cpp \
bionic/futimens.cpp \
bionic/getcwd.cpp \
bionic/getdomainname.cpp \
libc_upstream_netbsd_src_files := \
upstream-netbsd/common/lib/libc/stdlib/random.c \
- upstream-netbsd/lib/libc/gen/ftw.c \
- upstream-netbsd/lib/libc/gen/nftw.c \
upstream-netbsd/lib/libc/gen/nice.c \
upstream-netbsd/lib/libc/gen/popen.c \
upstream-netbsd/lib/libc/gen/psignal.c \
--- /dev/null
+/*
+ * Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Sponsored in part by the Defense Advanced Research Projects
+ * Agency (DARPA) and Air Force Research Laboratory, Air Force
+ * Materiel Command, USAF, under agreement number F39502-99-1-0512.
+ */
+
+#include <errno.h>
+#include <fts.h>
+#include <ftw.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+static int do_nftw(const char *path,
+ int (*ftw_fn)(const char*, const struct stat*, int),
+ int (*nftw_fn)(const char*, const struct stat*, int, FTW*),
+ int nfds,
+ int nftw_flags) {
+ // TODO: nfds is currently unused.
+ if (nfds < 1) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ // Translate to fts_open options.
+ int fts_options = FTS_LOGICAL | FTS_COMFOLLOW | FTS_NOCHDIR;
+ if (nftw_fn) {
+ fts_options = FTS_COMFOLLOW | ((nftw_flags & FTW_PHYS) ? FTS_PHYSICAL : FTS_LOGICAL);
+ if (!(nftw_flags & FTW_CHDIR)) fts_options |= FTS_NOCHDIR;
+ if (nftw_flags & FTW_MOUNT) fts_options |= FTS_XDEV;
+ }
+ bool postorder = (nftw_flags & FTW_DEPTH) != 0;
+
+ // Call fts_open.
+ char* const paths[2] = { const_cast<char*>(path), nullptr };
+ FTS* fts = fts_open(paths, fts_options, nullptr);
+ if (fts == nullptr) {
+ return -1;
+ }
+
+ // Translate fts_read results into ftw/nftw callbacks.
+ int error = 0;
+ FTSENT* cur;
+ while (error == 0 && (cur = fts_read(fts)) != nullptr) {
+ int fn_flag;
+ switch (cur->fts_info) {
+ case FTS_D:
+ // In the postorder case, we'll translate FTS_DP to FTW_DP later.
+ // In the can't-access case, we'll translate FTS_DNR to FTW_DNR later.
+ if (postorder || access(cur->fts_path, R_OK) == -1) continue;
+ fn_flag = FTW_D;
+ break;
+ case FTS_DNR:
+ fn_flag = FTW_DNR;
+ break;
+ case FTS_DP:
+ if (!postorder) continue;
+ fn_flag = FTW_DP;
+ break;
+ case FTS_F:
+ case FTS_DEFAULT:
+ fn_flag = FTW_F;
+ break;
+ case FTS_NS:
+ case FTS_NSOK:
+ fn_flag = FTW_NS;
+ break;
+ case FTS_SL:
+ fn_flag = FTW_SL;
+ break;
+ case FTS_SLNONE:
+ fn_flag = (nftw_fn != nullptr) ? FTW_SLN : FTW_NS;
+ break;
+ case FTS_DC:
+ errno = ELOOP;
+ error = -1;
+ continue;
+ default:
+ error = -1;
+ continue;
+ }
+
+ // Call the appropriate function.
+ if (nftw_fn != nullptr) {
+ FTW ftw;
+ ftw.base = cur->fts_pathlen - cur->fts_namelen;
+ ftw.level = cur->fts_level;
+ error = nftw_fn(cur->fts_path, cur->fts_statp, fn_flag, &ftw);
+ } else {
+ error = ftw_fn(cur->fts_path, cur->fts_statp, fn_flag);
+ }
+ }
+
+ int saved_errno = errno;
+ if (fts_close(fts) != 0 && error == 0) {
+ error = -1;
+ } else {
+ errno = saved_errno;
+ }
+ return error;
+}
+
+int ftw(const char* path, int (*ftw_fn)(const char*, const struct stat*, int), int nfds) {
+ return do_nftw(path, ftw_fn, nullptr, nfds, 0);
+}
+
+int nftw(const char* path, int (*nftw_fn)(const char*, const struct stat*, int, FTW*),
+ int nfds, int nftw_flags) {
+ return do_nftw(path, nullptr, nftw_fn, nfds, nftw_flags);
+}
+++ /dev/null
-/* $NetBSD: ftw.c,v 1.1 2005/12/30 23:07:32 agc Exp $ */
-
-/* From OpenBSD: ftw.c,v 1.2 2003/07/21 21:15:32 millert Exp */
-
-/*
- * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- * Sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F39502-99-1-0512.
- */
-#include <sys/cdefs.h>
-
-#ifndef lint
-__RCSID("$NetBSD: ftw.c,v 1.1 2005/12/30 23:07:32 agc Exp $");
-#endif
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fts.h>
-#include <ftw.h>
-#include <limits.h>
-
-int
-ftw(const char *path, int (*fn)(const char *, const struct stat *, int),
- int nfds)
-{
- /* LINTED */
- char * const paths[2] = { __UNCONST(path), NULL };
- FTSENT *cur;
- FTS *ftsp;
- int fnflag, error, sverrno;
-
- /* XXX - nfds is currently unused */
- if (nfds < 1 || nfds > OPEN_MAX) {
- errno = EINVAL;
- return (-1);
- }
-
- ftsp = fts_open(paths, FTS_COMFOLLOW | FTS_NOCHDIR, NULL);
- if (ftsp == NULL)
- return (-1);
- error = 0;
- while ((cur = fts_read(ftsp)) != NULL) {
- switch (cur->fts_info) {
- case FTS_D:
- fnflag = FTW_D;
- break;
- case FTS_DNR:
- fnflag = FTW_DNR;
- break;
- case FTS_DP:
- /* we only visit in preorder */
- continue;
- case FTS_F:
- case FTS_DEFAULT:
- fnflag = FTW_F;
- break;
- case FTS_NS:
- case FTS_NSOK:
- case FTS_SLNONE:
- fnflag = FTW_NS;
- break;
- case FTS_SL:
- fnflag = FTW_SL;
- break;
- case FTS_DC:
- errno = ELOOP;
- /* FALLTHROUGH */
- default:
- error = -1;
- goto done;
- }
- error = fn(cur->fts_path, cur->fts_statp, fnflag);
- if (error != 0)
- break;
- }
-done:
- sverrno = errno;
- if (fts_close(ftsp) != 0 && error == 0)
- error = -1;
- else
- errno = sverrno;
- return (error);
-}
+++ /dev/null
-/* $NetBSD */
-
-/* From OpenBSD: nftw.c,v 1.2 2003/07/21 21:15:32 millert Exp */
-
-/*
- * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- * Sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F39502-99-1-0512.
- */
-
-#include <sys/cdefs.h>
-
-#ifndef lint
-__RCSID("$NetBSD: nftw.c,v 1.1 2005/12/30 23:07:32 agc Exp $");
-#endif
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fts.h>
-#include <ftw.h>
-#include <limits.h>
-
-int
-nftw(const char *path, int (*fn)(const char *, const struct stat *, int,
- struct FTW *), int nfds, int ftwflags)
-{
- /* LINTED */
- char * const paths[2] = { __UNCONST(path), NULL };
- struct FTW f;
- FTSENT *cur;
- FTS *ftsp;
- int ftsflags, fnflag, error, postorder, sverrno;
-
- /* XXX - nfds is currently unused */
- if (nfds < 1 || nfds > OPEN_MAX) {
- errno = EINVAL;
- return (-1);
- }
-
- ftsflags = FTS_COMFOLLOW;
- if (!(ftwflags & FTW_CHDIR))
- ftsflags |= FTS_NOCHDIR;
- if (ftwflags & FTW_MOUNT)
- ftsflags |= FTS_XDEV;
- if (ftwflags & FTW_PHYS)
- ftsflags |= FTS_PHYSICAL;
- postorder = (ftwflags & FTW_DEPTH) != 0;
- ftsp = fts_open(paths, ftsflags, NULL);
- if (ftsp == NULL)
- return (-1);
- error = 0;
- while ((cur = fts_read(ftsp)) != NULL) {
- switch (cur->fts_info) {
- case FTS_D:
- if (postorder)
- continue;
- fnflag = FTW_D;
- break;
- case FTS_DNR:
- fnflag = FTW_DNR;
- break;
- case FTS_DP:
- if (!postorder)
- continue;
- fnflag = FTW_DP;
- break;
- case FTS_F:
- case FTS_DEFAULT:
- fnflag = FTW_F;
- break;
- case FTS_NS:
- case FTS_NSOK:
- fnflag = FTW_NS;
- break;
- case FTS_SL:
- fnflag = FTW_SL;
- break;
- case FTS_SLNONE:
- fnflag = FTW_SLN;
- break;
- case FTS_DC:
- errno = ELOOP;
- /* FALLTHROUGH */
- default:
- error = -1;
- goto done;
- }
- f.base = cur->fts_pathlen - cur->fts_namelen;
- f.level = cur->fts_level;
- error = fn(cur->fts_path, cur->fts_statp, fnflag, &f);
- if (error != 0)
- break;
- }
-done:
- sverrno = errno;
- (void) fts_close(ftsp);
- errno = sverrno;
- return (error);
-}
#include <ftw.h>
+#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "TemporaryFile.h"
+#include <android-base/stringprintf.h>
#include <gtest/gtest.h>
static void MakeTree(const char* root) {
snprintf(path, sizeof(path), "%s/dangler", root);
ASSERT_EQ(0, symlink("/does-not-exist", path));
snprintf(path, sizeof(path), "%s/symlink", root);
- ASSERT_EQ(0, symlink("sub2", path));
+ ASSERT_EQ(0, symlink("dir/sub", path));
int fd;
snprintf(path, sizeof(path), "%s/regular", root);
ASSERT_TRUE(fpath != NULL);
ASSERT_TRUE(sb != NULL);
+ // Was it a case where the struct stat we're given is meaningless?
+ if (tflag == FTW_NS || tflag == FTW_SLN) {
+ // If so, double-check that we really can't stat.
+ struct stat sb;
+ EXPECT_EQ(-1, stat(fpath, &sb));
+ return;
+ }
+
+ // Otherwise check that the struct stat matches the type flag.
if (S_ISDIR(sb->st_mode)) {
- EXPECT_TRUE(tflag == FTW_D || tflag == FTW_DNR || tflag == FTW_DP) << fpath;
+ if (access(fpath, R_OK) == 0) {
+ EXPECT_TRUE(tflag == FTW_D || tflag == FTW_DP) << fpath << ' ' << tflag;
+ } else {
+ EXPECT_EQ(FTW_DNR, tflag) << fpath;
+ }
} else if (S_ISLNK(sb->st_mode)) {
EXPECT_EQ(FTW_SL, tflag) << fpath;
} else {
}
}
-void sanity_check_nftw(const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
+void sanity_check_nftw(const char* fpath, const struct stat* sb, int tflag, FTW* ftwbuf) {
sanity_check_ftw(fpath, sb, tflag);
ASSERT_EQ('/', fpath[ftwbuf->base - 1]) << fpath;
}
return 0;
}
-int check_nftw(const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
+int check_nftw(const char* fpath, const struct stat* sb, int tflag, FTW* ftwbuf) {
sanity_check_nftw(fpath, sb, tflag, ftwbuf);
return 0;
}
-int check_nftw64(const char* fpath, const struct stat64* sb, int tflag, struct FTW* ftwbuf) {
+int check_nftw64(const char* fpath, const struct stat64* sb, int tflag, FTW* ftwbuf) {
sanity_check_nftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag, ftwbuf);
return 0;
}
MakeTree(root.dirname);
ASSERT_EQ(0, nftw64(root.dirname, check_nftw64, 128, 0));
}
+
+template <typename StatT>
+static int bug_28197840_ftw(const char* path, const StatT*, int flag) {
+ EXPECT_EQ(strstr(path, "unreadable") != nullptr ? FTW_DNR : FTW_D, flag) << path;
+ return 0;
+}
+
+template <typename StatT>
+static int bug_28197840_nftw(const char* path, const StatT* sb, int flag, FTW*) {
+ return bug_28197840_ftw(path, sb, flag);
+}
+
+TEST(ftw, bug_28197840) {
+ // Drop root for this test, because root can still read directories even if
+ // permissions would imply otherwise.
+ if (getuid() == 0) {
+ passwd* pwd = getpwnam("shell");
+ ASSERT_EQ(0, setuid(pwd->pw_uid));
+ }
+
+ TemporaryDir root;
+
+ std::string path = android::base::StringPrintf("%s/unreadable-directory", root.dirname);
+ ASSERT_EQ(0, mkdir(path.c_str(), 0000)) << path;
+
+ ASSERT_EQ(0, ftw(root.dirname, bug_28197840_ftw<struct stat>, 128));
+ ASSERT_EQ(0, ftw64(root.dirname, bug_28197840_ftw<struct stat64>, 128));
+ ASSERT_EQ(0, nftw(root.dirname, bug_28197840_nftw<struct stat>, 128, FTW_PHYS));
+ ASSERT_EQ(0, nftw64(root.dirname, bug_28197840_nftw<struct stat64>, 128, FTW_PHYS));
+}