Skip to content

Commit e583890

Browse files
committed
Fix socket_recvfrom overflow on buffer size.
when passing PHP_INT_MAX for the $length param we get this (with ubsan) `ext/sockets/sockets.c:1409:36: runtime error: signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long int'` close GH-16382
1 parent 549bcdb commit e583890

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ PHP NEWS
3636
. Fixed bug GH-16385 (Unexpected null returned by session_set_cookie_params).
3737
(nielsdos)
3838

39+
- Sockets:
40+
. Fixed bug with overflow socket_recvfrom $length argument. (David Carlier)
41+
3942
- SPL:
4043
. Fixed bug GH-16337 (Use-after-free in SplHeap). (nielsdos)
4144

ext/sockets/sockets.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,8 @@ PHP_FUNCTION(socket_recvfrom)
14021402

14031403
/* overflow check */
14041404
/* Shouldthrow ? */
1405-
if ((arg3 + 2) < 3) {
1405+
1406+
if (arg3 <= 0 || arg3 > ZEND_LONG_MAX - 1) {
14061407
RETURN_FALSE;
14071408
}
14081409

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
socket_recvfrom overflow on length argument
3+
--EXTENSIONS--
4+
sockets
5+
--SKIPIF--
6+
<?php
7+
if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {
8+
die('skip not valid for Windows.');
9+
}
10+
--FILE--
11+
<?php
12+
$s = socket_create(AF_UNIX, SOCK_DGRAM, 0);
13+
$buf = $end = "";
14+
var_dump(socket_recvfrom($s, $buf, PHP_INT_MAX, 0, $end));
15+
var_dump(socket_recvfrom($s, $buf, -1, 0, $end));
16+
?>
17+
--EXPECT--
18+
bool(false)
19+
bool(false)

0 commit comments

Comments
 (0)