Sfoglia il codice sorgente

Prevent a server crash when using together APC cache and Mcrypt

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@2498 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 12 anni fa
parent
commit
a4dea4e6cf
2 ha cambiato i file con 26 aggiunte e 3 eliminazioni
  1. 16 0
      core/attributedef.class.inc.php
  2. 10 3
      core/simplecrypt.class.inc.php

+ 16 - 0
core/attributedef.class.inc.php

@@ -1609,6 +1609,22 @@ class AttributeEncryptedString extends AttributeString
 			self::$sKey = MetaModel::GetConfig()->GetEncryptionKey();
 		}
 	}
+	/**
+	 * When the attribute definitions are stored in APC cache:
+	 * 1) The static class variable $sKey is NOT serialized
+	 * 2) The object's constructor is NOT called upon wakeup
+	 * 3) mcrypt may crash the server if passed an empty key !!
+	 * 
+	 * So let's restore the key (if needed) when waking up
+	 **/
+	public function __wakeup()
+	{
+		if (self::$sKey == null)
+		{
+			self::$sKey = MetaModel::GetConfig()->GetEncryptionKey();
+		}
+	}
+	
 
 	protected function GetSQLCol() {return "TINYBLOB";}	
 

+ 10 - 3
core/simplecrypt.class.inc.php

@@ -220,9 +220,16 @@ class SimpleCryptMcryptEngine implements CryptEngine
     {
         $iv = substr($encrypted_data, 0, mcrypt_enc_get_iv_size($this->td));
         $string = substr($encrypted_data, mcrypt_enc_get_iv_size($this->td));       
-		mcrypt_generic_init($this->td, $key, $iv);
-		$decrypted_data = rtrim(mdecrypt_generic($this->td, $string), "\0");
-		mcrypt_generic_deinit($this->td);
+		$r = mcrypt_generic_init($this->td, $key, $iv);
+		if (($r < 0) || ($r === false))
+		{
+			$decrypted_data = '** decryption error **';
+		}
+		else
+		{
+			$decrypted_data = rtrim(mdecrypt_generic($this->td, $string), "\0");
+			mcrypt_generic_deinit($this->td);
+		}
         return $decrypted_data;
     }