Bug 339461. Fixed memory leak that caused random errors or hangs. Also increased NamedPipe Channels to 1024.

This commit is contained in:
Jim Norman 2007-12-05 20:38:20 +00:00
parent f28ce8f26b
commit 4df0612f64
4 changed files with 371 additions and 360 deletions

View File

@ -62,7 +62,7 @@ namespace AppModule.NamedPipes
return rcode; 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);
@ -206,7 +203,7 @@ namespace AppModule.NamedPipes
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;
} }
@ -214,7 +211,7 @@ namespace AppModule.NamedPipes
{ {
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))
@ -227,7 +224,7 @@ namespace AppModule.NamedPipes
// 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,7 +244,7 @@ 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());
} }
} }
@ -264,7 +256,6 @@ namespace AppModule.NamedPipes
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
@ -274,6 +265,7 @@ namespace AppModule.NamedPipes
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 // copy low and high part
lowPart = stats.AuthenticationId.LowPart; lowPart = stats.AuthenticationId.LowPart;
highPart = stats.AuthenticationId.HighPart; highPart = stats.AuthenticationId.HighPart;
@ -292,38 +284,51 @@ 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();
Console.WriteLine("error" + error.ToString()); System.Diagnostics.Debug.WriteLine("CASA: GetTokenInformation NOT successful " + 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();
System.Diagnostics.Debug.WriteLine("CASA: Close userToken failed: {0}", errCode.ToString());
} }
else else
{ {
int lastError = Marshal.GetLastWin32Error(); userToken = IntPtr.Zero;
uint errorcode = NamedPipeNative.GetLastError();
Console.WriteLine("OpenThreadToken Error: "+ errorcode.ToString() + " code2: "+rcode.ToString());
} }
Marshal.FreeHGlobal(userToken); if (!ImpersonateNative.RevertToSelf())
if (userTokenElevated != IntPtr.Zero)
{ {
Marshal.FreeHGlobal(userTokenElevated); System.Diagnostics.Debug.WriteLine("CASA: RevertToSelf failed");
}
}
else
{
uint errorcode = NamedPipeNative.GetLastError();
System.Diagnostics.Debug.WriteLine("CASA: OpenThreadToken Error: " + errorcode.ToString());
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
int error = Marshal.GetLastWin32Error(); int error = Marshal.GetLastWin32Error();
Console.WriteLine(ex.ToString()); System.Diagnostics.Debug.WriteLine(ex.ToString());
return rcode; return rcode;
} }
// end // end

View File

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

View File

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

View File

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