Subject: Re: bug#14371: Argument --parents ignores default ACLs

From: Paul Eggert <[email-masked]>
Date: Sat, 11 May 2013 19:25:53 -0700

Thanks for reporting the bug.

I installed the following patches into the coreutils master branch
and am marking this as done.

>From 10287448997642ea04d27372f9283e477a070a70 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sat, 11 May 2013 18:29:47 -0700
Subject: [PATCH 1/3] build: update gnulib submodule to latest

---
 gl/lib/regex_internal.h.diff | 6 +++---
 gl/modules/tempname.diff     | 7 ++++---
 gnulib                       | 2 +-
 3 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/gl/lib/regex_internal.h.diff b/gl/lib/regex_internal.h.diff
index fe0f2b7..f410882 100644
--- a/gl/lib/regex_internal.h.diff
+++ b/gl/lib/regex_internal.h.diff
@@ -1,11 +1,11 @@
 diff --git a/lib/regex_internal.h b/lib/regex_internal.h
-index 2b9f697..7f4e349 100644
+index 439444c..7242084 100644
 --- a/lib/regex_internal.h
 +++ b/lib/regex_internal.h
-@@ -823,7 +823,8 @@ re_string_wchar_at (const re_string_t *pstr, Idx idx)
+@@ -827,7 +827,8 @@ re_string_wchar_at (const re_string_t *pstr, Idx idx)
  # ifndef NOT_IN_libc
  static int
- internal_function __attribute ((pure))
+ internal_function __attribute__ ((pure, unused))
 -re_string_elem_size_at (const re_string_t *pstr, Idx idx)
 +re_string_elem_size_at (const re_string_t *pstr _UNUSED_PARAMETER_,
 +			Idx idx _UNUSED_PARAMETER_)
diff --git a/gl/modules/tempname.diff b/gl/modules/tempname.diff
index 3603a59..219deed 100644
--- a/gl/modules/tempname.diff
+++ b/gl/modules/tempname.diff
@@ -1,19 +1,20 @@
 diff --git a/modules/tempname b/modules/tempname
-index b4708d9..e003c41 100644
+index 7fafd72..4703517 100644
 --- a/modules/tempname
 +++ b/modules/tempname
-@@ -1,5 +1,5 @@
+@@ -1,2 +1,2 @@
  Description:
 -gen_tempname() function: create a private temporary file or directory.
 +gen_tempname, gen_tempname_len: create a private temporary file or directory.
 
  Files:
  lib/tempname.c
-@@ -11,6 +11,8 @@ extensions
+@@ -11,7 +11,9 @@ extensions
  fcntl-h
  gettimeofday
  lstat
 +randint
+ secure_getenv
 +stdbool
  stdint
  sys_stat
diff --git a/gnulib b/gnulib
index 4a82904..cda5c90 160000
--- a/gnulib
+++ b/gnulib
@@ -1 +1 @@
-Subproject commit 4a82904680e6974db7b9eed6a3ed4c6eb24ecbe4
+Subproject commit cda5c90820d55b4b1f52d6a6f5329a10668bd720
-- 
1.7.11.7

>From 26927c7266c1194c77a50abc49bc9cbd3854db14 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sat, 11 May 2013 19:17:10 -0700
Subject: [PATCH 2/3] mkdir: don't assume umask equals POSIX default ACL mask

This fixes Bug#14371, reported by Killer Bassist.
* NEWS: Document this.
* src/mkdir.c (struct mkdir_options): Remove member ancestor_mode.
New member umask_value.  All uses changed.
* src/mkdir.c (make_ancestor): Fix umask assumption.
* src/mkdir.c, src/mkfifo.c, src/mknod.c (main):
Leave umask alone.  This requires invoking lchmod after creating
the file, which introduces a race condition, but this can't be
avoided on hosts with "POSIX" default ACLs, and there's no easy
way with network file systems to tell what kind of host the
directory is on.
* tests/local.mk (all_tests): Add tests/mkdir/p-acl.sh.
* tests/mkdir/p-acl.sh: New file.
---
 NEWS                 |  5 +++++
 src/mkdir.c          | 25 ++++++++++++++++++-------
 src/mkfifo.c         | 11 ++++++++++-
 src/mknod.c          |  9 ++++++++-
 tests/local.mk       |  1 +
 tests/mkdir/p-acl.sh | 35 +++++++++++++++++++++++++++++++++++
 6 files changed, 77 insertions(+), 9 deletions(-)
 create mode 100755 tests/mkdir/p-acl.sh

