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.
This commit is contained in:
Florian Weimer 2018-06-27 17:55:38 +02:00
parent 4272059de2
commit 890c2ced35
2 changed files with 37 additions and 17 deletions

View File

@ -1,3 +1,9 @@
2018-06-27 Florian Weimer <fweimer@redhat.com>
[BZ #18023]
* sysdeps/unix/sysv/linux/gethostid.c (gethostid): Use struct
scratch_buffer instead of extend_alloca. Update comments.
2018-06-27 Florian Weimer <fweimer@redhat.com> 2018-06-27 Florian Weimer <fweimer@redhat.com>
[BZ #18023] [BZ #18023]

View File

@ -21,6 +21,7 @@
#include <unistd.h> #include <unistd.h>
#include <netdb.h> #include <netdb.h>
#include <not-cancel.h> #include <not-cancel.h>
#include <stdbool.h>
#define HOSTIDFILE "/etc/hostid" #define HOSTIDFILE "/etc/hostid"
@ -63,13 +64,12 @@ sethostid (long int id)
# include <sys/param.h> # include <sys/param.h>
# include <resolv/netdb.h> # include <resolv/netdb.h>
# include <netinet/in.h> # include <netinet/in.h>
# include <scratch_buffer.h>
long int long int
gethostid (void) gethostid (void)
{ {
char hostname[MAXHOSTNAMELEN + 1]; char hostname[MAXHOSTNAMELEN + 1];
size_t buflen;
char *buffer;
struct hostent hostbuf, *hp; struct hostent hostbuf, *hp;
int32_t id; int32_t id;
struct in_addr in; struct in_addr in;
@ -88,29 +88,43 @@ gethostid (void)
return id; return id;
} }
/* Getting from the file was not successful. An intelligent guess for /* Getting from the file was not successful. An intelligent guess
a unique number of a host is its IP address. Return this. */ 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') if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0')
/* This also fails. Return and arbitrary value. */ /* This also fails. Return and arbitrary value. */
return 0; return 0;
buflen = 1024; /* Determine the IP address of the host name. */
buffer = __alloca (buflen); struct scratch_buffer tmpbuf;
scratch_buffer_init (&tmpbuf);
/* To get the IP address we need to know the host name. */ while (true)
while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr) {
!= 0 int ret = __gethostbyname_r (hostname, &hostbuf,
|| hp == NULL) tmpbuf.data, tmpbuf.length, &hp, &herr);
if (herr != NETDB_INTERNAL || errno != ERANGE) if (ret == 0)
return 0; break;
else else
/* Enlarge buffer. */ {
buffer = extend_alloca (buffer, buflen, 2 * buflen); /* 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; in.s_addr = 0;
memcpy (&in, hp->h_addr, memcpy (&in, hp->h_addr,
(int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length); (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 /* For the return value to be not exactly the IP address we do some
bit fiddling. */ bit fiddling. */
return (int32_t) (in.s_addr << 16 | in.s_addr >> 16); return (int32_t) (in.s_addr << 16 | in.s_addr >> 16);