Blame SOURCES/coreutils-nfsv4-acls.patch

92de12
From 5a6af47c3db45b6303bac4dcd6da186fd5cd178c Mon Sep 17 00:00:00 2001
92de12
From: Ondrej Valousek <ondrej.valousek.xm@renesas.com>
92de12
Date: Fri, 2 Dec 2022 13:40:19 +0100
92de12
Subject: [PATCH 1/3] file-has-acl: Basic support for checking NFSv4 ACLs in
92de12
 Linux.
92de12
92de12
* lib/acl-internal.h (acl_nfs4_nontrivial): New declaration.
92de12
* lib/acl-internal.c (acl_nfs4_nontrivial): New function.
92de12
* lib/file-has-acl.c: Include <arpa/inet.h>.
92de12
(XATTR_NAME_NFSV4_ACL, TRIVIAL_NFS4_ACL_MAX_LENGTH): New macros.
92de12
(file_has_acl): Test for NFSv4 ACLs.
92de12
* doc/acl-nfsv4.txt: New file.
92de12
92de12
Upstream-commit: b0604a8e134dbcc307c0ffdd5ebd3693e9de7081
92de12
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
92de12
---
92de12
 doc/acl-nfsv4.txt  |  17 ++++++++
92de12
 lib/acl-internal.c | 100 +++++++++++++++++++++++++++++++++++++++++++++
92de12
 lib/acl-internal.h |   3 ++
92de12
 lib/file-has-acl.c |  21 ++++++++++
92de12
 4 files changed, 141 insertions(+)
92de12
 create mode 100644 doc/acl-nfsv4.txt
92de12
92de12
diff --git a/doc/acl-nfsv4.txt b/doc/acl-nfsv4.txt
92de12
new file mode 100644
92de12
index 0000000..71352f5
92de12
--- /dev/null
92de12
+++ b/doc/acl-nfsv4.txt
92de12
@@ -0,0 +1,17 @@
92de12
+General introduction:
92de12
+   https://linux.die.net/man/5/nfs4_acl
92de12
+
92de12
+The NFSv4 acls are defined in RFC7530 and as such, every NFSv4 server supporting ACLs
92de12
+will support this kind of ACLs (note the difference from POSIX draft ACLs)
92de12
+
92de12
+The ACLs can be obtained via the nfsv4-acl-tools, i.e.
92de12
+
92de12
+$ nfs4_getfacl <file>
92de12
+
92de12
+# file: <file>
92de12
+A::OWNER@:rwaDxtTnNcCy
92de12
+A::GROUP@:rwaDxtTnNcy
92de12
+A::EVERYONE@:rwaDxtTnNcy
92de12
+
92de12
+Gnulib is aiming to only provide a basic support of these, i.e. recognize trivial
92de12
+and non-trivial ACLs
92de12
diff --git a/lib/acl-internal.c b/lib/acl-internal.c
92de12
index be244c6..4c65dff 100644
92de12
--- a/lib/acl-internal.c
92de12
+++ b/lib/acl-internal.c
92de12
@@ -25,6 +25,9 @@
92de12
 
92de12
 #if USE_ACL && HAVE_ACL_GET_FILE /* Linux, FreeBSD, Mac OS X, IRIX, Tru64, Cygwin >= 2.5 */
92de12
 
92de12
+# include <string.h>
92de12
+# include <arpa/inet.h>
92de12
+
92de12
 # if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
92de12
 
92de12
 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
92de12
@@ -122,6 +125,103 @@ acl_default_nontrivial (acl_t acl)
92de12
   return (acl_entries (acl) > 0);
92de12
 }
92de12
 
