50f89d
commit 7a16bdbb9ff4122af0a28dc20996c95352011fdd
50f89d
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
50f89d
Date:   Wed Aug 29 16:36:44 2018 -0300
50f89d
50f89d
    Fix misreported errno on preadv2/pwritev2 (BZ#23579)
50f89d
    
50f89d
    The fallback code of Linux wrapper for preadv2/pwritev2 executes
50f89d
    regardless of the errno code for preadv2, instead of the case where
50f89d
    the syscall is not supported.
50f89d
    
50f89d
    This fixes it by calling the fallback code iff errno is ENOSYS. The
50f89d
    patch also adds tests for both invalid file descriptor and invalid
50f89d
    iov_len and vector count.
50f89d
    
50f89d
    The only discrepancy between preadv2 and fallback code regarding
50f89d
    error reporting is when an invalid flags are used.  The fallback code
50f89d
    bails out earlier with ENOTSUP instead of EINVAL/EBADF when the syscall
50f89d
    is used.
50f89d
    
50f89d
    Checked on x86_64-linux-gnu on a 4.4.0 and 4.15.0 kernel.
50f89d
    
50f89d
            [BZ #23579]
50f89d
            * misc/tst-preadvwritev2-common.c (do_test_with_invalid_fd): New
50f89d
            test.
50f89d
            * misc/tst-preadvwritev2.c, misc/tst-preadvwritev64v2.c (do_test):
50f89d
            Call do_test_with_invalid_fd.
50f89d
            * sysdeps/unix/sysv/linux/preadv2.c (preadv2): Use fallback code iff
50f89d
            errno is ENOSYS.
50f89d
            * sysdeps/unix/sysv/linux/preadv64v2.c (preadv64v2): Likewise.
50f89d
            * sysdeps/unix/sysv/linux/pwritev2.c (pwritev2): Likewise.
50f89d
            * sysdeps/unix/sysv/linux/pwritev64v2.c (pwritev64v2): Likewise.
50f89d
50f89d
diff --git a/misc/tst-preadvwritev2-common.c b/misc/tst-preadvwritev2-common.c
50f89d
index f889a21544947042..50b9da3fea56d288 100644
50f89d
--- a/misc/tst-preadvwritev2-common.c
50f89d
+++ b/misc/tst-preadvwritev2-common.c
50f89d
@@ -19,9 +19,6 @@
50f89d
 #include <limits.h>
50f89d
 #include <support/check.h>
50f89d
 
50f89d
-static void
50f89d
-do_test_with_invalid_flags (void)
50f89d
-{
50f89d
 #ifndef RWF_HIPRI
50f89d
 # define RWF_HIPRI 0
50f89d
 #endif
50f89d
@@ -39,6 +36,68 @@ do_test_with_invalid_flags (void)
50f89d
 #endif
50f89d
 #define RWF_SUPPORTED	(RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT \
50f89d
 			 | RWF_APPEND)
50f89d
+
50f89d
+static void
50f89d
+do_test_with_invalid_fd (void)
50f89d
+{
50f89d
+  char buf[256];
50f89d
+  struct iovec iov = { buf, sizeof buf };
50f89d
+
50f89d
+  /* Check with flag being 0 to use the fallback code which calls pwritev
50f89d
+     or writev.  */
50f89d
+  TEST_VERIFY (preadv2 (-1, &iov, 1, -1, 0) == -1);
50f89d
+  TEST_COMPARE (errno, EBADF);
50f89d
+  TEST_VERIFY (pwritev2 (-1, &iov, 1, -1, 0) == -1);
50f89d
+  TEST_COMPARE (errno, EBADF);
50f89d
+
50f89d
+  /* Same tests as before but with flags being different than 0.  Since
50f89d
+     there is no emulation for any flag value, fallback code returns
50f89d
+     ENOTSUP.  This is different running on a kernel with preadv2/pwritev2
50f89d
+     support, where EBADF is returned).  */
50f89d
+  TEST_VERIFY (preadv2 (-1, &iov, 1, 0, RWF_HIPRI) == -1);
50f89d
+  TEST_VERIFY (errno == EBADF || errno == ENOTSUP);
50f89d
+  TEST_VERIFY (pwritev2 (-1, &iov, 1, 0, RWF_HIPRI) == -1);
50f89d
+  TEST_VERIFY (errno == EBADF || errno == ENOTSUP);
50f89d
+}
50f89d
+
50f89d
+static void
50f89d
+do_test_with_invalid_iov (void)
50f89d
+{
50f89d
+  {
50f89d
+    char buf[256];
50f89d
+    struct iovec iov;
50f89d
+
50f89d
+    iov.iov_base = buf;
50f89d
+    iov.iov_len = (size_t)SSIZE_MAX + 1;
50f89d
+
50f89d
+    TEST_VERIFY (preadv2 (temp_fd, &iov, 1, 0, 0) == -1);
50f89d
+    TEST_COMPARE (errno, EINVAL);
50f89d
+    TEST_VERIFY (pwritev2 (temp_fd, &iov, 1, 0, 0) == -1);
50f89d
+    TEST_COMPARE (errno, EINVAL);
50f89d
+
50f89d
+    /* Same as for invalid file descriptor tests, emulation fallback
50f89d
+       first checks for flag value and return ENOTSUP.  */
50f89d
+    TEST_VERIFY (preadv2 (temp_fd, &iov, 1, 0, RWF_HIPRI) == -1);
50f89d
+    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
50f89d
+    TEST_VERIFY (pwritev2 (temp_fd, &iov, 1, 0, RWF_HIPRI) == -1);
50f89d
+    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
50f89d
+  }
50f89d
+
50f89d
+  {
50f89d
+    /* An invalid iovec buffer should trigger an invalid memory access
50f89d
+       or an error (Linux for instance returns EFAULT).  */
50f89d
+    struct iovec iov[IOV_MAX+1] = { 0 };
50f89d
+
50f89d
+    TEST_VERIFY (preadv2 (temp_fd, iov, IOV_MAX + 1, 0, RWF_HIPRI) == -1);
50f89d
+    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
50f89d
+    TEST_VERIFY (pwritev2 (temp_fd, iov, IOV_MAX + 1, 0, RWF_HIPRI) == -1);
50f89d
+    TEST_VERIFY (errno == EINVAL || errno == ENOTSUP);
50f89d
+  }
50f89d
+}
50f89d
+
50f89d
+static void
50f89d
+do_test_with_invalid_flags (void)
50f89d
+{
50f89d
   /* Set the next bit from the mask of all supported flags.  */
50f89d
   int invalid_flag = RWF_SUPPORTED != 0 ? __builtin_clz (RWF_SUPPORTED) : 2;
50f89d
   invalid_flag = 0x1 << ((sizeof (int) * CHAR_BIT) - invalid_flag);
50f89d
diff --git a/misc/tst-preadvwritev2.c b/misc/tst-preadvwritev2.c
50f89d
index be22802dbe00317f..cb58cbe41ecc639d 100644
50f89d
--- a/misc/tst-preadvwritev2.c
50f89d
+++ b/misc/tst-preadvwritev2.c
50f89d
@@ -30,6 +30,8 @@ do_test (void)
50f89d
 {
50f89d
   do_test_with_invalid_flags ();
50f89d
   do_test_without_offset ();
50f89d
+  do_test_with_invalid_fd ();
50f89d
+  do_test_with_invalid_iov ();
50f89d
 
50f89d
   return do_test_with_offset (0);
50f89d
 }
50f89d
diff --git a/misc/tst-preadvwritev64v2.c b/misc/tst-preadvwritev64v2.c
50f89d
index 8d3cc32b284dbf4c..6a9de54c786acc53 100644
50f89d
--- a/misc/tst-preadvwritev64v2.c
50f89d
+++ b/misc/tst-preadvwritev64v2.c
50f89d
@@ -32,6 +32,8 @@ do_test (void)
50f89d
 {
50f89d
   do_test_with_invalid_flags ();
50f89d
   do_test_without_offset ();
50f89d
+  do_test_with_invalid_fd ();
50f89d
+  do_test_with_invalid_iov ();
50f89d
 
50f89d
   return do_test_with_offset (0);
50f89d
 }
50f89d
diff --git a/sysdeps/unix/sysv/linux/preadv2.c b/sysdeps/unix/sysv/linux/preadv2.c
50f89d
index c8bf0764ef2629fc..bb08cbc5fd96962e 100644
50f89d
--- a/sysdeps/unix/sysv/linux/preadv2.c
50f89d
+++ b/sysdeps/unix/sysv/linux/preadv2.c
50f89d
@@ -32,7 +32,7 @@ preadv2 (int fd, const struct iovec *vector, int count, off_t offset,
50f89d
 # ifdef __NR_preadv2
50f89d
   ssize_t result = SYSCALL_CANCEL (preadv2, fd, vector, count,
50f89d
 				   LO_HI_LONG (offset), flags);
50f89d
-  if (result >= 0)
50f89d
+  if (result >= 0 || errno != ENOSYS)
50f89d
     return result;
50f89d
 # endif
50f89d
   /* Trying to emulate the preadv2 syscall flags is troublesome:
50f89d
diff --git a/sysdeps/unix/sysv/linux/preadv64v2.c b/sysdeps/unix/sysv/linux/preadv64v2.c
50f89d
index d7400a0252a8c6a1..b72a047347b1db0e 100644
50f89d
--- a/sysdeps/unix/sysv/linux/preadv64v2.c
50f89d
+++ b/sysdeps/unix/sysv/linux/preadv64v2.c
50f89d
@@ -30,7 +30,7 @@ preadv64v2 (int fd, const struct iovec *vector, int count, off64_t offset,
50f89d
 #ifdef __NR_preadv64v2
50f89d
   ssize_t result = SYSCALL_CANCEL (preadv64v2, fd, vector, count,
50f89d
 				   LO_HI_LONG (offset), flags);
50f89d
-  if (result >= 0)
50f89d
+  if (result >= 0 || errno != ENOSYS)
50f89d
     return result;
50f89d
 #endif
50f89d
   /* Trying to emulate the preadv2 syscall flags is troublesome:
50f89d
diff --git a/sysdeps/unix/sysv/linux/pwritev2.c b/sysdeps/unix/sysv/linux/pwritev2.c
50f89d
index 29c2264c8f3d949a..26333ebd43c5f0af 100644
50f89d
--- a/sysdeps/unix/sysv/linux/pwritev2.c
50f89d
+++ b/sysdeps/unix/sysv/linux/pwritev2.c
50f89d
@@ -28,7 +28,7 @@ pwritev2 (int fd, const struct iovec *vector, int count, off_t offset,
50f89d
 # ifdef __NR_pwritev2
50f89d
   ssize_t result = SYSCALL_CANCEL (pwritev2, fd, vector, count,
50f89d
 				   LO_HI_LONG (offset), flags);
50f89d
-  if (result >= 0)
50f89d
+  if (result >= 0 || errno != ENOSYS)
50f89d
     return result;
50f89d
 # endif
50f89d
   /* Trying to emulate the pwritev2 syscall flags is troublesome:
50f89d
diff --git a/sysdeps/unix/sysv/linux/pwritev64v2.c b/sysdeps/unix/sysv/linux/pwritev64v2.c
50f89d
index 42da321149bce40d..17ea905aa6a8db94 100644
50f89d
--- a/sysdeps/unix/sysv/linux/pwritev64v2.c
50f89d
+++ b/sysdeps/unix/sysv/linux/pwritev64v2.c
50f89d
@@ -30,7 +30,7 @@ pwritev64v2 (int fd, const struct iovec *vector, int count, off64_t offset,
50f89d
 #ifdef __NR_pwritev64v2
50f89d
   ssize_t result = SYSCALL_CANCEL (pwritev64v2, fd, vector, count,
50f89d
 				   LO_HI_LONG (offset), flags);
50f89d
-  if (result >= 0)
50f89d
+  if (result >= 0 || errno != ENOSYS)
50f89d
     return result;
50f89d
 #endif
50f89d
   /* Trying to emulate the pwritev2 syscall flags is troublesome: