Blame tests/seccomp.c

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