-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix #81992: SplFixedArray::setSize() causes use-after-free #11959
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--TEST-- | ||
Bug #81992 (SplFixedArray::setSize() causes use-after-free) | ||
--FILE-- | ||
<?php | ||
class InvalidDestructor { | ||
public function __destruct() { | ||
global $obj; | ||
var_dump($obj[0]); | ||
try { | ||
var_dump($obj[2]); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
try { | ||
var_dump($obj[4]); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
} | ||
} | ||
|
||
$obj = new SplFixedArray(5); | ||
$obj[0] = str_repeat("A", 10); | ||
$obj[2] = str_repeat('B', 10); | ||
$obj[3] = new InvalidDestructor(); | ||
$obj[4] = str_repeat('C', 10); | ||
$obj->setSize(2); | ||
?> | ||
--EXPECT-- | ||
string(10) "AAAAAAAAAA" | ||
Index invalid or out of range | ||
Index invalid or out of range |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--TEST-- | ||
Bug #81992 (SplFixedArray::setSize() causes use-after-free) - setSize variation | ||
--FILE-- | ||
<?php | ||
class InvalidDestructor { | ||
public function __construct( | ||
private int $desiredSize, | ||
private SplFixedArray $obj, | ||
) {} | ||
|
||
public function __destruct() { | ||
echo "In destructor\n"; | ||
$this->obj->setSize($this->desiredSize); | ||
echo "Destroyed, size is now still ", $this->obj->getSize(), "\n"; | ||
} | ||
} | ||
|
||
class DestructorLogger { | ||
public function __construct(private int $id) {} | ||
|
||
public function __destruct() { | ||
echo "Destroyed the logger with id ", $this->id, "\n"; | ||
} | ||
} | ||
|
||
function test(int $desiredSize) { | ||
$obj = new SplFixedArray(5); | ||
$obj[0] = str_repeat("A", 10); | ||
$obj[1] = new DestructorLogger(1); | ||
$obj[2] = str_repeat('B', 10); | ||
$obj[3] = new InvalidDestructor($desiredSize, $obj); | ||
$obj[4] = new DestructorLogger(4); | ||
$obj->setSize(2); | ||
echo "Size is now ", $obj->getSize(), "\n"; | ||
echo "Done\n"; | ||
} | ||
|
||
echo "--- Smaller size test ---\n"; | ||
test(1); | ||
echo "--- Equal size test ---\n"; | ||
test(2); | ||
echo "--- Larger size test ---\n"; | ||
test(10); | ||
?> | ||
--EXPECT-- | ||
--- Smaller size test --- | ||
In destructor | ||
Destroyed, size is now still 2 | ||
Destroyed the logger with id 4 | ||
Destroyed the logger with id 1 | ||
Size is now 1 | ||
Done | ||
--- Equal size test --- | ||
In destructor | ||
Destroyed, size is now still 2 | ||
Destroyed the logger with id 4 | ||
Size is now 2 | ||
Done | ||
Destroyed the logger with id 1 | ||
--- Larger size test --- | ||
In destructor | ||
Destroyed, size is now still 2 | ||
Destroyed the logger with id 4 | ||
Size is now 10 | ||
Done | ||
Destroyed the logger with id 1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Shouldn't this be set before
spl_fixedarray_dtor
to avoid the same issue on deallocation of the whole array? Edit: Ah, we're usingsize
in there. Thinking about it, we might even do other shenanigans like modify the array in the destructor of released objects...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'll have a closer look, I'm currently fixing the case of calling resize within resize, still causing uaf :/ I think I have a solution for that though.
I'll have a deeper look at your comment after that.