]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
preadv2/pwritev2: Handle offset == -1 [BZ #22753]
authorFlorian Weimer <fweimer@redhat.com>
Fri, 2 Feb 2018 09:46:26 +0000 (10:46 +0100)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 28 Sep 2018 18:16:02 +0000 (15:16 -0300)
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
(cherry picked from commit d4b4a00a462348750bb18544eb30853ee6ac5d10)

14 files changed:
ChangeLog
NEWS
manual/llio.texi
misc/tst-preadvwritev-common.c
misc/tst-preadvwritev2.c
misc/tst-preadvwritev64v2.c
sysdeps/posix/preadv2.c
sysdeps/posix/preadv64v2.c
sysdeps/posix/pwritev2.c
sysdeps/posix/pwritev64v2.c
sysdeps/unix/sysv/linux/preadv2.c
sysdeps/unix/sysv/linux/preadv64v2.c
sysdeps/unix/sysv/linux/pwritev2.c
sysdeps/unix/sysv/linux/pwritev64v2.c

index 6848c7d550778b2df017801fa6c0fb5aee74c89f..4baa85bcfcb65e4e72aca34e52b3317b5f58351a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2018-09-28  Florian Weimer  <fweimer@redhat.com>
+
+       [BZ #22753]
+       * sysdeps/posix/preadv2.c (preadv2): Handle offset == -1.
+       * sysdeps/posix/preadv64v2.c (preadv64v2): Likewise.
+       * sysdeps/posix/pwritev2.c (pwritev2): Likewise.
+       * sysdeps/posix/pwritev64v2.c (pwritev64v2): Likweise.
+       * sysdeps/unix/sysv/linux/preadv2.c (preadv2): Likewise.
+       * sysdeps/unix/sysv/linux/preadv64v2.c (preadv64v2): Likewise.
+       * sysdeps/unix/sysv/linux/pwritev2.c (pwritev2): Likewise.
+       * sysdeps/unix/sysv/linux/pwritev64v2.c (pwritev64v2): Likweise.
+       * manual/llio.texi (Scatter-Gather): Mention offset -1.
+       * misc/tst-preadvwritev-common.c (do_test_without_offset): New.
+       * misc/tst-preadvwritev2.c (do_test): Call it.
+       * misc/tst-preadvwritev64v2.c (do_test): Likewise.
+       * NEWS: Add bug fixed.
+
 2018-09-06  Stefan Liebler  <stli@linux.ibm.com>
 
        * sysdeps/unix/sysv/linux/spawni.c (maybe_script_execute):
diff --git a/NEWS b/NEWS
index a868a652f71ae10224ca0901b42a773e8c24377e..d5e3403e8ba52ff44a8bd847478ddb3937069139 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -135,6 +135,7 @@ The following bugs are resolved with this release:
     (CVE-2018-1000001)
   [22685] powerpc: Fix syscalls during early process initialization
   [22715] x86-64: Properly align La_x86_64_retval to VEC_SIZE
+  [22753] libc: preadv2/pwritev2 fallback code should handle offset=-1
   [22774] malloc: Integer overflow in malloc (CVE-2018-6551)
   [22786] Fix path length overflow in realpath
   [23005] resolv: Fix crash in resolver on memory allocation failure
index e72c53c7858149cf2005cd3c36559c4619390f44..4ce9ee360eb5167b9e6d650675343d007ffce808 100644 (file)
@@ -754,9 +754,13 @@ When the source file is compiled using @code{_FILE_OFFSET_BITS == 64} on a
 @c This is a syscall for Linux v4.6.  The sysdeps/posix fallback emulation
 @c is also MT-Safe since it calls preadv.
 
-This function is similar to the @code{preadv} function, with the difference
-it adds an extra @var{flags} parameter of type @code{int}.  The supported
-@var{flags} are dependent of the underlying system.  For Linux it supports:
+This function is similar to the @code{preadv} function, with the
+difference it adds an extra @var{flags} parameter of type @code{int}.
+Additionally, if @var{offset} is @math{-1}, the current file position
+is used and updated (like the @code{readv} function).
+
+The supported @var{flags} are dependent of the underlying system.  For
+Linux it supports:
 
 @vtable @code
 @item RWF_HIPRI
@@ -823,10 +827,13 @@ When the source file is compiled using @code{_FILE_OFFSET_BITS == 64} on a
 @c This is a syscall for Linux v4.6.  The sysdeps/posix fallback emulation
 @c is also MT-Safe since it calls pwritev.
 
-This function is similar to the @code{pwritev} function, with the difference
-it adds an extra @var{flags} parameter of type @code{int}.  The supported
-@var{flags} are dependent of the underlying system and for Linux it supports
-the same ones as for @code{preadv2}.
+This function is similar to the @code{pwritev} function, with the
+difference it adds an extra @var{flags} parameter of type @code{int}.
+Additionally, if @var{offset} is @math{-1}, the current file position
+should is used and updated (like the @code{writev} function).
+
+The supported @var{flags} are dependent of the underlying system.  For
+Linux, the supported flags are the same as those for @code{preadv2}.
 
 When the source file is compiled with @code{_FILE_OFFSET_BITS == 64} the
 @code{pwritev2} function is in fact @code{pwritev64v2} and the type
index 676d4953ac798b1dbf8141777293577d006d0696..7f9a63f1f5740abb93e75243dd82d476a1f204dc 100644 (file)
@@ -16,6 +16,7 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
+#include <array_length.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <errno.h>
@@ -25,6 +26,7 @@
 
 #include <support/check.h>
 #include <support/temp_file.h>
+#include <support/xunistd.h>
 
 static char *temp_filename;
 static int temp_fd;
@@ -50,6 +52,42 @@ do_prepare (int argc, char **argv)
   pwritev (__fd, __iov, __iovcnt, __offset)
 #endif
 
+static __attribute__ ((unused)) void
+do_test_without_offset (void)
+{
+  xftruncate (temp_fd, 0);
+
+  xwrite (temp_fd, "123", 3);
+  xlseek (temp_fd, 2, SEEK_SET);
+  {
+    struct iovec iov[] =
+      {
+        { (void *) "abc", 3 },
+        { (void *) "xyzt", 4 },
+      };
+    TEST_COMPARE (PWRITEV (temp_fd, iov, array_length (iov), -1), 7);
+  }
+  TEST_COMPARE (xlseek (temp_fd, 0, SEEK_CUR), 9);
+
+  xlseek (temp_fd, 1, SEEK_SET);
+  char buf1[3];
+  char buf2[2];
+  {
+    struct iovec iov[] =
+      {
+        { buf1, sizeof (buf1) },
+        { buf2, sizeof (buf2) },
+      };
+    TEST_COMPARE (PREADV (temp_fd, iov, array_length (iov), -1),
+                  sizeof (buf1) + sizeof (buf2));
+    TEST_COMPARE (memcmp ("2ab", buf1, sizeof (buf1)), 0);
+    TEST_COMPARE (memcmp ("cx", buf2, sizeof (buf2)), 0);
+    TEST_COMPARE (xlseek (temp_fd, 0, SEEK_CUR), 6);
+  }
+
+  xftruncate (temp_fd, 0);
+}
+
 static int
 do_test_with_offset (off_t offset)
 {
index 682c7579da8e2d3b37ab5abdb76728b9c297a0bb..225f5e59c15b21708ecda7b3347f823216ad5990 100644 (file)
@@ -29,6 +29,7 @@ static int
 do_test (void)
 {
   do_test_with_invalid_flags ();
+  do_test_without_offset ();
 
   return do_test_with_offset (0);
 }
index 9ddc7625f043f00ff6d0decf1d17186c05518644..facdbdca616d98dc064d19cfeeb974136b783b53 100644 (file)
@@ -31,6 +31,7 @@ static int
 do_test (void)
 {
   do_test_with_invalid_flags ();
+  do_test_without_offset ();
 
   return do_test_with_offset (0);
 }
index d27f7028ed8754aed1ee4fa3b1644b17ccf277c4..fe73d3cbead5b2d025ec7719a2c69303ede0c79f 100644 (file)
@@ -32,7 +32,10 @@ preadv2 (int fd, const struct iovec *vector, int count, OFF_T offset,
       return -1;
     }
 
-  return preadv (fd, vector, count, offset);
+  if (offset == -1)
+    return __readv (fd, vector, count);
+  else
+    return preadv (fd, vector, count, offset);
 }
 
 #endif
index ce7cb40bf2014ddc72f9a057b72985d2e5143152..8569c8b39898851c957e074b6f0150b32ccf8f22 100644 (file)
@@ -29,7 +29,10 @@ preadv64v2 (int fd, const struct iovec *vector, int count, OFF_T offset,
       return -1;
     }
 
-  return preadv64 (fd, vector, count, offset);
+  if (offset == -1)
+    return __readv (fd, vector, count);
+  else
+    return preadv64 (fd, vector, count, offset);
 }
 
 #ifdef __OFF_T_MATCHES_OFF64_T
index 7ec8cbc4074a3c0aeb016a71ce51aa52ba3f3e88..b24b491a815cc2d58e9c0a1c6cf4d1200ac10a2e 100644 (file)
@@ -32,7 +32,10 @@ pwritev2 (int fd, const struct iovec *vector, int count, OFF_T offset,
       return -1;
     }
 
-  return pwritev (fd, vector, count, offset);
+  if (offset == -1)
+    return __writev (fd, vector, count);
+  else
+    return pwritev (fd, vector, count, offset);
 }
 
 #endif
index be98aeed9d574beb7ccf32b676df1d584cd22abb..ae4c4284c24f75175fa264ded80535a3513f6ec1 100644 (file)
@@ -30,7 +30,10 @@ pwritev64v2 (int fd, const struct iovec *vector, int count, OFF_T offset,
       return -1;
     }
 
-  return pwritev64 (fd, vector, count, offset);
+  if (offset == -1)
+    return __writev (fd, vector, count);
+  else
+    return pwritev64 (fd, vector, count, offset);
 }
 
 #ifdef __OFF_T_MATCHES_OFF64_T
index 137e2dd7913f3264e7203ae6a7cbc2308f4fc886..a7a3aeeee691f84c56cf74d55094e0a1cb23c7d4 100644 (file)
@@ -49,7 +49,10 @@ preadv2 (int fd, const struct iovec *vector, int count, off_t offset,
       __set_errno (ENOTSUP);
       return -1;
     }
-  return preadv (fd, vector, count, offset);
+  if (offset == -1)
+    return __readv (fd, vector, count);
+  else
+    return preadv (fd, vector, count, offset);
 }
 
 #endif
index 8f413253f458dfeb9df09dcdcbfcea08808bf46e..53c946861c2ba6d8d53cab499faf40c9b02f4f3f 100644 (file)
@@ -47,7 +47,11 @@ preadv64v2 (int fd, const struct iovec *vector, int count, off64_t offset,
       __set_errno (ENOTSUP);
       return -1;
     }
-  return preadv64 (fd, vector, count, offset);
+
+  if (offset == -1)
+    return __readv (fd, vector, count);
+  else
+    return preadv64 (fd, vector, count, offset);
 }
 
 #ifdef __OFF_T_MATCHES_OFF64_T
index 8e5032fe2feda8f21c927762a9e86f9531fe490c..17677df98d7ae05c48648f109db462650771dcab 100644 (file)
@@ -45,7 +45,10 @@ pwritev2 (int fd, const struct iovec *vector, int count, off_t offset,
       __set_errno (ENOTSUP);
       return -1;
     }
-  return pwritev (fd, vector, count, offset);
+  if (offset == -1)
+    return __writev (fd, vector, count);
+  else
+    return pwritev (fd, vector, count, offset);
 }
 
 #endif
index d2800c6657fa406538369e2573802b6cba51765d..f5753eede5b3f857d73deef1b665824351ceea08 100644 (file)
@@ -47,7 +47,10 @@ pwritev64v2 (int fd, const struct iovec *vector, int count, off64_t offset,
       __set_errno (ENOTSUP);
       return -1;
     }
-  return pwritev64 (fd, vector, count, offset);
+  if (offset == -1)
+    return __writev (fd, vector, count);
+  else
+    return pwritev64 (fd, vector, count, offset);
 }
 
 #ifdef __OFF_T_MATCHES_OFF64_T