winsecur.c 9.6 KB

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