Cleanup, dead code removal, some pretty insignificant security stuff that's based on recommendations.

This commit is contained in:
Adam Ierymenko 2014-04-18 00:14:12 -07:00
parent 5f45977e3e
commit 7831c4bfef
6 changed files with 31 additions and 33 deletions

@ -380,12 +380,12 @@ public:
}
/**
* Unconditionally zero buffer's underlying memory
* Unconditionally and securely zero buffer's underlying memory
*/
inline void zeroAll()
inline void burn()
throw()
{
memset(_b,0,sizeof(_b));
Utils::burn(_b,sizeof(_b));
}
/**

@ -67,6 +67,8 @@ public:
*/
Peer();
~Peer() { Utils::burn(_key,sizeof(_key)); }
/**
* Construct a new peer
*

@ -239,13 +239,16 @@ void Topology::_dumpPeers()
if (fwrite(buf.data(),buf.size(),1,pd) != 1) {
fclose(pd);
Utils::rm(pdpath);
buf.burn();
return;
}
buf.clear();
buf.burn();
}
} catch ( ... ) {
fclose(pd);
Utils::rm(pdpath);
buf.burn();
return;
}
}
@ -254,12 +257,15 @@ void Topology::_dumpPeers()
if (fwrite(buf.data(),buf.size(),1,pd) != 1) {
fclose(pd);
Utils::rm(pdpath);
buf.burn();
return;
}
buf.burn();
}
fclose(pd);
Utils::lockDownFile(pdpath.c_str(),false);
buf.burn();
}
void Topology::_loadPeers()
@ -301,6 +307,7 @@ void Topology::_loadPeers()
fclose(pd);
Utils::rm(pdpath);
buf.burn();
}
} // namespace ZeroTier

@ -395,22 +395,6 @@ std::string Utils::trim(const std::string &s)
return s.substr(start,end - start);
}
void Utils::stdsprintf(std::string &s,const char *fmt,...)
throw(std::bad_alloc,std::length_error)
{
char buf[65536];
va_list ap;
va_start(ap,fmt);
int n = vsnprintf(buf,sizeof(buf),fmt,ap);
va_end(ap);
if ((n >= (int)sizeof(buf))||(n < 0))
throw std::length_error("printf result too large");
s.append(buf);
}
unsigned int Utils::snprintf(char *buf,unsigned int len,const char *fmt,...)
throw(std::length_error)
{

@ -85,6 +85,20 @@ public:
return (diff == 0ULL);
}
/**
* Securely zero memory
*
* This just uses volatile to ensure that it's never optimized out.
*/
static inline void burn(void *ptr,unsigned int len)
throw()
{
volatile unsigned char *p = (unsigned char *)ptr;
volatile unsigned char *e = p + len;
while (p != e)
*(p++) = (unsigned char)0;
}
/**
* Delete a file
*
@ -432,21 +446,12 @@ public:
*/
static std::string trim(const std::string &s);
/**
* Like sprintf, but appends to std::string
*
* @param s String to append to
* @param fmt Printf format string
* @param ... Format arguments
* @throws std::bad_alloc Memory allocation failure
* @throws std::length_error Format + args exceeds internal buffer maximum
*/
static void stdsprintf(std::string &s,const char *fmt,...)
throw(std::bad_alloc,std::length_error);
/**
* Variant of snprintf that is portable and throws an exception
*
* This just wraps the local implementation whatever it's called, while
* performing a few other checks and adding exceptions for overflow.
*
* @param buf Buffer to write to
* @param len Length of buffer in bytes
* @param fmt Format string

@ -461,8 +461,8 @@ static int testPacket()
unsigned char salsaKey[32],hmacKey[32];
Packet a,b;
a.zeroAll();
b.zeroAll();
a.burn();
b.burn();
for(unsigned int i=0;i<32;++i) {
salsaKey[i] = (unsigned char)rand();