Skip to content

[Bug] Potential buffer overflow in php_cli_server_startup_workers #8989

Closed
@yiyuaner

Description

@yiyuaner

Description

In the file sapi/cli/php_cli_server.c, the function php_cli_server_startup_workers has the following code:

void php_cli_server_startup_workers(void) {
    char *workers = getenv("PHP_CLI_SERVER_WORKERS");
    if (!workers) {
        return;
    }
    php_cli_server_workers_max = ZEND_ATOL(workers); 
    
    if (php_cli_server_workers_max > 1) { 
        php_cli_server_workers = calloc(
			php_cli_server_workers_max, sizeof(pid_t));
        ...
    }
}

The variable php_cli_server_workers_max is parsed from environment variable and thus is controlled. When setting php_cli_server_workers_max to a large value (e.g., INT64_MAX), the multiplication php_cli_server_workers_max * sizeof(pid_t) could wrap to a small value. A buffer smaller than expected will be allocated and this can lead to subsequent buffer overflow.

Notice that the C standard does not clearly states that calloc will check for multiplication overflow itself (see here). It will be better to also restrict the maximum value for php_cli_server_workers_max in the code.

PHP Version

github master

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