Bug #45584 Host name cache does not work as a cache
Submitted: 18 Jun 2009 7:53 Modified: 7 Mar 2010 2:15
Reporter: Alexander Nozdrin Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: General Severity:S3 (Non-critical)
Version:5.4 OS:Any
Assigned to: Alexander Nozdrin CPU Architecture:Any

[18 Jun 2009 7:53] Alexander Nozdrin
Description:
First of all, this problem has been described among
the comments in Bug#38247. Separating it as a separate
bug for easier tracking.

The host name cache in the server was introduced
for two reasons:

  - caching client host names by IP addresses
    (saving from extra DNS lookups);

  - counting client connection errors;

Looking up the hostname cache by IP address was broken in 6.0, for
different reasons.

First, the key used in the cache, for IPv4 alone, should be only the
client IP address (in_addr), not a struct sockaddr_storage, which
contains:
  - the peer port number
  - the client IPv4 address.

Having the port number part of the key defeated the purpose of the
hostname cache.

Secondly, with IPv4 and IPv6 coexisting in the network route and in
the network stack in the server (for dual stacks), the same client can
connect to the same server but have an apparent IP address that
varies.

Variations can be:
  - the client is seen as a pure IPv4 client
  - the client is seen as an IPv6 client, using IPv4 compat addresses
  - the client is seen as an IPv6 client, using IPv4 mapped addresses

With a non unique 'apparent' IP address, looking up the hostname cache
fails.

Third, implementing a member as :
  char ip[sizeof(struct sockaddr_storage)];
instead of:
  struct sockaddr_storage ip;
has the effect of removing the alignment of struct sockaddr_storage,
and is a bad practice that can cause bugs.

How to repeat:
Connect from different clients on the same host and see that
a new entry will be created each time.

Suggested fix:
- Always use IPv6 address as a key lookup in the hostname cache;

- Use "normalized" keys: the key is normalized to that IPv4,
  IPv6 compat IPv4, IPv6 mapped IPv4 and IPv6 addresses are all
  mapped to the same space (IPv6) and to a unique representation
  (IPv6 mapped IPv4 for IPv4).

- Use (struct in6_addr) for storing the keys.
[5 Aug 2009 17:50] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/80200

2844 Alexander Nozdrin	2009-08-05
      Fix for Bug#45584 (Host name cache does not work as a cache).
      
      The problem is described in the bug report.
      
      The solution is the following:
      
        - Make hostname cache key type of (char *);
      
        - Use string representation of normalized IPv6 addresses
          as hostname cache keys when IPv6 is supported.
      
          Use string representation of normalized IPv4 addresses
          as hostname cache keys when IPv6 is not supported.
      
        - Use only the host part of client address for hostname
          cache keys;
      
        - Actually resolve IP addresses to hostnames,
          not to IP strings;
      
        - Minimal supported Windows version has been changed to Windows XP.
          We don't support Windows 2000 for the newer versions any more.
      
          Windows XP has IPv6 support, so declaring it minimal supported
          version removes much of "windows portability hassle".
[7 Aug 2009 19:53] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/80395

2848 Alexander Nozdrin	2009-08-07
      Postfix for Bug#45584.
      
      The problem is that the host name cache key must be
      of char[HOST_ENTRY_KEY_SIZE].
[10 Aug 2009 4:18] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/80416

2850 Alexander Nozdrin	2009-08-10
      Postfix for Bug#45584:
      
        - Initialize vio->remote and vio->addLen in vio_peer_addr()
          even when vio->localhost is set.
      
        - Postpone ip_key construction in ip_to_hostname().
[10 Aug 2009 12:32] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/80441

2852 Alexander Nozdrin	2009-08-10
      Another postfix for Bug#45584: convert value to host-byte-order before
      comparing with the INADDR_LOOPBACK constant.
[24 Aug 2009 13:53] Bugs System
Pushed into 5.4.4-alpha (revid:alik@sun.com-20090824135126-2rngffvth14a8bpj) (version source revid:vvaintroub@mysql.com-20090810221320-rxaj1gc1kbrbr6up) (merge vers: 5.4.4-alpha) (pib:11)
[28 Aug 2009 12:27] Jon Stephens
Documented bugfix in the 5.4.4 and NDB-7.0.7 changelogs as follows:

      The hostname cache failed to work correctly.
[9 Oct 2009 23:21] Paul DuBois
Noted in 6.0.14 changelog.
[3 Dec 2009 9:09] Bugs System
Pushed into 5.6.0-beta (revid:alik@sun.com-20091202160026-699xnqx8c6a6t2lk) (version source revid:alik@sun.com-20091125105323-j82ts9ns936i45vv) (merge vers: 5.6.0-beta) (pib:13)
[3 Dec 2009 9:10] Bugs System
Pushed into 6.0.14-alpha (revid:alik@sun.com-20091202161413-u81nw1foglcu7nao) (version source revid:alik@sun.com-20091125105553-vvedtv1bpmugb1nj) (merge vers: 6.0.14-alpha) (pib:13)
[3 Dec 2009 20:45] Paul DuBois
Noted in 5.6.0 changelog.

Already fixed in 6.0.x.
[6 Mar 2010 10:51] Bugs System
Pushed into 5.5.3-m3 (revid:alik@sun.com-20100306103849-hha31z2enhh7jwt3) (version source revid:wlad@sol-20091204200504-9d3d3b6ad75ijkqo) (merge vers: 5.6.0-beta) (pib:16)
[7 Mar 2010 2:15] Paul DuBois
Moved 5.6.0 changelog entry to 5.5.3.