Skip to content

Fix GH-16812: UAF on readline_info() after readline_write_history() c… #16813

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

Closed
wants to merge 5 commits into from
Closed
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
6 changes: 4 additions & 2 deletions ext/readline/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ PHP_FUNCTION(readline_info)
add_assoc_long(return_value,"attempted_completion_over",rl_attempted_completion_over);
} else {
if (zend_string_equals_literal_ci(what,"line_buffer")) {
oldstr = rl_line_buffer;
oldstr = strdup(rl_line_buffer ? rl_line_buffer : "");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strdup("") doesn't seem to be necessary (that's already handled by SAFE_STRING(). It's not wrong though, and performance is likely irrelevant here.

if (value) {
if (!try_convert_to_string(value)) {
RETURN_THROWS();
Expand All @@ -191,7 +191,8 @@ PHP_FUNCTION(readline_info)
rl_line_buffer = malloc(Z_STRLEN_P(value) + 1);
} else if (strlen(oldstr) < Z_STRLEN_P(value)) {
rl_extend_line_buffer(Z_STRLEN_P(value) + 1);
oldstr = rl_line_buffer;
free(oldstr);
oldstr = strdup(rl_line_buffer ? rl_line_buffer : "");
Comment on lines +194 to +195
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we touch this code path (libreadline only), we should also run the test against libreadline.

}
memcpy(rl_line_buffer, Z_STRVAL_P(value), Z_STRLEN_P(value) + 1);
#else
Expand All @@ -208,6 +209,7 @@ PHP_FUNCTION(readline_info)
#endif
}
RETVAL_STRING(SAFE_STRING(oldstr));
free(oldstr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, good idea!

} else if (zend_string_equals_literal_ci(what, "point")) {
RETVAL_LONG(rl_point);
#ifndef PHP_WIN32
Expand Down
15 changes: 15 additions & 0 deletions ext/readline/tests/gh16812.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
GH-16812 readline_info(): UAF
--EXTENSIONS--
readline
--SKIPIF--
<?php
if (getenv('SKIP_REPEAT')) die("skip readline has global state");
?>
--FILE--
<?php
readline_write_history(NULL);
var_dump(readline_info('line_buffer', 'test'));
?>
--EXPECT--
string(0) ""