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