Skip to content

Commit 93021c6

Browse files
committed
Fix GH-15628: php_stream_memory_get_buffer() not zero-terminated
We're reasonably sure that appending the NUL is not an OOB write, since the memory stream implementation uses `zend_string` APIs instead of fiddling with the buffer. We don't add a regression test because that would require to set up something in the zend_test extension, and regressions are supposed to be caught by external consumers of this API, such as mailparse. Closes GH-15648.
1 parent bf9929a commit 93021c6

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ PHP NEWS
2525
. Fixed bug GH-15432 (Heap corruption when querying a vector). (cmb,
2626
Kamil Tekiela)
2727

28+
- Streams:
29+
. Fixed bug GH-15628 (php_stream_memory_get_buffer() not zero-terminated).
30+
(cmb)
31+
2832
29 Aug 2024, PHP 8.2.23
2933

3034
- Core:

main/streams/memory.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static ssize_t php_stream_memory_write(php_stream *stream, const char *buf, size
6060
if (count) {
6161
ZEND_ASSERT(buf != NULL);
6262
memcpy(ZSTR_VAL(ms->data) + ms->fpos, (char*) buf, count);
63+
ZSTR_VAL(ms->data)[ZSTR_LEN(ms->data)] = '\0';
6364
ms->fpos += count;
6465
}
6566
return count;
@@ -240,6 +241,7 @@ static int php_stream_memory_set_option(php_stream *stream, int option, int valu
240241
size_t old_size = ZSTR_LEN(ms->data);
241242
ms->data = zend_string_realloc(ms->data, newsize, 0);
242243
memset(ZSTR_VAL(ms->data) + old_size, 0, newsize - old_size);
244+
ZSTR_VAL(ms->data)[ZSTR_LEN(ms->data)] = '\0';
243245
}
244246
return PHP_STREAM_OPTION_RETURN_OK;
245247
}

0 commit comments

Comments
 (0)