92de12
+#  define ACE4_WHO_OWNER    "OWNER@"
92de12
+#  define ACE4_WHO_GROUP    "GROUP@"
92de12
+#  define ACE4_WHO_EVERYONE "EVERYONE@"
92de12
+
92de12
+#  define ACE4_ACCESS_ALLOWED_ACE_TYPE   0
92de12
+#  define ACE4_ACCESS_DENIED_ACE_TYPE    1
92de12
+
92de12
+/* ACE flag values */
92de12
+#  define ACE4_IDENTIFIER_GROUP          0x00000040
92de12
+#  define ROUNDUP(x, y)                  (((x) + (y) - 1) & - (y))
92de12
+
92de12
+int
92de12
+acl_nfs4_nontrivial (char *xattr, int len)
92de12
+{
92de12
+  int      bufs = len;
92de12
+  uint32_t num_aces = ntohl (*((uint32_t*)(xattr))), /* Grab the number of aces in the acl */
92de12
+           num_a_aces = 0,
92de12
+           num_d_aces = 0;
92de12
+  char *bufp = xattr;
92de12
+
92de12
+  bufp += 4;  /* sizeof(uint32_t); */
92de12
+  bufs -= 4;
92de12
+
92de12
+  for (uint32_t ace_n = 0; num_aces > ace_n ; ace_n++)
92de12
+    {
92de12
+      int      d_ptr;
92de12
+      uint32_t flag,
92de12
+               wholen,
92de12
+               type;
92de12
+
92de12
+      /* Get the acl type */
92de12
+      if (bufs <= 0)
92de12
+        return -1;
92de12
+
92de12
+      type = ntohl (*((uint32_t*)bufp));
92de12
+
92de12
+      bufp += 4;
92de12
+      bufs -= 4;
92de12
+      if (bufs <= 0)
92de12
+        return -1;
92de12
+
92de12
+      flag = ntohl (*((uint32_t*)bufp));
92de12
+      /* As per RFC 7530, the flag should be 0, but we are just generous to Netapp
92de12
+       * and also accept the Group flag
92de12
+       */
92de12
+      if (flag & ~ACE4_IDENTIFIER_GROUP)
92de12
+        return 1;
92de12
+
92de12
+      /* we skip mask -
92de12
+       * it's too risky to test it and it does not seem to be actually needed */
92de12
+      bufp += 2*4;
92de12
+      bufs -= 2*4;
92de12
+
92de12
+      if (bufs <= 0)
92de12
+        return -1;
92de12
+
92de12
+      wholen = ntohl (*((uint32_t*)bufp));
92de12
+
92de12
+      bufp += 4;
92de12
+      bufs -= 4;
92de12
+
92de12
+      /* Get the who string */
92de12
+      if (bufs <= 0)
92de12
+        return -1;
92de12
+
92de12
+      /* for trivial ACL, we expect max 5 (typically 3) ACES, 3 Allow, 2 deny */
92de12
+      if (((strncmp (bufp, ACE4_WHO_OWNER, wholen) == 0)
92de12
+          || (strncmp (bufp, ACE4_WHO_GROUP, wholen) == 0))
92de12
+          &&  wholen == 6)
92de12
+        {
92de12
+          if (type == ACE4_ACCESS_ALLOWED_ACE_TYPE)
92de12
+            num_a_aces++;
92de12
+          if (type == ACE4_ACCESS_DENIED_ACE_TYPE)
92de12
+            num_d_aces++;
92de12
+        }
92de12
+      else
92de12
+        if ((strncmp (bufp, ACE4_WHO_EVERYONE, wholen) == 0)
92de12
+            && (type == ACE4_ACCESS_ALLOWED_ACE_TYPE)
92de12
+            && (wholen == 9))
92de12
+          num_a_aces++;
92de12
+        else
92de12
+          return 1;
92de12
+
92de12
+      d_ptr = ROUNDUP (wholen, 4);
92de12
+      bufp += d_ptr;
92de12
+      bufs -= d_ptr;
92de12
+
92de12
+      /* Make sure we aren't outside our domain */
92de12
+      if (bufs < 0)
92de12
+        return -1;
92de12
+
92de12
+    }
92de12
+  return !((num_a_aces <= 3) && (num_d_aces <= 2)
92de12
+         && (num_a_aces + num_d_aces == num_aces));
92de12
+
92de12
+}
92de12
+
92de12
 # endif
92de12
 
92de12
 #elif USE_ACL && HAVE_FACL && defined GETACL /* Solaris, Cygwin < 2.5, not HP-UX */
