Skip to content

Commit

Permalink
Add checks for malformed frames
Browse files Browse the repository at this point in the history
  • Loading branch information
aacuevas committed Dec 20, 2023
1 parent 31523a8 commit 95d72b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions api/liboni/oni.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,10 @@ int oni_read_frame(const oni_ctx ctx, oni_frame_t **frame)
assert(iframe->private.f.data_sz > 0 && "Zero-sized frame");
assert(iframe->private.f.data_sz <= ctx->max_read_frame_size && "Invalid frame size");

if (iframe->private.f.data_sz == 0
|| iframe->private.f.data_sz > ctx->max_read_frame_size)
return ONI_EBADFRAME;

// Find read size (+ padding)
size_t rsize = iframe->private.f.data_sz;
rsize += rsize % sizeof(oni_fifo_dat_t);
Expand Down Expand Up @@ -890,6 +894,10 @@ const char *oni_error_str(int err)
return "Attempted to directly read or write a protected "
"configuration option";
}
case ONI_EBADFRAME:
{
return "Received malformed frame";
}
default:
return "Unknown error";
}
Expand Down Expand Up @@ -1202,6 +1210,8 @@ static int _oni_read_buffer(oni_ctx ctx, void **data, size_t size, int allow_ref
else
remaining = 0;

assert(remaining >= 0 && "buffer inversion");

// TODO: Is there a way to get rid of allow_refill?
// NB: Frames must reference a single buffer, so we must refill if less
// than max possible frame size on the first read within oni_read_frame().
Expand Down
3 changes: 2 additions & 1 deletion api/liboni/onidefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ enum {
ONI_ENOTWRITEDEV = -25, // Frame allocation attempted for a non-writable device
ONI_EDEVIDXREPEAT = -26, // Device table contains repeated device indices
ONI_EPROTCONFIG = -27, // Attempted to directly read or write a protected configuration option
ONI_EBADFRAME = -28, // Received malformed frame

// NB: Always at bottom
ONI_MINERRORNUM = -28
ONI_MINERRORNUM = -29
};

// Registers available in the specification
Expand Down

0 comments on commit 95d72b4

Please sign in to comment.