Repository navigation
Port MemoryMappedFileSecurity and add extensions for MemoryMappedFile - #465
carlossanlop wants to merge 4 commits into
Conversation
|
@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. |
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.
| } | ||
| 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)) { } |
There was a problem hiding this comment.
Are these formatting changes intentional? IIRC, /t:GenerateReferenceSource generates extra space after base.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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" /> |
There was a problem hiding this comment.
This is again dependency from inbox binary to a binary shipping as package. Are all combinations of this going to work well?
There was a problem hiding this comment.
I think this is fine. System.Security.AccessControl is OOB only for the ref assembly but the implementation is part of the shared framework:
Given that we have some inbox projects already depending on it:
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) { } |
There was a problem hiding this comment.
Do we really need internal APIs in ref file?
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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" /> |
There was a problem hiding this comment.
System.Resources.ResourceManager should not be needed
|
|
||
| namespace System.IO.MemoryMappedFiles | ||
| { | ||
| public static class MemoryMappedFileAcl |
There was a problem hiding this comment.
Would it be better to let these implementations to be auto-generated in System.IO.FileSystem.AccessControl with a better PlatformNotSupportedException?
There was a problem hiding this comment.
What's the command to auto generate the files?
There was a problem hiding this comment.
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.
|
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 |
|
Why do we need the API with that problematic |
@jkotas, is your question why do we need the overload that takes a
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.
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? |
When the user calls If the user called If the user called |
|
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. |
|
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. |
Ok, I have not realized that. I agree that both should be on the same plan. |
Those were options 2.1 and 2.2. These were the options in Immo's approval comment:
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 Should I attempt option 2.1 instead? |
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. |
|
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. |
|
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. Only once we have that we should be looking at the specialized construction paths, like the path that takes ACLs in this PR. |
|
I will close the PR. We can come back to it if a customer requests it. |
|
OK, thanks @carlossanlop |
Fixes #941
We did not port
System.IO.MemoryMappedFiles.MemoryMappedFileSecurityto 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 toSystem.IO.FileSystem.AccessControland add extension methods forMemoryMappedFilethere so Windows specific codebases can be ported and retain their security model.