Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion seccomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Copy Markdown

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 after die().

fclose(file); // Add this line to close the file on error
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Done leave commented out lines in production code;
  • The cast was fine, you should leave it;
  • Add a check for too much memory requested:
if (len_bytes > MAX_BPF_SIZE)
    die(...)

prog->filter = malloc(len_bytes);
if (prog->filter == NULL) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct for several reasons:

  • the next functions rely on this data being available
  • freeing will make for a dangling pointer
  • why bother with all the code above to just discard data just loaded

}

int seccomp(unsigned int operation, unsigned int flags, void *args) {
Expand Down