Bug #78479 [NEW]: gethostbyname can incorrectly return 0.0.0.0

From: Date: Fri, 30 Aug 2019 21:17:20 +0000
Subject: Bug #78479 [NEW]: gethostbyname can incorrectly return 0.0.0.0
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222498@lists.php.net to get a copy of this message
From:             ryan at arctype dot co
Operating system: Ubuntu Linux
PHP version:      master-Git-2019-08-30 (Git)
Package:          *Network Functions
Bug Type:         Bug
Bug description:gethostbyname can incorrectly return 0.0.0.0

Description:
------------
Consider: 

<?php echo gethostbyname("www.google.com"); ?>
38.7.248.176

<?php echo gethostbyname("downloads.wordpress.org"); ?>
0.0.0.0

;; ANSWER SECTION:
www.google.com.		229	IN	A	216.58.193.196

;; ANSWER SECTION:
downloads.wordpress.org. 300	IN	A	198.143.164.250

The DNS on the host is running fine, DNS for downloads.wordpress.org is
ok. We are however running on a dual-stack ipv4/v6 host. 

www.google.com has an AAAA record, downloads.wordpress.org does not. 

I suspect that gethostbyname() is returning an AAAA record from the
system call to gethostbyname(3), which returns a struct hostent: 

           struct hostent {
               char  *h_name;            /* official name of host */
               char **h_aliases;         /* alias list */
               int    h_addrtype;        /* host address type */
               int    h_length;          /* length of address */
               char **h_addr_list;       /* list of addresses */
           }

While the PHP docs explicitly state that PHP's gethostbyname() returns
the IPv4 address, the implementation does not restrict or check if the
system call actually returns an AF_INET h_addrtype instead of AF_INET6,
always assuming a v4 and copying sizeof(in_addr):
https://github.com/php/php-src/blob/master/ext/standard/dns.c#L272


According to the Linux man pages, "The gethostbyname*(),
gethostbyaddr*(), herror(), and hstrerror() functions are obsolete. 
Applications should use getaddrinfo(3), getnameinfo(3), and
gai_strerror(3) instead."

"The  gethostbyaddr()  function  returns  a  structure  of  type 
hostent  for the given host address addr of length len and address type
type.  Valid address types are AF_INET and
       AF_INET6."

 
This problem has manifested elsewhere in this Wordpress bug report from
a dual-stack host: https://core.trac.wordpress.org/ticket/38291


-- 
Edit bug report at https://bugs.php.net/bug.php?id=78479&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=78479&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=78479&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=78479&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=78479&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=78479&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=78479&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=78479&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=78479&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=78479&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=78479&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78479&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=78479&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=78479&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=78479&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=78479&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=78479&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=78479&r=mysqlcfg



Thread (6 messages)

« previous php.bugs (#222498) next »