Skip to content

Fix GH-13984: Buffer size is now checked before memcmp #13991

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

Conversation

SakiTakamachi
Copy link
Member

@SakiTakamachi SakiTakamachi commented Apr 17, 2024

fixes #13984

This was also reproduced in 8.2.
Can the push test detect overflow?

@SakiTakamachi SakiTakamachi marked this pull request as ready for review April 17, 2024 15:53
@SakiTakamachi SakiTakamachi changed the base branch from master to PHP-8.2 April 17, 2024 15:56
@@ -751,7 +751,7 @@ static char *make_filename_safe(const char *filename)
}
return estrdup(filename);
}
if (*filename && memcmp(filename, ":memory:", sizeof(":memory:"))) {
if (*filename && (sizeof(filename) != sizeof(":memory:") || memcmp(filename, ":memory:", sizeof(":memory:")) != 0)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

)))) was too hard to see, so I added != 0. The meaning remains the same.

Copy link
Member

Choose a reason for hiding this comment

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

This is wrong, sizeof(filename) is not the length, it is the size of the pointer.
I also think the != 0 addition is wrong for memcmp?

Copy link
Member

Choose a reason for hiding this comment

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

why bothering with memcmp anyway ? why not just any str*cmp ?

Copy link
Member Author

Choose a reason for hiding this comment

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

@nielsdos

This is wrong, sizeof(filename) is not the length, it is the size of the pointer.

Right. I entered sizeof with the intention of strlen...

I also think the != 0 addition is wrong for memcmp?

Since it becomes true when 1 and -1, it seems to be the same because it becomes true when != 0, but am I wrong?

@devnexen

why bothering with memcmp anyway ? why not just any str*cmp ?

I thought I had to convert it to zend_string, but there were some that could be used as char, so I fixed it.

Copy link
Member

Choose a reason for hiding this comment

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

Right, I misread the original condition. So !=0 is fine.

pdo_sqlite
--FILE--
<?php
$dbfile = 'z.db';
Copy link
Member Author

@SakiTakamachi SakiTakamachi Apr 17, 2024

Choose a reason for hiding this comment

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

I did this because specifying the absolute path would make the string longer and not be overloaded.

Copy link
Member

Choose a reason for hiding this comment

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

Hm right... This can clash with local files though, so perhaps it's better to use 13991 as filename?

?>
--CLEAN--
<?php
@unlink(getcwd() . '/z.db');
Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a better way?

Copy link
Member

Choose a reason for hiding this comment

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

We should be careful somehow that we don't destroy files or users running the tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed to skip if a file with the same name already exists.

@@ -751,7 +751,7 @@ static char *make_filename_safe(const char *filename)
}
return estrdup(filename);
}
if (*filename && memcmp(filename, ":memory:", sizeof(":memory:"))) {
if (*filename && (sizeof(filename) != sizeof(":memory:") || memcmp(filename, ":memory:", sizeof(":memory:")) != 0)) {
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong, sizeof(filename) is not the length, it is the size of the pointer.
I also think the != 0 addition is wrong for memcmp?

pdo_sqlite
--FILE--
<?php
$dbfile = 'z.db';
Copy link
Member

Choose a reason for hiding this comment

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

Hm right... This can clash with local files though, so perhaps it's better to use 13991 as filename?

?>
--CLEAN--
<?php
@unlink(getcwd() . '/z.db');
Copy link
Member

Choose a reason for hiding this comment

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

We should be careful somehow that we don't destroy files or users running the tests.

@nielsdos
Copy link
Member

Can the push test detect overflow?

No, it seems ASAN isn't ran on this branch on push.

@@ -751,7 +751,7 @@ static char *make_filename_safe(const char *filename)
}
return estrdup(filename);
}
if (*filename && memcmp(filename, ":memory:", sizeof(":memory:"))) {
if (*filename && zend_binary_strcmp(filename, strlen(filename)-1, ":memory:", strlen(":memory:")-1)) {
Copy link
Member

Choose a reason for hiding this comment

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

I know very little pdo sqlite, but I do not get why -1 for the 2 strlen ? Legit question :)

@@ -751,7 +751,7 @@ static char *make_filename_safe(const char *filename)
}
return estrdup(filename);
}
if (*filename && memcmp(filename, ":memory:", sizeof(":memory:"))) {
if (*filename && zend_binary_strcmp(filename, strlen(filename)-1, ":memory:", strlen(":memory:")-1)) {
Copy link
Member

Choose a reason for hiding this comment

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

Subtracting 1 from strlen is not necessary, the subtraction of 1 from sizeof is because sizeof also counts the NULL terminator but strlen does not.

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking about that same kind of confusion, just wanted to confirm.

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems like I'm a little too sleepy....Sorry for making me check again and again. (AM 1:43 JST)

Copy link
Member

Choose a reason for hiding this comment

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

it s ok Saki :) relax, as nielsdos mentioned, an "attacker" can t do anything really malicious here. no rush :)

@@ -751,7 +751,7 @@ static char *make_filename_safe(const char *filename)
}
return estrdup(filename);
}
if (*filename && memcmp(filename, ":memory:", sizeof(":memory:"))) {
if (*filename && zend_binary_strcmp(filename, strlen(filename), ":memory:", strlen(":memory:"))) {
Copy link
Member

Choose a reason for hiding this comment

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

I think I prefer just a regular call to strcmp, simpler and prevents computing the length too.
The binary strcmp doesn't rlly have an advantage when using strlen.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed it :)

Copy link
Member

@devnexen devnexen left a comment

Choose a reason for hiding this comment

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

LCTM, at least the code part :)

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

Tested locally, test correctly reproduces the issue and patch seems right.
Thanks Saki.

@SakiTakamachi
Copy link
Member Author

SakiTakamachi commented Apr 17, 2024

Thanks for the check. I'll merge it tomorrow!

SakiTakamachi added a commit that referenced this pull request Apr 17, 2024
* PHP-8.2:
  Fix GH-13984: Buffer size is now checked before memcmp (#13991)
SakiTakamachi added a commit that referenced this pull request Apr 17, 2024
* PHP-8.3:
  Fix GH-13984: Buffer size is now checked before memcmp (#13991)
@SakiTakamachi SakiTakamachi deleted the fix/gh-13984 branch June 13, 2024 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Heap Buffer Overflow in sqlite extension
3 participants