-
Notifications
You must be signed in to change notification settings - Fork 257
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
Initial implementation of landlock calls #903
base: master
Are you sure you want to change the base?
Changes from all commits
3c7abc1
156eaa3
e91eccb
2e83f68
7e3c513
4687eea
b366e00
244c4a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#include "helper.h" | ||
#include "landlock_helpers.h" | ||
#include "ptbox.h" | ||
|
||
#include <dirent.h> | ||
|
@@ -90,13 +91,62 @@ int cptbox_child_run(const struct child_config *config) { | |
kill(getpid(), SIGSTOP); | ||
|
||
#if !PTBOX_FREEBSD | ||
// landlock setup | ||
int ruleset_fd, rc; | ||
struct landlock_ruleset_attr ruleset_attr = { | ||
.handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE | LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_READ_FILE | | ||
LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_REMOVE_DIR | | ||
LANDLOCK_ACCESS_FS_REMOVE_FILE | LANDLOCK_ACCESS_FS_MAKE_REG | | ||
LANDLOCK_ACCESS_FS_MAKE_SYM | LANDLOCK_ACCESS_FS_MAKE_DIR | LANDLOCK_ACCESS_FS_REFER, | ||
}; | ||
|
||
int landlock_version = get_landlock_version(); | ||
if (landlock_version < 2) { | ||
// ABI not old enough. Skip to seccomp | ||
goto seccomp_setup; | ||
} | ||
|
||
ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); | ||
if (ruleset_fd < 0) { | ||
perror("Failed to create a ruleset"); | ||
return PTBOX_SPAWN_FAIL_LANDLOCK; | ||
} | ||
|
||
// Note: WRITE must imply READ. This is required because even if one rule allows writing and another allows reading, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
// unless there is a rule that allows both, a call with O_RDWR will fail. | ||
if (landlock_add_rules(ruleset_fd, config->landlock_read_exact_files, | ||
LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_EXECUTE) || | ||
landlock_add_rules(ruleset_fd, config->landlock_read_exact_dirs, LANDLOCK_ACCESS_FS_READ_DIR) || | ||
landlock_add_rules(ruleset_fd, config->landlock_read_recursive_dirs, | ||
LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_EXECUTE | LANDLOCK_ACCESS_FS_READ_DIR) || | ||
landlock_add_rules(ruleset_fd, config->landlock_write_exact_files, | ||
LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_EXECUTE | LANDLOCK_ACCESS_FS_WRITE_FILE) || | ||
landlock_add_rules(ruleset_fd, config->landlock_write_exact_dirs, LANDLOCK_ACCESS_FS_READ_DIR) || | ||
landlock_add_rules(ruleset_fd, config->landlock_write_recursive_dirs, | ||
LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_EXECUTE | LANDLOCK_ACCESS_FS_READ_DIR | | ||
LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_REMOVE_DIR | | ||
LANDLOCK_ACCESS_FS_REMOVE_FILE | LANDLOCK_ACCESS_FS_READ_DIR | | ||
LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_MAKE_SYM | LANDLOCK_ACCESS_FS_MAKE_DIR | | ||
LANDLOCK_ACCESS_FS_REFER)) { | ||
// landlock_add_rules logs errors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this comment have more details? I don't understand what it's referring to, is this comment unrelated to the following |
||
close(ruleset_fd); | ||
return PTBOX_SPAWN_FAIL_LANDLOCK; | ||
} | ||
|
||
rc = landlock_restrict_self(ruleset_fd, 0); | ||
close(ruleset_fd); | ||
if (rc) { | ||
perror("Failed to enforce ruleset"); | ||
return PTBOX_SPAWN_FAIL_LANDLOCK; | ||
} | ||
|
||
seccomp_setup: | ||
scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_TRACE(0)); | ||
if (!ctx) { | ||
fprintf(stderr, "Failed to initialize seccomp context!"); | ||
goto seccomp_init_fail; | ||
} | ||
|
||
int rc; | ||
// By default, the native architecture is added to the filter already, so we add all the non-native ones. | ||
// This will bloat the filter due to additional architectures, but a few extra compares in the BPF matters | ||
// very little when syscalls are rare and other overhead is expensive. | ||
|
@@ -175,6 +225,26 @@ int cptbox_child_run(const struct child_config *config) { | |
#endif | ||
} | ||
|
||
int get_landlock_version() { | ||
#if !PTBOX_FREEBSD | ||
char *sandbox_mode = getenv("DMOJ_SANDBOX_MODE"); | ||
if (sandbox_mode != nullptr && strcmp(sandbox_mode, "ptrace+seccomp") == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a surprising place for this check to live, and I think the check should be stricter than this. We should validate that the string is one of:
and otherwise bail out. Otherwise we can't confidently make changes to the interpretation of this environment variable, as we'd have been too lax in its inputs in earlier versions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that this location is strange. Where should it live? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think living in this file is probably fine, and it should return an enum. Then it should be consulted in the spawn routine alongside the Landlock version we have available. My concern here is more about subtly overloading the semantics of We should bail if someone requests:
This sanity check could exist in |
||
// Allow disabling of landlock. | ||
return 0; | ||
} | ||
|
||
int landlock_version = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION); | ||
if (landlock_version >= 0) { | ||
return landlock_version; | ||
} | ||
if (errno == ENOSYS || errno == EOPNOTSUPP) | ||
return 0; | ||
return -1; | ||
#else | ||
return 0; // FreeBSD does not have landlock | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How? Consumers of this function throw a python error, but what would be appropriate to do here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. I think |
||
#endif | ||
} | ||
|
||
// From python's _posixsubprocess | ||
static int pos_int_from_ascii(char *name) { | ||
int num = 0; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
#define PTBOX_SPAWN_FAIL_TRACEME 204 | ||
#define PTBOX_SPAWN_FAIL_EXECVE 205 | ||
#define PTBOX_SPAWN_FAIL_SETAFFINITY 206 | ||
#define PTBOX_SPAWN_FAIL_LANDLOCK 207 | ||
|
||
struct child_config { | ||
unsigned long memory; | ||
|
@@ -27,8 +28,16 @@ struct child_config { | |
int *seccomp_handlers; | ||
// 64 cores ought to be enough for anyone. | ||
unsigned long cpu_affinity_mask; | ||
const char **landlock_read_exact_files; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may end up looking a little cleaner if we have a struct {
const char **read_exact_files;
...
} landlock; or even const struct {
char **read_exact_files;
...
} landlock; |
||
const char **landlock_read_exact_dirs; | ||
const char **landlock_read_recursive_dirs; | ||
const char **landlock_write_exact_files; | ||
const char **landlock_write_exact_dirs; | ||
const char **landlock_write_recursive_dirs; | ||
}; | ||
|
||
int get_landlock_version(); | ||
|
||
void cptbox_closefrom(int lowfd); | ||
int cptbox_child_run(const struct child_config *config); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.