Uploaded image for project: 'Facter'
  1. Facter
  2. FACT-1722

off-by-one error in using gethostname()

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Normal
    • Resolution: Fixed
    • Affects Version/s: FACT 3.7.1
    • Fix Version/s: FACT 3.10.0
    • Component/s: CLI
    • Labels:
      None
    • Template:
    • Team:
      Platform OS
    • Method Found:
      Customer Feedback
    • Release Notes:
      Bug Fix
    • Release Notes Summary:
      Fixes an off-by-one error around comparison of the hostname length to the HOST_NAME_MAX limit; This previously produced an error when reading hostnames of exactly 64 bytes on Linux hosts.
    • QA Risk Assessment:
      Needs Assessment

      Description

      PR filed as https://github.com/puppetlabs/facter/pull/1605

      On Linux hosts with hostnames of exactly 64 bytes, facter fqdn fails with the same error from FACT-1238. Digging into the spec, the Linux manpage for gethostname(2) says...

      POSIX.1 guarantees that "Host names (not including the terminating null byte)
      are limited to HOST_NAME_MAX bytes". On Linux, HOST_NAME_MAX is
      defined with the value 64, which has been the limit since Linux 1.0
      (earlier kernels imposed a limit of 8 bytes).
      The "you have to count the null byte" behavior is confirmed in the spec (link), so this should be true everywhere vaguely unix-like.

      {HOST_NAME_MAX}

      Maximum length of a host name (not including the terminating null) as returned from the gethostname() function.
      Here's a trivial example:

      tmaher@lowryder:~/puppet-fqdn-bug$ hostname -f
      lowryder.pw0n.me
      tmaher@lowryder:~/puppet-fqdn-bug$ ./offbyone
      HOST_NAME_MAX: 64
      PASS: lowryder
      PASS: size 65, lowryder
      tmaher@lowryder:~/puppet-fqdn-bug$ sudo hostname a23456789.b23456789.c23456789.d23456789.e23456789.f23456789.wxyz
      tmaher@lowryder:~/puppet-fqdn-bug$ hostname
      a23456789.b23456789.c23456789.d23456789.e23456789.f23456789.wxyz
      tmaher@lowryder:~/puppet-fqdn-bug$ host=$(hostname); echo -n $host | wc -c
      64
      tmaher@lowryder:~/puppet-fqdn-bug$ ./offbyone
      HOST_NAME_MAX: 64
      FAIL: size 64, error File name too long (errno 36)
      PASS: size 65, a23456789.b23456789.c23456789.d23456789.e23456789.f23456789.wxyz
      

      tmaher@lowryder:~/puppet-fqdn-bug$ cat offbyone.c
      #include <unistd.h>
      #include <limits.h>
      #include <netinet/in.h>
      #include <arpa/inet.h>
      #include <stdlib.h>
      #include <stdio.h>
      #include <errno.h>
      #include <string.h>
       
      int main(void){
        int size = sysconf(_SC_HOST_NAME_MAX);
        char buffer[256];
        printf("HOST_NAME_MAX: %d\n", size);
        errno = 0;
       
        if (gethostname(buffer, size) != 0){
          printf("FAIL: size %d, error %s (errno %d)\n", size, strerror(errno), errno);
        } else {
          printf("PASS: %s\n", buffer);
        }
       
        size += 1;
        if (gethostname(buffer, size) != 0){
          printf("FAIL: size %d, error %s (errno %d)\n", size, strerror(errno), errno);
        } else {
          printf("PASS: size %d, %s\n", size, buffer);
        }
       
        return 0;
      }
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            tmaher Tom Maher
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Zendesk Support