Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ext/pdo_odbc/odbc_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,9 @@ static int odbc_stmt_get_col(pdo_stmt_t *stmt, int colno, zval *result, enum pdo
}
ssize_t to_fetch_byte = to_fetch_len + 1;
char *buf2 = emalloc(to_fetch_byte);
zend_string *str = zend_string_init(C->data, to_fetch_byte, 0);
size_t used = to_fetch_len;
ssize_t seed_len = to_fetch_len > (LONG_COLUMN_BUFFER_SIZE - 1) ? (LONG_COLUMN_BUFFER_SIZE - 1) : to_fetch_len;
zend_string *str = zend_string_init(C->data, seed_len + 1, 0);
size_t used = seed_len;

do {
C->fetched_len = 0;
Expand Down
45 changes: 45 additions & 0 deletions ext/pdo_odbc/tests/gh22349.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
GH-22349 (Heap over-read fetching a long column past the internal buffer)
--EXTENSIONS--
pdo_odbc
--SKIPIF--
<?php
require 'ext/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require 'ext/pdo/tests/pdo_test.inc';
$db = PDOTest::test_factory('ext/pdo_odbc/tests/common.phpt');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);

$db->exec('DROP TABLE test_gh22349');
if (false === $db->exec('CREATE TABLE test_gh22349 (data text)')
&& false === $db->exec('CREATE TABLE test_gh22349 (data CLOB)')
&& false === $db->exec('CREATE TABLE test_gh22349 (data longtext)')) {
die("BORK: no large text column type available here: " . implode(", ", $db->errorInfo()) . "\n");
}

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// The driver fetches a long column into an internal buffer of roughly one
// memory page and reassembles the remainder. Exercise values that span and
// exceed that buffer so the seeded length must match the bytes present.
foreach ([4096, 8192, 65536] as $len) {
$db->exec('DELETE FROM test_gh22349');
$text = str_repeat('A', $len);
$db->exec("INSERT INTO test_gh22349 VALUES ('$text')");
$got = $db->query('SELECT data FROM test_gh22349')->fetchColumn();
printf("%d: %s\n", $len, ($got === $text) ? 'ok' : ('MISMATCH len=' . strlen($got)));
}
?>
--CLEAN--
<?php
require 'ext/pdo/tests/pdo_test.inc';
$db = PDOTest::test_factory('ext/pdo_odbc/tests/common.phpt');
$db->exec('DROP TABLE test_gh22349');
?>
--EXPECT--
4096: ok
8192: ok
65536: ok
7 changes: 3 additions & 4 deletions ext/standard/proc_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,6 @@ PHP_FUNCTION(proc_open)

if (newprocok == FALSE) {
DWORD dw = GetLastError();
close_all_descriptors(descriptors, ndesc);
char *msg = php_win32_error_to_msg(dw);
php_error_docref(NULL, E_WARNING, "CreateProcess failed: %s", msg);
php_win32_error_msg_free(msg);
Expand All @@ -1394,7 +1393,6 @@ PHP_FUNCTION(proc_open)

if (close_parentends_of_pipes(&factions, descriptors, ndesc) == FAILURE) {
posix_spawn_file_actions_destroy(&factions);
close_all_descriptors(descriptors, ndesc);
goto exit_fail;
}

Expand All @@ -1414,7 +1412,6 @@ PHP_FUNCTION(proc_open)
}
posix_spawn_file_actions_destroy(&factions);
if (r != 0) {
close_all_descriptors(descriptors, ndesc);
php_error_docref(NULL, E_WARNING, "posix_spawn() failed: %s", strerror(r));
goto exit_fail;
}
Expand Down Expand Up @@ -1456,7 +1453,6 @@ PHP_FUNCTION(proc_open)
_exit(127);
} else if (child < 0) {
/* Failed to fork() */
close_all_descriptors(descriptors, ndesc);
php_error_docref(NULL, E_WARNING, "Fork failed: %s", strerror(errno));
goto exit_fail;
}
Expand Down Expand Up @@ -1546,6 +1542,9 @@ PHP_FUNCTION(proc_open)
} else {
exit_fail:
_php_free_envp(env);
if (descriptors) {
close_all_descriptors(descriptors, ndesc);
}
RETVAL_FALSE;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
proc_open() does not leak file descriptors when descriptor setup fails mid-spec
--SKIPIF--
<?php
if (!function_exists("proc_open")) die("skip proc_open() unavailable");
if (!@is_dir("/proc/self/fd")) die("skip requires /proc/self/fd");
?>
--FILE--
<?php
$before = count(scandir("/proc/self/fd"));
for ($i = 0; $i < 100; $i++) {
// Index 0 opens a real pipe; index 1 is invalid, so setup fails after the
// pipe is already open. The aborted call must release the pipe fds.
@proc_open("true", [0 => ["pipe", "r"], 1 => ["bogus_type"]], $pipes);
}
$after = count(scandir("/proc/self/fd"));
var_dump($after <= $before + 2);
?>
--EXPECT--
bool(true)