-
Notifications
You must be signed in to change notification settings - Fork 15
add additional memory access validation in seccomp.c #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -47,17 +47,41 @@ FILE* sc_must_read_and_validate_header_from_file(const char *profile_path, struc | |
| void sc_must_read_filter_from_file(FILE *file, uint32_t len_bytes, struct sock_fprog *prog) | ||
| { | ||
| prog->len = len_bytes / sizeof(struct sock_filter); | ||
| prog->filter = (struct sock_filter *)malloc(MAX_BPF_SIZE); | ||
| // When reading syscall numbers or other data, ensure they are within valid ranges. This should be done in the loop where the file is being read: | ||
| if (prog->len > MAX_BPF_SIZE / sizeof(struct sock_filter)) { | ||
| die("seccomp filter too large"); | ||
| fclose(file); // Add this line to close the file on error | ||
|
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 function should not close the file by itself as it received it open already. This call will make an unexpected side effect. |
||
| return NULL; // Add this line to handle the error properly | ||
|
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 function is void, so this is usesless. |
||
| } | ||
|
|
||
| // Ensure any dynamically allocated memory is properly managed. For example, if you allocate memory for a buffer, make sure to free it: | ||
| // prog->filter = (struct sock_filter *)malloc(MAX_BPF_SIZE); | ||
|
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.
|
||
| prog->filter = malloc(len_bytes); | ||
| if (prog->filter == NULL) { | ||
|
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 next check is the same, remove it. |
||
| die("cannot allocate %u bytes of memory for seccomp filter", len_bytes); | ||
| fclose(file); // Add this line to close the file on error | ||
| return NULL; // Add this line to handle the error properly | ||
| } | ||
|
|
||
| if (prog->filter == NULL) { | ||
| die("cannot allocate %u bytes of memory for seccomp filter ", len_bytes); | ||
| } | ||
| // When reading data into buffers, ensure boundary checks are in place to prevent buffer overflows: | ||
| size_t num_read = fread(prog->filter, 1, len_bytes, file); | ||
| if (num_read != len_bytes) { | ||
| die("short read for filter %zu != %zu\n", num_read, len_bytes); | ||
| fclose(file); // Add this line to close the file on error | ||
| return NULL; // Add this line to handle the error properly | ||
| } | ||
|
|
||
| if (ferror(file)) { | ||
| die("cannot read filter"); | ||
| } | ||
| if (num_read != len_bytes) { | ||
|
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. Again, remove repeating check |
||
| die("short read for filter %zu != %i", num_read, len_bytes); | ||
| } | ||
|
|
||
| free(prog->filter); // Add this line to free the allocated memory | ||
|
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 not correct for several reasons:
|
||
| } | ||
|
|
||
| int seccomp(unsigned int operation, unsigned int flags, void *args) { | ||
|
|
||
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.
die()effectively closes the whole program, so it is no return. No use of calling stuff afterdie().