Skip to content

ipv6 filter integer overflow #18309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chongwick opened this issue Apr 11, 2025 · 0 comments
Closed

ipv6 filter integer overflow #18309

chongwick opened this issue Apr 11, 2025 · 0 comments

Comments

@chongwick
Copy link

Description

The following code:

<?php

const IPV6_COUNT = 100000;
const IPV6_LENGTH = 39;

function generateIPv6(): string {
    $hex = random_bytes(16);
    return '0'. str_replace('.', '', bin2hex($hex)). ':';
}

function validateIPv6($ip): bool {
    return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)!== false;
}

function testIPv6($ipv6_test): void {
    foreach ($ipv6_test as $ip => $exp) {
        $out = validateIPv6($ip);
        if ($exp!== $out) {
            echo "$ip failed (expected ". ($exp? 'true' : 'false'). ", got ". ($out? 'true' : 'false'). ")\n";
        }
    }
}

$ipv6_test = array_fill_keys(array_map('generateIPv6', range(1, IPV6_COUNT)), true);
testIPv6($ipv6_test);

?>

Resulted in this output:

/nightly_php/php-src/ext/filter/logical_filters.c:827:14: runtime error: signed integer overflow: 146455890 * 16 cannot be represented in type 'int'

PHP Version

nightly

Operating System

ubuntu 22.04

@nielsdos nielsdos self-assigned this Apr 11, 2025
@nielsdos nielsdos marked this as a duplicate of #18310 Apr 11, 2025
@nielsdos nielsdos changed the title integer overflow in logical_filters.c:827:14 ipv6 filter integer overflow Apr 11, 2025
nielsdos added a commit to nielsdos/php-src that referenced this issue Apr 11, 2025
The intermediate computation can cause a signed integer overflow, but
the input is correctly rejected later on by the check on variable `n`.
Solve this by using an unsigned number.
@nielsdos nielsdos linked a pull request Apr 11, 2025 that will close this issue
nielsdos added a commit that referenced this issue Apr 11, 2025
* PHP-8.3:
  Fix GH-18309: ipv6 filter integer overflow
  Fix GH-18304: Changing the properties of a DateInterval through dynamic properties triggers a SegFault
nielsdos added a commit that referenced this issue Apr 11, 2025
* PHP-8.4:
  Fix GH-18309: ipv6 filter integer overflow
  Fix GH-18304: Changing the properties of a DateInterval through dynamic properties triggers a SegFault
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants