summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorguenther <guenther@openbsd.org>2011-11-06 15:00:34 +0000
committerguenther <guenther@openbsd.org>2011-11-06 15:00:34 +0000
commitccafd5ad91871007293da6d25cde8995eea61b02 (patch)
tree4cb68165979f9806843021bcd6ebb2cc3de94f8c
parentCorrectly report WB and WT cache modes on v7 (they were swapped). ok uwe@ (diff)
downloadwireguard-openbsd-ccafd5ad91871007293da6d25cde8995eea61b02.tar.xz
wireguard-openbsd-ccafd5ad91871007293da6d25cde8995eea61b02.zip
Regress tests for negative offsets, wrapping offsets, and the lack of
interaction of pwrite/pwritev with O_APPEND. Based on a patch from Alexander Polakov (polachok at gmail.com) Further tests with deraadt@
-rw-r--r--regress/sys/kern/pread/pread.c27
-rw-r--r--regress/sys/kern/preadv/preadv.c27
-rw-r--r--regress/sys/kern/pwrite/pwrite.c31
-rw-r--r--regress/sys/kern/pwritev/pwritev.c33
4 files changed, 102 insertions, 16 deletions
diff --git a/regress/sys/kern/pread/pread.c b/regress/sys/kern/pread/pread.c
index 4a57babdb1b..ae24d7a27b1 100644
--- a/regress/sys/kern/pread/pread.c
+++ b/regress/sys/kern/pread/pread.c
@@ -1,13 +1,14 @@
-/* $OpenBSD: pread.c,v 1.3 2011/11/05 15:43:04 guenther Exp $ */
+/* $OpenBSD: pread.c,v 1.4 2011/11/06 15:00:34 guenther Exp $ */
/*
* Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
int
main(int argc, char *argv[])
@@ -45,6 +46,24 @@ main(int argc, char *argv[])
if (c != magic[1])
errx(1, "read2 %c != %c", c, magic[1]);
+ if ((ret = pread(fd, &c, 1, -1)) != -1)
+ errx(1, "pread with negative offset succeeded,\
+ returning %d", ret);
+ if (errno != EINVAL)
+ err(1, "pread with negative offset");
+
+ if ((ret = pread(fd, &c, 3, LLONG_MAX)) != -1)
+ errx(1, "pread with wrapping offset succeeded,\
+ returning %d", ret);
+ if (errno != EINVAL)
+ err(1, "pread with wrapping offset");
+
+ if (read(fd, &c, 1) != 1)
+ err(1, "read3");
+
+ if (c != magic[2])
+ errx(1, "read3 %c != %c", c, magic[2]);
+
close(fd);
/* also, verify that pread fails on ttys */
diff --git a/regress/sys/kern/preadv/preadv.c b/regress/sys/kern/preadv/preadv.c
index 8bb2036f188..0ef03b4159e 100644
--- a/regress/sys/kern/preadv/preadv.c
+++ b/regress/sys/kern/preadv/preadv.c
@@ -1,15 +1,16 @@
-/* $OpenBSD: preadv.c,v 1.3 2011/11/05 15:43:04 guenther Exp $ */
+/* $OpenBSD: preadv.c,v 1.4 2011/11/06 15:00:34 guenther Exp $ */
/*
* Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
*/
#include <sys/types.h>
#include <sys/uio.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
int
main(int argc, char *argv[])
@@ -56,6 +57,24 @@ main(int argc, char *argv[])
if (c != magic[1])
errx(1, "read2 %c != %c", c, magic[1]);
+ if ((ret = preadv(fd, iv, 2, -1)) != -1)
+ errx(1, "preadv with negative offset succeeded,\
+ returning %d", ret);
+ if (errno != EINVAL)
+ err(1, "pread with negative offset");
+
+ if ((ret = preadv(fd, iv, 2, LLONG_MAX)) != -1)
+ errx(1, "preadv with wrapping offset succeeded,\
+ returning %d", ret);
+ if (errno != EINVAL)
+ err(1, "pread with wrapping offset");
+
+ if (read(fd, &c, 1) != 1)
+ err(1, "read3");
+
+ if (c != magic[2])
+ errx(1, "read3 %c != %c", c, magic[2]);
+
close(fd);
/* also, verify that preadv fails on ttys */
diff --git a/regress/sys/kern/pwrite/pwrite.c b/regress/sys/kern/pwrite/pwrite.c
index 1cfeacab090..caf0cf7b725 100644
--- a/regress/sys/kern/pwrite/pwrite.c
+++ b/regress/sys/kern/pwrite/pwrite.c
@@ -1,14 +1,15 @@
-/* $OpenBSD: pwrite.c,v 1.4 2011/11/05 15:43:04 guenther Exp $ */
+/* $OpenBSD: pwrite.c,v 1.5 2011/11/06 15:00:34 guenther Exp $ */
/*
* Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
*/
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
int
main(int argc, char *argv[])
@@ -49,6 +50,28 @@ main(int argc, char *argv[])
if (memcmp(buf, "0000125400", 10) != 0)
errx(1, "data mismatch: %s != %s", buf, "0000125400");
+ if ((ret = pwrite(fd, &magic[5], 1, -1)) != -1)
+ errx(1, "pwrite with negative offset succeeded,\
+ returning %d", ret);
+ if (errno != EINVAL)
+ err(1, "pwrite with negative offset");
+
+ if ((ret = pwrite(fd, &magic[5], 1, LLONG_MAX)) != -1)
+ errx(1, "pwrite with wrapping offset succeeded,\
+ returning %d", ret);
+ if (errno != EFBIG && errno != EINVAL)
+ err(1, "pwrite with wrapping offset");
+
+ /* pwrite should be unaffected by O_APPEND */
+ if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_APPEND))
+ err(1, "fcntl");
+ if (pwrite(fd, &magic[2], 3, 2) != 3)
+ err(1, "pwrite");
+ if (pread(fd, buf, 10, 0) != 10)
+ err(1, "pread");
+ if (memcmp(buf, "0023425400", 10) != 0)
+ errx(1, "data mismatch: %s != %s", buf, "0023425400");
+
close(fd);
/* also, verify that pwrite fails on ttys */
diff --git a/regress/sys/kern/pwritev/pwritev.c b/regress/sys/kern/pwritev/pwritev.c
index 759ecb19123..787d6608aeb 100644
--- a/regress/sys/kern/pwritev/pwritev.c
+++ b/regress/sys/kern/pwritev/pwritev.c
@@ -1,16 +1,17 @@
-/* $OpenBSD: pwritev.c,v 1.4 2011/11/05 15:43:04 guenther Exp $ */
+/* $OpenBSD: pwritev.c,v 1.5 2011/11/06 15:00:34 guenther Exp $ */
/*
* Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
*/
#include <sys/types.h>
#include <sys/uio.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
int
main(int argc, char *argv[])
@@ -54,9 +55,33 @@ main(int argc, char *argv[])
if (pread(fd, buf, 10, 0) != 10)
err(1, "pread");
+ iov[1].iov_base = &magic[1];
+ iov[1].iov_len = 2;
+ if ((ret = pwritev(fd, iov, 2, -1)) != -1)
+ errx(1, "pwritev with negative offset succeeded,\
+ returning %d", ret);
+ if (errno != EINVAL)
+ err(1, "pwritev with negative offset");
+
+ if ((ret = pwritev(fd, iov, 2, LLONG_MAX)) != -1)
+ errx(1, "pwritev with wrapping offset succeeded,\
+ returning %d", ret);
+ if (errno != EFBIG && errno != EINVAL)
+ err(1, "pwritev with wrapping offset");
+
if (memcmp(buf, "0000895800", 10) != 0)
errx(1, "data mismatch: %s != %s", buf, "0000895800");
+ /* pwrite should be unaffected by O_APPEND */
+ if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_APPEND))
+ err(1, "fcntl");
+ if (pwritev(fd, iov, 2, 2) != 4)
+ err(1, "pwritev");
+ if (pread(fd, buf, 10, 0) != 10)
+ err(1, "pread");
+ if (memcmp(buf, "0089125800", 10) != 0)
+ errx(1, "data mismatch: %s != %s", buf, "0089125800");
+
close(fd);
/* also, verify that pwritev fails on ttys */