92de12
diff --git a/lib/acl-internal.h b/lib/acl-internal.h
92de12
index 9353376..2a249ff 100644
92de12
--- a/lib/acl-internal.h
92de12
+++ b/lib/acl-internal.h
92de12
@@ -147,6 +147,9 @@ rpl_acl_set_fd (int fd, acl_t acl)
92de12
 #   define acl_entries rpl_acl_entries
92de12
 extern int acl_entries (acl_t);
92de12
 #  endif
92de12
+/* Return 1 if given ACL in XDR format is non-trivial
92de12
+ * Return 0 if it is trivial */
92de12
+extern int acl_nfs4_nontrivial (char *, int);
92de12
 
92de12
 #  if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
92de12
 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
92de12
diff --git a/lib/file-has-acl.c b/lib/file-has-acl.c
92de12
index e02f062..1710234 100644
92de12
--- a/lib/file-has-acl.c
92de12
+++ b/lib/file-has-acl.c
92de12
@@ -32,6 +32,11 @@
92de12
 #if GETXATTR_WITH_POSIX_ACLS
92de12
 # include <sys/xattr.h>
92de12
 # include <linux/xattr.h>
92de12
+# include <arpa/inet.h>
92de12
+# ifndef XATTR_NAME_NFSV4_ACL
92de12
+#  define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
92de12
+# endif
92de12
+# define TRIVIAL_NFS4_ACL_MAX_LENGTH 128
92de12
 #endif
92de12
 
92de12
 /* Return 1 if NAME has a nontrivial access control list,
92de12
@@ -67,6 +72,22 @@ file_has_acl (char const *name, struct stat const *sb)
92de12
             return 1;
92de12
         }
92de12
 
92de12
+      if (ret < 0)
92de12
+        { /* we might be on NFS, so try to check NFSv4 ACLs too */
92de12
+          char xattr[TRIVIAL_NFS4_ACL_MAX_LENGTH];
92de12
+
92de12
+          errno = 0; /* we need to reset errno set by the previous getxattr() */
92de12
+          ret = getxattr (name, XATTR_NAME_NFSV4_ACL, xattr, TRIVIAL_NFS4_ACL_MAX_LENGTH);
92de12
+          if (ret < 0 && errno == ENODATA)
92de12
+            ret = 0;
92de12
+          else
92de12
+            if (ret < 0 && errno == ERANGE)
92de12
+              return 1;  /* we won't fit into the buffer, so non-trivial ACL is presented */
92de12
+            else
92de12
+              if (ret > 0)
92de12
+                /* looks like trivial ACL, but we need to investigate further */
92de12
+                return acl_nfs4_nontrivial (xattr, ret);
92de12
+        }
92de12
       if (ret < 0)
92de12
         return - acl_errno_valid (errno);
92de12
       return ret;
92de12
-- 
92de12
2.38.1
92de12
92de12
92de12
From c5266d204a446bea619fa18da8520dceb0a54192 Mon Sep 17 00:00:00 2001
92de12
From: Paul Eggert <eggert@cs.ucla.edu>
92de12
Date: Fri, 23 Dec 2022 15:18:29 -0800
92de12
Subject: [PATCH 2/3] file-has-acl: improve recent NFSv4 support
92de12
MIME-Version: 1.0
92de12
Content-Type: text/plain; charset=UTF-8
92de12
Content-Transfer-Encoding: 8bit
92de12
92de12
This fixes a link failure with emacsclient on GNU/Linux.  This
92de12
program wants file_has_acl but none of the other ACL primitives,
92de12
so it doesn’t link acl-internal.o; this way it doesn’t need to
92de12
link with -lacl.  While I was at it I reviewed the recent changes,
92de12
fixed some unlikely overflow bugs, and adjusted to GNU style.
92de12
* doc/acl-nfsv4.txt: Remove.  Its contents are now in a
92de12
comment in lib/file-has-acl.c.
92de12
* lib/acl-internal.c, lib/acl-internal.h: Move recent changes
92de12
relating to acl_nfs4_nontrivial to lib/file-has-acl.c, so that
92de12
there is no trouble linking programs that need only file_has_acl.
92de12
* lib/file-has-acl.c (acl_nfs4_nontrivial): Move here from
92de12
lib/acl-internal.c, so that we needn't link -lacl in
92de12
programs that want only file_has_acl, such as emacsclient.
92de12
Do not assume a char buffer is aligned for uint32_t.
92de12
Check more carefully for buffer read overrun.
92de12
Allow up to 6 ACEs, since other code does; but check
92de12
that they’re distinct.  Avoid integer overflow.
92de12
Use memcmp rather than strncmp to compare memory blocks.
92de12
(file_has_acl): Preserve initial errno instead of setting to 0.
92de12
Allocate a bit more room for trivial ACL buffer.
92de12
Use EINVAL for botchedk NFSv4 ACLs (which shouldn’t happen).
92de12
92de12
Upstream-commit: 35bd46f0c816948dc1a0430c8ba8b10a01167320
92de12
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
92de12
---
92de12
 doc/acl-nfsv4.txt  |  17 ------
