fixed GH #2700: Invalid read of memory in Poco::Environment::set which may cause crashes.

This commit is contained in:
Günter Obiltschnig 2019-06-22 18:19:22 +02:00
parent 27335031e1
commit 5ba6ec6885
2 changed files with 15 additions and 15 deletions

View File

@ -37,7 +37,7 @@ FastMutex EnvironmentImpl::_mutex;
std::string EnvironmentImpl::getImpl(const std::string& name)
{
FastMutex::ScopedLock lock(_mutex);
const char* val = getenv(name.c_str());
if (val)
return std::string(val);
@ -57,11 +57,11 @@ bool EnvironmentImpl::hasImpl(const std::string& name)
void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
{
FastMutex::ScopedLock lock(_mutex);
std::string var = name;
var.append("=");
var.append(value);
_map[name] = var;
std::swap(_map[name], var);
if (putenv((char*) _map[name].c_str()))
{
std::string msg = "cannot set environment variable: ";
@ -156,14 +156,14 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
int rc = getifaddrs(&ifaphead);
if (rc) return;
for (struct ifaddrs* ifap = ifaphead; ifap; ifap = ifap->ifa_next)
for (struct ifaddrs* ifap = ifaphead; ifap; ifap = ifap->ifa_next)
{
if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_LINK)
if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_LINK)
{
struct sockaddr_dl* sdl = reinterpret_cast<struct sockaddr_dl*>(ifap->ifa_addr);
caddr_t ap = LLADDR(sdl);
int alen = sdl->sdl_alen;
if (ap && alen > 0)
if (ap && alen > 0)
{
std::memcpy(&id, ap, sizeof(id));
break;
@ -187,9 +187,9 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
#include <net/if.h>
#ifndef __CYGWIN__
#include <net/if_arp.h>
#else // workaround for Cygwin, which does not have if_arp.h
#else // workaround for Cygwin, which does not have if_arp.h
#define ARPHRD_ETHER 1 /* Ethernet 10Mbps */
#endif
#endif
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
@ -221,7 +221,7 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
if (std::sscanf(buffer, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &id[0], &id[1], &id[2], &id[3], &id[4], &id[5]) == 6)
return;
}
}
}
// if that did not work, search active interfaces
int sock = socket(PF_INET, SOCK_DGRAM, 0);
@ -258,7 +258,7 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
}
for (const char* ptr = buf; ptr < buf + ifc.ifc_len;)
{
const struct ifreq* ifr = reinterpret_cast<const struct ifreq*>(ptr);
const struct ifreq* ifr = reinterpret_cast<const struct ifreq*>(ptr);
int rc = ioctl(sock, SIOCGIFHWADDR, ifr);
if (rc != -1)
{

View File

@ -46,7 +46,7 @@ FastMutex EnvironmentImpl::_mutex;
std::string EnvironmentImpl::getImpl(const std::string& name)
{
FastMutex::ScopedLock lock(_mutex);
const char* val = getenv(name.c_str());
if (val)
return std::string(val);
@ -66,11 +66,11 @@ bool EnvironmentImpl::hasImpl(const std::string& name)
void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
{
FastMutex::ScopedLock lock(_mutex);
std::string var = name;
var.append("=");
var.append(value);
_map[name] = var;
std::swap(_map[name], var);
if (putenv((char*) _map[name].c_str()))
{
std::string msg = "cannot set environment variable: ";
@ -112,7 +112,7 @@ std::string EnvironmentImpl::osArchitectureImpl()
return "sh";
#else
return "unknown";
#endif
#endif
}
@ -149,7 +149,7 @@ void EnvironmentImpl::nodeIdImpl(NodeId& id)
return;
}
}
else break;
else break;
++ifIndex;
}
throw SystemException("cannot get Ethernet hardware address");