diff --git a/NEWS b/NEWS
index ae6251d..eec93df 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,11 @@ GNU coreutils NEWS                                    -*- outline -*-
   the relative link on the dereferenced path of an existing link.
   [This bug was introduced when --relative was added in coreutils-8.16.]
 
+  mkdir, mkfifo, and mknod now work better when creating a file in a directory
+  with a default ACL whose umask disagrees with the process's umask, on a
+  system such as GNU/Linux where directory ACL umasks override process umasks.
+  [bug introduced in coreutils-6.0]
+
   tail --retry -f now waits for the files specified to appear.  Before, tail
   would immediately exit when such a file is inaccessible during the initial
   open.
diff --git a/src/mkdir.c b/src/mkdir.c
index a94f96e..b36237a 100644
--- a/src/mkdir.c
+++ b/src/mkdir.c
@@ -81,8 +81,8 @@ struct mkdir_options
      made.  */
   int (*make_ancestor_function) (char const *, char const *, void *);
 
-  /* Mode for ancestor directory.  */
-  mode_t ancestor_mode;
+  /* Umask value in effect.  */
+  mode_t umask_value;
 
   /* Mode for directory itself.  */
   mode_t mode;
@@ -112,10 +112,21 @@ static int
 make_ancestor (char const *dir, char const *component, void *options)
 {
   struct mkdir_options const *o = options;
-  int r = mkdir (component, o->ancestor_mode);
+  int r;
+  mode_t user_wx = S_IWUSR | S_IXUSR;
+  bool self_denying_umask = (o->umask_value & user_wx) != 0;
+  if (self_denying_umask)
+    umask (o->umask_value & ~user_wx);
+  r = mkdir (component, S_IRWXUGO);
+  if (self_denying_umask)
+    {
+      int mkdir_errno = errno;
+      umask (o->umask_value);
+      errno = mkdir_errno;
+    }
   if (r == 0)
     {
-      r = ! (o->ancestor_mode & S_IRUSR);
+      r = (o->umask_value & S_IRUSR) != 0;
       announce_mkdir (dir, options);
     }
   return r;
@@ -191,8 +202,8 @@ main (int argc, char **argv)
   if (options.make_ancestor_function || specified_mode)
     {
       mode_t umask_value = umask (0);
-
-      options.ancestor_mode = (S_IRWXUGO & ~umask_value) | (S_IWUSR | S_IXUSR);
+      umask (umask_value);
+      options.umask_value = umask_value;
 
       if (specified_mode)
         {
@@ -205,7 +216,7 @@ main (int argc, char **argv)
           free (change);
         }
       else
-        options.mode = S_IRWXUGO & ~umask_value;
+        options.mode = S_IRWXUGO;
     }
 
   exit (savewd_process_files (argc - optind, argv + optind,
diff --git a/src/mkfifo.c b/src/mkfifo.c
index 76291e5..78ff909 100644
--- a/src/mkfifo.c
+++ b/src/mkfifo.c
@@ -116,10 +116,13 @@ main (int argc, char **argv)
   newmode = MODE_RW_UGO;
   if (specified_mode)
     {
+      mode_t umask_value;
       struct mode_change *change = mode_compile (specified_mode);
       if (!change)
         error (EXIT_FAILURE, 0, _("invalid mode"));
-      newmode = mode_adjust (newmode, false, umask (0), change, NULL);
+      umask_value = umask (0);
+      umask (umask_value);
+      newmode = mode_adjust (newmode, false, umask_value, change, NULL);
       free (change);
       if (newmode & ~S_IRWXUGO)
         error (EXIT_FAILURE, 0,
@@ -132,6 +135,12 @@ main (int argc, char **argv)
         error (0, errno, _("cannot create fifo %s"), quote (argv[optind]));
         exit_status = EXIT_FAILURE;
       }
+    else if (specified_mode && lchmod (argv[optind], newmode) != 0)
+      {
+        error (0, errno, _("cannot set permissions of `%s'"),
+               quote (argv[optind]));
+        exit_status = EXIT_FAILURE;
+      }
 
   exit (exit_status);
 }
diff --git a/src/mknod.c b/src/mknod.c
index 7cfc708..a384ad3 100644
--- a/src/mknod.c
+++ b/src/mknod.c
@@ -122,10 +122,13 @@ main (int argc, char **argv)
   newmode = MODE_RW_UGO;
   if (specified_mode)
     {
+      mode_t umask_value;
       struct mode_change *change = mode_compile (specified_mode);
       if (!change)
         error (EXIT_FAILURE, 0, _("invalid mode"));
-      newmode = mode_adjust (newmode, false, umask (0), change, NULL);
+      umask_value = umask (0);
+      umask (umask_value);
+      newmode = mode_adjust (newmode, false, umask_value, change, NULL);
       free (change);
       if (newmode & ~S_IRWXUGO)
         error (EXIT_FAILURE, 0,
@@ -226,5 +229,9 @@ main (int argc, char **argv)
       usage (EXIT_FAILURE);
     }
 
+  if (specified_mode && lchmod (argv[optind], newmode) != 0)
+    error (EXIT_FAILURE, errno, _("cannot set permissions of `%s'"),
+           quote (argv[optind]));
+
   exit (EXIT_SUCCESS);
 }
diff --git a/tests/local.mk b/tests/local.mk
index fb5cc63..5ec7d98 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -555,6 +555,7 @@ all_tests =					\
   tests/mkdir/p-1.sh				\
   tests/mkdir/p-2.sh				\
   tests/mkdir/p-3.sh				\
+  tests/mkdir/p-acl.sh				\
   tests/mkdir/p-slashdot.sh			\
   tests/mkdir/p-thru-slink.sh			\
   tests/mkdir/p-v.sh				\
diff --git a/tests/mkdir/p-acl.sh b/tests/mkdir/p-acl.sh
new file mode 100755
index 0000000..f1be628
--- /dev/null
+++ b/tests/mkdir/p-acl.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+# Test "mkdir -p" with default ACLs.
+
+# Copyright (C) 1997-2013 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ mkdir
+
+require_setfacl_
+
+mkdir d || framework_failure_
+setfacl -d -m group::rwx d || framework_failure_
+umask 077
+
+mkdir --parents d/e || fail=1
+ls_l=$(ls -ld d/e) || fail=1
+case $ls_l in
+  d???rw[sx]*) ;;
+  *) fail=1 ;;
+esac
+
+Exit $fail
-- 
1.7.11.7

>From 7a4d509292c62704c265254eb702321f7e533277 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sat, 11 May 2013 19:21:06 -0700
Subject: [PATCH 3/3] maint: add FIXME comment

---
 src/copy.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/copy.c b/src/copy.c
index 5c0ee1e..c1c8273 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -2747,8 +2747,12 @@ owner_failure_ok (struct cp_options const *x)
   return ((errno == EPERM || errno == EINVAL) && !x->owner_privileges);
 }
 
-/* Return the user's umask, caching the result.  */
+/* Return the user's umask, caching the result.
 
+   FIXME: If the destination's parent directory has has a default ACL,
+   some operating systems (e.g., GNU/Linux's "POSIX" ACLs) use that
+   ACL's mask rather than the process umask.  Currently, the callers
+   of cached_umask incorrectly assume that this situation cannot occur.  */
 extern mode_t
 cached_umask (void)
 {
-- 
1.7.11.7