Blame tests/11-basic-basic_errors.c

Packit 56e23f
/**
Packit 56e23f
 * Seccomp Library test program
Packit 56e23f
 *
Packit 56e23f
 * Copyright IBM Corp. 2012
Packit 56e23f
 * Author: Corey Bryant <coreyb@linux.vnet.ibm.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
int main(int argc, char *argv[])
Packit 56e23f
{
Packit 56e23f
	int rc;
Packit 56e23f
	scmp_filter_ctx ctx;
Packit 56e23f
	uint32_t attr;
Packit 56e23f
Packit 56e23f
	/* seccomp_init errors */
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_ALLOW + 1);
Packit 56e23f
	if (ctx != NULL)
Packit 56e23f
		return -1;
Packit 56e23f
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_ALLOW);
Packit 56e23f
	if (ctx == NULL)
Packit 56e23f
		return -1;
Packit 56e23f
	seccomp_release(ctx);
Packit 56e23f
	ctx = NULL;
Packit 56e23f
Packit Service 10c312
	/* ensure that seccomp_reset(NULL, ...) is accepted */
Packit Service 10c312
	rc = seccomp_reset(NULL, SCMP_ACT_ALLOW);
Packit Service 10c312
	if (rc != 0)
Packit 56e23f
		return -1;
Packit 56e23f
Packit 56e23f
	/* seccomp_load error */
Packit 56e23f
	rc = seccomp_load(ctx);
Packit 56e23f
	if (rc != -EINVAL)
Packit 56e23f
		return -1;
Packit 56e23f
Packit 56e23f
	/* seccomp_syscall_priority errors */
Packit 56e23f
	rc = seccomp_syscall_priority(ctx, SCMP_SYS(read), 1);
Packit 56e23f
	if (rc != -EINVAL)
Packit 56e23f
		return -1;
Packit 56e23f
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_ALLOW);
Packit 56e23f
	if (ctx == NULL)
Packit 56e23f
		return -1;
Packit 56e23f
	else {
Packit 56e23f
		rc = seccomp_syscall_priority(ctx, -10, 1);
Packit 56e23f
		if (rc != -EINVAL)
Packit 56e23f
			return -1;
Packit 56e23f
	}
Packit 56e23f
	seccomp_release(ctx);
Packit 56e23f
	ctx = NULL;
Packit 56e23f
Packit 56e23f
	/* seccomp_rule_add errors */
Packit 56e23f
	rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 1,
Packit 56e23f
			      SCMP_A0(SCMP_CMP_EQ, 0));
Packit 56e23f
	if (rc != -EINVAL)
Packit 56e23f
		return -1;
Packit 56e23f
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_ALLOW);
Packit 56e23f
	if (ctx == NULL)
Packit 56e23f
		return -1;
Packit 56e23f
	else {
Packit 56e23f
		rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
Packit Service 10c312
		if (rc != -EACCES)
Packit 56e23f
			return -1;
Packit 56e23f
		rc = seccomp_rule_add(ctx, SCMP_ACT_KILL - 1, SCMP_SYS(read), 0);
Packit 56e23f
		if (rc != -EINVAL)
Packit 56e23f
			return -1;
Packit 56e23f
		rc = seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(read), 7);
Packit 56e23f
		if (rc != -EINVAL)
Packit 56e23f
			return -1;
Packit 56e23f
		rc = seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(read), 7,
Packit 56e23f
				      SCMP_A0(SCMP_CMP_EQ, 0),
Packit 56e23f
				      SCMP_A1(SCMP_CMP_EQ, 0),
Packit 56e23f
				      SCMP_A2(SCMP_CMP_EQ, 0),
Packit 56e23f
				      SCMP_A3(SCMP_CMP_EQ, 0),
Packit 56e23f
				      SCMP_A4(SCMP_CMP_EQ, 0),
Packit 56e23f
				      SCMP_A5(SCMP_CMP_EQ, 0),
Packit 56e23f
				      SCMP_CMP(6, SCMP_CMP_EQ, 0));
Packit 56e23f
		if (rc != -EINVAL)
Packit 56e23f
			return -1;
Packit 56e23f
		rc = seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(read), 1,
Packit 56e23f
				      SCMP_A0(_SCMP_CMP_MIN, 0));
