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