b77676
From e3a7b9c8f26ca3b5a9aedd4c1d596a4a8504c812 Mon Sep 17 00:00:00 2001
b77676
From: Pavel Raiskup <praiskup@redhat.com>
b77676
Date: Thu, 4 Jun 2015 09:30:38 +0200
b77676
Subject: [PATCH] acls: don't mistakenly set default ACLs
b77676
b77676
Upstream commits
b77676
efbf4cce0b93e8c5a5fc93335b917bbeae2f67cb
b77676
9df17e6005e7a333399e3dc21a7afec75565c767
b77676
7fe7adcbb985e78aaf9f78051fa26167779be1f6
b77676
---
b77676
 configure.ac       |   2 +
b77676
 src/xattrs.c       |  37 ++++++++++++---
b77676
 tests/Makefile.am  |   1 +
b77676
 tests/acls03.at    | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++
b77676
 tests/testsuite.at |   1 +
b77676
 5 files changed, 165 insertions(+), 7 deletions(-)
b77676
 create mode 100644 tests/acls03.at
b77676
b77676
diff --git a/configure.ac b/configure.ac
b77676
index 9b3e0c8..7ccb579 100644
b77676
--- a/configure.ac
b77676
+++ b/configure.ac
b77676
@@ -85,6 +85,8 @@ if test "x$with_posix_acls" != "xno"; then
b77676
   AC_SEARCH_LIBS([acl_set_fd],    [acl pacl],, [with_posix_acl=no])
b77676
   AC_SEARCH_LIBS([acl_to_text],   [acl pacl],, [with_posix_acl=no])
b77676
   AC_SEARCH_LIBS([acl_from_text], [acl pacl],, [with_posix_acl=no])
b77676
+  AC_SEARCH_LIBS([acl_delete_def_file], [acl pacl],, [with_posix_acl=no])
b77676
+  AC_SEARCH_LIBS([acl_free], [acl pacl],, [with_posix_acl=no])
b77676
   if test "x$with_posix_acls" != xno; then
b77676
     AC_DEFINE(HAVE_POSIX_ACLS,,[Define when we have working POSIX acls])
b77676
   fi
b77676
diff --git a/src/xattrs.c b/src/xattrs.c
b77676
index bdf6ba0..5bd4b02 100644
b77676
--- a/src/xattrs.c
b77676
+++ b/src/xattrs.c
b77676
@@ -61,6 +61,7 @@ static struct
b77676
 acl_t acl_get_file_at (int dirfd, const char *file, acl_type_t type);
b77676
 int acl_set_file_at (int dirfd, const char *file, acl_type_t type, acl_t acl);
b77676
 int file_has_acl_at (int dirfd, char const *, struct stat const *);
b77676
+int acl_delete_def_file_at (int, char const *);
b77676
 
b77676
 /* acl_get_file_at */
b77676
 #define AT_FUNC_NAME acl_get_file_at
b77676
@@ -88,6 +89,17 @@ int file_has_acl_at (int dirfd, char const *, struct stat const *);
b77676
 #undef AT_FUNC_POST_FILE_PARAM_DECLS
b77676
 #undef AT_FUNC_POST_FILE_ARGS
b77676
 
b77676
+/* acl_delete_def_file_at */
b77676
+#define AT_FUNC_NAME acl_delete_def_file_at
b77676
+#define AT_FUNC_F1 acl_delete_def_file
b77676
+#define AT_FUNC_POST_FILE_PARAM_DECLS
b77676
+#define AT_FUNC_POST_FILE_ARGS
b77676
+#include "at-func.c"
b77676
+#undef AT_FUNC_NAME
b77676
+#undef AT_FUNC_F1
b77676
+#undef AT_FUNC_POST_FILE_PARAM_DECLS
b77676
+#undef AT_FUNC_POST_FILE_ARGS
b77676
+
b77676
 /* gnulib file_has_acl_at */
b77676
 #define AT_FUNC_NAME file_has_acl_at
b77676
 #define AT_FUNC_F1 file_has_acl
b77676
@@ -187,7 +199,8 @@ fixup_extra_acl_fields (char *ptr)
b77676
   return ptr;
b77676
 }
b77676
 
b77676
-/* "system.posix_acl_access" */
b77676
+/* Set the "system.posix_acl_access/system.posix_acl_default" extended
b77676
+   attribute.  Called only when acls_option > 0. */
b77676
 static void
