Skip to content

Port MemoryMappedFileSecurity and add extensions for MemoryMappedFile - #465

Closed
carlossanlop wants to merge 4 commits into
dotnet:masterfrom
carlossanlop:MemoryMappedFileAcl
Closed

carlossanlop wants to merge 4 commits into
dotnet:masterfrom
carlossanlop:MemoryMappedFileAcl

Conversation

@carlossanlop

@carlossanlop carlossanlop commented Dec 3, 2019 •

Copy link
Copy Markdown
Contributor

Fixes #941

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.

@carlossanlop carlossanlop added this to the 5.0 milestone Dec 3, 2019
@carlossanlop carlossanlop self-assigned this Dec 3, 2019
@stephentoub

Copy link
Copy Markdown
Member

@carlossanlop, why is this marked as a draft?

@carlossanlop
carlossanlop marked this pull request as ready for review February 18, 2020 00:42
@carlossanlop

Copy link
Copy Markdown
Contributor Author

@carlossanlop, why is this marked as a draft?

@stephentoub it wasn't ready before my last commit. I just forgot to mark it as ready for review. It is (mostly) ready now. Thanks for noticing.

@stephentoub

Copy link
Copy Markdown
Member

it wasn't ready before my last commit. I just forgot to mark it as ready for review.

Ok, thanks. Why are we opening PRs at all before they're ready for review?

…t takes an IDisposable, add unit tests to ensure security objects are verified after MMF creation.

Fix minor unit test failures

Remove unnecessary leaveOpen in new MemoryMappedFile constructor. Add unit test that disposes FileStream after creating the MemoryMappedFile, but before reading the MemoryMappedFile contents.

Fix unit tests that fail or not fail when security is null.
…MemoryMappedFiles. Expose the refs in FileSystem.AccessControl.
@carlossanlop
carlossanlop requested a review from jkotas April 13, 2020 18:55
}
public sealed partial class FileSystemAccessRule : System.Security.AccessControl.AccessRule
{
public FileSystemAccessRule(System.Security.Principal.IdentityReference identity, System.Security.AccessControl.FileSystemRights fileSystemRights, 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)) { }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these formatting changes intentional? IIRC, /t:GenerateReferenceSource generates extra space after base.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not execute a command using /t:GenerateReferenceSource. What's the full command I should run?
I probably just need to revert the change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After building the src and having the src shape that you want you need to run dotnet msbuild /t:GenerateReferenceSource in the source project.

<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Threading" />
<Reference Include="System.Runtime" />
<Reference Include="System.Security.AccessControl" />

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is again dependency from inbox binary to a binary shipping as package. Are all combinations of this going to work well?

@safern safern Apr 13, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine. System.Security.AccessControl is OOB only for the ref assembly but the implementation is part of the shared framework:

<IsNETCoreApp>true</IsNETCoreApp>
<IsNETCoreAppRef>false</IsNETCoreAppRef>

Given that we have some inbox projects already depending on it:

<Reference Include="System.Security.AccessControl" />

If I remember correctly we made AccessControl that way specifically to be able to reference it from inbox binaries.
cc: @ericstj

