-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Clean up seccomp tests #4945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Clean up seccomp tests #4945
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
69ee132
tests: move pylint config to pyproject.toml
pb8o deeb487
style: disable pylint too-few-public-methods
pb8o 9a492fa
tests: cleanup test_seccomp
pb8o 5104188
docs: reflect that seccompiler --basic filters are deprecated
pb8o 350e04c
tests: add test to validate seccomp filters
pb8o File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// This is used by `test_seccomp_validate.py` | ||
|
||
#include <linux/types.h> | ||
#include <linux/filter.h> | ||
#include <linux/seccomp.h> | ||
#include <sys/prctl.h> | ||
#include <sys/stat.h> | ||
|
||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <fcntl.h> | ||
|
||
|
||
void install_bpf_filter(char *bpf_file) { | ||
int fd = open(bpf_file, O_RDONLY); | ||
if (fd == -1) { | ||
perror("open"); | ||
exit(EXIT_FAILURE); | ||
} | ||
struct stat sb; | ||
if (fstat(fd, &sb) == -1) { | ||
perror("stat"); | ||
exit(EXIT_FAILURE); | ||
} | ||
size_t size = sb.st_size; | ||
size_t insn_len = size / sizeof(struct sock_filter); | ||
struct sock_filter *filterbuf = (struct sock_filter*)malloc(size); | ||
if (read(fd, filterbuf, size) == -1) { | ||
perror("read"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
/* Install seccomp filter */ | ||
struct sock_fprog prog = { | ||
.len = (unsigned short)(insn_len), | ||
.filter = filterbuf, | ||
}; | ||
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { | ||
perror("prctl(NO_NEW_PRIVS)"); | ||
exit(EXIT_FAILURE); | ||
} | ||
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { | ||
perror("prctl(SECCOMP)"); | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
|
||
|
||
int main(int argc, char **argv) { | ||
/* parse arguments */ | ||
if (argc < 3) { | ||
fprintf(stderr, "Usage: %s BPF_FILE ARG0..\n", argv[0]); | ||
exit(EXIT_FAILURE); | ||
} | ||
char *bpf_file = argv[1]; | ||
long syscall_id = atoi(argv[2]); | ||
long arg0, arg1, arg2, arg3; | ||
arg0 = arg1 = arg2 = arg3 = 0; | ||
if (argc > 3) arg0 = atoi(argv[3]); | ||
if (argc > 4) arg1 = atoi(argv[4]); | ||
if (argc > 5) arg2 = atoi(argv[5]); | ||
if (argc > 6) arg3 = atoi(argv[6]); | ||
|
||
/* read seccomp filter from file */ | ||
if (strcmp(bpf_file, "/dev/null") != 0) { | ||
install_bpf_filter(bpf_file); | ||
} | ||
|
||
long res = syscall(syscall_id, arg0, arg1, arg2, arg3); | ||
printf("%ld\n", res); | ||
return EXIT_SUCCESS; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Fixtures for security tests""" | ||
|
||
import json | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from host_tools.cargo_build import run_seccompiler_bin | ||
|
||
|
||
@pytest.fixture() | ||
def seccompiler(tmp_path): | ||
"A seccompiler helper fixture" | ||
|
||
class Seccompiler: | ||
"A seccompiler helper class" | ||
|
||
def compile(self, data: dict, basic=False) -> Path: | ||
"Use seccompiler-bin to compile a filter from a dict" | ||
inp = tmp_path / "input.json" | ||
inp.write_text(json.dumps(data)) | ||
bpf = tmp_path / "output.bpfmap" | ||
run_seccompiler_bin(bpf_path=bpf, json_path=inp, basic=basic) | ||
return bpf | ||
|
||
return Seccompiler() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.