diff options
author | Florian Weimer <fweimer@redhat.com> | 2018-06-27 17:55:38 +0200 |
---|---|---|
committer | Florian Weimer <fweimer@redhat.com> | 2018-06-27 17:55:38 +0200 |
commit | 890c2ced35cfbf2de7d787fd37caf23d250da531 (patch) | |
tree | 591974afd82babb6ea28a5efe3d2693873a3137a /sysdeps/unix/sysv | |
parent | 4272059de256fb20a7b19b5f8509e0c8d27beaf4 (diff) | |
download | glibc-890c2ced35cfbf2de7d787fd37caf23d250da531.tar.gz glibc-890c2ced35cfbf2de7d787fd37caf23d250da531.tar.xz glibc-890c2ced35cfbf2de7d787fd37caf23d250da531.zip |
gethostid (Linux variant): Switch to struct scratch_buffer [BZ #18023]
Previously, extend_alloca was used without alloca accounting, which could have been problematic with large NSS results.
Diffstat (limited to 'sysdeps/unix/sysv')
-rw-r--r-- | sysdeps/unix/sysv/linux/gethostid.c | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/sysdeps/unix/sysv/linux/gethostid.c b/sysdeps/unix/sysv/linux/gethostid.c index 73c56e57e5..2e20f034dc 100644 --- a/sysdeps/unix/sysv/linux/gethostid.c +++ b/sysdeps/unix/sysv/linux/gethostid.c @@ -21,6 +21,7 @@ #include <unistd.h> #include <netdb.h> #include <not-cancel.h> +#include <stdbool.h> #define HOSTIDFILE "/etc/hostid" @@ -63,13 +64,12 @@ sethostid (long int id) # include <sys/param.h> # include <resolv/netdb.h> # include <netinet/in.h> +# include <scratch_buffer.h> long int gethostid (void) { char hostname[MAXHOSTNAMELEN + 1]; - size_t buflen; - char *buffer; struct hostent hostbuf, *hp; int32_t id; struct in_addr in; @@ -88,29 +88,43 @@ gethostid (void) return id; } - /* Getting from the file was not successful. An intelligent guess for - a unique number of a host is its IP address. Return this. */ + /* Getting from the file was not successful. An intelligent guess + for a unique number of a host is its IP address. To get the IP + address we need to know the host name. */ if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0') /* This also fails. Return and arbitrary value. */ return 0; - buflen = 1024; - buffer = __alloca (buflen); - - /* To get the IP address we need to know the host name. */ - while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr) - != 0 - || hp == NULL) - if (herr != NETDB_INTERNAL || errno != ERANGE) - return 0; - else - /* Enlarge buffer. */ - buffer = extend_alloca (buffer, buflen, 2 * buflen); + /* Determine the IP address of the host name. */ + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); + while (true) + { + int ret = __gethostbyname_r (hostname, &hostbuf, + tmpbuf.data, tmpbuf.length, &hp, &herr); + if (ret == 0) + break; + else + { + /* Enlarge the buffer on ERANGE. */ + if (herr == NETDB_INTERNAL && errno == ERANGE) + { + if (!scratch_buffer_grow (&tmpbuf)) + return 0; + } + /* Other errors are a failure. Return an arbitrary value. */ + else + { + scratch_buffer_free (&tmpbuf); + return 0; + } + } + } in.s_addr = 0; memcpy (&in, hp->h_addr, (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length); - + scratch_buffer_free (&tmpbuf); /* For the return value to be not exactly the IP address we do some bit fiddling. */ return (int32_t) (in.s_addr << 16 | in.s_addr >> 16); |