1
0

winsecur.c 10 KB

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