Skip to content

Commit 32be79d

Browse files
committed
Fix stream_select() issue with OpenSSL buffer
Ensure data from OpenSSL internal buffer has been transfered to PHP stream buffer before a select() emulation operation is performed Addresses bug #65137 https://bugs.php.net/bug.php?id=65137 Conflicts: ext/openssl/xp_ssl.c
1 parent 52f35b9 commit 32be79d

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

ext/openssl/xp_ssl.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,19 @@ static int php_openssl_sockop_cast(php_stream *stream, int castas, void **ret TS
871871

872872
case PHP_STREAM_AS_FD_FOR_SELECT:
873873
if (ret) {
874+
if (sslsock->ssl_active) {
875+
/* OpenSSL has an internal buffer which select() cannot see. If we don't
876+
fetch it into the stream's buffer, no activity will be reported on the
877+
stream even though there is data waiting to be read - but we only fetch
878+
the number of bytes OpenSSL has ready to give us since we weren't asked
879+
for any data at this stage. This is only likely to cause issues with
880+
non-blocking streams, but it's harmless to always do it. */
881+
int bytes;
882+
while ((bytes = SSL_pending(sslsock->ssl_handle)) > 0) {
883+
php_stream_fill_read_buffer(stream, (size_t)bytes);
884+
}
885+
}
886+
874887
*(int *)ret = sslsock->s.socket;
875888
}
876889
return SUCCESS;

main/php_streams.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t coun
301301
#define php_stream_write_string(stream, str) _php_stream_write(stream, str, strlen(str) TSRMLS_CC)
302302
#define php_stream_write(stream, buf, count) _php_stream_write(stream, (buf), (count) TSRMLS_CC)
303303

304+
PHPAPI void _php_stream_fill_read_buffer(php_stream *stream, size_t size TSRMLS_DC);
305+
#define php_stream_fill_read_buffer(stream, size) _php_stream_fill_read_buffer((stream), (size) TSRMLS_CC)
306+
304307
#ifdef ZTS
305308
PHPAPI size_t _php_stream_printf(php_stream *stream TSRMLS_DC, const char *fmt, ...) PHP_ATTRIBUTE_FORMAT(printf, 3, 4);
306309
#else

main/streams/streams.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remov
573573

574574
/* {{{ generic stream operations */
575575

576-
static void php_stream_fill_read_buffer(php_stream *stream, size_t size TSRMLS_DC)
576+
PHPAPI void _php_stream_fill_read_buffer(php_stream *stream, size_t size TSRMLS_DC)
577577
{
578578
/* allocate/fill the buffer */
579579

@@ -741,7 +741,7 @@ PHPAPI size_t _php_stream_read(php_stream *stream, char *buf, size_t size TSRMLS
741741
break;
742742
}
743743
} else {
744-
php_stream_fill_read_buffer(stream, size TSRMLS_CC);
744+
php_stream_fill_read_buffer(stream, size);
745745

746746
toread = stream->writepos - stream->readpos;
747747
if (toread > size) {
@@ -977,7 +977,7 @@ PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen,
977977
}
978978
}
979979

980-
php_stream_fill_read_buffer(stream, toread TSRMLS_CC);
980+
php_stream_fill_read_buffer(stream, toread);
981981

982982
if (stream->writepos - stream->readpos == 0) {
983983
break;
@@ -1052,7 +1052,7 @@ PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *re
10521052

10531053
to_read_now = MIN(maxlen - buffered_len, stream->chunk_size);
10541054

1055-
php_stream_fill_read_buffer(stream, buffered_len + to_read_now TSRMLS_CC);
1055+
php_stream_fill_read_buffer(stream, buffered_len + to_read_now);
10561056

10571057
just_read = STREAM_BUFFERED_AMOUNT(stream) - buffered_len;
10581058

0 commit comments

Comments
 (0)