-
Notifications
You must be signed in to change notification settings - Fork 7.9k
fix GH-13952 sqlite pdo::quote null bytes #13953
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,8 +224,27 @@ static zend_string* sqlite_handle_quoter(pdo_dbh_t *dbh, const zend_string *unqu | |
{ | ||
char *quoted; | ||
if (ZSTR_LEN(unquoted) > (INT_MAX - 3) / 2) { | ||
php_error_docref(NULL, E_WARNING, "String is too long to be quoted"); | ||
return NULL; | ||
} | ||
// check if it contains a null byte | ||
if (memchr(ZSTR_VAL(unquoted), '\0', ZSTR_LEN(unquoted))) { | ||
if(((ZSTR_LEN(unquoted) * 2) + 3) > (INT_MAX - 3) / 2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The overflow check is incorrect, leading to a potential integer overflow resulting in a buffer overflow. |
||
php_error_docref(NULL, E_WARNING, "Binary string is too long to be quoted"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment of warnings here. |
||
return NULL; | ||
} | ||
zend_string *quoted_str = zend_string_alloc((ZSTR_LEN(unquoted) * 2) + 4, 0); // 4: x'hex'\x00 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't open-code arithmetic in allocation functions, use safe_alloc variants that can deal with overflow. |
||
// x'hex' | ||
quoted_str->val[0] = 'x'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't open-code |
||
quoted_str->val[1] = '\''; | ||
for (size_t i = 0; i < ZSTR_LEN(unquoted); i++) { | ||
sprintf(quoted_str->val + 2 + (i * 2), "%02x", (unsigned char)ZSTR_VAL(unquoted)[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #13952 (comment): I agree, this seems like the wrong approach. |
||
} | ||
quoted_str->val[2 + (ZSTR_LEN(unquoted) * 2)] = '\''; | ||
quoted_str->val[3 + (ZSTR_LEN(unquoted) * 2)] = '\0'; | ||
quoted_str->len = (ZSTR_LEN(unquoted) * 2) + 3; | ||
return quoted_str; | ||
} | ||
quoted = safe_emalloc(2, ZSTR_LEN(unquoted), 3); | ||
/* TODO use %Q format? */ | ||
sqlite3_snprintf(2*ZSTR_LEN(unquoted) + 3, quoted, "'%q'", ZSTR_VAL(unquoted)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--TEST-- | ||
Bug #GH-13952 (sqlite PDO::quote silently corrupts strings with null bytes) | ||
--EXTENSIONS-- | ||
pdo | ||
pdo_sqlite | ||
--FILE-- | ||
<?php | ||
$pdo = new PDO("sqlite::memory:", null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); | ||
var_dump($pdo->query("SELECT " . $pdo->quote("foo\x00bar"))->fetch(PDO::FETCH_NUM)[0] === "foo\x00bar"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need more tests, and actually print maybe even what the quote output is. |
||
?> | ||
--EXPECT-- | ||
bool(true) |
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.
I don't think adding warnings is appropriate?
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.
I actually think a warning here is appropriate: when quote() fails, an error of some kind should be generated to explain why quote failed. Having no error generated is inappropriate IMO
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.
Right, I overlooked this was targeted to master