summaryrefslogtreecommitdiff
path: root/tests/seccomp.c
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2015-11-15 15:51:14 +0100
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2015-11-15 16:32:15 +0100
commitbb4f4dadb4f1db196217053a1fe3af06edfe41fb (patch)
treee425c82dedb6d1df94b1708599d31650a817b38c /tests/seccomp.c
parenta6da9bc746b0949d199e05fa56fb29b146a11c56 (diff)
downloadgnutls-bb4f4dadb4f1db196217053a1fe3af06edfe41fb.tar.gz
tests: check operation of TLS and DTLS under seccomp when configured with --enable-seccomp-tests
Diffstat (limited to 'tests/seccomp.c')
-rw-r--r--tests/seccomp.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/tests/seccomp.c b/tests/seccomp.c
new file mode 100644
index 0000000000..97971e5cb5
--- /dev/null
+++ b/tests/seccomp.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2013 Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS test suite.
+ *
+ * ocserv is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * ocserv is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <stdio.h>
+
+#ifdef HAVE_LIBSECCOMP
+
+#include <seccomp.h>
+#include <errno.h>
+#include <string.h>
+#include "utils.h"
+
+int disable_system_calls(void)
+{
+ int ret;
+ scmp_filter_ctx ctx;
+
+ /*ctx = seccomp_init(SCMP_ACT_ERRNO(EPERM));*/
+ ctx = seccomp_init(SCMP_ACT_TRAP);
+ if (ctx == NULL) {
+ fprintf(stderr, "could not initialize seccomp");
+ return -1;
+ }
+
+#define ADD_SYSCALL(name, ...) \
+ ret = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(name), __VA_ARGS__); \
+ /* libseccomp returns EDOM for pseudo-syscalls due to a bug */ \
+ if (ret < 0 && ret != -EDOM) { \
+ fprintf(stderr, "could not add " #name " to seccomp filter: %s", strerror(-ret)); \
+ ret = -1; \
+ goto fail; \
+ }
+
+ ADD_SYSCALL(nanosleep, 0);
+ ADD_SYSCALL(time, 0);
+ ADD_SYSCALL(gettimeofday, 0);
+#if defined(HAVE_CLOCK_GETTIME)
+ ADD_SYSCALL(clock_gettime, 0);
+#endif
+
+ ADD_SYSCALL(getrusage, 0);
+
+ /* recv/send for the default pull/push functions. It is unknown
+ * which syscall is used by libc and varies from system to system
+ * so we enable all */
+ ADD_SYSCALL(recvmsg, 0);
+ ADD_SYSCALL(sendmsg, 0);
+ ADD_SYSCALL(send, 0);
+ ADD_SYSCALL(recv, 0);
+ ADD_SYSCALL(sendto, 0);
+ ADD_SYSCALL(recvfrom, 0);
+
+ /* writev() is used explicitly */
+ ADD_SYSCALL(writev, 0);
+
+ /* to read from /dev/urandom */
+ ADD_SYSCALL(read, 0);
+ ADD_SYSCALL(getrandom, 0);
+
+ /* we use it in select */
+ ADD_SYSCALL(sigprocmask, 0);
+ ADD_SYSCALL(rt_sigprocmask, 0);
+
+ /* used in to detect reading timeouts */
+ ADD_SYSCALL(select, 0);
+ /* in x86, glibc uses _newselect() */
+ ADD_SYSCALL(_newselect, 0);
+
+ /* for memory allocation */
+ ADD_SYSCALL(brk, 0);
+
+ /* the following are for generic operations, not specific to
+ * gnutls. */
+ ADD_SYSCALL(close, 0);
+ ADD_SYSCALL(exit, 0);
+ ADD_SYSCALL(exit_group, 0);
+
+ /* allow returning from signal handlers */
+ ADD_SYSCALL(sigreturn, 0);
+ ADD_SYSCALL(rt_sigreturn, 0);
+
+ ret = seccomp_load(ctx);
+ if (ret < 0) {
+ fprintf(stderr, "could not load seccomp filter");
+ ret = -1;
+ goto fail;
+ }
+
+ ret = 0;
+
+fail:
+ seccomp_release(ctx);
+ return ret;
+}
+#else
+int disable_system_calls(void)
+{
+ return 0;
+}
+#endif