Blame tests/17-sim-arch_merge.c

Packit 56e23f
/**
Packit 56e23f
 * Seccomp Library test program
Packit 56e23f
 *
Packit 56e23f
 * Copyright (c) 2012 Red Hat <pmoore@redhat.com>
Packit 56e23f
 * Author: Paul Moore <paul@paul-moore.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 <unistd.h>
Packit 56e23f
Packit 56e23f
#include <seccomp.h>
Packit 56e23f
Packit 56e23f
#include "util.h"
Packit 56e23f
Packit 56e23f
int main(int argc, char *argv[])
Packit 56e23f
{
Packit 56e23f
	int rc;
Packit 56e23f
	struct util_options opts;
Packit 56e23f
	scmp_filter_ctx ctx_64 = NULL, ctx_32 = NULL;
Packit 56e23f
Packit 56e23f
	rc = util_getopt(argc, argv, &opts);
Packit 56e23f
	if (rc < 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
Packit 56e23f
	ctx_32 = seccomp_init(SCMP_ACT_KILL);
Packit 56e23f
	if (ctx_32 == NULL) {
Packit 56e23f
		rc = -ENOMEM;
Packit 56e23f
		goto out_all;
Packit 56e23f
	}
Packit 56e23f
	ctx_64 = seccomp_init(SCMP_ACT_KILL);
Packit 56e23f
	if (ctx_64 == NULL) {
Packit 56e23f
		rc = -ENOMEM;
Packit 56e23f
		goto out_all;
Packit 56e23f
	}
Packit 56e23f
Packit 56e23f
	rc = seccomp_arch_remove(ctx_32, SCMP_ARCH_NATIVE);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out;
Packit 56e23f
	rc = seccomp_arch_remove(ctx_64, SCMP_ARCH_NATIVE);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out;
Packit 56e23f
Packit 56e23f
	rc = seccomp_arch_add(ctx_32, SCMP_ARCH_X86);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
	rc = seccomp_arch_add(ctx_64, SCMP_ARCH_X86_64);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
Packit 56e23f
	rc = seccomp_rule_add(ctx_32, SCMP_ACT_ALLOW, SCMP_SYS(read), 1,
Packit 56e23f
			      SCMP_A0(SCMP_CMP_EQ, STDIN_FILENO));
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
Packit 56e23f
	rc = seccomp_rule_add(ctx_32, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
Packit 56e23f
			      SCMP_A0(SCMP_CMP_EQ, STDOUT_FILENO));
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
Packit 56e23f
	rc = seccomp_rule_add(ctx_32, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
Packit 56e23f
			      SCMP_A0(SCMP_CMP_EQ, STDERR_FILENO));
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
Packit 56e23f
	rc = seccomp_rule_add(ctx_32, SCMP_ACT_ALLOW, SCMP_SYS(close), 0);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
Packit 56e23f
	rc = seccomp_rule_add(ctx_64, SCMP_ACT_ALLOW, SCMP_SYS(socket), 0);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
Packit 56e23f
	rc = seccomp_rule_add(ctx_64, SCMP_ACT_ALLOW, SCMP_SYS(connect), 0);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
Packit 56e23f
	rc = seccomp_rule_add(ctx_64, SCMP_ACT_ALLOW, SCMP_SYS(shutdown), 0);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
Packit 56e23f
	rc = seccomp_merge(ctx_64, ctx_32);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out_all;
Packit 56e23f
Packit 56e23f
	/* NOTE: ctx_32 is no longer valid at this point */
Packit 56e23f
Packit 56e23f
	rc = util_filter_output(&opts, ctx_64);
Packit 56e23f
	if (rc)
Packit 56e23f
		goto out;
Packit 56e23f
Packit 56e23f
out:
Packit 56e23f
	seccomp_release(ctx_64);
Packit 56e23f
	return (rc < 0 ? -rc : rc);
Packit 56e23f
out_all:
Packit 56e23f
	seccomp_release(ctx_32);
Packit 56e23f
	goto out;
Packit 56e23f
}