Bug 339461. Fixed memory leak that caused random errors or hangs. Also increased NamedPipe Channels to 1024.
This commit is contained in:
		| @@ -30,39 +30,39 @@ using HANDLE = System.IntPtr; | |||||||
|  |  | ||||||
| namespace AppModule.NamedPipes | namespace AppModule.NamedPipes | ||||||
| { | { | ||||||
| 	#region Comments |     #region Comments | ||||||
| 	/// <summary> |     /// <summary> | ||||||
| 	/// A utility class that exposes named pipes operations. |     /// A utility class that exposes named pipes operations. | ||||||
| 	/// </summary> |     /// </summary> | ||||||
| 	/// <remarks> |     /// <remarks> | ||||||
| 	/// This class uses the exposed exposed kernel32.dll methods by the  |     /// This class uses the exposed exposed kernel32.dll methods by the  | ||||||
| 	/// <see cref="AppModule.NamedPipes.NamedPipeNative">NamedPipeNative</see> class |     /// <see cref="AppModule.NamedPipes.NamedPipeNative">NamedPipeNative</see> class | ||||||
| 	/// to provided controlled named pipe functionality. |     /// to provided controlled named pipe functionality. | ||||||
| 	/// </remarks> |     /// </remarks> | ||||||
| 	#endregion |     #endregion | ||||||
| 	public sealed class ImpersonateWrapper  |     public sealed class ImpersonateWrapper | ||||||
| 	{ |     { | ||||||
|  |  | ||||||
| 		public const int TOKEN_QUERY = 0X00000008; |         public const int TOKEN_QUERY = 0X00000008; | ||||||
|  |  | ||||||
| 		const int ERROR_NO_MORE_ITEMS = 259; |         const int ERROR_NO_MORE_ITEMS = 259; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 		// Client USERID stuff |         // Client USERID stuff | ||||||
| 		// 1. call ImpersonateNamedPipeClient(hPipe) |         // 1. call ImpersonateNamedPipeClient(hPipe) | ||||||
| 		// 2. call OpenThreadToken(GetCurrentThread(), |         // 2. call OpenThreadToken(GetCurrentThread(), | ||||||
| 		//							TOKEN_QUERY | TOKEN_QUERY_SOURCE, |         //							TOKEN_QUERY | TOKEN_QUERY_SOURCE, | ||||||
| 		//							FALSE, |         //							FALSE, | ||||||
| 		//							phUserToken); |         //							phUserToken); | ||||||
|  |  | ||||||
| 		public static int ImpersonateNamedPipeClient(IntPtr hPipeHandle) |  | ||||||
| 		{ |  | ||||||
| 			int rcode = ImpersonateNative.ImpersonateNamedPipeClient(hPipeHandle);					 |  | ||||||
| 			return rcode; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
|  |         public static int ImpersonateNamedPipeClient(IntPtr hPipeHandle) | ||||||
|  |         { | ||||||
|  |             int rcode = ImpersonateNative.ImpersonateNamedPipeClient(hPipeHandle); | ||||||
|  |             return rcode; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  | #if DEBUG | ||||||
| 		static int PerformDump(HANDLE token) | 		static int PerformDump(HANDLE token) | ||||||
| 		{ | 		{ | ||||||
| 			StringBuilder sb = new StringBuilder(); | 			StringBuilder sb = new StringBuilder(); | ||||||
| @@ -80,14 +80,11 @@ namespace AppModule.NamedPipes | |||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			tokUser = (ImpersonateNative.TOKEN_USER) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_USER) );			 | 			tokUser = (ImpersonateNative.TOKEN_USER) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_USER) );			 | ||||||
| 			//sb.Append(DumpAccountSid(tokUser.User.Sid)); |  | ||||||
| 			IntPtr pUserID = tokUser.User.Sid; | 			IntPtr pUserID = tokUser.User.Sid; | ||||||
| 			//Console.WriteLine("UserID: " + pUserID); |  | ||||||
| 			 | 			 | ||||||
| 			DumpAccountSid(pUserID); | 			DumpAccountSid(pUserID); | ||||||
| 			Marshal.FreeHGlobal( tu ); | 			Marshal.FreeHGlobal( tu ); | ||||||
|  |  | ||||||
|  |  | ||||||
| 			tu = Marshal.AllocHGlobal(bufLength); | 			tu = Marshal.AllocHGlobal(bufLength); | ||||||
| 			cb = bufLength; | 			cb = bufLength; | ||||||
|  |  | ||||||
| @@ -155,6 +152,7 @@ namespace AppModule.NamedPipes | |||||||
| 			Console.WriteLine("Acct info: "+ sb.ToString()); | 			Console.WriteLine("Acct info: "+ sb.ToString()); | ||||||
| 			return sb.ToString(); | 			return sb.ToString(); | ||||||
| 		} | 		} | ||||||
|  | #endif | ||||||
|  |  | ||||||
|         public static bool GetLinkedToken(IntPtr token, out ImpersonateNative.TOKEN_LINKED_TOKEN linkedToken) |         public static bool GetLinkedToken(IntPtr token, out ImpersonateNative.TOKEN_LINKED_TOKEN linkedToken) | ||||||
|         { |         { | ||||||
| @@ -169,7 +167,6 @@ namespace AppModule.NamedPipes | |||||||
|  |  | ||||||
|                 ImpersonateNative.GetTokenInformation(token, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenLinkedToken, IntPtr.Zero, TokenInfLength, ref TokenInfLength); |                 ImpersonateNative.GetTokenInformation(token, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenLinkedToken, IntPtr.Zero, TokenInfLength, ref TokenInfLength); | ||||||
|  |  | ||||||
|  |  | ||||||
|                 try |                 try | ||||||
|                 { |                 { | ||||||
|                     ptrLinkedToken = Marshal.AllocHGlobal(TokenInfLength); |                     ptrLinkedToken = Marshal.AllocHGlobal(TokenInfLength); | ||||||
| @@ -198,36 +195,36 @@ namespace AppModule.NamedPipes | |||||||
|             return TokenInfoSuccess; |             return TokenInfoSuccess; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| 		public static int GetLocalUserID(PipeHandle handle, ref int lowPart, ref int highPart, ref string SidString, ref int lowPartElevated, ref int highPartElevated) |         public static int GetLocalUserID(PipeHandle handle, ref int lowPart, ref int highPart, ref string SidString, ref int lowPartElevated, ref int highPartElevated) | ||||||
| 		{			 |         { | ||||||
| 			int rcode = -1; |             int rcode = -1; | ||||||
| 			// get client userID |             // get client userID | ||||||
| 			int code = ImpersonateNative.ImpersonateNamedPipeClient(handle.Handle); |             int code = ImpersonateNative.ImpersonateNamedPipeClient(handle.Handle); | ||||||
| 			if (code == 0) |             if (code == 0) | ||||||
| 			{ |             { | ||||||
| 				uint lastError = NamedPipeNative.GetLastError(); |                 uint lastError = NamedPipeNative.GetLastError(); | ||||||
| 				Console.WriteLine("ImpersonateNamedPipeClient Error: "+rcode.ToString()); |                 System.Diagnostics.Debug.WriteLine("CASA: ImpersonateNamedPipeClient Error: " + rcode.ToString()); | ||||||
| 				return -1; |                 return -1; | ||||||
| 			} |             } | ||||||
|  |  | ||||||
| 			try  |             try | ||||||
| 			{ |             { | ||||||
| 				IntPtr hThread = ImpersonateNative.GetCurrentThread(); |                 IntPtr hThread = ImpersonateNative.GetCurrentThread(); | ||||||
| 				uint iDesiredInfo = 24; //TOKEN_QUERY | TOKEN_QUERY_SOURCE; |                 uint iDesiredInfo = 24; //TOKEN_QUERY | TOKEN_QUERY_SOURCE; | ||||||
| 				IntPtr userToken = Marshal.AllocHGlobal(4); |                 IntPtr userToken = IntPtr.Zero; | ||||||
|                 IntPtr userTokenElevated = IntPtr.Zero; |                 IntPtr userTokenElevated = IntPtr.Zero; | ||||||
|  |  | ||||||
| 				if (ImpersonateNative.OpenThreadToken(hThread, iDesiredInfo, true, out userToken)) |                 if (ImpersonateNative.OpenThreadToken(hThread, iDesiredInfo, true, out userToken)) | ||||||
| 				{ |                 { | ||||||
| 					StringBuilder sb = new StringBuilder(); |                     StringBuilder sb = new StringBuilder(); | ||||||
| 					ImpersonateNative.TOKEN_USER tokUser; |                     ImpersonateNative.TOKEN_USER tokUser; | ||||||
| 					const int bufLength = 256; |                     const int bufLength = 256; | ||||||
| 					IntPtr tu = Marshal.AllocHGlobal( bufLength ); |                     IntPtr tu = Marshal.AllocHGlobal(bufLength); | ||||||
| 					int cb = bufLength; |                     int cb = bufLength; | ||||||
|  |  | ||||||
|                     // on Vista use the elevated token if there is one.                     |                     // on Vista use the elevated token if there is one.                     | ||||||
|                     System.OperatingSystem os = System.Environment.OSVersion; |                     System.OperatingSystem os = System.Environment.OSVersion; | ||||||
|                     System.Diagnostics.Trace.WriteLine("OS Version: "  + os.Version.ToString()); |  | ||||||
|                     if (os.Version.Major > 5) |                     if (os.Version.Major > 5) | ||||||
|                     { |                     { | ||||||
|                         if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenElevationType, tu, cb, ref cb)) |                         if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenElevationType, tu, cb, ref cb)) | ||||||
| @@ -235,16 +232,11 @@ namespace AppModule.NamedPipes | |||||||
|                             int iTokenType; |                             int iTokenType; | ||||||
|                             iTokenType = (int)Marshal.PtrToStructure(tu, typeof(int)); |                             iTokenType = (int)Marshal.PtrToStructure(tu, typeof(int)); | ||||||
|  |  | ||||||
|                             System.Diagnostics.Trace.WriteLine("Token Type: " + iTokenType.ToString()); |  | ||||||
|                             if (iTokenType == (int)ImpersonateNative.TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited) |                             if (iTokenType == (int)ImpersonateNative.TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited) | ||||||
|                             { |                             { | ||||||
|                                 System.Diagnostics.Trace.WriteLine("Getting linked token"); |  | ||||||
|  |  | ||||||
|                                 ImpersonateNative.TOKEN_LINKED_TOKEN newLinkedToken; |                                 ImpersonateNative.TOKEN_LINKED_TOKEN newLinkedToken; | ||||||
|                                 if (GetLinkedToken(userToken, out newLinkedToken)) |                                 if (GetLinkedToken(userToken, out newLinkedToken)) | ||||||
|                                 { |                                 { | ||||||
|                                     //userToken = newLinkedToken.LinkedToken; |  | ||||||
|                                     userTokenElevated = Marshal.AllocHGlobal(4); |  | ||||||
|                                     userTokenElevated = newLinkedToken.LinkedToken; |                                     userTokenElevated = newLinkedToken.LinkedToken; | ||||||
|                                 } |                                 } | ||||||
|                             } |                             } | ||||||
| @@ -252,33 +244,33 @@ namespace AppModule.NamedPipes | |||||||
|                         else |                         else | ||||||
|                         { |                         { | ||||||
|                             uint error = ImpersonateNative.GetLastError(); |                             uint error = ImpersonateNative.GetLastError(); | ||||||
|                             System.Diagnostics.Trace.WriteLine("linked token error: " +  error.ToString()); |                             System.Diagnostics.Debug.WriteLine("CASA: linked token error: " + error.ToString()); | ||||||
|                         } |                         } | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
|                     cb = bufLength; |                     cb = bufLength; | ||||||
|                     if (ImpersonateNative.GetTokenInformation( userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenUser, tu, cb, ref cb )) |                     if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenUser, tu, cb, ref cb)) | ||||||
| 					{					 |                     { | ||||||
| 						tokUser = (ImpersonateNative.TOKEN_USER) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_USER) );				 |                         tokUser = (ImpersonateNative.TOKEN_USER)Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_USER)); | ||||||
| 						IntPtr pUserID = tokUser.User.Sid;								 |                         IntPtr pUserID = tokUser.User.Sid; | ||||||
| 						Marshal.FreeHGlobal( tu ); |                         Marshal.FreeHGlobal(tu); | ||||||
|  |  | ||||||
| 						// get SID |                         // get SID						 | ||||||
| 						//string SidString = null; |                         ImpersonateNative.ConvertSidToStringSid(pUserID, ref SidString); | ||||||
| 						ImpersonateNative.ConvertSidToStringSid(pUserID, ref SidString); |  | ||||||
|  |  | ||||||
| 						// get token states |                         // get token states | ||||||
| 						tu = Marshal.AllocHGlobal(bufLength); |                         tu = Marshal.AllocHGlobal(bufLength); | ||||||
| 						cb = bufLength; |                         cb = bufLength; | ||||||
| 						ImpersonateNative.TOKEN_STATISTICS stats;			 |                         ImpersonateNative.TOKEN_STATISTICS stats; | ||||||
| 						if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenStatistics, tu, cb, ref cb)) |                         if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenStatistics, tu, cb, ref cb)) | ||||||
| 						{					 |                         { | ||||||
| 							stats = (ImpersonateNative.TOKEN_STATISTICS) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_STATISTICS)); |                             stats = (ImpersonateNative.TOKEN_STATISTICS)Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_STATISTICS)); | ||||||
| 							// copy low and high part										 |  | ||||||
| 							lowPart = stats.AuthenticationId.LowPart; |                             // copy low and high part										 | ||||||
| 							highPart = stats.AuthenticationId.HighPart;	 |                             lowPart = stats.AuthenticationId.LowPart; | ||||||
| 							rcode = -1;		 |                             highPart = stats.AuthenticationId.HighPart; | ||||||
| 						} |                             rcode = -1; | ||||||
|  |                         } | ||||||
|  |  | ||||||
|                         // get elevated token stats  |                         // get elevated token stats  | ||||||
|                         if (userTokenElevated != IntPtr.Zero) |                         if (userTokenElevated != IntPtr.Zero) | ||||||
| @@ -292,44 +284,57 @@ namespace AppModule.NamedPipes | |||||||
|                                 highPartElevated = stats.AuthenticationId.HighPart; |                                 highPartElevated = stats.AuthenticationId.HighPart; | ||||||
|                                 rcode = -1; |                                 rcode = -1; | ||||||
|                             } |                             } | ||||||
|  |  | ||||||
|  |                             if (!ImpersonateNative.CloseHandle(userTokenElevated)) | ||||||
|  |                             { | ||||||
|  |                                 uint errCode = ImpersonateNative.GetLastError(); | ||||||
|  |                                 System.Diagnostics.Debug.WriteLine("CASA: Closing elevated handle failed: {0}", errCode.ToString()); | ||||||
|  |                             } | ||||||
|  |  | ||||||
|  |                             userTokenElevated = IntPtr.Zero; | ||||||
|                         } |                         } | ||||||
| 					} |                     } | ||||||
| 					else |                     else | ||||||
| 					{ |                     { | ||||||
| 						Console.WriteLine("GetTokenInformation NOT successful"); |                         uint error = NamedPipeNative.GetLastError(); | ||||||
| 						uint error = NamedPipeNative.GetLastError(); |                         System.Diagnostics.Debug.WriteLine("CASA: GetTokenInformation NOT successful " + error.ToString()); | ||||||
| 						Console.WriteLine("error" + error.ToString());				 |                     } | ||||||
| 					} |  | ||||||
|  |  | ||||||
|                     Marshal.FreeHGlobal(tu); |                     Marshal.FreeHGlobal(tu); | ||||||
|  |  | ||||||
| 					// close handle |                     // close handles                     | ||||||
| 					ImpersonateNative.CloseHandle(hThread); |                     if (!ImpersonateNative.CloseHandle(userToken)) | ||||||
| 					ImpersonateNative.RevertToSelf(); |                     { | ||||||
| 				} |                         uint errCode = ImpersonateNative.GetLastError(); | ||||||
| 				else |                         System.Diagnostics.Debug.WriteLine("CASA: Close userToken failed: {0}", errCode.ToString()); | ||||||
| 				{ |                     } | ||||||
| 					int lastError = Marshal.GetLastWin32Error(); |                     else | ||||||
| 					uint errorcode = NamedPipeNative.GetLastError(); |                     { | ||||||
| 					Console.WriteLine("OpenThreadToken Error: "+ errorcode.ToString() + " code2: "+rcode.ToString()); |                         userToken = IntPtr.Zero; | ||||||
| 				} |                     } | ||||||
|  |  | ||||||
|  |                     if (!ImpersonateNative.RevertToSelf()) | ||||||
|  |                     { | ||||||
|  |                         System.Diagnostics.Debug.WriteLine("CASA: RevertToSelf failed"); | ||||||
|  |                     } | ||||||
|  |  | ||||||
|                 Marshal.FreeHGlobal(userToken); |  | ||||||
|                 if (userTokenElevated != IntPtr.Zero) |  | ||||||
|                 { |  | ||||||
|                     Marshal.FreeHGlobal(userTokenElevated); |  | ||||||
|                 } |                 } | ||||||
| 			} |                 else | ||||||
| 			catch (Exception ex) |                 { | ||||||
| 			{ |                     uint errorcode = NamedPipeNative.GetLastError(); | ||||||
| 				int error = Marshal.GetLastWin32Error(); |                     System.Diagnostics.Debug.WriteLine("CASA: OpenThreadToken Error: " + errorcode.ToString()); | ||||||
| 				Console.WriteLine(ex.ToString()); |                 } | ||||||
| 				return rcode; |             } | ||||||
| 			} |             catch (Exception ex) | ||||||
| 			// end |             { | ||||||
|  |                 int error = Marshal.GetLastWin32Error(); | ||||||
|  |                 System.Diagnostics.Debug.WriteLine(ex.ToString()); | ||||||
|  |                 return rcode; | ||||||
|  |             } | ||||||
|  |             // end | ||||||
|  |  | ||||||
| 			return rcode; |             return rcode; | ||||||
|  |  | ||||||
| 		} |         } | ||||||
| 	} |     } | ||||||
| } | } | ||||||
							
								
								
									
										2
									
								
								CASA/micasad/cache/SecretStore.cs
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								CASA/micasad/cache/SecretStore.cs
									
									
									
									
										vendored
									
									
								
							| @@ -896,7 +896,7 @@ namespace sscs.cache | |||||||
