Repository navigation
Description
Summary
We did not port System.IO.MemoryMappedFiles.MemoryMappedFileSecurity to Core as it wasn't cross-plat. There is still a need to set security on memory mapped files for Windows, just as there is for files/directories/etc. We should port these to System.IO.FileSystem.AccessControl and add extension methods for MemoryMappedFile there so Windows specific codebases can be ported and retain their security model.
Proposal
Port the following types from .NET Framework to Core and add them to System.IO.FileSystem.AccessControl:
namespace System.IO.MemoryMappedFiles
{
// See enum's full definition in .NET Framework
[Flags]
public enum MemoryMappedFileRights;
public class MemoryMappedFileSecurity : ObjectSecurity<MemoryMappedFileRights>
{
public MemoryMappedFileSecurity();
}
}Add the following extension methods for MemoryMappedFile to System.IO.FileSystemAclExtensions:
namespace System.IO
{
public static class FileSystemAclExtensions
{
public static MemoryMappedFile CreateFromFile(
this MemoryMappedFileSecurity memoryMappedFileSecurity,
FileStream fileStream,
string mapName,
long capacity,
MemoryMappedFileAccess access,
HandleInheritability inheritability,
bool leaveOpen);
public static MemoryMappedFile CreateNew(
this MemoryMappedFileSecurity memoryMappedFileSecurity,
string mapName,
long capacity,
MemoryMappedFileAccess access,
MemoryMappedFileOptions options,
HandleInheritability inheritability);
public static MemoryMappedFile CreateOrOpenMappedFile(
this MemoryMappedFileSecurity memoryMappedFileSecurity,
string mapName,
long capacity,
MemoryMappedFileAccess access,
MemoryMappedFileOptions options,
HandleInheritability inheritability);
public static MemoryMappedFileSecurity GetAccessControl(
this MemoryMappedFile memoryMappedFile);
public static void SetAccessControl(
this MemoryMappedFile memoryMappedFile,
MemoryMappedFileSecurity memoryMappedFileSecurity);
}
}Details
We could create a new extensions class, but this still can logically fit with "file system". Same goes for the assembly- there isn't enough value in creating a new assembly for this.
Creating a non-extension static class is also possible, but I'd rather try to come up with a more generic security model that can abstract platform differences and add a more x-plat friendly model. Hanging platform specific helpers off of platform specific types contains this somewhat.
I didn't make more descriptive names as these match exactly with what is missing on MemoryMappedFile. Theory is it helps discovery.
Note that the implementation of MemoryMappedFileSecurity is trivial, it adds nothing to ObjectSecurity<T> other than the T. Implementing the extension methods is a little more complicated- we may want to try and factor out a more generic set of CreateFile helpers that take NativeObjectSecurity to reduce duplication.
Related Issues
dotnet/corefx#29546 MemoryMappedFile is missing CreateFromFile overload that takes MemoryMappedFileSecurity
dotnet/corefx#41614 API Proposal: Add file and directory creation methods that take an ACL
CC: @danmosemsft, @ericstj, @terrajobst
Activity
terrajobst commentedon Oct 10, 2019
Video
We should introduce a new static type in the same assembly as
FileSystemAclExtensions. The pattern we settled on was suffixing the type withAcl.carlossanlop commentedon Nov 21, 2019
@terrajobst your comment with the approved APIs does not mention adding the
MemoryMappedFileSecurityclass, which does not exist in Core and needs to be ported from Framework.Edit: From an email conversation, Immo confirmed that existing Framework APIs do not need to be mentioned in the API approval to get ported.
carlossanlop commentedon Dec 16, 2019
@terrajobst @bartonjs @stephentoub @GrabYourPitchforks @JeremyKuhne @jkotas ,
We need to add this API to this proposal and make it public:
runtime/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.cs
Line 18 in 4f9ae42
As you can see in the current PR discussion, we cannot create the
MemoryMappedFileobject with the currently available public surface area. The only option we have is to create the object via reflection, by calling the private constructor that receives the handle, which is inefficient.We cannot call the private constructor because the ACL APIs live in a different assembly (
System.IO.FileSystem.AccessControl) than whereMemoryMappedFileslives (System.IO.MemoryMappedFiles).For some context, the other new ACL APIs did not have this problem, because:
FileInfoandDirectoryInfo, we were working within the same assemblySystem.IO, so we were able to call the necessary internal methods that lived in FileSystem or in FileStream.EventWaitHandle,MutexandSemaphore, those clases exposed their internal safe handle via a property getter, so we were able to create a handle with the proper security, then create the synchronization object, then switch the old handle with the secured handle.AnonymousPipeServerStream, we added a new internal constructor that received the security object, and we were able to call it because the new APIs were added within the same assembly.carlossanlop commentedon Dec 17, 2019
@terrajobst @bartonjs @stephentoub @GrabYourPitchforks @JeremyKuhne @jkotas ,
We also need to include two new classes to the API proposal, they were not mentioned in the main description. They did not exist in Framework, the concept is new in Core. All the ACL changes we have already fixed had similar classes: an
AccessRuleand anAuditRule. We need them forMemoryMappedFilestoo:JeremyKuhne commentedon Dec 17, 2019
Why do we need these? Can you provide more context/links?
carlossanlop commentedon Dec 17, 2019
You can see in my PR that the new
MemoryMappedFileSecurityclass needs to define two properties:AccessRuleTypeandAuditRuleType.All the previous ACL changes already had Security/AuditRule/AccessRule classes defined.
The
AuditRuleandAccessRuleobjects allow setting the rights and the access control of the security object. For example, in theMutexSecurityPR, I added unit tests that verify the correct values for these objects.4 remaining items
carlossanlop commentedon Apr 14, 2020
We are closing the PR. Will reopen once there is a customer request for this.
RogueAtom118 commentedon Dec 10, 2020
I feel being able to set access permissions for MemoryMappedFiles (shared memory) is needed in ".NET5". I'm currently using MemoryMappedFiles to communicate with my Blazor Server (using CreateOrOpen). In ".NET Framework 4.8" I simply pass the MemoryMappedFileSecurity parameter giving my Blazor Server access to my shared memory. Can we add this capability to ".NET5-windows" please? Any advice? (btw I need the high performance of shared memory)
adamsitnik commentedon Nov 23, 2022
The need for this feature was asked internally within MS. To unblock the customer, I've copy-pasted the old code from .NET Framework, polished it a bit and tested with custom security descriptors. You can find it here.
carlossanlop commentedon Jan 26, 2024
Reopening. Besides the external request in the comment above, there's also an additional internal request, so it's worth pursuing.
carlossanlop commentedon Jan 26, 2024
The original attempt to porting the APIS was in this PR #465 . Instead of attempting to bring the missing additional constructor #33206 , we can follow the suggestions provided by @jkotas in this comment: #465 (comment) .