Skip to content

API Proposal: Port MemoryMappedFileSecurity and add extensions for MemoryMappedFile #941

Open

Description

@JeremyKuhne

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

  1. terrajobst commented on Oct 10, 2019

    @terrajobst
    Contributor

    Video

    We should introduce a new static type in the same assembly as FileSystemAclExtensions. The pattern we settled on was suffixing the type with Acl.

    namespace System.IO.MemoryMappedFiles
    {
        public static class MemoryMappedFileAcl
        {
            public static MemoryMappedFile CreateFromFile(
                FileStream fileStream,
                string mapName,
                long capacity,
                MemoryMappedFileAccess access,
                MemoryMappedFileSecurity memoryMappedFileSecurity,
                HandleInheritability inheritability,
                bool leaveOpen);
    
            public static MemoryMappedFile CreateNew(
                string mapName,
                long capacity,
                MemoryMappedFileAccess access,
                MemoryMappedFileOptions options,
                MemoryMappedFileSecurity memoryMappedFileSecurity,
                HandleInheritability inheritability);
    
            public static MemoryMappedFile CreateOrOpen(
                string mapName,
                long capacity,
                MemoryMappedFileAccess access,
                MemoryMappedFileOptions options,
                MemoryMappedFileSecurity memoryMappedFileSecurity,
                HandleInheritability inheritability);
    
            // Those are fine as extensions:
    
            public static MemoryMappedFileSecurity GetAccessControl(
                this MemoryMappedFile memoryMappedFile);
    
            public static void SetAccessControl(
                this MemoryMappedFile memoryMappedFile,
                MemoryMappedFileSecurity memoryMappedFileSecurity);
        }
    }
  2. carlossanlop commented on Nov 21, 2019

    @carlossanlop
    Contributor

    @terrajobst your comment with the approved APIs does not mention adding the MemoryMappedFileSecurity class, 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.

  3. transferred this issue fromdotnet/corefxon Dec 16, 2019
  4. added
    untriaged
    on Dec 16, 2019
  5. added
    api-approved
    and removed
    untriaged
    on Dec 16, 2019
  6. added this to the 5.0 milestone on Dec 16, 2019
  7. carlossanlop commented on Dec 16, 2019

    @carlossanlop
    Contributor

    @terrajobst @bartonjs @stephentoub @GrabYourPitchforks @JeremyKuhne @jkotas ,

    We need to add this API to this proposal and make it public:

    private MemoryMappedFile(SafeMemoryMappedFileHandle handle)

    As you can see in the current PR discussion, we cannot create the MemoryMappedFile object 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 where MemoryMappedFiles lives (System.IO.MemoryMappedFiles).

    For some context, the other new ACL APIs did not have this problem, because:

    • For FileInfo and DirectoryInfo, we were working within the same assembly System.IO, so we were able to call the necessary internal methods that lived in FileSystem or in FileStream.
    • For EventWaitHandle, Mutex and Semaphore, 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.
    • For 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.
  8. carlossanlop commented on Dec 17, 2019

    @carlossanlop
    Contributor

    @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 AccessRule and an AuditRule. We need them for MemoryMappedFiles too:

    namespace System.IO
    {
        public sealed class MemoryMappedFileAccessRule : System.Security.AccessControl.AccessRule
        {
            public MemoryMappedFileAccessRule(System.Security.Principal.IdentityReference identity, System.IO.MemoryMappedFiles.MemoryMappedFileRights eventRights, System.Security.AccessControl.AccessControlType type) : base(default(System.Security.Principal.IdentityReference), default(int), default(bool), default(System.Security.AccessControl.InheritanceFlags), default(System.Security.AccessControl.PropagationFlags), default(System.Security.AccessControl.AccessControlType)) { }
            public MemoryMappedFileAccessRule(string identity, System.IO.MemoryMappedFiles.MemoryMappedFileRights eventRights, System.Security.AccessControl.AccessControlType type) : base(default(System.Security.Principal.IdentityReference), default(int), default(bool), default(System.Security.AccessControl.InheritanceFlags), default(System.Security.AccessControl.PropagationFlags), default(System.Security.AccessControl.AccessControlType)) { }
    
            public MemoryMappedFileAccessRule(System.Security.Principal.IdentityReference identity, int accessMask, bool isInherited, System.Security.AccessControl.InheritanceFlags inheritanceFlags, System.Security.AccessControl.PropagationFlags propagationFlags, System.Security.AccessControl.AccessControlType type) : base(default(System.Security.Principal.IdentityReference), default(int), default(bool), default(System.Security.AccessControl.InheritanceFlags), default(System.Security.AccessControl.PropagationFlags), default(System.Security.AccessControl.AccessControlType)) { }
            public System.IO.MemoryMappedFiles.MemoryMappedFileRights MemoryMappedFileRights { get; }
        }
        public sealed class MemoryMappedFileAuditRule : System.Security.AccessControl.AuditRule
        {
            public MemoryMappedFileAuditRule(System.Security.Principal.IdentityReference identity, System.IO.MemoryMappedFiles.MemoryMappedFileRights eventRights, System.Security.AccessControl.AuditFlags flags) : this(default(System.Security.Principal.IdentityReference), default(int), default(bool), default(System.Security.AccessControl.InheritanceFlags), default(System.Security.AccessControl.PropagationFlags), default(System.Security.AccessControl.AuditFlags)) { }
            public MemoryMappedFileAuditRule(System.Security.Principal.IdentityReference identity, int accessMask, bool isInherited, System.Security.AccessControl.InheritanceFlags inheritanceFlags, System.Security.AccessControl.PropagationFlags propagationFlags, System.Security.AccessControl.AuditFlags auditFlags) : base(default(System.Security.Principal.IdentityReference), default(int), default(bool), default(System.Security.AccessControl.InheritanceFlags), default(System.Security.AccessControl.PropagationFlags), default(System.Security.AccessControl.AuditFlags)) { }
            public System.IO.MemoryMappedFiles.MemoryMappedFileRights MemoryMappedFileRights { get; }
        }
    }
  9. JeremyKuhne commented on Dec 17, 2019

    @JeremyKuhne
    MemberAuthor

    We also need to include two new classes to the API proposal

    Why do we need these? Can you provide more context/links?

  10. carlossanlop commented on Dec 17, 2019

    @carlossanlop
    Contributor

    You can see in my PR that the new MemoryMappedFileSecurity class needs to define two properties: AccessRuleType and AuditRuleType.

    All the previous ACL changes already had Security/AuditRule/AccessRule classes defined.

    The AuditRule and AccessRule objects allow setting the rights and the access control of the security object. For example, in the MutexSecurity PR, I added unit tests that verify the correct values for these objects.

  11. removed
    api-approved
    on Dec 17, 2019
  12. 4 remaining items

  13. added
    untriaged
    on Feb 23, 2020
  14. removed
    untriaged
    on Feb 26, 2020
  15. removed their assignment
    on Mar 25, 2020
  16. carlossanlop commented on Apr 14, 2020

    @carlossanlop
    Contributor

    We are closing the PR. Will reopen once there is a customer request for this.

  17. RogueAtom118 commented on Dec 10, 2020

    @RogueAtom118

    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)

  18. ghost locked as resolved and limited conversation to collaborators on Jan 9, 2021
  19. adamsitnik commented on Nov 23, 2022

    @adamsitnik
    Member

    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.

  20. carlossanlop commented on Jan 26, 2024

    @carlossanlop
    Contributor

    Reopening. Besides the external request in the comment above, there's also an additional internal request, so it's worth pursuing.

  21. carlossanlop commented on Jan 26, 2024

    @carlossanlop
    Contributor

    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) .

  22. modified the milestones: 5.0.0, Future on Feb 6, 2024
  23. removed their assignment
    on Jan 29, 2025
  24. unlocked this conversation on Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workarea-System.IO

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions