Blame SOURCES/coreutils-nfsv4-acls.patch

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