security_p.c 9.8 KB

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