-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
405 additions
and
307 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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.