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