[HQ-93] un-clobber stack data in sigar_gethostbyname
This commit is contained in:
parent
f1a13a3361
commit
5c26b463d9
|
@ -239,4 +239,23 @@ int sigar_get_iftype(const char *name, int *type, int *inst);
|
|||
#define SIGAR_NIC_ETHERNET "Ethernet"
|
||||
#define SIGAR_NIC_NETROM "AMPR NET/ROM"
|
||||
|
||||
#ifdef WIN32
|
||||
#else
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
#define SIGAR_GETHOSTBYNAME_LEN 512
|
||||
#if defined(__hpux) || defined(_AIX)
|
||||
#define SIGAR_HAS_HOSTENT_DATA
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
char buffer[SIGAR_GETHOSTBYNAME_LEN];
|
||||
struct hostent hs;
|
||||
int error;
|
||||
#ifdef SIGAR_HAS_HOSTENT_DATA
|
||||
struct hostent_data hd;
|
||||
#endif
|
||||
} sigar_hostent_t;
|
||||
|
||||
#endif
|
||||
|
|
|
@ -30,7 +30,8 @@
|
|||
|
||||
char *sigar_uitoa(char *buf, unsigned int n, int *len);
|
||||
|
||||
struct hostent *sigar_gethostbyname(const char *name);
|
||||
struct hostent *sigar_gethostbyname(const char *name,
|
||||
sigar_hostent_t *data);
|
||||
|
||||
SIGAR_INLINE char *sigar_skip_line(char *buffer, int buflen);
|
||||
|
||||
|
|
43
src/sigar.c
43
src/sigar.c
|
@ -1547,34 +1547,26 @@ static int fqdn_ip_get(sigar_t *sigar, char *name)
|
|||
return SIGAR_OK;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
#else
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
#define GETHOSTBYNAME_LEN 512
|
||||
#if defined(__hpux) || defined(_AIX)
|
||||
#define HAS_HOSTENT_DATA
|
||||
#endif
|
||||
|
||||
struct hostent *sigar_gethostbyname(const char *name)
|
||||
struct hostent *sigar_gethostbyname(const char *name,
|
||||
sigar_hostent_t *data)
|
||||
{
|
||||
char buffer[GETHOSTBYNAME_LEN];
|
||||
struct hostent hs, *hp;
|
||||
int err;
|
||||
#if defined(HAS_HOSTENT_DATA)
|
||||
struct hostent_data hd;
|
||||
#endif
|
||||
struct hostent *hp = NULL;
|
||||
|
||||
#if defined(__linux__)
|
||||
gethostbyname_r(name, &hs, buffer, sizeof(buffer),
|
||||
&hp, &err);
|
||||
gethostbyname_r(name, &data->hs,
|
||||
data->buffer, sizeof(data->buffer),
|
||||
&hp, &data->error);
|
||||
#elif defined(__sun)
|
||||
hp = gethostbyname_r(name, &hs, buffer, sizeof(buffer),
|
||||
&err);
|
||||
#elif defined(HAS_HOSTENT_DATA)
|
||||
gethostbyname_r(name, &hs, &hd);
|
||||
hp = &hs;
|
||||
hp = gethostbyname_r(name, &data->hs,
|
||||
data->buffer, sizeof(data->buffer),
|
||||
&data->error);
|
||||
#elif defined(SIGAR_HAS_HOSTENT_DATA)
|
||||
if (gethostbyname_r(name, &data->hs, &data->hd) == 0) {
|
||||
hp = &data->hs;
|
||||
}
|
||||
else {
|
||||
data->error = h_errno;
|
||||
}
|
||||
#else
|
||||
hp = gethostbyname(name);
|
||||
#endif
|
||||
|
@ -1593,6 +1585,7 @@ struct hostent *sigar_gethostbyname(const char *name)
|
|||
|
||||
SIGAR_DECLARE(int) sigar_fqdn_get(sigar_t *sigar, char *name, int namelen)
|
||||
{
|
||||
sigar_hostent_t data;
|
||||
struct hostent *p;
|
||||
char domain[SIGAR_FQDN_LEN + 1];
|
||||
#ifdef WIN32
|
||||
|
@ -1618,7 +1611,7 @@ SIGAR_DECLARE(int) sigar_fqdn_get(sigar_t *sigar, char *name, int namelen)
|
|||
}
|
||||
|
||||
/* XXX use _r versions of these functions. */
|
||||
if (!(p = sigar_gethostbyname(name))) {
|
||||
if (!(p = sigar_gethostbyname(name, &data))) {
|
||||
if (SIGAR_LOG_IS_DEBUG(sigar)) {
|
||||
sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
|
||||
"[fqdn] gethostbyname(%s) failed: %s",
|
||||
|
|
|
@ -496,12 +496,13 @@ int sigar_cpu_mhz_from_model(char *model)
|
|||
static enum clnt_stat get_sockaddr(struct sockaddr_in *addr, char *host)
|
||||
{
|
||||
register struct hostent *hp;
|
||||
sigar_hostent_t data;
|
||||
|
||||
memset(addr, 0, sizeof(struct sockaddr_in));
|
||||
addr->sin_family = AF_INET;
|
||||
|
||||
if ((addr->sin_addr.s_addr = inet_addr(host)) == -1) {
|
||||
if (!(hp = sigar_gethostbyname(host))) {
|
||||
if (!(hp = sigar_gethostbyname(host, &data))) {
|
||||
return RPC_UNKNOWNHOST;
|
||||
}
|
||||
memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
|
||||
|
|
Loading…
Reference in New Issue