通过低完整性进程访问MemoryMappedFile [英] Gaining access to a MemoryMappedFile from low-integrity process

查看:155
本文介绍了通过低完整性进程访问MemoryMappedFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在中等完整性进程上创建MemoryMappedFile,然后在低完整性子进程上打开相同的文件,并将此共享内存用于IPC.没有实际的磁盘文件(使用MemoryMappedFile.CreateNew).

I'm trying to create a MemoryMappedFile on a medium-integrity process, then open the same file on a low-integrity child process and use this shared memory for IPC. There's no real disk file (using MemoryMappedFile.CreateNew).

我的问题是低完整性进程无法打开共享内存,并引发以下错误:"System.UnauthorizedAccessException:对路径的访问被拒绝.".考虑到我要从低完整性进程进行写访问,我对此并不感到惊讶,但是如何授予它访问权限?

My problem is that the low-integrity process cannot open the shared memory, throwing this: "System.UnauthorizedAccessException: Access to the path is denied.". I'm not surprised that this is the case, given that I want write access from the low-integrity process, but how do you grant it access?

这是我的代码:

中等完整性过程:

MemoryMappedFileSecurity security = new MemoryMappedFileSecurity();
var file = MemoryMappedFile.CreateNew("test", 4096, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, security, HandleInheritability.Inheritable);

var view = file.CreateViewAccessor();
view.Write(0, true);

完整性较低的过程:

try
{
    MemoryMappedFile file = MemoryMappedFile.OpenExisting("test", MemoryMappedFileRights.ReadWrite);
    var view = file.CreateViewAccessor();
    var v = view.ReadBoolean(0);
    Log.Info("MAPPED: " + v);
}
catch (Exception e)
{
    Log.Info("Error: " + e);
}


如果两个过程均以中等完整性运行,则工作正常.阅读后,我尝试在中等完整性过程中设置SDDL字符串像这样:


Works fine if both processes work in medium integrity. After reading this, I tried setting the SDDL string on the medium integrity process like this:

security.SetSecurityDescriptorSddlForm("S:(ML;;NW;;;LW)");

但是,这给了我另一个例外,这一次是在创建内存映射文件时:"System.IO.IOException:客户端不拥有必需的特权."还是不太确定这是否是正确的方法,对于Win32/C ++示例如何转换为C#,我还不太清楚.

But that gives me another exception, this time when the memory mapped file is created: "System.IO.IOException: A required privilege is not held by the client.". Not really sure this is the right way to do it anyway, I'm not really clear on how the Win32/C++ examples translates to C#...

有人对此有更多了解吗?

Anyone know anything more about this?

推荐答案

好的,找到了可行的解决方案.有两个问题:

Okay, got a working solution. There were two problems:

  1. 将空的MemoryMappedFileSecurity对象传递给MemoryMappedFile.CreateNew()使得即使对于同一进程,也无法访问映射的内存.这就解释了我在评论中的错误("System.UnauthorizedAccessException:拒绝访问路径").

  1. Passing an empty MemoryMappedFileSecurity object to MemoryMappedFile.CreateNew() made the mapped memory inaccessible even to the same process. That explained my error in my comment ("System.UnauthorizedAccessException: Access to the path is denied").

我实际上无法使security.SetSecurityDescriptorSddlForm正常工作(尽管google公开了其他尝试,但没有一个对我有用).相反,我使用了以下解决方案: https://stackoverflow.com/a/14424623/5105846 .据我所知,它做同样的事情,只是改用了PInvoke.因此,我只调用了InterProcessSecurity.SetLowIntegrityLevel(file.SafeMemoryMappedFileHandle),它使它可以从低完整性子进程中访问.成功!

I couldn't actually get security.SetSecurityDescriptorSddlForm to work (and even though google reveals several other attempts at this, none of them worked for me). Instead, I used this solution: https://stackoverflow.com/a/14424623/5105846. As far as I can tell, it does the same thing, but using PInvoke instead. So I just called InterProcessSecurity.SetLowIntegrityLevel(file.SafeMemoryMappedFileHandle), and it made it accessible from the low-integrity child process. Success!

这不是完美的解决方案,但我现在需要的只是一个可行的解决方案.谢谢哈里的帮助!

Not the perfect solution, but a working one is all I need for now. Thanks Harry for your help!

这篇关于通过低完整性进程访问MemoryMappedFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