I have two Windows services that should share data with each other, and I intend to use SharedMemory (FileMapping) for such purpose. Another process is running in the user space that also should be able to read from that memory.
Well, the FileMapping is created in one service and can be accessed for read from both the "other service" and the user process. However, it would if the "other service" can also write to this memory (while locking against by a mutex, of course). But if I only try to use OpenFileMapping with FILE_MAP_WRITE from the "other service", ERROR_ACCESS_DENIED (5) is returned (FILE_MAP_READ works - of course). The same happens when I try to aquire the mutex.
I assume that there is something wrong with the SSDL string below, but I have no idea what I have to put in there. Can anyone help, please?
Service creates the FileMapping as follows (error checking ommitted for better read; you maybe have already seen party of them):
PSECURITY_DESCRIPTOR getSecurityDescriptor(const QString &sddl)
{
QString m_sddl = sddl;
PSECURITY_DESCRIPTOR m_pSecDesc;
if ((DWORD)(LOBYTE(LOWORD(GetVersion()))) >= 6)
{
// Found this - is this really required??
m_sddl += "S:(ML;;NW;;;ME)";
}
if(!ConvertStringSecurityDescriptorToSecurityDescriptorW(
(LPCWSTR)*m_sddl.utf16(), SDDL_REVISION_1, &m_pSecDesc, nullptr))
{
return nullptr;
}
return m_pSecDesc;
}
bool publishData(...)
{
PSECURITY_DESCRIPTOR m_pSecDesc = getSecurityDescriptor("D:(A;;GR;;;AU)(A;;GA;;;LS)");
if (!m_pSecDesc)
{
...
return false;
}
SECURITY_ATTRIBUTES secAttr;
secAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
secAttr.lpSecurityDescriptor = m_pSecDesc;
secAttr.bInheritHandle = false;
// Ommitted: We also create a mutex "Global\\MyMutex" here with the same SECURITY_ATTRIBUTES...
QString m_mapFileName = "Global\\MySharedMemory";
m_hMapFile = CreateFileMappingW(INVALID_HANDLE_VALUE, &secAttr, PAGE_READWRITE,
0, data.size(), (LPCWSTR)(m_mapFileName.utf16()));
...
The "other service" attempt to open it this way:
hMapFile = OpenFileMappingW(FILE_MAP_WRITE, false, L"Global\\MySharedMemory");
The same happens if i try to aquire the mutex mentoined above by:
hMutex = OpenMutexW(MUTEX_MODIFY_STATE, false, L"Global\\MyMutex");
...
DWORD ret = WaitForSingleObject(hMutex, 1000); // -> ERROR_ACCESS_DENIED (5)