Thread: Global shared memory in a system service

  1. #1
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195

    Global shared memory in a system service

    I have a system service running that created a global shared memory buffer using file mapping. The service runs fine, but when i try to have an application access the file mapping, the call to CreateFileMapping() returns 5, access denied. Here is the code

    Code:
    hCommandBuffer = CreateFileMapping((HANDLE)INVALID_HANDLE_VALUE , NULL , PAGE_READWRITE | SEC_COMMIT , 0 , sizeof(COMMANDBUFFER) , "Elysia Makes Me Hot Buffer");
    if(hCommandBuffer == NULL){
      sprintf(szError , "%d\n" , GetLastError());
      MessageBox(NULL , szError , "Failed to allocate handle to global shared memory" , MB_OK);
      }
    pCommandBuffer = (COMMANDBUFFER*)MapViewOfFile(hCommandBuffer , FILE_MAP_WRITE , 0 , 0 , sizeof(COMMANDBUFFER));
    if(pCommandBuffer == NULL){
      sprintf(szError , "%d\n" , GetLastError());
      MessageBox(NULL , szError , "Failed to allocate global shared memory" , MB_OK);
      }
    If I run the application without the system service installed, it doesnt have a problem, So I think it might have something to do with system services running at higher (lower numerically) privledge level.
    Last edited by abachler; 03-24-2008 at 01:42 PM.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Does the code work outside of a sys serv?
    Are you sure another instance of the serv isn't doesn't already have it mapped?
    Does the serv have file handling permissions?

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Yarin View Post
    Does the code work outside of a sys serv?
    Yes, the application cretes the file mapping fine. The system service also creates it fine, but the application get denied access if the system service has already created a file mapping. Both the service and the application shoudl be able to map the file at the same time, to achieve interprocess memory sharing.

    Are you sure another instance of the serv isn't doesn't already have it mapped?
    Wouldnt matter, since they are supposed to be able to share the mapping this way.
    Does the serv have file handling permissions?
    I am assuming so, since the service runs fine. Only the application has a problem, and then only when the file mapping has already been created.

    UPDATE

    I solved the problem by having the application create the file mapping. This is a bit of a kludge since the system service has to keep trying to open the mapping, which consumes clock cycles until it find it, but at least i am getting proper function now.
    Last edited by abachler; 03-24-2008 at 04:38 PM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The problem is most likely due to the service running under a different account, which causes different default permissions to be used when you pass NULL to "LPSECURITY_ATTRIBUTES" parameter(s).

    Here are some options:
    Create a Null DACL, which is basically "wide-open" security. Access will never be denied.
    Code:
        SECURITY_ATTRIBUTES SecAttr, *pSec = 0;
        SECURITY_DESCRIPTOR SecDesc;
    
        if (InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION) &&
            SetSecurityDescriptorDacl(&SecDesc, TRUE, (PACL)0, FALSE))
        {
            SecAttr.nLength = sizeof(SecAttr);
            SecAttr.lpSecurityDescriptor = &SecDesc;
            SecAttr.bInheritHandle = TRUE;
            pSec = &SecAttr;
        }//if
    
        // use pSec for any LPSECURITY_ATTRIBUTES parameters
    Or, here's sample code that'll give you a little more security:
    http://msdn2.microsoft.com/en-us/lib...95(VS.85).aspx
    or
    http://msdn2.microsoft.com/en-us/lib...98(VS.85).aspx

    gg

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    As a single tear rolls down his face, abachler chokes out "I love you man".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. How to delete a attached shared memory.
    By maven in forum C Programming
    Replies: 3
    Last Post: 10-17-2006, 05:56 AM
  4. Shared Memory
    By wardej2 in forum Linux Programming
    Replies: 8
    Last Post: 10-21-2005, 07:48 AM
  5. Shared memory woes
    By joshdick in forum C Programming
    Replies: 4
    Last Post: 07-28-2005, 08:59 AM