Skip to content

Commit

Permalink
Continue syscalls normally for no matches
Browse files Browse the repository at this point in the history
If no rule matches, there is no reason to proxy the syscall with
identical arguments: Just continue the syscall normally in the
supervised process.
  • Loading branch information
vimpostor committed Dec 10, 2024
1 parent e4c11e9 commit 851ef82
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src/bin/seccomp/seccomp_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ int handle_req(struct seccomp_notif *req,

int dirfd = -1, proxy_dirfd = -1;
char pathname[PATH_MAX];
char proxy_pathname[PATH_MAX];
const char *proxy_pathname = NULL;
int flags;
mode_t mode;
struct open_how how;
Expand All @@ -191,7 +191,7 @@ int handle_req(struct seccomp_notif *req,
}
if (!found) {
fprintf(stderr, "huh? trapped system call %d that does not appear on our list?\n", req->data.nr);
// allow target to continue the syscall normally
// continue the syscall normally
resp->flags |= SECCOMP_USER_NOTIF_FLAG_CONTINUE;
resp->error = 0;
resp->val = 0;
Expand Down Expand Up @@ -242,7 +242,17 @@ int handle_req(struct seccomp_notif *req,
goto out;
}
// Get the redirected file path
strcpy(proxy_pathname, find_match(pathname));
if (!find_match(&proxy_pathname, pathname)) {
// continue the syscall normally
resp->flags |= SECCOMP_USER_NOTIF_FLAG_CONTINUE;
resp->error = 0;
resp->val = 0;
if (ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, resp) < 0 && errno != ENOENT) {
ret = -1;
perror("ioctl send");
}
goto out;
}

if (nr == __NR_openat2) {
// read the special how struct
Expand Down
15 changes: 9 additions & 6 deletions src/lib/copycat.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,32 @@ void read_config() {
fclose(f);
}

const char *find_match(const char *source) {
// Returns true if a match was found
bool find_match(const char **match, const char *query) {
for (size_t i = 0; i < rules.size; ++i) {
size_t rulesrc_len = strlen(rules.table[i].source);
// check if we have a recursive rule (match only prefix) or if we have to check for a literal match
size_t chars_to_compare = rulesrc_len;
if (!rules.table[i].match_prefix) {
chars_to_compare = MAX(chars_to_compare, strlen(source));
chars_to_compare = MAX(chars_to_compare, strlen(query));
}

// does the rule match?
if (!strncmp(source, rules.table[i].source, chars_to_compare)) {
if (!strncmp(query, rules.table[i].source, chars_to_compare)) {
char *result = (char *) rules.table[i].dest;
if (rules.table[i].replace_prefix_only) {
// extend result with rest of the input source
// this means we have just replaced the prefix
strcpy(path_buffer, result);
result = path_buffer;
strcat(result, source + rulesrc_len);
strcat(result, query + rulesrc_len);
}
return result;
*match = result;
return true;
}
}
return source;
*match = query;
return false;
}


Expand Down
2 changes: 1 addition & 1 deletion src/lib/copycat.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void add_rule(char *source, char *destination);
void parse_rule(char *line);
void parse_rules(char *rls);
void read_config();
const char *find_match(const char *source);
bool find_match(const char **match, const char *query);

long original_openat2(int dirfd, const char *pathname, struct open_how *how, size_t size);

Expand Down

0 comments on commit 851ef82

Please sign in to comment.