Blame tests/50-sim-hash_collision.c

Packit 56e23f
/**
Packit 56e23f
 * Seccomp Library test program
Packit 56e23f
 *
Packit 56e23f
 * Copyright (c) 2019 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 <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 = NULL;
Packit 56e23f
Packit 56e23f
	rc = util_getopt(argc, argv, &opts);
Packit 56e23f
	if (rc < 0)
Packit 56e23f
		goto out;
Packit 56e23f
Packit 56e23f
	rc = seccomp_api_set(1);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		return -rc;
Packit 56e23f
Packit 56e23f
	ctx = seccomp_init(SCMP_ACT_ERRNO(100));
Packit 56e23f
	if (ctx == NULL)
Packit 56e23f
		return ENOMEM;
Packit 56e23f
Packit 56e23f
	rc = seccomp_arch_remove(ctx, SCMP_ARCH_NATIVE);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out;
Packit 56e23f
	rc = seccomp_arch_add(ctx, SCMP_ARCH_X86_64);
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out;
Packit 56e23f
Packit 56e23f
	/* libseccomp utilizes a hash table to manage BPF blocks.  It
Packit 56e23f
	 * currently employs MurmurHash3 where the key is the hashed values
Packit 56e23f
	 * of the BPF instruction blocks, the accumulator start, and the
Packit 56e23f
	 * accumulator end.  Changes to the hash algorithm will likely affect
Packit 56e23f
	 * this test.
Packit 56e23f
	 */
Packit 56e23f
Packit 56e23f
	/* The following rules were derived from an issue reported by Tor:
Packit 56e23f
	 * https://github.com/seccomp/libseccomp/issues/148
Packit 56e23f
	 *
Packit 56e23f
	 * In the steps below, syscall 1001 is configured similarly to how
Packit 56e23f
	 * Tor configured socket.  The fairly complex rules below led to
Packit 56e23f
	 * a hash collision with rt_sigaction (syscall 1000) in this test.
Packit 56e23f
	 */
Packit 56e23f
Packit 56e23f
	rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, 1001, 3,
Packit 56e23f
			      SCMP_A0(SCMP_CMP_EQ, 1),
Packit 56e23f
			      SCMP_A1(SCMP_CMP_MASKED_EQ, 0xf, 2),
Packit 56e23f
			      SCMP_A2(SCMP_CMP_EQ, 3));
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out;
Packit 56e23f
	rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, 1001, 2,
Packit 56e23f
			      SCMP_A0(SCMP_CMP_EQ, 1),
Packit 56e23f
			      SCMP_A1(SCMP_CMP_MASKED_EQ, 0xf, 1));
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out;
Packit 56e23f
Packit 56e23f
Packit 56e23f
	rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, 1000, 1,
Packit 56e23f
			      SCMP_A0(SCMP_CMP_EQ, 2));
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out;
Packit 56e23f
	rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, 1000, 1,
Packit 56e23f
			      SCMP_A0(SCMP_CMP_EQ, 1));
Packit 56e23f
	if (rc != 0)
Packit 56e23f
		goto out;
Packit 56e23f
Packit 56e23f
	rc = util_filter_output(&opts, ctx);
Packit 56e23f
	if (rc)
Packit 56e23f
		goto out;
Packit 56e23f
Packit 56e23f
out:
Packit 56e23f
	seccomp_release(ctx);
Packit 56e23f
	return (rc < 0 ? -rc : rc);
Packit 56e23f
}