안녕하세요. 아주 오랜만에 글을 쓰게 되었습니다.
인생사 머가 그리 바쁜지, 일은 해도 해도 계속 생기네요 ㅠㅠ;;;

서비스스 뿐만 아니라, 시스템이나 관리자 권한으로 생성한 MMF나 혹은 PIPE를 유저 권한으로
읽거나 하려면 반드시라고 해도 좋을 만큼 권한에 관한 문제가 발생합니다.

   DWORD dwRes;
   PSID pEveryoneSID = NULL, pAdminSID = NULL;
   PACL pACL = NULL;
   PSECURITY_DESCRIPTOR pSD = NULL;
   EXPLICIT_ACCESS ea;
   SID_IDENTIFIER_AUTHORITY SIDAuthWorld SECURITY_WORLD_SID_AUTHORITY;
   SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;

  SECURITY_ATTRIBUTES sa;

   // Create a well-known SID for the Everyone group.
   if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
                    SECURITY_WORLD_RID,
                    0, 0, 0, 0, 0, 0, 0,
                    &pEveryoneSID))
   {
       printf("AllocateAndInitializeSid Error %u\n", GetLastError());
       goto Cleanup;
   }

   // Initialize an EXPLICIT_ACCESS structure for an ACE.
   // The ACE will allow Everyone read access to the key.
   ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
   ea.grfAccessPermissions = GENERIC_READ;
   ea.grfAccessMode = SET_ACCESS;
   ea.grfInheritance= NO_INHERITANCE;
   ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
   ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
   ea.Trustee.ptstrName  = (LPTSTR) pEveryoneSID;

   // Create a SID for the BUILTIN\Administrators group.
   if(! AllocateAndInitializeSid(&SIDAuthNT, 2,
                    SECURITY_BUILTIN_DOMAIN_RID,
                    DOMAIN_ALIAS_RID_ADMINS,
                    0, 0, 0, 0, 0, 0,
                    &pAdminSID))
   {
       printf("AllocateAndInitializeSid Error %u\n", GetLastError());
       goto Cleanup;
   }


   // Create a new ACL that contains the new ACEs.
   dwRes = SetEntriesInAcl(1, &ea, NULL, &pACL);
   if (ERROR_SUCCESS != dwRes)
   {
       printf("SetEntriesInAcl Error %u\n", GetLastError());
       goto Cleanup;
   }

   // Initialize a security descriptor.
   pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
   if (NULL == pSD)
   {
       printf("LocalAlloc Error %u\n", GetLastError());
       goto Cleanup;
   }

   if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
   {
       printf("InitializeSecurityDescriptor Error %u\n", GetLastError());
       goto Cleanup;
   }

   // Add the ACL to the security descriptor.
   if (!SetSecurityDescriptorDacl(pSD,
           TRUE,     // bDaclPresent flag
           pACL,
           FALSE))   // not a default DACL
   {
       printf("SetSecurityDescriptorDacl Error %u\n",
               GetLastError());
       goto Cleanup;
   }

   // Initialize a security attributes structure.
   sa.nLength = sizeof (SECURITY_ATTRIBUTES);
   sa.lpSecurityDescriptor = pSD;
   sa.bInheritHandle = FALSE;

위의 코드는 보안 속성을 생성하는 코드입니다. MSDN에 나온 샘플 일부를 추려서 만든 코드인데요.
큼지막하게 생긴 애들만 주의해 보시고, 수정해 주시면 원하시는 권한을 풀어줄 수 있습니다.

핸들 = ::CreateFileMapping(파일핸들, &sa, ... 나머지 인자들)...

위 코드의 설명은 Users 그룹에 포함된 사용자에게 Read Only 권한을 주도록 만든 것입니다.

+ Recent posts