winsecur.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. int got_advapi(void)
  13. {
  14. static int attempted = FALSE;
  15. static int successful;
  16. static HMODULE advapi;
  17. if (!attempted) {
  18. attempted = TRUE;
  19. advapi = load_system32_dll("advapi32.dll");
  20. successful = advapi &&
  21. GET_WINDOWS_FUNCTION(advapi, GetSecurityInfo) &&
  22. GET_WINDOWS_FUNCTION(advapi, SetSecurityInfo) &&
  23. GET_WINDOWS_FUNCTION(advapi, OpenProcessToken) &&
  24. GET_WINDOWS_FUNCTION(advapi, GetTokenInformation) &&
  25. GET_WINDOWS_FUNCTION(advapi, InitializeSecurityDescriptor) &&
  26. GET_WINDOWS_FUNCTION(advapi, SetSecurityDescriptorOwner) &&
  27. GET_WINDOWS_FUNCTION(advapi, SetEntriesInAclA);
  28. }
  29. return successful;
  30. }
  31. PSID get_user_sid(void)
  32. {
  33. HANDLE proc = NULL, tok = NULL;
  34. TOKEN_USER *user = NULL;
  35. DWORD toklen, sidlen;
  36. PSID sid = NULL, ret = NULL;
  37. if (usersid)
  38. return usersid;
  39. if (!got_advapi())
  40. goto cleanup;
  41. if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
  42. GetCurrentProcessId())) == NULL)
  43. goto cleanup;
  44. if (!p_OpenProcessToken(proc, TOKEN_QUERY, &tok))
  45. goto cleanup;
  46. if (!p_GetTokenInformation(tok, TokenUser, NULL, 0, &toklen) &&
  47. GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  48. goto cleanup;
  49. if ((user = (TOKEN_USER *)LocalAlloc(LPTR, toklen)) == NULL)
  50. goto cleanup;
  51. if (!p_GetTokenInformation(tok, TokenUser, user, toklen, &toklen))
  52. goto cleanup;
  53. sidlen = GetLengthSid(user->User.Sid);
  54. sid = (PSID)smalloc(sidlen);
  55. if (!CopySid(sidlen, sid, user->User.Sid))
  56. goto cleanup;
  57. /* Success. Move sid into the return value slot, and null it out
  58. * to stop the cleanup code freeing it. */
  59. ret = usersid = sid;
  60. sid = NULL;
  61. cleanup:
  62. if (proc != NULL)
  63. CloseHandle(proc);
  64. if (tok != NULL)
  65. CloseHandle(tok);
  66. if (user != NULL)
  67. LocalFree(user);
  68. if (sid != NULL)
  69. sfree(sid);
  70. return ret;
  71. }
  72. int getsids(char **error)
  73. {
  74. SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
  75. SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
  76. int ret;
  77. *error = NULL;
  78. if (!usersid) {
  79. if ((usersid = get_user_sid()) == NULL) {
  80. *error = dupprintf("unable to construct SID for current user: %s",
  81. win_strerror(GetLastError()));
  82. goto cleanup;
  83. }
  84. }
  85. if (!worldsid) {
  86. if (!AllocateAndInitializeSid(&world_auth, 1, SECURITY_WORLD_RID,
  87. 0, 0, 0, 0, 0, 0, 0, &worldsid)) {
  88. *error = dupprintf("unable to construct SID for world: %s",
  89. win_strerror(GetLastError()));
  90. goto cleanup;
  91. }
  92. }
  93. if (!networksid) {
  94. if (!AllocateAndInitializeSid(&nt_auth, 1, SECURITY_NETWORK_RID,
  95. 0, 0, 0, 0, 0, 0, 0, &networksid)) {
  96. *error = dupprintf("unable to construct SID for "
  97. "local same-user access only: %s",
  98. win_strerror(GetLastError()));
  99. goto cleanup;
  100. }
  101. }
  102. ret = TRUE;
  103. cleanup:
  104. return ret;
  105. }
  106. int make_private_security_descriptor(DWORD permissions,
  107. PSECURITY_DESCRIPTOR *psd,
  108. PACL *acl,
  109. char **error)
  110. {
  111. EXPLICIT_ACCESS ea[3];
  112. int acl_err;
  113. int ret = FALSE;
  114. *psd = NULL;
  115. *acl = NULL;
  116. *error = NULL;
  117. if (!getsids(error))
  118. goto cleanup;
  119. memset(ea, 0, sizeof(ea));
  120. ea[0].grfAccessPermissions = permissions;
  121. ea[0].grfAccessMode = REVOKE_ACCESS;
  122. ea[0].grfInheritance = NO_INHERITANCE;
  123. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  124. ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
  125. ea[1].grfAccessPermissions = permissions;
  126. ea[1].grfAccessMode = GRANT_ACCESS;
  127. ea[1].grfInheritance = NO_INHERITANCE;
  128. ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  129. ea[1].Trustee.ptstrName = (LPTSTR)usersid;
  130. ea[2].grfAccessPermissions = permissions;
  131. ea[2].grfAccessMode = REVOKE_ACCESS;
  132. ea[2].grfInheritance = NO_INHERITANCE;
  133. ea[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  134. ea[2].Trustee.ptstrName = (LPTSTR)networksid;
  135. acl_err = p_SetEntriesInAclA(3, ea, NULL, acl);
  136. if (acl_err != ERROR_SUCCESS || *acl == NULL) {
  137. *error = dupprintf("unable to construct ACL: %s",
  138. win_strerror(acl_err));
  139. goto cleanup;
  140. }
  141. *psd = (PSECURITY_DESCRIPTOR)
  142. LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  143. if (!*psd) {
  144. *error = dupprintf("unable to allocate security descriptor: %s",
  145. win_strerror(GetLastError()));
  146. goto cleanup;
  147. }
  148. if (!InitializeSecurityDescriptor(*psd, SECURITY_DESCRIPTOR_REVISION)) {
  149. *error = dupprintf("unable to initialise security descriptor: %s",
  150. win_strerror(GetLastError()));
  151. goto cleanup;
  152. }
  153. if (!SetSecurityDescriptorOwner(*psd, usersid, FALSE)) {
  154. *error = dupprintf("unable to set owner in security descriptor: %s",
  155. win_strerror(GetLastError()));
  156. goto cleanup;
  157. }
  158. if (!SetSecurityDescriptorDacl(*psd, TRUE, *acl, FALSE)) {
  159. *error = dupprintf("unable to set DACL in security descriptor: %s",
  160. win_strerror(GetLastError()));
  161. goto cleanup;
  162. }
  163. ret = TRUE;
  164. cleanup:
  165. if (!ret) {
  166. if (*psd) {
  167. LocalFree(*psd);
  168. *psd = NULL;
  169. }
  170. if (*acl) {
  171. LocalFree(*acl);
  172. *acl = NULL;
  173. }
  174. } else {
  175. sfree(*error);
  176. *error = NULL;
  177. }
  178. return ret;
  179. }
  180. static int really_restrict_process_acl(char **error)
  181. {
  182. EXPLICIT_ACCESS ea[2];
  183. int acl_err;
  184. int ret=FALSE;
  185. PACL acl = NULL;
  186. static const DWORD nastyace=WRITE_DAC | WRITE_OWNER |
  187. PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD |
  188. PROCESS_DUP_HANDLE |
  189. PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION |
  190. PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE |
  191. PROCESS_SUSPEND_RESUME;
  192. if (!getsids(error))
  193. goto cleanup;
  194. memset(ea, 0, sizeof(ea));
  195. /* Everyone: deny */
  196. ea[0].grfAccessPermissions = nastyace;
  197. ea[0].grfAccessMode = DENY_ACCESS;
  198. ea[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  199. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  200. ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
  201. /* User: user ace */
  202. ea[1].grfAccessPermissions = ~nastyace & 0x1fff;
  203. ea[1].grfAccessMode = GRANT_ACCESS;
  204. ea[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  205. ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  206. ea[1].Trustee.ptstrName = (LPTSTR)usersid;
  207. acl_err = p_SetEntriesInAclA(2, ea, NULL, &acl);
  208. if (acl_err != ERROR_SUCCESS || acl == NULL) {
  209. *error = dupprintf("unable to construct ACL: %s",
  210. win_strerror(acl_err));
  211. goto cleanup;
  212. }
  213. if (ERROR_SUCCESS != p_SetSecurityInfo
  214. (GetCurrentProcess(), SE_KERNEL_OBJECT,
  215. OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
  216. usersid, NULL, acl, NULL)) {
  217. *error = dupprintf("Unable to set process ACL: %s",
  218. win_strerror(GetLastError()));
  219. goto cleanup;
  220. }
  221. ret=TRUE;
  222. cleanup:
  223. if (!ret) {
  224. if (acl) {
  225. LocalFree(acl);
  226. acl = NULL;
  227. }
  228. }
  229. return ret;
  230. }
  231. #endif /* !defined NO_SECURITY */
  232. /*
  233. * Lock down our process's ACL, to present an obstacle to malware
  234. * trying to write into its memory. This can't be a full defence,
  235. * because well timed malware could attack us before this code runs -
  236. * even if it was unconditionally run at the very start of main(),
  237. * which we wouldn't want to do anyway because it turns out in practie
  238. * that interfering with other processes in this way has significant
  239. * non-infringing uses on Windows (e.g. screen reader software).
  240. *
  241. * If we've been requested to do this and are unsuccessful, bomb out
  242. * via modalfatalbox rather than continue in a less protected mode.
  243. *
  244. * This function is intentionally outside the #ifndef NO_SECURITY that
  245. * covers the rest of this file, because when PuTTY is compiled
  246. * without the ability to restrict its ACL, we don't want it to
  247. * silently pretend to honour the instruction to do so.
  248. */
  249. void restrict_process_acl(void)
  250. {
  251. char *error = NULL;
  252. int ret;
  253. #if !defined NO_SECURITY
  254. ret = really_restrict_process_acl(&error);
  255. #else
  256. ret = FALSE;
  257. error = dupstr("ACL restrictions not compiled into this binary");
  258. #endif
  259. if (!ret)
  260. modalfatalbox("Could not restrict process ACL: %s", error);
  261. }