Potential NULL pointer dereference in php_dom_create_fake_namespace_decl_node_ptr()

Open Beginner friendly
#25,397 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
backend

Research direction

Start in ext/dom/php_dom.c and inspect php_dom_create_fake_namespace_decl_node_ptr(), then trace how dom_object_namespace_node_clone_obj() and php_dom_create_fake_namespace_decl() handle its result. Check the xmlNewNs() allocation failure path and run the relevant DOM tests. Done means the failure is handled without dereferencing a NULL pointer and the callers handle creation failure safely.

Written by the indexing model from the issue text.

Description

Bug Status: Needs Triage
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

Dominant language
C
Stars
41.8k
Forks
8.5k
Avg merge
2d 13h
Merged PRs (30d)
145

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from php/php-src

All issues in php/php-src

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.