Potential NULL pointer dereference in php_dom_create_fake_namespace_decl_node_ptr()
Description
Hi, my static analyzer, Svace, has found a potential bug. Could you take a look and provide some feedback? (File - /ext/dom/php_dom.c)
The function php_dom_create_fake_namespace_decl_node_ptr() dereferences the return value of xmlNewNs() without checking whether it is NULL.
xmlNsPtr curns = xmlNewNs(NULL, original->href, NULL);
if (original->prefix) {
curns->prefix = xmlStrdup(original->prefix);
}
<?php
Since xmlNewNs() may return NULL on allocation failure, a non-NULL original->prefix can cause a NULL pointer dereference when accessing curns->prefix.
Impact
This may lead to a crash of the PHP process, potentially resulting in a denial of service (DoS) when processing DOM namespace nodes.
The affected function is called from dom_object_namespace_node_clone_obj() and php_dom_create_fake_namespace_decl(), making the issue relevant to namespace node cloning and creation.
Proposed Solution
Check the return value of xmlNewNs() before dereferencing curns and handle allocation failures appropriately. Additionally, ensure that callers correctly handle a failure to create the fake namespace declaration node.
PHP Version
PHP 8.3.31
Operating System
No response
Hi @Ti-Mis however the impact would be nowhere near DoS level, if we would "fix" it, it would be more hardening failed malloc allocations through libxml2 than actually fixing something.
Is there a possibility of such a problem arising? If you believe that it is safe or that it cannot happen, that answer would also be acceptable.
@devnexen Should we expect any comments? If you think this isn’t a problem, then let’s close the issue.