public partial class MemoryMappedFile : System.IDisposable
{
internal MemoryMappedFile() { }
internal MemoryMappedFile(Microsoft.Win32.SafeHandles.SafeMemoryMappedFileHandle handle) { }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need internal APIs in ref file?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The zero argument one, yeah, since it makes the type not appear to have a default public ctor. The other two I agree shouldn't be here.

Is this file being hand-edited, vs generated with dotnet msbuild /t:GenerateReferenceSource from the src directory (after building the src directory)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was hand edited. I did not know I had to run that command. I'll do that now.

<ItemGroup>
<Reference Include="System.IO.FileSystem" />
<Reference Include="System.Runtime" />
<Reference Include="System.Resources.ResourceManager" />

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.Resources.ResourceManager should not be needed


namespace System.IO.MemoryMappedFiles
{
public static class MemoryMappedFileAcl

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to let these implementations to be auto-generated in System.IO.FileSystem.AccessControl with a better PlatformNotSupportedException?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the command to auto generate the files?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have a way to auto-generate only a list of APIs, we can only auto-generate the not supported assemblies using GenAPI at compile time for all the APIs defined in the ref assembly. @ericstj can correct me if I'm wrong.

@jkotas

jkotas commented Apr 13, 2020

Copy link
Copy Markdown
Member

I do not think it is a good trade-off to introduce all this forwarding, layering inversion between ref and src, etc. just to avoid introducing one extra public API to make this work cleanly.

@carlossanlop

Copy link
Copy Markdown
Contributor Author

I do not think it is a good trade-off to introduce all this forwarding, layering inversion between ref and src, etc. just to avoid introducing one extra public API to make this work cleanly.

The most recent changes would address the first suggested option in the API Review. I had other 2 suggestions, before applying the third one. Would you agree with one of the other two options? cc @stephentoub @bartonjs

@jkotas

jkotas commented Apr 13, 2020

Copy link
Copy Markdown
Member

Why do we need the API with that problematic leaveOpen argument at all?

@stephentoub

Copy link
Copy Markdown
Member

Why do we need the API with that problematic leaveOpen argument at all?

@jkotas, is your question why do we need the overload that takes a FileStream? From my perspective, if we provide one that takes a FileStream, we'd need to either provide the leaveOpen argument, or not provide it and have the default behavior be leaveOpen == false, which is the default behavior in other APIs when such a leaveOpen isn't specified; from a consistency standpoint, it'd be concerning to me to have the behavior be equivalent to leaveOpen == true... and it's the leaveOpen == false behavior that's more challenging here. If the question was about having the FileStream-based overload at all, I'll let @carlossanlop answer; I'm not familiar with the exact customer requests driving adding these APIs.

I do not think it is a good trade-off to introduce all this forwarding, layering inversion between ref and src, etc. just to avoid introducing one extra public API to make this work cleanly.

Don't we already have the same layering in the equivalent APIs for System.IO.Pipes? Seems desirable to employ the same solution in both places; if we don't want to go the expose-it-from-a-higher-level-ref-but-implement-it-in-the-main-library approach, we should ideally change it in Pipes, too.

The most recent changes would address the first suggested option in the API Review. I had other 2 suggestions, before applying the third one. Would you agree with one of the other two options?

IIRC, the others were actually the preferred approach as discussed in the API review, e.g. unseal SafeMemoryMappedFileHandle, and have an internal derived type in the access control library that stores the FileStream and overrides ReleaseHandle to Dispose of it as well. Then the MemoryMappedFile ctor would just take a SafeMemoryMappedFileHandle.

That said, looking at it again, it seems problematic to add any such constructor. MemoryMappedFile currently doesn't have any constructors, instead having the various CreateXx methods. Adding a new ctor, IMHO, is going to make the type harder to use, because it'll then have a constructor, albeit a very complicated one that no one should be using, and it'll make it less likely that folks will understand they need to use the factories. We should probably follow-suit, and have a CreateFromHandle method, or something similarly named.

Taking a step back, how important are these new APIs at all? Are we adding these just for completeness, or are there developers that have been asking for these for real needs?

@carlossanlop

Copy link
Copy Markdown
Contributor Author

Why do we need the API with that problematic leaveOpen argument at all?

When the user calls MemoryMappedFile.CreateFromFile we need to root the passed fileStream.

If the user called CreateFromFile with leaveOpen = false, then the rooted fileStream needs to be disposed when the MemoryMappedFile is disposed.

If the user called CreateFromFile with leaveOpen = true, the rooted fileStream will not get disposed when the MemoryMappedFile is disposed, but we ensured it will remain alive because it was rooted.

@jkotas

jkotas commented Apr 14, 2020

Copy link
Copy Markdown
Member

Why can't we have just the API that leaves the FileStream alone (just gets the handle from it) and always make the user responsible for disposing the FileStream?

@stephentoub

Copy link
Copy Markdown
Member

Why can't we have just the API that leaves the FileStream alone (just gets the handle from it) and always make the user responsible for disposing the FileStream?

Pretty much every other API in the framework that takes a Stream takes ownership of the stream by default, unless a leaveOpen parameter is used and set to true.

@jkotas

jkotas commented Apr 14, 2020

Copy link
Copy Markdown
Member

If we are taking ownership of the stream, can we close it as soon as we get the handle out of it and not wait until the memory mapping is closed?

@stephentoub

Copy link
Copy Markdown
Member

If we are taking ownership of the stream, can we close it as soon as we get the handle out of it and not wait until the memory mapping is closed?

That would differ in behavior from the existing almost-identical overloads that take a FileStream (and leaveOpen) and just don't take the ACL-related args. It'd be observably inconsistent to differ in that regard. But whether it would cause anyone problems, I don't know.

@jkotas

jkotas commented Apr 14, 2020

Copy link
Copy Markdown
Member

Don't we already have the same layering in the equivalent APIs for System.IO.Pipes?

Ok, I have not realized that. I agree that both should be on the same plan.

@carlossanlop

carlossanlop commented Apr 14, 2020 •

Copy link
Copy Markdown
Contributor Author

@stephentoub

IIRC, the others were actually the preferred approach as discussed in the API review, e.g. unseal SafeMemoryMappedFileHandle

Those were options 2.1 and 2.2. These were the options in Immo's approval comment:

We're fine with taking the handle parameter but we have concerns with the fileStream parameter because it seems plumbing, not part of a sensible API.
Option 1: Move the implementation from the higher level assembly to the assembly containing MemoryMappedFile (while the ref assembly setup)
Option 2.1 "Smuggle" the file stream via the SafeMemoryMappedFileHandle via a field
Option 2.2 "Smuggle" the file stream via the SafeMemoryMappedFileHandle unsealing & deriving from SafeMemoryMappedFileHandle
Option 3: do what's proposed

how important are these new APIs at all? Are we adding these just for completeness, or are there developers that have been asking for these for real needs?

These ACL APIs are being added for completeness. @JeremyKuhne created API Proposals for all the types that had similar ACL needs. All others have been merged, and this one for MemoryMappedFile had the lowest priority because there was no user request.

Should I attempt option 2.1 instead?

@stephentoub

Copy link
Copy Markdown
Member

because there was no user request

If literally no one wants it, we shouldn't spend any more time on it ;-)

@danmoseley

Copy link
Copy Markdown
Contributor

If literally no one wants it, we shouldn't spend any more time on it ;-)

I'd potentially agree if we didn't support use of ACL's on MemoryMappedFiles, but in this case we are currently offering only an API that invites insecure use. We shouldn't need a customer request to add back API to make it possible to do the job securely. As it stands, a successful port to .NET Core of code using ACL's on MMF (apisof.net shows folks have run the apiport tool on such code) may necessitate changing the code to make it less secure.

@danmoseley

Copy link
Copy Markdown
Contributor

Having said that -- if we think doing this is not going to help those customers be successful (eg because we're not happy with the API) -- then I certainly am not arguing to do the wrong thing 😄 I think it would be better to sit on the problem until we feel confident we can do something useful.

@jkotas

jkotas commented Apr 14, 2020

Copy link
Copy Markdown
Member

There are many ways to obtain file mapping handle: opening or creating it with special security permissions, or creating it using a specialized API (e.g. CreateFileMappingNuma), or get it by cloning out of process handle, or ... . The first thing we should be doing in situation like this is to build the simplest possible extension point that allows people to be successful: Add a constructor that takes SafeMemoryMappedFileHandle that covers all these cases. People may need to use P/Invoke to obtain the desired file mapping handle, but the rest will be handled by the framework.

Only once we have that we should be looking at the specialized construction paths, like the path that takes ACLs in this PR.

@carlossanlop

Copy link
Copy Markdown
Contributor Author

I will close the PR. We can come back to it if a customer requests it.

@danmoseley

Copy link
Copy Markdown
Contributor

OK, thanks @carlossanlop

@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API Proposal: Port MemoryMappedFileSecurity and add extensions for MemoryMappedFile

7 participants