92de12
 lib/acl-internal.c | 100 -----------------------------------
92de12
 lib/acl-internal.h |   3 --
92de12
 lib/file-has-acl.c | 129 +++++++++++++++++++++++++++++++++++++++------
92de12
 4 files changed, 113 insertions(+), 136 deletions(-)
92de12
 delete mode 100644 doc/acl-nfsv4.txt
92de12
92de12
diff --git a/doc/acl-nfsv4.txt b/doc/acl-nfsv4.txt
92de12
deleted file mode 100644
92de12
index 71352f5..0000000
92de12
--- a/doc/acl-nfsv4.txt
92de12
+++ /dev/null
92de12
@@ -1,17 +0,0 @@
92de12
-General introduction:
92de12
-   https://linux.die.net/man/5/nfs4_acl
92de12
-
92de12
-The NFSv4 acls are defined in RFC7530 and as such, every NFSv4 server supporting ACLs
92de12
-will support this kind of ACLs (note the difference from POSIX draft ACLs)
92de12
-
92de12
-The ACLs can be obtained via the nfsv4-acl-tools, i.e.
92de12
-
92de12
-$ nfs4_getfacl <file>
92de12
-
92de12
-# file: <file>
92de12
-A::OWNER@:rwaDxtTnNcCy
92de12
-A::GROUP@:rwaDxtTnNcy
92de12
-A::EVERYONE@:rwaDxtTnNcy
92de12
-
92de12
-Gnulib is aiming to only provide a basic support of these, i.e. recognize trivial
92de12
-and non-trivial ACLs
92de12
diff --git a/lib/acl-internal.c b/lib/acl-internal.c
92de12
index 4c65dff..be244c6 100644
92de12
--- a/lib/acl-internal.c
92de12
+++ b/lib/acl-internal.c
92de12
@@ -25,9 +25,6 @@
92de12
 
92de12
 #if USE_ACL && HAVE_ACL_GET_FILE /* Linux, FreeBSD, Mac OS X, IRIX, Tru64, Cygwin >= 2.5 */
92de12
 
92de12
-# include <string.h>
92de12
-# include <arpa/inet.h>
92de12
-
92de12
 # if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
92de12
 
92de12
 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
92de12
@@ -125,103 +122,6 @@ acl_default_nontrivial (acl_t acl)
92de12
   return (acl_entries (acl) > 0);
92de12
 }
92de12
 
