Skip to content

Commit e15d7b3

Browse files
committed
Fixes an issue where global mutexes were created with insuficient rights litedb-org#2206
1 parent 3792e82 commit e15d7b3

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

LiteDB/Client/Shared/SharedEngine.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
using System;
33
using System.Collections.Generic;
44
using System.IO;
5-
using System.Threading;
6-
#if NETFRAMEWORK
75
using System.Security.AccessControl;
86
using System.Security.Principal;
9-
#endif
7+
using System.Threading;
108

119
namespace LiteDB
1210
{
@@ -25,24 +23,30 @@ public SharedEngine(EngineSettings settings)
2523

2624
try
2725
{
28-
#if NETFRAMEWORK
29-
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
30-
MutexRights.FullControl, AccessControlType.Allow);
31-
32-
var securitySettings = new MutexSecurity();
33-
securitySettings.AddAccessRule(allowEveryoneRule);
34-
35-
_mutex = new Mutex(false, "Global\\" + name + ".Mutex", out _, securitySettings);
36-
#else
37-
_mutex = new Mutex(false, "Global\\" + name + ".Mutex");
38-
#endif
26+
_mutex = CreateMutex(name);
3927
}
4028
catch (NotSupportedException ex)
4129
{
4230
throw new PlatformNotSupportedException("Shared mode is not supported in platforms that do not implement named mutex.", ex);
4331
}
4432
}
4533

34+
private static Mutex CreateMutex(string name)
35+
{
36+
if (!OperatingSystem.IsWindows())
37+
{
38+
return new Mutex(false, "Global\\" + name + ".Mutex");
39+
}
40+
41+
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
42+
MutexRights.FullControl, AccessControlType.Allow);
43+
44+
var securitySettings = new MutexSecurity();
45+
securitySettings.AddAccessRule(allowEveryoneRule);
46+
47+
return MutexAcl.Create(false, "Global\\" + name + ".Mutex", out _, securitySettings);
48+
}
49+
4650
/// <summary>
4751
/// Open database in safe mode
4852
/// </summary>

LiteDB/LiteDB.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
<ItemGroup>
4949
<PackageReference Include="System.Buffers" Version="4.5.1"/>
50+
<PackageReference Include="System.Threading.AccessControl" Version="6.0.0" />
5051
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.25">
5152
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
5253
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)