|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2006-09-02 00:01 UTC] christian dot schuster at s2000 dot tu-chemnitz dot de
 Description: ------------ Using stream_socket_client() with a context containing a valid local IPv6 binding address does not actually bind the socket to that address, but fails silently. This is a "side effect" of a possible buffer overflow: In main/network.c, php_network_connect_socket_to_host() uses a "struct sockaddr", and references it via a pointer to "struct sockaddr_in" or "struct sockaddr_in6". For IPv4, this is usually sufficient - for IPv6 it is not. Upon the subsequent call to inet_pton(), some memory beyond the "struct sockaddr" is accessed. A "struct sockaddr_in" or "struct sockaddr_in6" should be used instead, depending on the protocol. PHP6 is affected by this bug, too. Proposed patch: http://www-user.tu-chemnitz.de/~chschu/patches/php-stream_socket_client-bind.patch Reproduce code: --------------- /* sample code for illegal use of struct sockaddr */ #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> int main(int, char**) { struct sockaddr local_address; struct sockaddr_in6 *in6 = (struct sockaddr_in6*)&local_address; inet_pton(AF_INET6, "::1", &in6->sin6_addr); } Expected result: ---------------- Normal program termination. Actual result: -------------- "Segmentation fault" on inet_pton(). PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 12:00:01 2025 UTC | 
<?php # create context containing binding address $context = stream_context_create(); stream_context_set_option($context, "socket", "bindto", "[your:local:ipv6:address::here]"); # connect to some server $handle = stream_socket_client("tcp://www.kame.net:80", $errno, $errstr, 5, STREAM_CLIENT_CONNECT, $context); # print local name echo stream_socket_get_name($handle, false); # close connection fclose($handle); ?>The output of the above script does not depend on whatever "your:local:ipv6:address::here" is replaced with. It should be something like "your:local:ipv6:address::here:port" - though I'd prefer "[your:local:ipv6:address::here]:port", but that's another point. Another thing I noticed: Appending a port number to the binding address ("[your:local:ipv6:address::here]:port") triggers a warning: Warning: stream_socket_client(): failed to bind to 'your:local:ipv6:address::here:port', system said: Invalid argument in test.php on line 11