b77676
 xattrs__acls_set (struct tar_stat_info const *st,
b77676
                   char const *file_name, int type,
b77676
@@ -199,15 +212,25 @@ xattrs__acls_set (struct tar_stat_info const *st,
b77676
     {
b77676
       /* assert (strlen (ptr) == len); */
b77676
       ptr = fixup_extra_acl_fields (ptr);
b77676
-
b77676
       acl = acl_from_text (ptr);
b77676
-      acls_option = 1;
b77676
     }
b77676
-  else if (acls_option > 0)
b77676
+  else if (def)
b77676
+    {
b77676
+      /* No "default" IEEE 1003.1e ACL set for directory.  At this moment,
b77676
+         FILE_NAME may already have inherited default acls from parent
b77676
+         directory;  clean them up. */
b77676
+      if (acl_delete_def_file_at (chdir_fd, file_name))
b77676
+        WARNOPT (WARN_XATTR_WRITE,
b77676
+                (0, errno,
b77676
+                 _("acl_delete_def_file_at: Cannot drop default POSIX ACLs "
b77676
+                   "for file '%s'"),
b77676
+                 file_name));
b77676
+      return;
b77676
+    }
b77676
+  else
b77676
+    /* There is nothing like "acl_delete_def_file" for non-default acls, we
b77676
+       need to re-set ACLs based on permission bits */
b77676
     acl = perms2acl (st->stat.st_mode);
b77676
-  else
b77676
-    return;  /* don't call acl functions unless we first hit an ACL, or
b77676
-		--acls was passed explicitly */
b77676
 
b77676
   if (!acl)
b77676
     {
b77676
diff --git a/tests/Makefile.am b/tests/Makefile.am
b77676
index b0da439..228e936 100644
b77676
--- a/tests/Makefile.am
b77676
+++ b/tests/Makefile.am
b77676
@@ -179,6 +179,7 @@ TESTSUITE_AT = \
b77676
  xattr05.at\
b77676
  acls01.at\
b77676
  acls02.at\
b77676
+ acls03.at\
b77676
  selnx01.at\
b77676
  selacl01.at\
b77676
  capabs_raw01.at
b77676
diff --git a/tests/acls03.at b/tests/acls03.at
b77676
new file mode 100644
b77676
index 0000000..83c5bdc
b77676
--- /dev/null
b77676
+++ b/tests/acls03.at
b77676
@@ -0,0 +1,131 @@
b77676
+# Process this file with autom4te to create testsuite. -*- Autotest -*-
b77676
+#
b77676
+# Test suite for GNU tar.
b77676
+# Copyright 2013, 2014 Free Software Foundation, Inc.
b77676
+
b77676
+# This file is part of GNU tar.
b77676
+
b77676
+# GNU tar is free software; you can redistribute it and/or modify
b77676
+# it under the terms of the GNU General Public License as published by
b77676
+# the Free Software Foundation; either version 3 of the License, or
b77676
+# (at your option) any later version.
b77676
+
b77676
+# GNU tar is distributed in the hope that it will be useful,
b77676
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
b77676
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b77676
+# GNU General Public License for more details.
b77676
+
b77676
+# You should have received a copy of the GNU General Public License
b77676
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
b77676
+#
b77676
+# Test description:
b77676
+#
b77676
+# Check the storing/restoring with/without default ACLs.  When --acls is passed,
b77676
+# restored directory tree should always match archive contents (even when the
b77676
+# archive does not contain any ACLs).
b77676
+#
b77676
+# References:
b77676
+# http://www.mail-archive.com/bug-tar@gnu.org/msg04355.html
b77676
+
b77676
+AT_SETUP([acls: default ACLs])
b77676
+AT_KEYWORDS([xattrs acls acls03])
b77676
+
b77676
+m4_define([ACL_LISTDIR], [
b77676
+    cd $1
b77676
+    $1="$(find d1 | sort | xargs -n 1 getfacl)"
b77676
+    cd ..
b77676
+])
b77676
+
b77676
+m4_define([ACL_ASSERT], [
b77676
+    echo "$$1" > $1.log
b77676
+    echo "$$2" > $2.log
b77676
+    if test ! "$$1" "$3" "$$2"; then
b77676
+        echo "bad '$1' against '$2' output"
b77676
+    fi
b77676
+])
b77676
+
b77676
+AT_TAR_CHECK([
b77676
+AT_XATTRS_UTILS_PREREQ
b77676
+AT_ACLS_PREREQ
b77676
+AT_SORT_PREREQ
b77676
+
b77676
+MYNAME=$( id -un )
b77676
+MYGROUP=$( id -gn )
b77676
+
b77676
+# Prepare directory structure with default ACLs
b77676
+mkdir -p pure/d1/d2
b77676
+genfile --file pure/d1/f2a
b77676
+genfile --file pure/d1/f2b
b77676
+genfile --file pure/d1/d2/f3a
b77676
+genfile --file pure/d1/d2/f3b
b77676
+setfacl    -m g:$MYGROUP:r-x pure/d1
b77676
+setfacl -d -m g:$MYGROUP:rwx pure/d1
b77676
+setfacl -d -m u:$MYNAME:rwx  pure/d1
b77676
+# "*a" files have "some" additional ACLs
b77676
+setfacl    -m u:$MYNAME:--- pure/d1/d2/f3a
b77676
+setfacl    -m u:$MYNAME:--- pure/d1/f2a
b77676
+
b77676
+# use default format (no acls stored)
b77676
+tar -cf noacl.tar -C pure d1
b77676
+
b77676
+# use posix format, acls stored
b77676
+tar --acls -cf acl.tar -C pure d1
b77676
+
b77676
+# Directory names are chosen based on "how the files were extracted from
b77676
+# archive".  Equivalent no* tags are used also:
b77676
+#   ^sacl_    — extracted archive has stored ACLs
b77676
+#   _def_     — target directory (-C) has default ACLs
b77676
+#   _optacl$  — extraction was done with --acls option
b77676
+
b77676
+mkdir sacl_def_optacl
b77676
+mkdir sacl_def_optnoacl
b77676
+mkdir sacl_nodef_optacl
b77676
+mkdir sacl_nodef_optnoacl
b77676
+mkdir nosacl_def_optacl
b77676
+mkdir nosacl_def_optnoacl
b77676
+mkdir nosacl_nodef_optacl
b77676
+mkdir nosacl_nodef_optnoacl
b77676
+
b77676
+setfacl -d -m u:$MYNAME:---  nosacl_def_optnoacl sacl_def_optnoacl sacl_def_optacl nosacl_def_optacl
b77676
+setfacl -d -m g:$MYGROUP:--- nosacl_def_optnoacl sacl_def_optnoacl sacl_def_optacl nosacl_def_optacl
b77676
+
b77676
+tar -xf acl.tar -C sacl_nodef_optnoacl
b77676
+tar --acls -xf acl.tar -C sacl_nodef_optacl
b77676
+tar -xf acl.tar -C sacl_def_optnoacl
b77676
+tar --acls -xf acl.tar -C sacl_def_optacl
b77676
+tar -xf noacl.tar -C nosacl_def_optnoacl
b77676
+# _NO_ ACLs in output
b77676
+tar -xf noacl.tar -C nosacl_nodef_optnoacl
b77676
+tar -xf noacl.tar -C nosacl_nodef_optacl
b77676
+tar -cf noacl_repackaged.tar -C nosacl_nodef_optnoacl d1
b77676
+# _NO_ ACLs in output (even when default ACLs exist)
b77676
+tar --acls -xf noacl_repackaged.tar -C nosacl_def_optacl
b77676
+
b77676
+ACL_LISTDIR(pure)
b77676
+
b77676
+ACL_LISTDIR(sacl_def_optacl)
b77676
+ACL_LISTDIR(sacl_def_optnoacl)
b77676
+ACL_LISTDIR(sacl_nodef_optacl)
b77676
+ACL_LISTDIR(sacl_nodef_optnoacl)
b77676
+ACL_LISTDIR(nosacl_def_optacl)
b77676
+ACL_LISTDIR(nosacl_def_optnoacl)
b77676
+ACL_LISTDIR(nosacl_nodef_optacl)
b77676
+ACL_LISTDIR(nosacl_nodef_optnoacl)
b77676
+
b77676
+ACL_ASSERT(pure, sacl_def_optacl, =)
b77676
+
b77676
+ACL_ASSERT(sacl_def_optacl,     sacl_nodef_optacl,      =)
b77676
+ACL_ASSERT(sacl_def_optnoacl,   nosacl_def_optnoacl,    =)
b77676
+ACL_ASSERT(sacl_nodef_optnoacl, nosacl_nodef_optnoacl,  =)
b77676
+ACL_ASSERT(nosacl_def_optacl,   nosacl_nodef_optacl,    =)
b77676
+ACL_ASSERT(nosacl_def_optacl,   nosacl_nodef_optnoacl,  =)
b77676
+
b77676
+ACL_ASSERT(sacl_def_optacl,     sacl_def_optnoacl,      !=)
b77676
+ACL_ASSERT(sacl_def_optacl,     nosacl_def_optnoacl,    !=)
b77676
+ACL_ASSERT(nosacl_def_optnoacl, nosacl_nodef_optnoacl,  !=)
b77676
+],
b77676
+[0],
b77676
+[],
b77676
+[])
b77676
+
b77676
+AT_CLEANUP
b77676
diff --git a/tests/testsuite.at b/tests/testsuite.at
b77676
index d5925b3..10cf26a 100644
b77676
--- a/tests/testsuite.at
b77676
+++ b/tests/testsuite.at
b77676
@@ -341,6 +341,7 @@ m4_include([xattr05.at])
b77676
 
b77676
 m4_include([acls01.at])
b77676
 m4_include([acls02.at])
b77676
+m4_include([acls03.at])
b77676
 
b77676
 m4_include([selnx01.at])
b77676
 m4_include([selacl01.at])
b77676
-- 
b77676
2.1.0
b77676