Skip to content

Commit db943f4

Browse files
committed
Detect EOF while reading in libssl
If we hit an EOF while reading in libssl then we will report an error back to the application (SSL_ERROR_SYSCALL) but errno will be 0. We add an error to the stack (which means we instead return SSL_ERROR_SSL) and therefore give a hint as to what went wrong. Contains a partial fix for #10880 Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org> Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com> (Merged from #10882)
1 parent b22a499 commit db943f4

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

crypto/bio/bss_sock.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ static int sock_read(BIO *b, char *out, int outl)
101101
if (ret <= 0) {
102102
if (BIO_sock_should_retry(ret))
103103
BIO_set_retry_read(b);
104+
else if (ret == 0)
105+
b->flags |= BIO_FLAGS_IN_EOF;
104106
}
105107
}
106108
return ret;
@@ -151,6 +153,9 @@ static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
151153
case BIO_CTRL_FLUSH:
152154
ret = 1;
153155
break;
156+
case BIO_CTRL_EOF:
157+
ret = (b->flags & BIO_FLAGS_IN_EOF) != 0 ? 1 : 0;
158+
break;
154159
default:
155160
ret = 0;
156161
break;

crypto/err/openssl.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,6 +2851,7 @@ SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES:242:unable to load ssl3 md5 routines
28512851
SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES:243:unable to load ssl3 sha1 routines
28522852
SSL_R_UNEXPECTED_CCS_MESSAGE:262:unexpected ccs message
28532853
SSL_R_UNEXPECTED_END_OF_EARLY_DATA:178:unexpected end of early data
2854+
SSL_R_UNEXPECTED_EOF_WHILE_READING:294:unexpected eof while reading
28542855
SSL_R_UNEXPECTED_MESSAGE:244:unexpected message
28552856
SSL_R_UNEXPECTED_RECORD:245:unexpected record
28562857
SSL_R_UNINITIALIZED:276:uninitialized

include/openssl/bio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ extern "C" {
169169
*/
170170
# define BIO_FLAGS_MEM_RDONLY 0x200
171171
# define BIO_FLAGS_NONCLEAR_RST 0x400
172+
# define BIO_FLAGS_IN_EOF 0x800
172173

173174
typedef union bio_addr_st BIO_ADDR;
174175
typedef struct bio_addrinfo_st BIO_ADDRINFO;

include/openssl/sslerr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Generated by util/mkerr.pl DO NOT EDIT
3-
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3+
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
44
*
55
* Licensed under the OpenSSL license (the "License"). You may not use
66
* this file except in compliance with the License. You can obtain a copy
@@ -733,6 +733,7 @@ int ERR_load_SSL_strings(void);
733733
# define SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES 243
734734
# define SSL_R_UNEXPECTED_CCS_MESSAGE 262
735735
# define SSL_R_UNEXPECTED_END_OF_EARLY_DATA 178
736+
# define SSL_R_UNEXPECTED_EOF_WHILE_READING 294
736737
# define SSL_R_UNEXPECTED_MESSAGE 244
737738
# define SSL_R_UNEXPECTED_RECORD 245
738739
# define SSL_R_UNINITIALIZED 276

ssl/record/rec_layer_s3.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ int ssl3_read_n(SSL *s, size_t n, size_t max, int extend, int clearold,
296296
ret = BIO_read(s->rbio, pkt + len + left, max - left);
297297
if (ret >= 0)
298298
bioread = ret;
299+
if (ret <= 0
300+
&& !BIO_should_retry(s->rbio)
301+
&& BIO_eof(s->rbio)) {
302+
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_READ_N,
303+
SSL_R_UNEXPECTED_EOF_WHILE_READING);
304+
}
299305
} else {
300306
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_N,
301307
SSL_R_READ_BIO_NOT_SET);

ssl/ssl_err.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Generated by util/mkerr.pl DO NOT EDIT
3-
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3+
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
44
*
55
* Licensed under the OpenSSL license (the "License"). You may not use
66
* this file except in compliance with the License. You can obtain a copy
@@ -1205,6 +1205,8 @@ static const ERR_STRING_DATA SSL_str_reasons[] = {
12051205
"unexpected ccs message"},
12061206
{ERR_PACK(ERR_LIB_SSL, 0, SSL_R_UNEXPECTED_END_OF_EARLY_DATA),
12071207
"unexpected end of early data"},
1208+
{ERR_PACK(ERR_LIB_SSL, 0, SSL_R_UNEXPECTED_EOF_WHILE_READING),
1209+
"unexpected eof while reading"},
12081210
{ERR_PACK(ERR_LIB_SSL, 0, SSL_R_UNEXPECTED_MESSAGE), "unexpected message"},
12091211
{ERR_PACK(ERR_LIB_SSL, 0, SSL_R_UNEXPECTED_RECORD), "unexpected record"},
12101212
{ERR_PACK(ERR_LIB_SSL, 0, SSL_R_UNINITIALIZED), "uninitialized"},

0 commit comments

Comments
 (0)