security.c 9.8 KB

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