Skip to content

Commit

Permalink
Correct the fixed size handling for dgram_pair and dgram_mem
Browse files Browse the repository at this point in the history
Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from #21866)
  • Loading branch information
t8m authored and hlandau committed Aug 30, 2023
1 parent bd3b026 commit b56b034
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 7 additions & 4 deletions crypto/bio/bss_dgram_pair.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ struct bio_dgram_pair_st {
unsigned int no_trunc : 1; /* Reads fail if they would truncate */
unsigned int local_addr_enable : 1; /* Can use BIO_MSG->local? */
unsigned int role : 1; /* Determines lock order */
unsigned int fixed_size : 1; /* Affects BIO_s_dgram_mem only */
unsigned int grows_on_write : 1; /* Set for BIO_s_dgram_mem only */
};

#define MIN_BUF_LEN (1024)
Expand Down Expand Up @@ -306,6 +306,8 @@ static int dgram_mem_init(BIO *bio)
return 0;
}

b->grows_on_write = 1;

bio->init = 1;
return 1;
}
Expand Down Expand Up @@ -469,7 +471,7 @@ static int dgram_pair_ctrl_set_write_buf_size(BIO *bio, size_t len)
}

b->req_buf_len = len;
b->fixed_size = 1;
b->grows_on_write = 0;
return 1;
}

Expand Down Expand Up @@ -1145,7 +1147,8 @@ static ossl_inline size_t compute_rbuf_growth(size_t target, size_t current)
}

/* Must hold local write lock */
static size_t dgram_pair_write_inner(struct bio_dgram_pair_st *b, const uint8_t *buf, size_t sz)
static size_t dgram_pair_write_inner(struct bio_dgram_pair_st *b,
const uint8_t *buf, size_t sz)
{
size_t total_written = 0;

Expand All @@ -1166,7 +1169,7 @@ static size_t dgram_pair_write_inner(struct bio_dgram_pair_st *b, const uint8_t
if (dst_len == 0) {
size_t new_len;

if (!b->fixed_size) /* resizeable only unless size not set explicitly */
if (!b->grows_on_write) /* resize only if size not set explicitly */
break;
/* increase the size */
new_len = compute_rbuf_growth(b->req_buf_len + sz, b->req_buf_len);
Expand Down
14 changes: 10 additions & 4 deletions test/bio_dgram_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ static int test_bio_dgram_pair(int idx)
} else {
if (!TEST_ptr(bio1 = bio2 = BIO_new(BIO_s_dgram_mem())))
goto err;
if (idx == 1 && !TEST_true(BIO_set_write_buf_size(bio1, 20 * 1024)))
goto err;
}

mtu1 = BIO_dgram_get_mtu(bio1);
Expand All @@ -535,7 +537,7 @@ static int test_bio_dgram_pair(int idx)
if (!TEST_int_le(mtu1, sizeof(scratch) - 4))
goto err;

for (i = 0; idx == 0 || i < 9; ++i) {
for (i = 0; total < 1 * 1024 * 1024; ++i) {
if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
goto err;

Expand All @@ -548,10 +550,14 @@ static int test_bio_dgram_pair(int idx)
goto err;

total += blen;
if (!TEST_size_t_lt(total, 1 * 1024 * 1024))
goto err;
}

if (idx <= 1 && !TEST_size_t_lt(total, 1 * 1024 * 1024))
goto err;

if (idx == 2 && !TEST_size_t_ge(total, 1 * 1024 * 1024))
goto err;

/*
* Should be able to fit at least 9 datagrams in default write buffer size
* in worst case
Expand Down Expand Up @@ -766,7 +772,7 @@ int setup_tests(void)
#if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
ADD_ALL_TESTS(test_bio_dgram, OSSL_NELEM(bio_dgram_cases));
# if !defined(OPENSSL_NO_CHACHA)
ADD_ALL_TESTS(test_bio_dgram_pair, 2);
ADD_ALL_TESTS(test_bio_dgram_pair, 3);
# endif
#endif

Expand Down

0 comments on commit b56b034

Please sign in to comment.