From nobody@FreeBSD.org  Fri Oct 31 00:39:26 2008
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id C37AD106564A
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 31 Oct 2008 00:39:26 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id B08B88FC08
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 31 Oct 2008 00:39:26 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id m9V0dQIY020087
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 31 Oct 2008 00:39:26 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id m9V0dQPN020086;
	Fri, 31 Oct 2008 00:39:26 GMT
	(envelope-from nobody)
Message-Id: <200810310039.m9V0dQPN020086@www.freebsd.org>
Date: Fri, 31 Oct 2008 00:39:26 GMT
From: Ben Jackson <ben@ben.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: find exits if -fstype test fails with EACCES
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         128493
>Category:       bin
>Synopsis:       [patch] find(1) exits if -fstype test fails with EACCES
>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:   Fri Oct 31 00:40:00 UTC 2008
>Closed-Date:    
>Last-Modified:  Fri Apr  3 16:30:03 UTC 2009
>Originator:     Ben Jackson
>Release:        7.1-BETA
>Organization:
>Environment:
FreeBSD kronos.home.ben.com 7.1-BETA FreeBSD 7.1-BETA #0: Wed Sep 17 23:09:13 PDT 2008     bjj@kronos.home.ben.com:/usr/src/sys/amd64/compile/GENERIC  amd64
>Description:
I have a filesystem mounted on /bak which is mode 770 (actually the dir is mode 660 and a smbfs is mounted on it which promotes that to 770 on the directory nodes).  The locate db fails to build because it uses find with -fstype which exits very early when statfs returns EACCES on /bak.

I don't know if it's actually a bug that statfs is failing (so this may need to be resolved in the kernel) but it's easy to tolerate it in find.


>How-To-Repeat:
mkdir /bak
chmod 660 /bak
mount_smbfs ... /bak
/etc/periodic/weekly/310.locate
# or better, su to nobody and run the find in /usr/libexec/locate.updatedb by hand

>Fix:
Possibly the perms check in the statfs() system call should change.  If it's correct as-is, the following patch to find tolerates the failure:

kronos /usr/src # diff -c usr.bin/find/function.c.orig usr.bin/find/function.c
*** usr.bin/find/function.c.orig        Thu Oct 30 17:27:13 2008
--- usr.bin/find/function.c     Thu Oct 30 17:30:29 2008
***************
*** 833,839 ****
  /*
   * -fstype functions --
   *
!  *    True if the file is of a certain type.
   */
  int
  f_fstype(PLAN *plan, FTSENT *entry)
--- 833,839 ----
  /*
   * -fstype functions --
   *
!  *    True if the filesystem is of a certain type.
   */
  int
  f_fstype(PLAN *plan, FTSENT *entry)
***************
*** 843,856 ****
        struct statfs sb;
        static int val_type, val_flags;
        char *p, save[2] = {0,0};

        if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
                return 0;

        /* Only check when we cross mount point. */
        if (first || curdev != entry->fts_statp->st_dev) {
-               curdev = entry->fts_statp->st_dev;
-
                /*
                 * Statfs follows symlinks; find wants the link's filesystem,
                 * not where it points.
--- 843,855 ----
        struct statfs sb;
        static int val_type, val_flags;
        char *p, save[2] = {0,0};
+       int e;

        if ((plan->flags & F_MTMASK) == F_MTUNKNOWN)
                return 0;

        /* Only check when we cross mount point. */
        if (first || curdev != entry->fts_statp->st_dev) {
                /*
                 * Statfs follows symlinks; find wants the link's filesystem,
                 * not where it points.
***************
*** 868,881 ****
                } else
                        p = NULL;

!               if (statfs(entry->fts_accpath, &sb))
!                       err(1, "%s", entry->fts_accpath);

                if (p) {
                        p[0] = save[0];
                        p[1] = save[1];
                }

                first = 0;

                /*
--- 867,887 ----
                } else
                        p = NULL;

!               e = statfs(entry->fts_accpath, &sb);

                if (p) {
                        p[0] = save[0];
                        p[1] = save[1];
                }

+               if (e) {
+                       if (errno != EACCES)
+                               err(1, "%s", entry->fts_accpath);
+                       else
+                               return 0;
+               }
+
+               curdev = entry->fts_statp->st_dev;
                first = 0;

                /*


>Release-Note:
>Audit-Trail:

From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org, ben@ben.com
Cc:  
Subject: Re: bin/128493: [patch] find(1) exits if -fstype test fails with
	EACCES
Date: Fri, 3 Apr 2009 18:26:22 +0200

 Such strange permissions on mount points break more than just statfs:
 non-root users will not be able to access .. in /bak. This is documented
 in the CAVEATS section of mount(8).
 
 In fact, find(1) aborts for me because of that (7.2-PRERELEASE).
 find: fts_read: Permission denied
 Your patch seems only helpful if you -prune away the problematic
 directories.
 
 I also notice that your patch does not apply because all tabs have been
 expanded to spaces.
 
 -- 
 Jilles Tjoelker
>Unformatted:
