Blame tests/17-sim-arch_merge.c

Packit Service 8eee21
/**
Packit Service 8eee21
 * Seccomp Library test program
Packit Service 8eee21
 *
Packit Service 8eee21
 * Copyright (c) 2012 Red Hat <pmoore@redhat.com>
Packit Service 8eee21
 * Author: Paul Moore <paul@paul-moore.com>
Packit Service 8eee21
 */
Packit Service 8eee21
Packit Service 8eee21
/*
Packit Service 8eee21
 * This library is free software; you can redistribute it and/or modify it
Packit Service 8eee21
 * under the terms of version 2.1 of the GNU Lesser General Public License as
Packit Service 8eee21
 * published by the Free Software Foundation.
Packit Service 8eee21
 *
Packit Service 8eee21
 * This library is distributed in the hope that it will be useful, but WITHOUT
Packit Service 8eee21
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit Service 8eee21
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
Packit Service 8eee21
 * for more details.
Packit Service 8eee21
 *
Packit Service 8eee21
 * You should have received a copy of the GNU Lesser General Public License
Packit Service 8eee21
 * along with this library; if not, see <http://www.gnu.org/licenses>.
Packit Service 8eee21
 */
Packit Service 8eee21
Packit Service 8eee21
#include <errno.h>
Packit Service 8eee21
#include <unistd.h>
Packit Service 8eee21
Packit Service 8eee21
#include <seccomp.h>
Packit Service 8eee21
Packit Service 8eee21
#include "util.h"
Packit Service 8eee21
Packit Service 8eee21
int main(int argc, char *argv[])
Packit Service 8eee21
{
Packit Service 8eee21
	int rc;
Packit Service 8eee21
	struct util_options opts;
Packit Service 8eee21
	scmp_filter_ctx ctx_64 = NULL, ctx_32 = NULL;
Packit Service 8eee21
Packit Service 8eee21
	rc = util_getopt(argc, argv, &opts);
Packit Service 8eee21
	if (rc < 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
Packit Service 8eee21
	ctx_32 = seccomp_init(SCMP_ACT_KILL);
Packit Service 8eee21
	if (ctx_32 == NULL) {
Packit Service 8eee21
		rc = -ENOMEM;
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
	}
Packit Service 8eee21
	ctx_64 = seccomp_init(SCMP_ACT_KILL);
Packit Service 8eee21
	if (ctx_64 == NULL) {
Packit Service 8eee21
		rc = -ENOMEM;
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
	}
Packit Service 8eee21
Packit Service 8eee21
	rc = seccomp_arch_remove(ctx_32, SCMP_ARCH_NATIVE);
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out;
Packit Service 8eee21
	rc = seccomp_arch_remove(ctx_64, SCMP_ARCH_NATIVE);
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out;
Packit Service 8eee21
Packit Service 8eee21
	rc = seccomp_arch_add(ctx_32, SCMP_ARCH_X86);
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
	rc = seccomp_arch_add(ctx_64, SCMP_ARCH_X86_64);
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
Packit Service 8eee21
	rc = seccomp_rule_add(ctx_32, SCMP_ACT_ALLOW, SCMP_SYS(read), 1,
Packit Service 8eee21
			      SCMP_A0(SCMP_CMP_EQ, STDIN_FILENO));
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
Packit Service 8eee21
	rc = seccomp_rule_add(ctx_32, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
Packit Service 8eee21
			      SCMP_A0(SCMP_CMP_EQ, STDOUT_FILENO));
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
Packit Service 8eee21
	rc = seccomp_rule_add(ctx_32, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
Packit Service 8eee21
			      SCMP_A0(SCMP_CMP_EQ, STDERR_FILENO));
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
Packit Service 8eee21
	rc = seccomp_rule_add(ctx_32, SCMP_ACT_ALLOW, SCMP_SYS(close), 0);
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
Packit Service 8eee21
	rc = seccomp_rule_add(ctx_64, SCMP_ACT_ALLOW, SCMP_SYS(socket), 0);
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
Packit Service 8eee21
	rc = seccomp_rule_add(ctx_64, SCMP_ACT_ALLOW, SCMP_SYS(connect), 0);
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
Packit Service 8eee21
	rc = seccomp_rule_add(ctx_64, SCMP_ACT_ALLOW, SCMP_SYS(shutdown), 0);
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
Packit Service 8eee21
	rc = seccomp_merge(ctx_64, ctx_32);
Packit Service 8eee21
	if (rc != 0)
Packit Service 8eee21
		goto out_all;
Packit Service 8eee21
Packit Service 8eee21
	/* NOTE: ctx_32 is no longer valid at this point */
Packit Service 8eee21
Packit Service 8eee21
	rc = util_filter_output(&opts, ctx_64);
Packit Service 8eee21
	if (rc)
Packit Service 8eee21
		goto out;
Packit Service 8eee21
Packit Service 8eee21
out:
Packit Service 8eee21
	seccomp_release(ctx_64);
Packit Service 8eee21
	return (rc < 0 ? -rc : rc);
Packit Service 8eee21
out_all:
Packit Service 8eee21
	seccomp_release(ctx_32);
Packit Service 8eee21
	goto out;
Packit Service 8eee21
}