Skip to content

Commit e04ba88

Browse files
faramir-devt8m
authored andcommitted
s_serve: Report an error if init-connection fails without an attempt to read.
Fixes: openssl#18047. Reviewed-by: Paul Dale <[email protected]> Reviewed-by: Tomas Mraz <[email protected]> (Merged from openssl#18283)
1 parent 6ef91d8 commit e04ba88

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

apps/s_server.c

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,30 @@ static void print_stats(BIO *bio, SSL_CTX *ssl_ctx)
22362236
SSL_CTX_sess_get_cache_size(ssl_ctx));
22372237
}
22382238

2239+
static long int count_reads_callback(BIO *bio, int cmd, const char *argp,
2240+
int argi, long int argl, long int ret)
2241+
{
2242+
unsigned int *p_counter = (unsigned int *)BIO_get_callback_arg(bio);
2243+
2244+
switch (cmd) {
2245+
case BIO_CB_READ: /* No break here */
2246+
case BIO_CB_GETS:
2247+
if (p_counter != NULL)
2248+
++*p_counter;
2249+
break;
2250+
default:
2251+
break;
2252+
}
2253+
2254+
if (s_debug) {
2255+
BIO_set_callback_arg(bio, (char *)bio_s_out);
2256+
ret = bio_dump_callback(bio, cmd, argp, argi, argl, ret);
2257+
BIO_set_callback_arg(bio, (char *)p_counter);
2258+
}
2259+
2260+
return ret;
2261+
}
2262+
22392263
static int sv_body(int s, int stype, int prot, unsigned char *context)
22402264
{
22412265
char *buf = NULL;
@@ -2353,10 +2377,7 @@ static int sv_body(int s, int stype, int prot, unsigned char *context)
23532377
SSL_set_accept_state(con);
23542378
/* SSL_set_fd(con,s); */
23552379

2356-
if (s_debug) {
2357-
BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
2358-
BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
2359-
}
2380+
BIO_set_callback(SSL_get_rbio(con), count_reads_callback);
23602381
if (s_msg) {
23612382
#ifndef OPENSSL_NO_SSL_TRACE
23622383
if (s_msg == 2)
@@ -2648,7 +2669,25 @@ static int sv_body(int s, int stype, int prot, unsigned char *context)
26482669
*/
26492670
if ((!async || !SSL_waiting_for_async(con))
26502671
&& !SSL_is_init_finished(con)) {
2672+
/*
2673+
* Count number of reads during init_ssl_connection.
2674+
* It helps us to distinguish configuration errors from errors
2675+
* caused by a client.
2676+
*/
2677+
unsigned int read_counter = 0;
2678+
2679+
BIO_set_callback_arg(SSL_get_rbio(con), (char *)&read_counter);
26512680
i = init_ssl_connection(con);
2681+
BIO_set_callback_arg(SSL_get_rbio(con), NULL);
2682+
2683+
/*
2684+
* If initialization fails without reads, then
2685+
* there was a fatal error in configuration.
2686+
*/
2687+
if (i <= 0 && read_counter == 0) {
2688+
ret = -1;
2689+
goto err;
2690+
}
26522691

26532692
if (i < 0) {
26542693
ret = 0;

0 commit comments

Comments
 (0)