|             } |             } | ||||||
|             catch (Exception e) |             catch (Exception e) | ||||||
|             { |             { | ||||||
|                 CSSSLogger.ExpLog(e.ToString()); |                 CSSSLogger.DbgLog("Desktop password not set"); | ||||||
|             } |             } | ||||||
|             return null; |             return null; | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -37,7 +37,7 @@ namespace sscs.communication.win { | |||||||
|  |  | ||||||
| 		public Hashtable Pipes; | 		public Hashtable Pipes; | ||||||
|  |  | ||||||
| 		private uint NumberPipes = 16; | 		private uint NumberPipes = 1024; | ||||||
| 		private uint OutBuffer = 65536; //512; | 		private uint OutBuffer = 65536; //512; | ||||||
| 		private uint InBuffer = 65536; //512; | 		private uint InBuffer = 65536; //512; | ||||||
| 		private const int MAX_READ_BYTES = 15000; | 		private const int MAX_READ_BYTES = 15000; | ||||||
|   | |||||||
| @@ -594,7 +594,7 @@ void RemoveServerSecret() | |||||||
|     RemoveSecret(SSCS_CRED_TYPE_SERVER_F); |     RemoveSecret(SSCS_CRED_TYPE_SERVER_F); | ||||||
| } | } | ||||||
|  |  | ||||||
| void RunTest() | void RunTest(char *SecretID) | ||||||
| { | { | ||||||
| 	{ | 	{ | ||||||
| 		SSCS_BASIC_CREDENTIAL	credential = {0}; | 		SSCS_BASIC_CREDENTIAL	credential = {0}; | ||||||
| @@ -604,7 +604,8 @@ void RunTest() | |||||||
| 		int rcode = 0; | 		int rcode = 0; | ||||||
| 		int iFlags = 0; | 		int iFlags = 0; | ||||||
|  |  | ||||||
| 		sscs_Utf8Strcpy(appSecretId.id, "NativeC.AppSecretID"); | 		//sscs_Utf8Strcpy(appSecretId.id, "NativeC.AppSecretID"); | ||||||
|  | 		sscs_Utf8Strcpy(appSecretId.id, SecretID); | ||||||
| 		appSecretId.len = sscs_Utf8Strlen(appSecretId.id) + 1; | 		appSecretId.len = sscs_Utf8Strlen(appSecretId.id) + 1; | ||||||
| 		 | 		 | ||||||
| 		credential.unFlags = USERNAME_TYPE_CN_F; | 		credential.unFlags = USERNAME_TYPE_CN_F; | ||||||
| @@ -765,6 +766,7 @@ void RunTests() | |||||||
| 	int iCount = 1; | 	int iCount = 1; | ||||||
| 	int iTemp = 0; | 	int iTemp = 0; | ||||||
| 	int i = 0; | 	int i = 0; | ||||||
|  | 	char inputID[20]; | ||||||
|  |  | ||||||
| 	printf("Enter number interations to run (default 1): ");	 | 	printf("Enter number interations to run (default 1): ");	 | ||||||
| 	gets(runtimes); | 	gets(runtimes); | ||||||
| @@ -773,9 +775,13 @@ void RunTests() | |||||||
| 	if (iTemp > 1) | 	if (iTemp > 1) | ||||||
| 		iCount=iTemp; | 		iCount=iTemp; | ||||||
|  |  | ||||||
|  | 	printf("Enter secretID: ");		 | ||||||
|  | 	gets(inputID); | ||||||
|  |  | ||||||
| 	for (i=0; i<iCount; i++) | 	for (i=0; i<iCount; i++) | ||||||
| 	{ | 	{ | ||||||
| 		RunTest(); | 		printf("Count: %d\r\n", i); | ||||||
|  | 		RunTest(&inputID); | ||||||
| 		printf("\r\n"); | 		printf("\r\n"); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user