win_sec_attr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright (c) 2015 Timothee "TTimo" Besset All rights reserved.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. and/or sell copies of the Software, and to permit persons to whom
  8. the Software is furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. */
  19. #include "../src/nn.h"
  20. #include "../src/pair.h"
  21. #include "../src/pubsub.h"
  22. #include "../src/ipc.h"
  23. #include "testutil.h"
  24. #include <accctrl.h>
  25. #include <sddl.h>
  26. #include <aclapi.h>
  27. /* Windows only. Custom SECURITY_ATTRIBUTES on a socket. */
  28. #define PIPE_NAME "win_sec_attr.ipc"
  29. #define SOCKET_ADDRESS "ipc://" PIPE_NAME
  30. int main ()
  31. {
  32. int sb;
  33. int sc;
  34. SECURITY_ATTRIBUTES sec;
  35. BOOL ret;
  36. SID SIDAuthUsers;
  37. DWORD SIDSize;
  38. EXPLICIT_ACCESS xa;
  39. PACL pACL;
  40. DWORD ret2;
  41. int ret3;
  42. void *void_ret_value = NULL;
  43. size_t void_ret_size = sizeof (void_ret_value);
  44. HANDLE pipeHandle = NULL;
  45. PSID pSidOwner = NULL;
  46. BOOL equal = FALSE;
  47. BOOL bret = FALSE;
  48. PACL dacl = NULL;
  49. PACE_HEADER ace = NULL;
  50. PACCESS_ALLOWED_ACE allowed_ace = NULL;
  51. PSID the_sid = NULL;
  52. PSECURITY_DESCRIPTOR sd = NULL;
  53. sc = test_socket (AF_SP, NN_PAIR);
  54. test_connect (sc, SOCKET_ADDRESS);
  55. sb = test_socket (AF_SP, NN_PAIR);
  56. memset (&sec, 0, sizeof (sec));
  57. sec.lpSecurityDescriptor = malloc (SECURITY_DESCRIPTOR_MIN_LENGTH);
  58. ret = InitializeSecurityDescriptor (sec.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
  59. nn_assert (ret);
  60. SIDSize = sizeof (SIDAuthUsers);
  61. ret = CreateWellKnownSid (WinAuthenticatedUserSid, NULL, &SIDAuthUsers, &SIDSize);
  62. nn_assert (ret);
  63. xa.grfAccessPermissions = GENERIC_READ | GENERIC_WRITE;
  64. xa.grfAccessMode = SET_ACCESS;
  65. xa.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  66. xa.Trustee.TrusteeForm = TRUSTEE_IS_SID;
  67. xa.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
  68. xa.Trustee.ptstrName = (LPSTR) &SIDAuthUsers;
  69. ret2 = SetEntriesInAcl (1, &xa, NULL, &pACL);
  70. nn_assert (ret2 == ERROR_SUCCESS);
  71. ret = SetSecurityDescriptorDacl (sec.lpSecurityDescriptor, TRUE, pACL, FALSE);
  72. nn_assert (ret);
  73. sec.nLength = sizeof (sec);
  74. sec.bInheritHandle = TRUE;
  75. ret3 = nn_setsockopt (sb, NN_IPC, NN_IPC_SEC_ATTR, &sec, sizeof (sec));
  76. nn_assert (ret3 == 0);
  77. test_bind (sb, SOCKET_ADDRESS);
  78. nn_sleep (200);
  79. test_send (sc, "0123456789012345678901234567890123456789");
  80. test_recv (sb, "0123456789012345678901234567890123456789");
  81. ret3 = nn_getsockopt (sb, NN_IPC, NN_IPC_SEC_ATTR, &void_ret_value, &void_ret_size);
  82. nn_assert (ret3 == 0);
  83. nn_assert (void_ret_value == &sec);
  84. /* Verify that the pipe has the same security descriptor that we set by
  85. comparing the ace of the kernel object to the one we created it with. */
  86. pipeHandle = CreateFileA ("\\\\.\\\\pipe\\" PIPE_NAME, READ_CONTROL, 0, NULL,
  87. OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
  88. nn_assert (pipeHandle != INVALID_HANDLE_VALUE);
  89. ret2 = GetSecurityInfo (pipeHandle, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,
  90. NULL, NULL, &dacl, NULL, &sd);
  91. nn_assert (ret2 == ERROR_SUCCESS);
  92. nn_assert (1 == dacl->AceCount);
  93. bret = GetAce (dacl, 0, &ace);
  94. nn_assert (bret == TRUE);
  95. nn_assert (ace->AceType == ACCESS_ALLOWED_ACE_TYPE);
  96. allowed_ace = (PACCESS_ALLOWED_ACE) ace;
  97. the_sid = (PSID) &(allowed_ace->SidStart);
  98. nn_assert (IsValidSid (the_sid));
  99. equal = EqualSid ((PSID) &(allowed_ace->SidStart), &SIDAuthUsers);
  100. nn_assert (equal);
  101. LocalFree (sd);
  102. test_close (sc);
  103. test_close (sb);
  104. LocalFree (pACL);
  105. free (sec.lpSecurityDescriptor);
  106. return 0;
  107. }