Skip to content

Commit 337b23b

Browse files
author
Ilia Alshanetsky
committed
Fixed bug #21760 (Use of uninitialized pointer inside php_read()).
Fixed 3 possible crashes due to integer overflow or invalid user input inside the sockets extension.
1 parent ccef2cf commit 337b23b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ext/sockets/sockets.c

+16
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ static int php_read(int bsd_socket, void *buf, size_t maxlen, int flags)
266266

267267
set_errno(0);
268268

269+
*t = '\0';
269270
while (*t != '\n' && *t != '\r' && n < maxlen) {
270271
if (m > 0) {
271272
t++;
@@ -828,6 +829,11 @@ PHP_FUNCTION(socket_read)
828829
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &arg1, &length, &type) == FAILURE)
829830
return;
830831

832+
/* overflow check */
833+
if ((length + 1) < 2) {
834+
RETURN_FALSE;
835+
}
836+
831837
tmpbuf = emalloc(length + 1);
832838

833839
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
@@ -1225,6 +1231,11 @@ PHP_FUNCTION(socket_recv)
12251231

12261232
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &php_sock_res, -1, le_socket_name, le_socket);
12271233

1234+
/* overflow check */
1235+
if ((len + 1) < 2) {
1236+
RETURN_FALSE;
1237+
}
1238+
12281239
recv_buf = emalloc(len + 1);
12291240
memset(recv_buf, 0, len + 1);
12301241

@@ -1301,6 +1312,11 @@ PHP_FUNCTION(socket_recvfrom)
13011312

13021313
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
13031314

1315+
/* overflow check */
1316+
if ((arg3 + 2) < 3) {
1317+
RETURN_FALSE;
1318+
}
1319+
13041320
recv_buf = emalloc(arg3 + 2);
13051321
memset(recv_buf, 0, arg3 + 2);
13061322

0 commit comments

Comments
 (0)