Skip to content
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

Add further sanity checking of hdr->error_no #805

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions lib/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
sizeof(struct vfio_user_header) + \
sizeof(struct vfio_user_region_access))

/*
* Maximum value we are prepared to accept in hdr->error_no. Somewhat arbitrary
* value low enough to avoid any signed conversion issues.
*/
#define SERVER_MAX_ERROR_NO (4096)

/*
* Structure used to hold an in-flight request+reply.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/tran_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ tran_pipe_recv(int fd, struct vfio_user_header *hdr, bool is_reply,
}

if (hdr->flags & VFIO_USER_F_ERROR) {
if (hdr->error_no <= 0) {
if (hdr->error_no <= 0 || hdr->error_no > SERVER_MAX_ERROR_NO) {
hdr->error_no = EINVAL;
}
return ERROR_INT(hdr->error_no);
Expand Down
2 changes: 1 addition & 1 deletion lib/tran_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ tran_sock_recv_fds(int sock, struct vfio_user_header *hdr, bool is_reply,
}

if (hdr->flags & VFIO_USER_F_ERROR) {
if (hdr->error_no <= 0) {
if (hdr->error_no <= 0 || hdr->error_no > SERVER_MAX_ERROR_NO) {
hdr->error_no = EINVAL;
}
return ERROR_INT(hdr->error_no);
Expand Down
3 changes: 2 additions & 1 deletion samples/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,8 @@ get_dirty_bitmap(int sock, struct client_dma_region *dma_region,
uint64_t bitmap_size = get_bitmap_size(dma_region->map.size,
sysconf(_SC_PAGESIZE));

size_t size = sizeof(*res) + sizeof(*report) + bitmap_size;
/* Saturating add to keep coverity happy. */
size_t size = satadd_u64(sizeof(*res) + sizeof(*report), bitmap_size);

void *data = calloc(1, size);
assert(data != NULL);
Expand Down