winsecur.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * winsecur.c: implementation of winsecur.h.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "putty.h"
  7. #if !defined NO_SECURITY
  8. #define WINSECUR_GLOBAL
  9. #include "winsecur.h"
  10. /* Initialised once, then kept around to reuse forever */
  11. static PSID worldsid, networksid, usersid;
  12. #ifdef MPEXT
  13. void win_secur_cleanup(void)
  14. {
  15. if (usersid)
  16. {
  17. sfree(usersid);
  18. usersid = NULL;
  19. }
  20. }
  21. #endif
  22. int got_advapi(void)
  23. {
  24. static int attempted = FALSE;
  25. static int successful;
  26. static HMODULE advapi;
  27. if (!attempted) {
  28. attempted = TRUE;
  29. advapi = load_system32_dll("advapi32.dll");
  30. successful = advapi &&
  31. GET_WINDOWS_FUNCTION(advapi, GetSecurityInfo) &&
  32. GET_WINDOWS_FUNCTION(advapi, SetSecurityInfo) &&
  33. GET_WINDOWS_FUNCTION(advapi, OpenProcessToken) &&
  34. GET_WINDOWS_FUNCTION(advapi, GetTokenInformation) &&
  35. GET_WINDOWS_FUNCTION(advapi, InitializeSecurityDescriptor) &&
  36. GET_WINDOWS_FUNCTION(advapi, SetSecurityDescriptorOwner) &&
  37. GET_WINDOWS_FUNCTION(advapi, SetEntriesInAclA);
  38. }
  39. return successful;
  40. }
  41. PSID get_user_sid(void)
  42. {
  43. HANDLE proc = NULL, tok = NULL;
  44. TOKEN_USER *user = NULL;
  45. DWORD toklen, sidlen;
  46. PSID sid = NULL, ret = NULL;
  47. if (usersid)
  48. return usersid;
  49. if (!got_advapi())
  50. goto cleanup;
  51. if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
  52. GetCurrentProcessId())) == NULL)
  53. goto cleanup;
  54. if (!p_OpenProcessToken(proc, TOKEN_QUERY, &tok))
  55. goto cleanup;
  56. if (!p_GetTokenInformation(tok, TokenUser, NULL, 0, &toklen) &&
  57. GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  58. goto cleanup;
  59. if ((user = (TOKEN_USER *)LocalAlloc(LPTR, toklen)) == NULL)
  60. goto cleanup;
  61. if (!p_GetTokenInformation(tok, TokenUser, user, toklen, &toklen))
  62. goto cleanup;
  63. sidlen = GetLengthSid(user->User.Sid);
  64. sid = (PSID)smalloc(sidlen);
  65. if (!CopySid(sidlen, sid, user->User.Sid))
  66. goto cleanup;
  67. /* Success. Move sid into the return value slot, and null it out
  68. * to stop the cleanup code freeing it. */
  69. ret = usersid = sid;
  70. sid = NULL;
  71. cleanup:
  72. if (proc != NULL)
  73. CloseHandle(proc);
  74. if (tok != NULL)
  75. CloseHandle(tok);
  76. if (user != NULL)
  77. LocalFree(user);
  78. if (sid != NULL)
  79. sfree(sid);
  80. return ret;
  81. }
  82. int getsids(char *error)
  83. {
  84. SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
  85. SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
  86. int ret;
  87. error=NULL;
  88. if (!usersid) {
  89. if ((usersid = get_user_sid()) == NULL) {
  90. error = dupprintf("unable to construct SID for current user: %s",
  91. win_strerror(GetLastError()));
  92. goto cleanup;
  93. }
  94. }
  95. if (!worldsid) {
  96. if (!AllocateAndInitializeSid(&world_auth, 1, SECURITY_WORLD_RID,
  97. 0, 0, 0, 0, 0, 0, 0, &worldsid)) {
  98. error = dupprintf("unable to construct SID for world: %s",
  99. win_strerror(GetLastError()));
  100. goto cleanup;
  101. }
  102. }
  103. if (!networksid) {
  104. if (!AllocateAndInitializeSid(&nt_auth, 1, SECURITY_NETWORK_RID,
  105. 0, 0, 0, 0, 0, 0, 0, &networksid)) {
  106. error = dupprintf("unable to construct SID for "
  107. "local same-user access only: %s",
  108. win_strerror(GetLastError()));
  109. goto cleanup;
  110. }
  111. }
  112. ret=TRUE;
  113. cleanup:
  114. if (ret) {
  115. sfree(error);
  116. error = NULL;
  117. }
  118. return ret;
  119. }
  120. int make_private_security_descriptor(DWORD permissions,
  121. PSECURITY_DESCRIPTOR *psd,
  122. PACL *acl,
  123. char **error)
  124. {
  125. EXPLICIT_ACCESS ea[3];
  126. int acl_err;
  127. int ret = FALSE;
  128. *psd = NULL;
  129. *acl = NULL;
  130. *error = NULL;
  131. if (!getsids(*error))
  132. goto cleanup;
  133. memset(ea, 0, sizeof(ea));
  134. ea[0].grfAccessPermissions = permissions;
  135. ea[0].grfAccessMode = REVOKE_ACCESS;
  136. ea[0].grfInheritance = NO_INHERITANCE;
  137. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  138. ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
  139. ea[1].grfAccessPermissions = permissions;
  140. ea[1].grfAccessMode = GRANT_ACCESS;
  141. ea[1].grfInheritance = NO_INHERITANCE;
  142. ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  143. ea[1].Trustee.ptstrName = (LPTSTR)usersid;
  144. ea[2].grfAccessPermissions = permissions;
  145. ea[2].grfAccessMode = REVOKE_ACCESS;
  146. ea[2].grfInheritance = NO_INHERITANCE;
  147. ea[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  148. ea[2].Trustee.ptstrName = (LPTSTR)networksid;
  149. acl_err = p_SetEntriesInAclA(3, ea, NULL, acl);
  150. if (acl_err != ERROR_SUCCESS || *acl == NULL) {
  151. *error = dupprintf("unable to construct ACL: %s",
  152. win_strerror(acl_err));
  153. goto cleanup;
  154. }
  155. *psd = (PSECURITY_DESCRIPTOR)
  156. LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  157. if (!*psd) {
  158. *error = dupprintf("unable to allocate security descriptor: %s",
  159. win_strerror(GetLastError()));
  160. goto cleanup;
  161. }
  162. if (!InitializeSecurityDescriptor(*psd, SECURITY_DESCRIPTOR_REVISION)) {
  163. *error = dupprintf("unable to initialise security descriptor: %s",
  164. win_strerror(GetLastError()));
  165. goto cleanup;
  166. }
  167. if (!SetSecurityDescriptorOwner(*psd, usersid, FALSE)) {
  168. *error = dupprintf("unable to set owner in security descriptor: %s",
  169. win_strerror(GetLastError()));
  170. goto cleanup;
  171. }
  172. if (!SetSecurityDescriptorDacl(*psd, TRUE, *acl, FALSE)) {
  173. *error = dupprintf("unable to set DACL in security descriptor: %s",
  174. win_strerror(GetLastError()));
  175. goto cleanup;
  176. }
  177. ret = TRUE;
  178. cleanup:
  179. if (!ret) {
  180. if (*psd) {
  181. LocalFree(*psd);
  182. *psd = NULL;
  183. }
  184. if (*acl) {
  185. LocalFree(*acl);
  186. *acl = NULL;
  187. }
  188. } else {
  189. sfree(*error);
  190. *error = NULL;
  191. }
  192. return ret;
  193. }
  194. int setprocessacl(char *error)
  195. {
  196. EXPLICIT_ACCESS ea[2];
  197. int acl_err;
  198. int ret=FALSE;
  199. PACL acl = NULL;
  200. static const DWORD nastyace=WRITE_DAC | WRITE_OWNER |
  201. PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD |
  202. PROCESS_DUP_HANDLE |
  203. PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION |
  204. PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE |
  205. PROCESS_SUSPEND_RESUME;
  206. if (!getsids(error))
  207. goto cleanup;
  208. memset(ea, 0, sizeof(ea));
  209. /* Everyone: deny */
  210. ea[0].grfAccessPermissions = nastyace;
  211. ea[0].grfAccessMode = DENY_ACCESS;
  212. ea[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  213. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  214. ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
  215. /* User: user ace */
  216. ea[1].grfAccessPermissions = ~nastyace & 0x1fff;
  217. ea[1].grfAccessMode = GRANT_ACCESS;
  218. ea[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  219. ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  220. ea[1].Trustee.ptstrName = (LPTSTR)usersid;
  221. acl_err = p_SetEntriesInAclA(2, ea, NULL, &acl);
  222. if (acl_err != ERROR_SUCCESS || acl == NULL) {
  223. error = dupprintf("unable to construct ACL: %s",
  224. win_strerror(acl_err));
  225. goto cleanup;
  226. }
  227. if (ERROR_SUCCESS != p_SetSecurityInfo
  228. (GetCurrentProcess(), SE_KERNEL_OBJECT,
  229. OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
  230. usersid, NULL, acl, NULL)) {
  231. error=dupprintf("Unable to set process ACL: %s",
  232. win_strerror(GetLastError()));
  233. goto cleanup;
  234. }
  235. ret=TRUE;
  236. cleanup:
  237. if (!ret) {
  238. if (acl) {
  239. LocalFree(acl);
  240. acl = NULL;
  241. }
  242. }
  243. return ret;
  244. }
  245. #endif /* !defined NO_SECURITY */