Blame tests/seccomp.c

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