bin/166056: [patch][bin] find fails with .: permission denied, even when using absolute paths

Matthew Story matthewstory at gmail.com
Tue Mar 13 19:10:04 UTC 2012


>Number:         166056
>Category:       bin
>Synopsis:       [patch][bin] find fails with .: permission denied, even when using absolute paths
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Mar 13 19:10:03 UTC 2012
>Closed-Date:
>Last-Modified:
>Originator:     Matthew Story
>Release:        8.2
>Organization:
>Environment:
FreeBSD axe0.blackskyresearch.net 8.2-RELEASE-p2 FreeBSD 8.2-RELEASE-p2 #0: Sun Jul  3 00:57:18 EDT 2011     root at osprey.blackskyresearch.net:/usr/obj/usr/src/sys/blackskyresearch-amd64-8-2-20110702  amd64
>Description:
When executing find on an absolute path (or many absolute path), find fatally errors if it cannot open ".".  This is particularly problematic when running find (or programs that exec find) from cron as an underprivileged user.

I can not find any reference to cwd in the POSIX 2008 specification for find, so it likely seems that this is not a violation of the specification, but it is also unnecessary.  The only reason for opening "." is for the -exec primative to fork&exec in cwd of the bounding process, due to chdir calls in fts_set.  If we set the FTS_NOCHDIR flag if we cannot open "." the need to chdir prior to -exec is removed.

Alex Ginzburg <alex at SPAMFREE tablethotels.com> originally discovered this behavior, so if the solution is picked up (or resolved in another way) he should be credited in the Reported By.
>How-To-Repeat:
>From shell

$ su -
# install -d -m 700 testfind
# cd testfind
# su notroot
$ find /usr/src/usr.bin/find
find: .: Permission denied 

Or from cron (original discovery case):

*  *   *   *   *   notroot find /usr/src/usr.bin/find # ...

The workaround we have been using is to always cd to a safe place before running find, or a script that makes use of find ...
>Fix:
apply patch (patch was made against -CURRENT).  patch will warn if it cannot open ".", and set the FTL_NOCHDIR flag before proceeding, below cases demonstrate functionality with&without the -exec flag

$ su -
# install -d -m 700 testfind
# cd testfind
# su notroot
$ # without exec flag
$ find /usr/src/usr.bin/find
find: .: Permission denied
/usr/src/usr.bin/find
/usr/src/usr.bin/find/extern.h
/usr/src/usr.bin/find/find.c
/usr/src/usr.bin/find/Makefile
/usr/src/usr.bin/find/function.c
/usr/src/usr.bin/find/getdate.y
/usr/src/usr.bin/find/find.h
/usr/src/usr.bin/find/find.core
/usr/src/usr.bin/find/main.c
/usr/src/usr.bin/find/find.1
/usr/src/usr.bin/find/operator.c
/usr/src/usr.bin/find/misc.c
/usr/src/usr.bin/find/option.c
/usr/src/usr.bin/find/ls.c
$ with exec flag
$ find /usr/src/usr.bin/find -exec pwd \;
find: .: Permission denied
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind
/usr/home/notroot/testfind

I think the warning here is now superfluous, but I kept it as I thought some might be used to seeing this as a fatal, and it might be less arresting to lower to a warning level error as opposed to removing it as an error condition all together.

Patch attached with submission follows:

Index: function.c
===================================================================
--- function.c	(revision 232929)
+++ function.c	(working copy)
@@ -644,7 +644,8 @@
 		/* NOTREACHED */
 	case 0:
 		/* change dir back from where we started */
-		if (!(plan->flags & F_EXECDIR) && fchdir(dotfd)) {
+		if (!(plan->flags & F_EXECDIR) && !(ftsoptions&FTS_NOCHDIR)
+			&& fchdir(dotfd)) {
 			warn("chdir");
 			_exit(1);
 		}
Index: find.c
===================================================================
--- find.c	(revision 232929)
+++ find.c	(working copy)
@@ -179,7 +179,7 @@
 
 	tree = fts_open(paths, ftsoptions, (issort ? find_compare : NULL));
 	if (tree == NULL)
-		err(1, "ftsopen");
+		err(1, "fts_open");
 
 	for (rval = 0; (entry = fts_read(tree)) != NULL;) {
 		if (maxdepth != -1 && entry->fts_level >= maxdepth) {
Index: main.c
===================================================================
--- main.c	(revision 232929)
+++ main.c	(working copy)
@@ -63,7 +63,7 @@
 
 time_t now;			/* time find was run */
 int dotfd;			/* starting directory */
-int ftsoptions;			/* options for the ftsopen(3) call */
+int ftsoptions;			/* options for the fts_open(3) call */
 int isdeprecated;		/* using deprecated syntax */
 int isdepth;			/* do directories on post-order visit */
 int isoutput;			/* user specified output operator */
@@ -150,8 +150,10 @@
 		usage();
 	*p = NULL;
 
-	if ((dotfd = open(".", O_RDONLY, 0)) < 0)
-		err(1, ".");
+	if ((dotfd = open(".", O_RDONLY, 0)) < 0) {
+		warn(".");
+		ftsoptions |= FTS_NOCHDIR;
+	}
 
 	exit(find_execute(find_formplan(argv), start));
 }


>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list