Page MenuHomePhabricator

Protect against PHP code execution via memcached/unserialize
Open, MediumPublic

Description

The PHP memcached library uses PHP serialization. If an attacker succeeds in placing malicious data in memcached (which does not require authentication), that malicious data will be unserialized when read via the PHP memcached library. unserialize is unsafe for untrusted data. Among other things, it may trigger code execution via the __wakeup method.

https://www.owasp.org/index.php/PHP_Object_Injection

We should find a way to prevent the PHP memcached library from unserializing arbitrary data. Perhaps it can be patched to use JSON serialization instead.

This task was factored out of T161647: RFC: Deprecate using php serialization inside MediaWiki.

Event Timeline

• dpatrick added a subscriber: tstarling.

This is especially important with T152126 open.

Change 432042 had a related patch set uploaded (by Tim Starling; owner: Tim Starling):
[mediawiki/libs/ScopedCallback@master] Protect against insecure unserialization of ScopedCallback

https://gerrit.wikimedia.org/r/432042

Change 432042 merged by jenkins-bot:
[mediawiki/libs/ScopedCallback@master] Protect against insecure unserialization of ScopedCallback

https://gerrit.wikimedia.org/r/432042

Change 661962 had a related patch set uploaded (by Aaron Schulz; owner: Aaron Schulz):
[mediawiki/core@master] objectcache: make ObjectCache log values that are not JSON serializable

https://gerrit.wikimedia.org/r/661962

Krinkle moved this task from General to libs/objectcache on the MediaWiki-libs-BagOStuff board.
Krinkle added a project: Platform Engineering.

This should help with the realization of the T161647 plan by PET. I hope it helps / suit as you see fit.

Change 661962 merged by jenkins-bot:

[mediawiki/core@master] objectcache: make BagOStuff log values that are not JSON serializable

https://gerrit.wikimedia.org/r/661962

@daniel: Hi, all related patches in Gerrit have been merged. Can this task be resolved (via Add Action... → Change Status in the dropdown menu), or is there more to do in this task? Asking as you are set as task assignee. Thanks in advance!

@daniel: Removing task assignee as this open task has been assigned for more than two years - See the email sent to task assignee on Feburary 22nd, 2023.
Please assign this task to yourself again if you still realistically [plan to] work on this task - it would be welcome! :)
If this task has been resolved in the meantime, or should not be worked on by anybody ("declined"), please update its task status via "Add Action… 🡒 Change Status".
Also see https://www.mediawiki.org/wiki/Bug_management/Assignee_cleanup for tips how to best manage your individual work in Phabricator. Thanks!

There is a discussion in T407403: Error: Invalid serialization data for DatePeriod object (which raises a different concern about serialization: that it is not necessarily stable over different PHP versions) on how this could be implemented.

Extremely hacky idea: before calling unserialize() or native methods that unserialize (Memcached::get() & co in the case of MemcachedPeclBagOStuff), uninstall the normal autoloader, and install a custom autoloader or unserialize_callback_func callback which checks against the list of allowed classes, and invokes the normal autoloader chain only if the class is on the allowed list.

This would not help with classes that are native or have already been loaded for the current request, but would still reduce the attack surface significantly.

Note that setting memcached.serializer = json or similar doesn't help here. At a low level, the memcached server stores a (value, flags) tuple, and the PHP Memcached library uses the flags to store how the value was serialized. Configuration only affects writes, the flag retrieved from the server will determine how the data is unserialized. So an attacker with direct write access to the memcached server can always force PHP serialization. This doesn't seem fixable using MemcachedPeclBagOStuff (unless upstream does something about it).