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
60 changes: 28 additions & 32 deletions ext/dba/libflatfile/flatfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@

#define FLATFILE_BLOCK_SIZE 1024

/* Parse the length prefix in `buf` into `num` and grow `buf` to hold it.
* atoi() narrows a malformed (e.g. negative) length to a huge size_t whose
* `+ FLATFILE_BLOCK_SIZE` would overflow erealloc(); the macro yields true in
* that case so the caller stops reading and the read stays within `buf_size`. */
#define FLATFILE_GROW_BUF(num, buf, buf_size) ( \
(num) = atoi(buf), \
(num) >= (buf_size) && ( \
(num) > SIZE_MAX - FLATFILE_BLOCK_SIZE \
|| ((buf) = erealloc((buf), (buf_size) = (num) + FLATFILE_BLOCK_SIZE), 0) \
) \
)

/*
* ret = -1 means that database was opened for read-only
* ret = 0 success
Expand Down Expand Up @@ -110,10 +122,8 @@ int flatfile_delete(flatfile *dba, datum key_datum) {
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
if (FLATFILE_GROW_BUF(num, buf, buf_size)) {
break;
}
pos = php_stream_tell(dba->fp);

Expand All @@ -133,10 +143,8 @@ int flatfile_delete(flatfile *dba, datum key_datum) {
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
if (FLATFILE_GROW_BUF(num, buf, buf_size)) {
break;
}
/* read in the value */
num = php_stream_read(dba->fp, buf, num);
Expand All @@ -160,10 +168,8 @@ int flatfile_findkey(flatfile *dba, datum key_datum) {
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
if (FLATFILE_GROW_BUF(num, buf, buf_size)) {
break;
}
num = php_stream_read(dba->fp, buf, num);

Expand All @@ -176,10 +182,8 @@ int flatfile_findkey(flatfile *dba, datum key_datum) {
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
if (FLATFILE_GROW_BUF(num, buf, buf_size)) {
break;
}
num = php_stream_read(dba->fp, buf, num);
}
Expand All @@ -200,10 +204,8 @@ datum flatfile_firstkey(flatfile *dba) {
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
if (FLATFILE_GROW_BUF(num, buf, buf_size)) {
break;
}
num = php_stream_read(dba->fp, buf, num);

Expand All @@ -216,10 +218,8 @@ datum flatfile_firstkey(flatfile *dba) {
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
if (FLATFILE_GROW_BUF(num, buf, buf_size)) {
break;
}
num = php_stream_read(dba->fp, buf, num);
}
Expand All @@ -242,20 +242,16 @@ datum flatfile_nextkey(flatfile *dba) {
if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
if (FLATFILE_GROW_BUF(num, buf, buf_size)) {
break;
}
num = php_stream_read(dba->fp, buf, num);

if (!php_stream_gets(dba->fp, buf, 15)) {
break;
}
num = atoi(buf);
if (num >= buf_size) {
buf_size = num + FLATFILE_BLOCK_SIZE;
buf = erealloc(buf, buf_size);
if (FLATFILE_GROW_BUF(num, buf, buf_size)) {
break;
}
num = php_stream_read(dba->fp, buf, num);

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