Blame tests/seccomp.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2013 Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * This file is part of GnuTLS test suite.
Packit aea12f
 *
Packit aea12f
 * ocserv is free software: you can redistribute it and/or modify it
Packit aea12f
 * under the terms of the GNU General Public License as published by
Packit aea12f
 * the Free Software Foundation, either version 2 of the License, or
Packit aea12f
 * (at your option) any later version.
Packit aea12f
 *
Packit aea12f
 * ocserv is distributed in the hope that it will be useful, but
Packit aea12f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
 * General Public License for more details.
Packit aea12f
 *
Packit aea12f
 * You should have received a copy of the GNU General Public License
Packit aea12f
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Packit aea12f
 */
Packit aea12f
Packit aea12f
#include <config.h>
Packit aea12f
#include <stdio.h>
Packit aea12f
#include "utils.h"
Packit aea12f
Packit aea12f
#ifdef HAVE_LIBSECCOMP
Packit aea12f
Packit aea12f
#include <seccomp.h>
Packit aea12f
#include <errno.h>
Packit aea12f
#include <string.h>
Packit aea12f
#if defined(__linux__)
Packit aea12f
#  include <sys/syscall.h>
Packit aea12f
#endif
Packit aea12f
Packit aea12f
int disable_system_calls(void)
Packit aea12f
{
Packit aea12f
	int ret;
Packit aea12f
	scmp_filter_ctx ctx;
Packit aea12f
Packit aea12f
	/*ctx = seccomp_init(SCMP_ACT_ERRNO(EPERM));*/
Packit aea12f
	ctx = seccomp_init(SCMP_ACT_TRAP);
Packit aea12f
	if (ctx == NULL) {
Packit aea12f
		fprintf(stderr, "could not initialize seccomp");
Packit aea12f
		return -1;
Packit aea12f
	}
Packit aea12f
Packit aea12f
#define ADD_SYSCALL(name, ...) \
Packit aea12f
	ret = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(name), __VA_ARGS__); \
Packit aea12f
	/* libseccomp returns EDOM for pseudo-syscalls due to a bug */ \
Packit aea12f
	if (ret < 0 && ret != -EDOM) { \
Packit aea12f
		fprintf(stderr, "could not add " #name " to seccomp filter: %s", strerror(-ret)); \
Packit aea12f
		ret = -1; \
Packit aea12f
		goto fail; \
Packit aea12f
	}
Packit aea12f
Packit aea12f
	ADD_SYSCALL(nanosleep, 0);
Packit aea12f
	ADD_SYSCALL(time, 0);
Packit aea12f
	ADD_SYSCALL(getpid, 0);
Packit aea12f
	ADD_SYSCALL(gettimeofday, 0);
Packit aea12f
#if defined(HAVE_CLOCK_GETTIME)
Packit aea12f
	ADD_SYSCALL(clock_gettime, 0);
Packit aea12f
#endif
Packit aea12f
Packit aea12f
	ADD_SYSCALL(getrusage, 0);
Packit aea12f
Packit aea12f
	/* recv/send for the default pull/push functions. It is unknown
Packit aea12f
	 * which syscall is used by libc and varies from system to system
Packit aea12f
	 * so we enable all */
Packit aea12f
	ADD_SYSCALL(recvmsg, 0);
Packit aea12f
	ADD_SYSCALL(sendmsg, 0);
Packit aea12f
	ADD_SYSCALL(send, 0);
Packit aea12f
	ADD_SYSCALL(recv, 0);
Packit aea12f
	ADD_SYSCALL(sendto, 0);
Packit aea12f
	ADD_SYSCALL(recvfrom, 0);
Packit aea12f
Packit aea12f
	/* to read from /dev/urandom */
Packit aea12f
	ADD_SYSCALL(read, 0);
Packit aea12f
#ifdef SYS_getrandom
Packit aea12f
	ADD_SYSCALL(getrandom, 0);
Packit aea12f
#endif
Packit aea12f
Packit aea12f
	/* we use it in select */
Packit aea12f
	ADD_SYSCALL(sigprocmask, 0);
Packit aea12f
	ADD_SYSCALL(rt_sigprocmask, 0);
Packit aea12f
Packit aea12f
	/* used in to detect reading timeouts */
Packit aea12f
	ADD_SYSCALL(poll, 0);
Packit aea12f
Packit aea12f
	/* for memory allocation */
Packit aea12f
	ADD_SYSCALL(brk, 0);
Packit aea12f
Packit aea12f
	/* the following are for generic operations, not specific to
Packit aea12f
	 * gnutls. */
Packit aea12f
	ADD_SYSCALL(close, 0);
Packit aea12f
	ADD_SYSCALL(exit, 0);
Packit aea12f
	ADD_SYSCALL(exit_group, 0);
Packit aea12f
Packit aea12f
	/* allow returning from signal handlers */
Packit aea12f
	ADD_SYSCALL(sigreturn, 0);
Packit aea12f
	ADD_SYSCALL(rt_sigreturn, 0);
Packit aea12f
Packit aea12f
	ret = seccomp_load(ctx);
Packit aea12f
	if (ret < 0) {
Packit aea12f
		fprintf(stderr, "could not load seccomp filter");
Packit aea12f
		ret = -1;
Packit aea12f
		goto fail;
Packit aea12f
	}
Packit aea12f
	
Packit aea12f
	ret = 0;
Packit aea12f
Packit aea12f
fail:
Packit aea12f
	seccomp_release(ctx);
Packit aea12f
	return ret;
Packit aea12f
}
Packit aea12f
#else
Packit aea12f
int disable_system_calls(void)
Packit aea12f
{
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
#endif