92de12
-#  define ACE4_WHO_OWNER    "OWNER@"
92de12
-#  define ACE4_WHO_GROUP    "GROUP@"
92de12
-#  define ACE4_WHO_EVERYONE "EVERYONE@"
92de12
-
92de12
-#  define ACE4_ACCESS_ALLOWED_ACE_TYPE   0
92de12
-#  define ACE4_ACCESS_DENIED_ACE_TYPE    1
92de12
-
92de12
-/* ACE flag values */
92de12
-#  define ACE4_IDENTIFIER_GROUP          0x00000040
92de12
-#  define ROUNDUP(x, y)                  (((x) + (y) - 1) & - (y))
92de12
-
92de12
-int
92de12
-acl_nfs4_nontrivial (char *xattr, int len)
92de12
-{
92de12
-  int      bufs = len;
92de12
-  uint32_t num_aces = ntohl (*((uint32_t*)(xattr))), /* Grab the number of aces in the acl */
92de12
-           num_a_aces = 0,
92de12
-           num_d_aces = 0;
92de12
-  char *bufp = xattr;
92de12
-
92de12
-  bufp += 4;  /* sizeof(uint32_t); */
92de12
-  bufs -= 4;
92de12
-
92de12
-  for (uint32_t ace_n = 0; num_aces > ace_n ; ace_n++)
92de12
-    {
92de12
-      int      d_ptr;
92de12
-      uint32_t flag,
92de12
-               wholen,
92de12
-               type;
92de12
-
92de12
-      /* Get the acl type */
92de12
-      if (bufs <= 0)
92de12
-        return -1;
92de12
-
92de12
-      type = ntohl (*((uint32_t*)bufp));
92de12
-
92de12
-      bufp += 4;
92de12
-      bufs -= 4;
92de12
-      if (bufs <= 0)
92de12
-        return -1;
92de12
-
92de12
-      flag = ntohl (*((uint32_t*)bufp));
92de12
-      /* As per RFC 7530, the flag should be 0, but we are just generous to Netapp
92de12
-       * and also accept the Group flag
92de12
-       */
92de12
-      if (flag & ~ACE4_IDENTIFIER_GROUP)
92de12
-        return 1;
92de12
-
92de12
-      /* we skip mask -
92de12
-       * it's too risky to test it and it does not seem to be actually needed */
92de12
-      bufp += 2*4;
92de12
-      bufs -= 2*4;
92de12
-
92de12
-      if (bufs <= 0)
92de12
-        return -1;
92de12
-
92de12
-      wholen = ntohl (*((uint32_t*)bufp));
92de12
-
92de12
-      bufp += 4;
92de12
-      bufs -= 4;
92de12
-
92de12
-      /* Get the who string */
92de12
-      if (bufs <= 0)
92de12
-        return -1;
92de12
-
92de12
-      /* for trivial ACL, we expect max 5 (typically 3) ACES, 3 Allow, 2 deny */
92de12
-      if (((strncmp (bufp, ACE4_WHO_OWNER, wholen) == 0)
92de12
-          || (strncmp (bufp, ACE4_WHO_GROUP, wholen) == 0))
92de12
-          &&  wholen == 6)
92de12
-        {
92de12
-          if (type == ACE4_ACCESS_ALLOWED_ACE_TYPE)
92de12
-            num_a_aces++;
92de12
-          if (type == ACE4_ACCESS_DENIED_ACE_TYPE)
92de12
-            num_d_aces++;
92de12
-        }
92de12
-      else
92de12
-        if ((strncmp (bufp, ACE4_WHO_EVERYONE, wholen) == 0)
92de12
-            && (type == ACE4_ACCESS_ALLOWED_ACE_TYPE)
92de12
-            && (wholen == 9))
92de12
-          num_a_aces++;
92de12
-        else
92de12
-          return 1;
92de12
-
92de12
-      d_ptr = ROUNDUP (wholen, 4);
92de12
-      bufp += d_ptr;
92de12
-      bufs -= d_ptr;
92de12
-
92de12
-      /* Make sure we aren't outside our domain */
92de12
-      if (bufs < 0)
92de12
-        return -1;
92de12
-
92de12
-    }
92de12
-  return !((num_a_aces <= 3) && (num_d_aces <= 2)
92de12
-         && (num_a_aces + num_d_aces == num_aces));
92de12
-
92de12
-}
92de12
-
92de12
 # endif
92de12
 
92de12
 #elif USE_ACL && HAVE_FACL && defined GETACL /* Solaris, Cygwin < 2.5, not HP-UX */
