1
0

winsecur.c 9.5 KB

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