winsecur.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. return ret;
  115. }
  116. int make_private_security_descriptor(DWORD permissions,
  117. PSECURITY_DESCRIPTOR *psd,
  118. PACL *acl,
  119. char **error)
  120. {
  121. EXPLICIT_ACCESS ea[3];
  122. int acl_err;
  123. int ret = FALSE;
  124. *psd = NULL;
  125. *acl = NULL;
  126. *error = NULL;
  127. if (!getsids(error))
  128. goto cleanup;
  129. memset(ea, 0, sizeof(ea));
  130. ea[0].grfAccessPermissions = permissions;
  131. ea[0].grfAccessMode = REVOKE_ACCESS;
  132. ea[0].grfInheritance = NO_INHERITANCE;
  133. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  134. ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
  135. ea[1].grfAccessPermissions = permissions;
  136. ea[1].grfAccessMode = GRANT_ACCESS;
  137. ea[1].grfInheritance = NO_INHERITANCE;
  138. ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  139. ea[1].Trustee.ptstrName = (LPTSTR)usersid;
  140. ea[2].grfAccessPermissions = permissions;
  141. ea[2].grfAccessMode = REVOKE_ACCESS;
  142. ea[2].grfInheritance = NO_INHERITANCE;
  143. ea[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  144. ea[2].Trustee.ptstrName = (LPTSTR)networksid;
  145. acl_err = p_SetEntriesInAclA(3, ea, NULL, acl);
  146. if (acl_err != ERROR_SUCCESS || *acl == NULL) {
  147. *error = dupprintf("unable to construct ACL: %s",
  148. win_strerror(acl_err));
  149. goto cleanup;
  150. }
  151. *psd = (PSECURITY_DESCRIPTOR)
  152. LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  153. if (!*psd) {
  154. *error = dupprintf("unable to allocate security descriptor: %s",
  155. win_strerror(GetLastError()));
  156. goto cleanup;
  157. }
  158. if (!InitializeSecurityDescriptor(*psd, SECURITY_DESCRIPTOR_REVISION)) {
  159. *error = dupprintf("unable to initialise security descriptor: %s",
  160. win_strerror(GetLastError()));
  161. goto cleanup;
  162. }
  163. if (!SetSecurityDescriptorOwner(*psd, usersid, FALSE)) {
  164. *error = dupprintf("unable to set owner in security descriptor: %s",
  165. win_strerror(GetLastError()));
  166. goto cleanup;
  167. }
  168. if (!SetSecurityDescriptorDacl(*psd, TRUE, *acl, FALSE)) {
  169. *error = dupprintf("unable to set DACL in security descriptor: %s",
  170. win_strerror(GetLastError()));
  171. goto cleanup;
  172. }
  173. ret = TRUE;
  174. cleanup:
  175. if (!ret) {
  176. if (*psd) {
  177. LocalFree(*psd);
  178. *psd = NULL;
  179. }
  180. if (*acl) {
  181. LocalFree(*acl);
  182. *acl = NULL;
  183. }
  184. } else {
  185. sfree(*error);
  186. *error = NULL;
  187. }
  188. return ret;
  189. }
  190. static int really_restrict_process_acl(char **error)
  191. {
  192. EXPLICIT_ACCESS ea[2];
  193. int acl_err;
  194. int ret=FALSE;
  195. PACL acl = NULL;
  196. static const DWORD nastyace=WRITE_DAC | WRITE_OWNER |
  197. PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD |
  198. PROCESS_DUP_HANDLE |
  199. PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION |
  200. PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE |
  201. PROCESS_SUSPEND_RESUME;
  202. if (!getsids(error))
  203. goto cleanup;
  204. memset(ea, 0, sizeof(ea));
  205. /* Everyone: deny */
  206. ea[0].grfAccessPermissions = nastyace;
  207. ea[0].grfAccessMode = DENY_ACCESS;
  208. ea[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  209. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  210. ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
  211. /* User: user ace */
  212. ea[1].grfAccessPermissions = ~nastyace & 0x1fff;
  213. ea[1].grfAccessMode = GRANT_ACCESS;
  214. ea[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  215. ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  216. ea[1].Trustee.ptstrName = (LPTSTR)usersid;
  217. acl_err = p_SetEntriesInAclA(2, ea, NULL, &acl);
  218. if (acl_err != ERROR_SUCCESS || acl == NULL) {
  219. *error = dupprintf("unable to construct ACL: %s",
  220. win_strerror(acl_err));
  221. goto cleanup;
  222. }
  223. if (ERROR_SUCCESS != p_SetSecurityInfo
  224. (GetCurrentProcess(), SE_KERNEL_OBJECT,
  225. OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
  226. usersid, NULL, acl, NULL)) {
  227. *error = dupprintf("Unable to set process ACL: %s",
  228. win_strerror(GetLastError()));
  229. goto cleanup;
  230. }
  231. ret=TRUE;
  232. cleanup:
  233. if (!ret) {
  234. if (acl) {
  235. LocalFree(acl);
  236. acl = NULL;
  237. }
  238. }
  239. return ret;
  240. }
  241. #endif /* !defined NO_SECURITY */
  242. /*
  243. * Lock down our process's ACL, to present an obstacle to malware
  244. * trying to write into its memory. This can't be a full defence,
  245. * because well timed malware could attack us before this code runs -
  246. * even if it was unconditionally run at the very start of main(),
  247. * which we wouldn't want to do anyway because it turns out in practie
  248. * that interfering with other processes in this way has significant
  249. * non-infringing uses on Windows (e.g. screen reader software).
  250. *
  251. * If we've been requested to do this and are unsuccessful, bomb out
  252. * via modalfatalbox rather than continue in a less protected mode.
  253. *
  254. * This function is intentionally outside the #ifndef NO_SECURITY that
  255. * covers the rest of this file, because when PuTTY is compiled
  256. * without the ability to restrict its ACL, we don't want it to
  257. * silently pretend to honour the instruction to do so.
  258. */
  259. void restrict_process_acl(void)
  260. {
  261. char *error = NULL;
  262. int ret;
  263. #if !defined NO_SECURITY
  264. ret = really_restrict_process_acl(&error);
  265. #else
  266. ret = FALSE;
  267. error = dupstr("ACL restrictions not compiled into this binary");
  268. #endif
  269. if (!ret)
  270. modalfatalbox("Could not restrict process ACL: %s", error);
  271. }