92de12
diff --git a/lib/acl-internal.h b/lib/acl-internal.h
92de12
index 2a249ff..9353376 100644
92de12
--- a/lib/acl-internal.h
92de12
+++ b/lib/acl-internal.h
92de12
@@ -147,9 +147,6 @@ rpl_acl_set_fd (int fd, acl_t acl)
92de12
 #   define acl_entries rpl_acl_entries
92de12
 extern int acl_entries (acl_t);
92de12
 #  endif
92de12
-/* Return 1 if given ACL in XDR format is non-trivial
92de12
- * Return 0 if it is trivial */
92de12
-extern int acl_nfs4_nontrivial (char *, int);
92de12
 
92de12
 #  if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
92de12
 /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
92de12
diff --git a/lib/file-has-acl.c b/lib/file-has-acl.c
92de12
index 1710234..676523b 100644
92de12
--- a/lib/file-has-acl.c
92de12
+++ b/lib/file-has-acl.c
92de12
@@ -29,14 +29,97 @@
92de12
 
92de12
 #include "acl-internal.h"
92de12
 
92de12
-#if GETXATTR_WITH_POSIX_ACLS
92de12
+#if USE_ACL && GETXATTR_WITH_POSIX_ACLS
92de12
+# include <string.h>
92de12
+# include <arpa/inet.h>
92de12
 # include <sys/xattr.h>
92de12
 # include <linux/xattr.h>
92de12
-# include <arpa/inet.h>
92de12
 # ifndef XATTR_NAME_NFSV4_ACL
92de12
 #  define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
92de12
 # endif
92de12
-# define TRIVIAL_NFS4_ACL_MAX_LENGTH 128
92de12
+
92de12
+enum {
92de12
+  /* ACE4_ACCESS_ALLOWED_ACE_TYPE = 0x00000000, */
92de12
+  ACE4_ACCESS_DENIED_ACE_TYPE  = 0x00000001,
92de12
+  ACE4_IDENTIFIER_GROUP        = 0x00000040
92de12
+};
92de12
+
92de12
+/* Return 1 if given ACL in XDR format is non-trivial, 0 if it is trivial.
92de12
+   -1 upon failure to determine it.  Possibly change errno.  Assume that
92de12
+   the ACL is valid, except avoid undefined behavior even if invalid.
92de12
+
92de12
+   See <https://linux.die.net/man/5/nfs4_acl>.  The NFSv4 acls are
92de12
+   defined in Internet RFC 7530 and as such, every NFSv4 server
92de12
+   supporting ACLs should support NFSv4 ACLs (they differ from from
92de12
+   POSIX draft ACLs).  The ACLs can be obtained via the
92de12
+   nfsv4-acl-tools, e.g., the nfs4_getfacl command.  Gnulib provides
92de12
+   only basic support of NFSv4 ACLs, i.e., recognize trivial vs
92de12
+   nontrivial ACLs.  */
92de12
+
92de12
+static int
92de12
+acl_nfs4_nontrivial (uint32_t *xattr, ssize_t nbytes)
92de12
+{
92de12
+  enum { BYTES_PER_NETWORK_UINT = 4};
92de12
+
92de12
+  /* Grab the number of aces in the acl.  */
92de12
+  nbytes -= BYTES_PER_NETWORK_UINT;
92de12
+  if (nbytes < 0)
92de12
+    return -1;
92de12
+  uint32_t num_aces = ntohl (*xattr++);
92de12
+  if (6 < num_aces)
92de12
+    return 1;
92de12
+  int ace_found = 0;
92de12
+
92de12
+  for (int ace_n = 0; ace_n < num_aces; ace_n++)
92de12
+    {
92de12
+      /* Get the acl type and flag.  Skip the mask; it's too risky to
92de12
+         test it and it does not seem to be needed.  Get the wholen.  */
92de12
+      nbytes -= 4 * BYTES_PER_NETWORK_UINT;
92de12
+      if (nbytes < 0)
92de12
+        return -1;
92de12
+      uint32_t type = ntohl (xattr[0]);
92de12
+      uint32_t flag = ntohl (xattr[1]);
92de12
+      uint32_t wholen = ntohl (xattr[3]);
92de12
+      xattr += 4;
92de12
+      int64_t wholen4 = wholen;
92de12
+      wholen4 = ((wholen4 + (BYTES_PER_NETWORK_UINT))
92de12
+                 & ~ (BYTES_PER_NETWORK_UINT - 1));
92de12
+
92de12
+      /* Trivial ACLs have only ACE4_ACCESS_ALLOWED_ACE_TYPE or
92de12
+         ACE4_ACCESS_DENIED_ACE_TYPE.  */
92de12
+      if (ACE4_ACCESS_DENIED_ACE_TYPE < type)
92de12
+        return 1;
92de12
+
92de12
+      /* RFC 7530 says FLAG should be 0, but be generous to NetApp and
92de12
+         also accept the group flag.  */
92de12
+      if (flag & ~ACE4_IDENTIFIER_GROUP)
92de12
+        return 1;
92de12
+
92de12
+      /* Get the who string.  Check NBYTES - WHOLEN4 before storing
92de12
+         into NBYTES, to avoid truncation on conversion.  */
92de12
+      if (nbytes - wholen4 < 0)
92de12
+        return -1;
92de12
+      nbytes -= wholen4;
92de12
+
92de12
+      /* For a trivial ACL, max 6 (typically 3) ACEs, 3 allow, 3 deny.
92de12
+         Check that there is at most one ACE of each TYPE and WHO.  */
92de12
+      int who2
92de12
+        = (wholen == 6 && memcmp (xattr, "OWNER@", 6) == 0 ? 0
92de12
+           : wholen == 6 && memcmp (xattr, "GROUP@", 6) == 0 ? 2
92de12
+           : wholen == 9 && memcmp (xattr, "EVERYONE@", 9) == 0 ? 4
92de12
+           : -1);
92de12
+      if (who2 < 0)
92de12
+        return 1;
92de12
+      int ace_found_bit = 1 << (who2 | type);
92de12
+      if (ace_found & ace_found_bit)
92de12
+        return 1;
92de12
+      ace_found |= ace_found_bit;
92de12
+
92de12
+      xattr = (uint32_t *) ((char *) xattr + wholen4);
92de12
+    }
92de12
+
92de12
+  return 0;
92de12
+}
92de12
 #endif
