-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix GH-15628: php_stream_memory_get_buffer() not zero-terminated #15648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
Sorry, fix is borked; back to draft. |
@nielsdos, this requires an additional check for interned strings, and the assumption that these are always zero-terminated. I don't like that hackery, but it might be okay. |
This should probably be fixed in the code where we modify the string, instead of the place where we get the buffer. |
This way there are no issues regarding interned strings.
Apparently, there are only two places where we would need to append a trailing NUL (less than I thought). And with this PR, the mailparse issues are gone. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps setting the NUL terminator can be moved to the place where the reallocation code is called, but this also works. LGTM
Supposedly slightly more efficient, but I find doing it after |
I would prefer to have zend_string API - maybe just some inline function that does memset sets '\0' at the end. I guess we might find more use case for it and seems cleaner to me. |
I'm not quite sure what you mean; just something like
I'm not against it, but I don't think we should introduce a new API in a stable branch (note that this targets PHP-8.2). |
memset(ZSTR_VAL(ms->data) + old_size, 0, newsize - old_size); | ||
ZSTR_VAL(ms->data)[ZSTR_LEN(ms->data)] = '\0'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be simplified
memset(ZSTR_VAL(ms->data) + old_size, 0, newsize - old_size); | |
ZSTR_VAL(ms->data)[ZSTR_LEN(ms->data)] = '\0'; | |
memset(ZSTR_VAL(ms->data) + old_size, 0, newsize - old_size + 1); // +1 to zero-terminate |
I was initially thinking about having wrappers around memset and memcpy but that doesn't really make much sense. The I guess it would be probably better to add in master though. I'm fine with this going as a bug fix in the meantime. |
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.