Skip to content

Commit

Permalink
QUIC: Harden ring buffer against internal misuse
Browse files Browse the repository at this point in the history
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from openssl#21895)
  • Loading branch information
hlandau committed Aug 31, 2023
1 parent ecb6cdf commit 6042189
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions include/internal/ring_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# pragma once

# include <openssl/e_os2.h> /* For 'ossl_inline' */
# include "internal/safe_math.h"

/*
* ==================================================================
Expand Down Expand Up @@ -39,6 +40,10 @@ struct ring_buf {
uint64_t ctail_offset;
};

OSSL_SAFE_MATH_UNSIGNED(u64, uint64_t)

#define MAX_OFFSET (((uint64_t)1) << 62) /* QUIC-imposed limit */

static ossl_inline int ring_buf_init(struct ring_buf *r)
{
r->start = NULL;
Expand Down Expand Up @@ -74,11 +79,15 @@ static ossl_inline int ring_buf_write_at(struct ring_buf *r,
{
size_t avail, idx, l;
unsigned char *start = r->start;
int i;
int i, err = 0;

avail = ring_buf_avail(r);
if (logical_offset < r->ctail_offset
|| logical_offset + buf_len > r->head_offset + avail)
|| safe_add_u64(logical_offset, buf_len, &err)
> safe_add_u64(r->head_offset, avail, &err)
|| safe_add_u64(r->head_offset, buf_len, &err)
> MAX_OFFSET
|| err)
return 0;

for (i = 0; buf_len > 0 && i < 2; ++i) {
Expand Down Expand Up @@ -113,6 +122,9 @@ static ossl_inline size_t ring_buf_push(struct ring_buf *r,
if (buf_len > avail)
buf_len = avail;

if (buf_len > MAX_OFFSET - r->head_offset)
buf_len = (size_t)(MAX_OFFSET - r->head_offset);

if (buf_len == 0)
break;

Expand Down Expand Up @@ -190,7 +202,7 @@ static ossl_inline void ring_buf_cpop_range(struct ring_buf *r,
{
assert(end >= start);

if (start > r->ctail_offset)
if (start > r->ctail_offset || end >= MAX_OFFSET)
return;

if (cleanse && r->alloc > 0 && end > r->ctail_offset) {
Expand Down

0 comments on commit 6042189

Please sign in to comment.