-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Specify a security descriptor when using global Mutex #110416
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Security.AccessControl; | ||
using System.Security.Principal; | ||
using System.Threading; | ||
using Xunit; | ||
|
||
namespace System.Diagnostics.Tests | ||
{ | ||
public static class MutexTests | ||
{ | ||
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInAppContainer))] // Can't create global objects in appcontainer | ||
public static void VerifySecurityIdentifier() | ||
{ | ||
string mutexName = $"{Guid.NewGuid():N}"; | ||
|
||
Mutex mutex = null; | ||
|
||
// We can't test with the same global mutex used by performance counters, since the mutex was likely already | ||
// initialized elsewhere and perhaps with with different ACLs, so we use a Guid to create a new mutex and | ||
// then simulate the behavior by calling into EnterMutex() like the performance monitor code does. | ||
#pragma warning disable CS0436 // Type conflicts with imported type | ||
NetFrameworkUtils.EnterMutex(mutexName, ref mutex); | ||
Comment on lines
+20
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is just testing the utility code without invoking the product at all? Could we instead do something to ensure the product code creates the Mutex, then open the Mutex created by the product and examine the ACL? I think the test should be able to access it since it's running as the same user. |
||
#pragma warning restore CS0436 | ||
|
||
try | ||
{ | ||
// This is the SID that is added by EnterMutex(). | ||
SecurityIdentifier authenticatedUserSid = new(WellKnownSidType.AuthenticatedUserSid, null); | ||
|
||
MutexSecurity security = mutex.GetAccessControl(); | ||
AuthorizationRuleCollection rules = security.GetAccessRules(true, true, typeof(SecurityIdentifier)); | ||
Assert.Equal(1, rules.Count); | ||
MutexAccessRule accessRule = (MutexAccessRule)rules[0]; | ||
SecurityIdentifier sid = (SecurityIdentifier)accessRule.IdentityReference; | ||
Assert.Equal(authenticatedUserSid, sid); | ||
} | ||
finally | ||
{ | ||
if (mutex != null) | ||
{ | ||
mutex.ReleaseMutex(); | ||
mutex.Close(); | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new dependency will also need to go into the ASP.NET shared framework:
runtime/src/libraries/NetCoreAppLibrary.props
Line 223 in 9a02107
Looks like it was already present in WindowsDesktop:
runtime/src/libraries/NetCoreAppLibrary.props
Line 242 in 9a02107