Blame tests/47-live-kill_process.c

Packit 56e23f
/**
Packit 56e23f
 * Seccomp Library test program
Packit 56e23f
 *
Packit 56e23f
 * Copyright (c) 2018 Oracle and/or its affiliates.  All rights reserved.
Packit 56e23f
 * Author: Tom Hromatka <tom.hromatka@oracle.com>
Packit 56e23f
 */
Packit 56e23f
Packit 56e23f
/*
Packit 56e23f
 * This library is free software; you can redistribute it and/or modify it
Packit 56e23f
 * under the terms of version 2.1 of the GNU Lesser General Public License as
Packit 56e23f
 * published by the Free Software Foundation.
Packit 56e23f
 *
Packit 56e23f
 * This library is distributed in the hope that it will be useful, but WITHOUT
Packit 56e23f
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit 56e23f
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
Packit 56e23f
 * for more details.
Packit 56e23f
 *
Packit 56e23f
 * You should have received a copy of the GNU Lesser General Public License
Packit 56e23f
 * along with this library; if not, see <http://www.gnu.org/licenses>.
Packit 56e23f
 */
Packit 56e23f
Packit 56e23f
#include <errno.h>
Packit 56e23f
#include <fcntl.h>
Packit 56e23f
#include <pthread.h>
Packit 56e23f
#include <sys/types.h>
Packit 56e23f
#include <sys/stat.h>
Packit 56e23f
#include <unistd.h>
Packit 56e23f
Packit 56e23f
#include <seccomp.h>
Packit 56e23f
Packit 56e23f
#include "util.h"
Packit 56e23f
Packit 56e23f
Packit 56e23f
static const unsigned int whitelist[] = {
Packit 56e23f
	SCMP_SYS(clone),
Packit 56e23f
	SCMP_SYS(exit),
Packit 56e23f
	SCMP_SYS(exit_group),
Packit 56e23f
	SCMP_SYS(futex),
Packit 56e23f
	SCMP_SYS(madvise),
Packit 56e23f
	SCMP_SYS(mmap),
Packit 56e23f
	SCMP_SYS(mprotect),
Packit 56e23f
	SCMP_SYS(munmap),
Packit 56e23f
	SCMP_SYS(nanosleep),
Packit 56e23f
	SCMP_SYS(set_robust_list),
Packit 56e23f
};
Packit 56e23f
Packit 56e23f
/**
Packit 56e23f
 * Child thread created via pthread_create()
Packit 56e23f
 *
Packit 56e23f
 * This thread will call a disallowed syscall.  It should
Packit 56e23f
 * cause the entire program to die (and not just this
Packit 56e23f
 * thread.)
Packit 56e23f
 */
Packit 56e23f
void *child_start(void *param)
Packit 56e23f
{
Packit 56e23f
	int fd;
Packit 56e23f
Packit 56e23f
	/* make a disallowed syscall */
Packit 56e23f
	fd = open("/dev/null", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
Packit 56e23f
	/* we should never get here.  seccomp should kill the entire
Packit 56e23f
	 * process when open() is called. */
Packit 56e23f
	if (fd >= 0)
Packit 56e23f
		close(fd);
Packit 56e23f
Packit 56e23f
	return NULL;
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
int main(int argc, char *argv[])
Packit 56e23f
{
Packit 56e23f
	int rc, i;
Packit 56e23f
	scmp_filter_ctx ctx = NULL;
Packit 56e23f
	pthread_t child_thread;
Packit 56e23f
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_KILL_PROCESS);
Packit 56e23f
	if (ctx == NULL)
Packit 56e23f
		return ENOMEM;
Packit 56e23f
Packit 56e23f
	for (i = 0; i < sizeof(whitelist) / sizeof(whitelist[0]); i++) {
Packit 56e23f
		rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, whitelist[i], 0);
Packit 56e23f
		if (rc != 0)
Packit 56e23f
			goto out;
Packit 56e23f
	}
Packit 56e23f
Packit 56e23f
	rc = seccomp_load(ctx);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out;
Packit 56e23f
Packit 56e23f
	rc = pthread_create(&child_thread, NULL, child_start, NULL);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out;
Packit 56e23f
Packit 56e23f
	/* sleep for a bit to ensure that the child thread has time to run */
Packit 56e23f
	sleep(1);
Packit 56e23f
Packit 56e23f
	/* we should never get here! */
Packit 56e23f
	rc = -EACCES;
Packit 56e23f
	goto out;
Packit 56e23f
Packit 56e23f
out:
Packit 56e23f
	seccomp_release(ctx);
Packit 56e23f
	return (rc < 0 ? -rc : rc);
Packit 56e23f
}