Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libc/sys check for EFAULT on reads and writes to m...



details:   https://anonhg.NetBSD.org/src/rev/043624257100
branches:  trunk
changeset: 825332:043624257100
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Jul 09 22:04:04 2017 +0000

description:
check for EFAULT on reads and writes to memory with not permission.

diffstat:

 tests/lib/libc/sys/t_write.c |  58 ++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 56 insertions(+), 2 deletions(-)

diffs (95 lines):

diff -r 26d7432f81d8 -r 043624257100 tests/lib/libc/sys/t_write.c
--- a/tests/lib/libc/sys/t_write.c      Sun Jul 09 20:53:09 2017 +0000
+++ b/tests/lib/libc/sys/t_write.c      Sun Jul 09 22:04:04 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_write.c,v 1.3 2017/01/13 19:27:23 christos Exp $ */
+/* $NetBSD: t_write.c,v 1.4 2017/07/09 22:04:04 christos Exp $ */
 
 /*-
  * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
@@ -29,9 +29,10 @@
 #include <sys/cdefs.h>
 __COPYRIGHT("@(#) Copyright (c) 2008\
  The NetBSD Foundation, inc. All rights reserved.");
-__RCSID("$NetBSD: t_write.c,v 1.3 2017/01/13 19:27:23 christos Exp $");
+__RCSID("$NetBSD: t_write.c,v 1.4 2017/07/09 22:04:04 christos Exp $");
 
 #include <sys/uio.h>
+#include <sys/mman.h>
 
 #include <atf-c.h>
 #include <errno.h>
@@ -42,6 +43,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <unistd.h>
+#include <paths.h>
 
 static void             sighandler(int);
 
@@ -213,6 +215,56 @@
        ATF_REQUIRE_EQ_MSG(errno, EINVAL, "got: %s", strerror(errno));
 }
 
+ATF_TC(write_fault);
+
+ATF_TC_HEAD(write_fault, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Check that writing to non-permitted space returns EFAULT");
+}
+
+ATF_TC_BODY(write_fault, tc)
+{
+       int fd[2];
+       ATF_REQUIRE(pipe(fd) != -1);
+       // Can't use /dev/null cause it doesn't access the buffer.
+
+       void *map = mmap(NULL, 8192, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);  
+       ATF_REQUIRE(map != MAP_FAILED);
+
+       ssize_t retval = write(fd[1], map, 8192); 
+
+       ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
+       ATF_REQUIRE_EQ_MSG(errno, EFAULT, "got: %s", strerror(errno));
+
+       close(fd[0]);         
+       close(fd[1]);         
+}
+
+ATF_TC(read_fault);
+
+ATF_TC_HEAD(read_fault, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Check that reading from non-permitted space returns EFAULT");
+}
+
+ATF_TC_BODY(read_fault, tc)
+{
+       int fd = open(_PATH_DEVZERO, O_RDONLY);
+       ATF_REQUIRE(fd != -1);
+
+       void *map = mmap(NULL, 8192, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);  
+       ATF_REQUIRE(map != MAP_FAILED);
+
+       ssize_t retval = read(fd, map, 8192); 
+
+       ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
+       ATF_REQUIRE_EQ_MSG(errno, EFAULT, "got: %s", strerror(errno));
+
+       close(fd);         
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 
@@ -221,6 +273,8 @@
        ATF_TP_ADD_TC(tp, write_pos);
        ATF_TP_ADD_TC(tp, write_ret);
        ATF_TP_ADD_TC(tp, writev_iovmax);
+       ATF_TP_ADD_TC(tp, write_fault);
+       ATF_TP_ADD_TC(tp, read_fault);
 
        return atf_no_error();
 }



Home | Main Index | Thread Index | Old Index