92de12
 
92de12
 /* Return 1 if NAME has a nontrivial access control list,
92de12
@@ -56,6 +139,7 @@ file_has_acl (char const *name, struct stat const *sb)
92de12
 # if GETXATTR_WITH_POSIX_ACLS
92de12
 
92de12
       ssize_t ret;
92de12
+      int initial_errno = errno;
92de12
 
92de12
       ret = getxattr (name, XATTR_NAME_POSIX_ACL_ACCESS, NULL, 0);
92de12
       if (ret < 0 && errno == ENODATA)
92de12
@@ -73,20 +157,33 @@ file_has_acl (char const *name, struct stat const *sb)
92de12
         }
92de12
 
92de12
       if (ret < 0)
92de12
-        { /* we might be on NFS, so try to check NFSv4 ACLs too */
92de12
-          char xattr[TRIVIAL_NFS4_ACL_MAX_LENGTH];
92de12
-
92de12
-          errno = 0; /* we need to reset errno set by the previous getxattr() */
92de12
-          ret = getxattr (name, XATTR_NAME_NFSV4_ACL, xattr, TRIVIAL_NFS4_ACL_MAX_LENGTH);
92de12
-          if (ret < 0 && errno == ENODATA)
92de12
-            ret = 0;
92de12
+        {
92de12
+          /* Check for NFSv4 ACLs.  The max length of a trivial
92de12
+             ACL is 6 words for owner, 6 for group, 7 for everyone,
92de12
+             all times 2 because there are both allow and deny ACEs.
92de12
+             There are 6 words for owner because of type, flag, mask,
92de12
+             wholen, "OWNER@"+pad and similarly for group; everyone is
92de12
+             another word to hold "EVERYONE@".  */
92de12
+          uint32_t xattr[2 * (6 + 6 + 7)];
92de12
+
92de12
+          ret = getxattr (name, XATTR_NAME_NFSV4_ACL, xattr, sizeof xattr);
92de12
+          if (ret < 0)
92de12
+            switch (errno)
92de12
+              {
92de12
+              case ENODATA: return 0;
92de12
+              case ERANGE : return 1; /* ACL must be nontrivial.  */
92de12
+              }
92de12
           else
92de12
-            if (ret < 0 && errno == ERANGE)
92de12
-              return 1;  /* we won't fit into the buffer, so non-trivial ACL is presented */
92de12
-            else
92de12
-              if (ret > 0)
92de12
-                /* looks like trivial ACL, but we need to investigate further */
92de12
-                return acl_nfs4_nontrivial (xattr, ret);
92de12
+            {
92de12
+              /* It looks like a trivial ACL, but investigate further.  */
92de12
+              ret = acl_nfs4_nontrivial (xattr, ret);
92de12
+              if (ret < 0)
92de12
+                {
92de12
+                  errno = EINVAL;
92de12
+                  return ret;
92de12
+                }
92de12
+              errno = initial_errno;
92de12
+            }
92de12
         }
