security_p.c 10 KB

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