Packit 56e23f
		if (rc != -EINVAL)
Packit 56e23f
			return -1;
Packit 56e23f
		rc = seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(read), 1,
Packit 56e23f
				      SCMP_A0(_SCMP_CMP_MAX, 0));
Packit 56e23f
		if (rc != -EINVAL)
Packit 56e23f
			return -1;
Packit 56e23f
		rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, -10001, 0);
Packit 56e23f
		if (rc != -EDOM)
Packit 56e23f
			return -1;
Packit 56e23f
	}
Packit 56e23f
	seccomp_release(ctx);
Packit 56e23f
	ctx = NULL;
Packit 56e23f
Packit 56e23f
	/* seccomp_rule_add_exact error */
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_ALLOW);
Packit 56e23f
	if (ctx == NULL)
Packit 56e23f
		return -1;
Packit 56e23f
	rc = seccomp_arch_remove(ctx, SCMP_ARCH_NATIVE);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		return -1;
Packit 56e23f
	rc = seccomp_arch_add(ctx, SCMP_ARCH_X86);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		return -1;
Packit 56e23f
	rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1,
Packit 56e23f
				    SCMP_A0(SCMP_CMP_EQ, 2));
Packit 56e23f
	if (rc != -EINVAL)
Packit 56e23f
		return -1;
Packit 56e23f
	seccomp_release(ctx);
Packit 56e23f
	ctx = NULL;
Packit 56e23f
Packit 56e23f
	/* errno values beyond MAX_ERRNO */
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_ALLOW);
Packit 56e23f
	if (ctx == NULL)
Packit 56e23f
		return -1;
Packit 56e23f
	rc = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(0xffff), 0, 0);
Packit 56e23f
	if (rc != -EINVAL)
Packit 56e23f
		return -1;
Packit 56e23f
	seccomp_release(ctx);
Packit 56e23f
	ctx = NULL;
Packit 56e23f
Packit 56e23f
	/* seccomp_export_pfc errors */
Packit 56e23f
	rc = seccomp_export_pfc(ctx, STDOUT_FILENO);
Packit 56e23f
	if (rc != -EINVAL)
Packit 56e23f
		return -1;
Packit 56e23f
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_ALLOW);
Packit 56e23f
	if (ctx == NULL)
Packit 56e23f
		return -1;
Packit 56e23f
	else {
Packit 56e23f
		rc = seccomp_export_pfc(ctx, sysconf(_SC_OPEN_MAX) - 1);
Packit Service 10c312
		if (rc != -ECANCELED)
Packit 56e23f
			return -1;
Packit 56e23f
	}
Packit 56e23f
	seccomp_release(ctx);
Packit 56e23f
	ctx = NULL;
Packit 56e23f
Packit 56e23f
	/* seccomp_export_bpf errors */
Packit 56e23f
	rc = seccomp_export_bpf(ctx, STDOUT_FILENO);
Packit 56e23f
	if (rc != -EINVAL)
Packit 56e23f
		return -1;
Packit 56e23f
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_ALLOW);
Packit 56e23f
	if (ctx == NULL)
Packit 56e23f
		return -1;
Packit 56e23f
	else {
Packit 56e23f
		rc = seccomp_export_bpf(ctx, sysconf(_SC_OPEN_MAX) - 1);
Packit Service 10c312
		if (rc != -ECANCELED)
Packit 56e23f
			return -1;
Packit 56e23f
	}
Packit 56e23f
	seccomp_release(ctx);
Packit 56e23f
	ctx = NULL;
Packit 56e23f
Packit 56e23f
	/* seccomp_attr_* errors */
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_ALLOW);
Packit 56e23f
	if (ctx == NULL)
Packit 56e23f
		return -1;
Packit 56e23f
	rc = seccomp_attr_get(ctx, 1000, &attr);
Packit Service 10c312
	if (rc != -EINVAL)
Packit 56e23f
		return -1;
Packit 56e23f
	rc = seccomp_attr_set(ctx, 1000, 1);
Packit Service 10c312
	if (rc != -EINVAL)
Packit 56e23f
		return -1;
Packit 56e23f
Packit 56e23f
	return 0;
Packit 56e23f
}