92de12
       if (ret < 0)
92de12
         return - acl_errno_valid (errno);
92de12
-- 
92de12
2.38.1
92de12
92de12
92de12
From faf965110372c82cd99e9f44f0c64f03cdabb2c1 Mon Sep 17 00:00:00 2001
92de12
From: Paul Eggert <eggert@cs.ucla.edu>
92de12
Date: Tue, 27 Dec 2022 20:00:58 -0800
92de12
Subject: [PATCH 3/3] file-has-acl: fix recently-introduced NFSv4 bug
92de12
92de12
* lib/file-has-acl.c (acl_nfs4_nontrivial): Fix off-by-one
92de12
error when rounding WHOLEN up to next multiple of 4.
92de12
Pacify GCC 12.2.1 -Wcast-align.
92de12
92de12
Upstream-commit: d65e5a8ba77595a598c9ddb8dfa09c4aea732659
92de12
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
92de12
---
92de12
 lib/file-has-acl.c | 9 +++++----
92de12
 1 file changed, 5 insertions(+), 4 deletions(-)
92de12
92de12
diff --git a/lib/file-has-acl.c b/lib/file-has-acl.c
92de12
index 676523b..7876edc 100644
92de12
--- a/lib/file-has-acl.c
92de12
+++ b/lib/file-has-acl.c
92de12
@@ -81,9 +81,10 @@ acl_nfs4_nontrivial (uint32_t *xattr, ssize_t nbytes)
92de12
       uint32_t flag = ntohl (xattr[1]);
92de12
       uint32_t wholen = ntohl (xattr[3]);
92de12
       xattr += 4;
92de12
-      int64_t wholen4 = wholen;
92de12
-      wholen4 = ((wholen4 + (BYTES_PER_NETWORK_UINT))
92de12
-                 & ~ (BYTES_PER_NETWORK_UINT - 1));
92de12
+      int whowords = (wholen / BYTES_PER_NETWORK_UINT
92de12
+                      + (wholen % BYTES_PER_NETWORK_UINT != 0));
92de12
+      int64_t wholen4 = whowords;
92de12
+      wholen4 *= BYTES_PER_NETWORK_UINT;
92de12
 
92de12
       /* Trivial ACLs have only ACE4_ACCESS_ALLOWED_ACE_TYPE or
92de12
          ACE4_ACCESS_DENIED_ACE_TYPE.  */
92de12
@@ -115,7 +116,7 @@ acl_nfs4_nontrivial (uint32_t *xattr, ssize_t nbytes)
92de12
         return 1;
92de12
       ace_found |= ace_found_bit;
92de12
 
92de12
-      xattr = (uint32_t *) ((char *) xattr + wholen4);
92de12
+      xattr += whowords;
92de12
     }
92de12
 
92de12
   return 0;
92de12
-- 
92de12
2.38.1
92de12