浏览代码

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

Adam Ierymenko 11 年之前
父节点
当前提交
7831c4bfef
共有 6 个文件被更改,包括 31 次插入33 次删除
  1. 3 3
      node/Buffer.hpp
  2. 2 0
      node/Peer.hpp
  3. 7 0
      node/Topology.cpp
  4. 0 16
      node/Utils.cpp
  5. 17 12
      node/Utils.hpp
  6. 2 2
      selftest.cpp

+ 3 - 3
node/Buffer.hpp

@@ -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));
 	}
 
 	/**

+ 2 - 0
node/Peer.hpp

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

+ 7 - 0
node/Topology.cpp

@@ -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

+ 0 - 16
node/Utils.cpp

@@ -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)
 {

+ 17 - 12
node/Utils.hpp

@@ -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

+ 2 - 2
selftest.cpp

@@ -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();