Skip to content
Open
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
24 changes: 24 additions & 0 deletions ext/dba/libflatfile/flatfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ int flatfile_delete(flatfile *dba, datum key_datum) {
}
num = atoi(buf);
if (num >= buf_size) {
if (num > SIZE_MAX - FLATFILE_BLOCK_SIZE) {
break;
}
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
Expand All @@ -135,6 +138,9 @@ int flatfile_delete(flatfile *dba, datum key_datum) {
}
num = atoi(buf);
if (num >= buf_size) {
if (num > SIZE_MAX - FLATFILE_BLOCK_SIZE) {
break;
}
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
Expand Down Expand Up @@ -162,6 +168,9 @@ int flatfile_findkey(flatfile *dba, datum key_datum) {
}
num = atoi(buf);
if (num >= buf_size) {
if (num > SIZE_MAX - FLATFILE_BLOCK_SIZE) {
break;
}
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
Expand All @@ -178,6 +187,9 @@ int flatfile_findkey(flatfile *dba, datum key_datum) {
}
num = atoi(buf);
if (num >= buf_size) {
if (num > SIZE_MAX - FLATFILE_BLOCK_SIZE) {
break;
}
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
Expand All @@ -202,6 +214,9 @@ datum flatfile_firstkey(flatfile *dba) {
}
num = atoi(buf);
if (num >= buf_size) {
if (num > SIZE_MAX - FLATFILE_BLOCK_SIZE) {
break;
}
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
Expand All @@ -218,6 +233,9 @@ datum flatfile_firstkey(flatfile *dba) {
}
num = atoi(buf);
if (num >= buf_size) {
if (num > SIZE_MAX - FLATFILE_BLOCK_SIZE) {
break;
}
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
Expand All @@ -244,6 +262,9 @@ datum flatfile_nextkey(flatfile *dba) {
}
num = atoi(buf);
if (num >= buf_size) {
if (num > SIZE_MAX - FLATFILE_BLOCK_SIZE) {
break;
}
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
Expand All @@ -254,6 +275,9 @@ datum flatfile_nextkey(flatfile *dba) {
}
num = atoi(buf);
if (num >= buf_size) {
if (num > SIZE_MAX - FLATFILE_BLOCK_SIZE) {
break;
}
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
}
Expand Down
31 changes: 31 additions & 0 deletions ext/dba/tests/dba_flatfile_oob.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
DBA FlatFile handler bounds with a malformed (negative) length field
--EXTENSIONS--
dba
--SKIPIF--
<?php
require_once __DIR__ . '/setup/setup_dba_tests.inc';
check_skip('flatfile');
?>
--FILE--
<?php
$db_file = __DIR__ . '/dba_flatfile_oob.db';
// A negative length narrows to a huge size_t and previously overran the read buffer.
file_put_contents($db_file, "-1\n" . str_repeat('A', 200000));

$db = dba_open($db_file, 'r', 'flatfile');
var_dump(dba_firstkey($db));
var_dump(dba_exists("AAAA", $db));
var_dump(dba_fetch("AAAA", $db));
dba_close($db);
echo "done\n";
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/dba_flatfile_oob.db');
?>
--EXPECT--
bool(false)
bool(false)
bool(false)
done
Loading