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;
}
#if DEBUG
static int PerformDump(HANDLE token)
{
StringBuilder sb = new StringBuilder();
@ -80,14 +80,11 @@ namespace AppModule.NamedPipes
}
tokUser = (ImpersonateNative.TOKEN_USER) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_USER) );
//sb.Append(DumpAccountSid(tokUser.User.Sid));
IntPtr pUserID = tokUser.User.Sid;
//Console.WriteLine("UserID: " + pUserID);
DumpAccountSid(pUserID);
Marshal.FreeHGlobal( tu );
tu = Marshal.AllocHGlobal(bufLength);
cb = bufLength;
@ -155,6 +152,7 @@ namespace AppModule.NamedPipes
Console.WriteLine("Acct info: "+ sb.ToString());
return sb.ToString();
}
#endif
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);
try
{
ptrLinkedToken = Marshal.AllocHGlobal(TokenInfLength);
@ -206,7 +203,7 @@ namespace AppModule.NamedPipes
if (code == 0)
{
uint lastError = NamedPipeNative.GetLastError();
Console.WriteLine("ImpersonateNamedPipeClient Error: "+rcode.ToString());
System.Diagnostics.Debug.WriteLine("CASA: ImpersonateNamedPipeClient Error: " + rcode.ToString());
return -1;
}
@ -214,7 +211,7 @@ namespace AppModule.NamedPipes
{
IntPtr hThread = ImpersonateNative.GetCurrentThread();
uint iDesiredInfo = 24; //TOKEN_QUERY | TOKEN_QUERY_SOURCE;
IntPtr userToken = Marshal.AllocHGlobal(4);
IntPtr userToken = IntPtr.Zero;
IntPtr userTokenElevated = IntPtr.Zero;
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.
System.OperatingSystem os = System.Environment.OSVersion;
System.Diagnostics.Trace.WriteLine("OS Version: " + os.Version.ToString());
if (os.Version.Major > 5)
{
if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenElevationType, tu, cb, ref cb))
@ -235,16 +232,11 @@ namespace AppModule.NamedPipes
int iTokenType;
iTokenType = (int)Marshal.PtrToStructure(tu, typeof(int));
System.Diagnostics.Trace.WriteLine("Token Type: " + iTokenType.ToString());
if (iTokenType == (int)ImpersonateNative.TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited)
{
System.Diagnostics.Trace.WriteLine("Getting linked token");
ImpersonateNative.TOKEN_LINKED_TOKEN newLinkedToken;
if (GetLinkedToken(userToken, out newLinkedToken))
{
//userToken = newLinkedToken.LinkedToken;
userTokenElevated = Marshal.AllocHGlobal(4);
userTokenElevated = newLinkedToken.LinkedToken;
}
}
@ -252,7 +244,7 @@ namespace AppModule.NamedPipes
else
{
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);
// get SID
//string SidString = null;
ImpersonateNative.ConvertSidToStringSid(pUserID, ref SidString);
// get token states
@ -274,6 +265,7 @@ namespace AppModule.NamedPipes
if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenStatistics, tu, cb, ref cb))
{
stats = (ImpersonateNative.TOKEN_STATISTICS)Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_STATISTICS));
// copy low and high part
lowPart = stats.AuthenticationId.LowPart;
highPart = stats.AuthenticationId.HighPart;
@ -292,38 +284,51 @@ namespace AppModule.NamedPipes
highPartElevated = stats.AuthenticationId.HighPart;
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
{
Console.WriteLine("GetTokenInformation NOT successful");
uint error = NamedPipeNative.GetLastError();
Console.WriteLine("error" + error.ToString());
System.Diagnostics.Debug.WriteLine("CASA: GetTokenInformation NOT successful " + error.ToString());
}
Marshal.FreeHGlobal(tu);
// close handle
ImpersonateNative.CloseHandle(hThread);
ImpersonateNative.RevertToSelf();
// close handles
if (!ImpersonateNative.CloseHandle(userToken))
{
uint errCode = ImpersonateNative.GetLastError();
System.Diagnostics.Debug.WriteLine("CASA: Close userToken failed: {0}", errCode.ToString());
}
else
{
int lastError = Marshal.GetLastWin32Error();
uint errorcode = NamedPipeNative.GetLastError();
Console.WriteLine("OpenThreadToken Error: "+ errorcode.ToString() + " code2: "+rcode.ToString());
userToken = IntPtr.Zero;
}
Marshal.FreeHGlobal(userToken);
if (userTokenElevated != IntPtr.Zero)
if (!ImpersonateNative.RevertToSelf())
{
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)
{
int error = Marshal.GetLastWin32Error();
Console.WriteLine(ex.ToString());
System.Diagnostics.Debug.WriteLine(ex.ToString());
return rcode;
}
// end

View File

@ -896,7 +896,7 @@ namespace sscs.cache
}
catch (Exception e)
{
CSSSLogger.ExpLog(e.ToString());
CSSSLogger.DbgLog("Desktop password not set");
}
return null;
}

View File

@ -37,7 +37,7 @@ namespace sscs.communication.win {
public Hashtable Pipes;
private uint NumberPipes = 16;
private uint NumberPipes = 1024;
private uint OutBuffer = 65536; //512;
private uint InBuffer = 65536; //512;
private const int MAX_READ_BYTES = 15000;

View File

@ -594,7 +594,7 @@ void RemoveServerSecret()
RemoveSecret(SSCS_CRED_TYPE_SERVER_F);
}
void RunTest()
void RunTest(char *SecretID)
{
{
SSCS_BASIC_CREDENTIAL credential = {0};
@ -604,7 +604,8 @@ void RunTest()
int rcode = 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;
credential.unFlags = USERNAME_TYPE_CN_F;
@ -765,6 +766,7 @@ void RunTests()
int iCount = 1;
int iTemp = 0;
int i = 0;
char inputID[20];
printf("Enter number interations to run (default 1): ");
gets(runtimes);
@ -773,9 +775,13 @@ void RunTests()
if (iTemp > 1)
iCount=iTemp;
printf("Enter secretID: ");
gets(inputID);
for (i=0; i<iCount; i++)
{
RunTest();
printf("Count: %d\r\n", i);
RunTest(&inputID);
printf("\r\n");
}
}