Skip to content

BUG: enabled binary tree optimization with no rules results in default action being ignored #370

@kolyshkin

Description

@kolyshkin

Surely this is a corner case, and enabling binary tree optimization is obviously useless then there are no rules, but it still feels like a bug.

/*

This code demonstrates the following issue with libseccomp <= 2.5.3.

When the binary tree optimization is enabled (i.e. SCMP_FLTATR_CTL_OPTIMIZE
attribute value is set to 2), and no rules are added to the filter, the default
action seems to be ignored:

	$ gcc -lseccomp opt.c && ./a.out
	optimize 2
	load
	Bad system call (core dumped)

Disabling the binary tree optimization and/or adding a single rule
eliminates the issue:

	$ gcc -lseccomp -DOPT_LEVEL=1 opt.c && ./a.out
	optimize 1
	load
	release

	$ gcc -lseccomp -DADD_RULE opt.c && ./a.out
	optimize 2
	rule_add
	load
	release

*/

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <seccomp.h>

#ifndef OPT_LEVEL
#define OPT_LEVEL 2
#endif

int main(int argc, char *argv[])
{
	int rc = -1;
	scmp_filter_ctx ctx;

	ctx = seccomp_init(SCMP_ACT_ALLOW);
	if (ctx == NULL) {
		fprintf(stderr, "seccomp_init failed\n");
		goto out;
	}

	printf("optimize %d\n", OPT_LEVEL);
	rc = seccomp_attr_set(ctx, SCMP_FLTATR_CTL_OPTIMIZE, OPT_LEVEL);
	if (rc < 0) {
		fprintf(stderr, "seccomp_attr_set: %s\n", strerror(-rc));
		goto out;
	}

#ifdef ADD_RULE
	printf("rule_add\n");
	rc = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(ENOSYS), SCMP_SYS(sync), 0);
	if (rc < 0) {
		fprintf(stderr, "seccomp_rule_add: %s\n", strerror(-rc));
		goto out;
	}
#endif

	printf("load\n");
	rc = seccomp_load(ctx);
	if (rc < 0) {
		fprintf(stderr, "seccomp_load: %s\n", strerror(-rc));
		goto out;
	}

out:
	printf("release\n");
	seccomp_release(ctx);
	return -rc;
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions