summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2018-04-27 18:07:31 +0000
committerjhb <jhb@FreeBSD.org>2018-04-27 18:07:31 +0000
commite721d1ec9168f194b473a5f2ba6cd8efb19e30d4 (patch)
treeba57d9414d25ed24f0b03f8f437645efdf895606 /tests
parent4223ca8e51c2eda332673d16f0dbf27e533a17a1 (diff)
downloadFreeBSD-src-e721d1ec9168f194b473a5f2ba6cd8efb19e30d4.zip
FreeBSD-src-e721d1ec9168f194b473a5f2ba6cd8efb19e30d4.tar.gz
MFC 332657:
Properly do a deep copy of the ioctls capability array for fget_cap(). fget_cap() tries to do a cheaper snapshot of a file descriptor without holding the file descriptor lock. This snapshot does not do a deep copy of the ioctls capability array, but instead uses a different return value to inform the caller to retry the copy with the lock held. However, filecaps_copy() was returning 1 to indicate that a retry was required, and fget_cap() was checking for 0 (actually '!filecaps_copy()'). As a result, fget_cap() did not do a deep copy of the ioctls array and just reused the original pointer. This cause multiple file descriptor entries to think they owned the same pointer and eventually resulted in duplicate frees. The only code path that I'm aware of that triggers this is to create a listen socket that has a restricted list of ioctls and then call accept() which calls fget_cap() with a valid filecaps structure from getsock_cap(). To fix, change the return value of filecaps_copy() to return true if it succeeds in copying the caps and false if it fails because the lock is required. I find this more intuitive than fixing the caller in this case. While here, change the return type from 'int' to 'bool'. Finally, make filecaps_copy() more robust in the failure case by not copying any of the source filecaps structure over. This avoids the possibility of leaking a pointer into a structure if a similar future caller doesn't properly handle the return value from filecaps_copy() at the expense of one more branch. I also added a test case that panics before this change and now passes.
Diffstat (limited to 'tests')
-rw-r--r--tests/sys/Makefile1
-rw-r--r--tests/sys/capsicum/Makefile9
-rw-r--r--tests/sys/capsicum/ioctls_test.c127
3 files changed, 137 insertions, 0 deletions
diff --git a/tests/sys/Makefile b/tests/sys/Makefile
index 1bcd76e..0c55fd1 100644
--- a/tests/sys/Makefile
+++ b/tests/sys/Makefile
@@ -4,6 +4,7 @@ TESTSDIR= ${TESTSBASE}/sys
TESTS_SUBDIRS+= acl
TESTS_SUBDIRS+= aio
+TESTS_SUBDIRS+= capsicum
TESTS_SUBDIRS+= fifo
TESTS_SUBDIRS+= file
TESTS_SUBDIRS+= fs
diff --git a/tests/sys/capsicum/Makefile b/tests/sys/capsicum/Makefile
new file mode 100644
index 0000000..d0b0aae
--- /dev/null
+++ b/tests/sys/capsicum/Makefile
@@ -0,0 +1,9 @@
+# $FreeBSD$
+
+TESTSDIR= ${TESTSBASE}/sys/capsicum
+
+ATF_TESTS_C+= ioctls_test
+
+WARNS?= 6
+
+.include <bsd.test.mk>
diff --git a/tests/sys/capsicum/ioctls_test.c b/tests/sys/capsicum/ioctls_test.c
new file mode 100644
index 0000000..08c5893
--- /dev/null
+++ b/tests/sys/capsicum/ioctls_test.c
@@ -0,0 +1,127 @@
+/*-
+ * Copyright (c) 2018 John Baldwin <jhb@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/capsicum.h>
+#include <sys/filio.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+/*
+ * A variant of ATF_REQUIRE that is suitable for use in child
+ * processes. This only works if the parent process is tripped up by
+ * the early exit and fails some requirement itself.
+ */
+#define CHILD_REQUIRE(exp) do { \
+ if (!(exp)) \
+ child_fail_require(__FILE__, __LINE__, \
+ #exp " not met"); \
+ } while (0)
+
+static __dead2 void
+child_fail_require(const char *file, int line, const char *str)
+{
+ char buf[128];
+
+ snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
+ write(2, buf, strlen(buf));
+ _exit(32);
+}
+
+/*
+ * Exercise the edge case of a custom ioctl list being copied from a
+ * listen socket to an accepted socket.
+ */
+ATF_TC_WITHOUT_HEAD(cap_ioctls__listen_copy);
+ATF_TC_BODY(cap_ioctls__listen_copy, tc)
+{
+ struct sockaddr_in sin;
+ cap_rights_t rights;
+ u_long cmds[] = { FIONREAD };
+ socklen_t len;
+ pid_t pid;
+ char dummy;
+ int s[2], status;
+
+ s[0] = socket(AF_INET, SOCK_STREAM, 0);
+ ATF_REQUIRE(s[0] > 0);
+
+ /* Bind to an arbitrary unused port. */
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_len = sizeof(sin);
+ sin.sin_family = AF_INET;
+ sin.sin_port = 0;
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ ATF_REQUIRE(bind(s[0], (struct sockaddr *)&sin, sizeof(sin)) == 0);
+
+ CHILD_REQUIRE(listen(s[0], 1) == 0);
+
+ len = sizeof(sin);
+ ATF_REQUIRE(getsockname(s[0], (struct sockaddr *)&sin, &len) == 0);
+ ATF_REQUIRE(len == sizeof(sin));
+
+ cap_rights_init(&rights, CAP_ACCEPT, CAP_IOCTL);
+ ATF_REQUIRE(cap_rights_limit(s[0], &rights) == 0);
+ ATF_REQUIRE(cap_ioctls_limit(s[0], cmds, nitems(cmds)) == 0);
+
+ pid = fork();
+ if (pid == 0) {
+ s[1] = accept(s[0], NULL, NULL);
+ CHILD_REQUIRE(s[1] > 0);
+
+ /* Close both sockets during exit(). */
+ exit(0);
+ }
+
+ ATF_REQUIRE(pid > 0);
+
+ ATF_REQUIRE(close(s[0]) == 0);
+ s[1] = socket(AF_INET, SOCK_STREAM, 0);
+ ATF_REQUIRE(s[1] > 0);
+ ATF_REQUIRE(connect(s[1], (struct sockaddr *)&sin, sizeof(sin)) == 0);
+ ATF_REQUIRE(read(s[1], &dummy, sizeof(dummy)) == 0);
+ ATF_REQUIRE(close(s[1]) == 0);
+
+ ATF_REQUIRE(wait(&status) == pid);
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE(WEXITSTATUS(status) == 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, cap_ioctls__listen_copy);
+
+ return (atf_no_error());
+}
OpenPOWER on IntegriCloud