Skip to content

Uaf in SplObjectStorage::removeAllExcept() with custom getHash() #21831

@iluuu1994

Description

@iluuu1994

Description

Originally reported by @kdsjZh.

The following code:

<?php

class S extends SplObjectStorage {                                                                                   
    public $other;                                                                                                   
 
    public function getHash($obj): string {                                                                          
        if ($this->other) {                               
            $this->other->offsetUnset($obj);  // free element                                                        
            $this->other = null;                                                                                     
        }
        return "x";  // not in this storage -> enter if-block                                                        
    }                                                                                                                
}
                                                                                                                     
$storage = new SplObjectStorage();                        
$obj = new stdClass();
$storage->offsetSet($obj);

$filter = new S();                                                                                                   
$filter->other = $storage;
                                                                                                                     
$storage->removeAllExcept($filter);  // UAF here

PHP Version

-

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions