winmiscs.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * winmiscs.c: Windows-specific standalone functions. Has the same
  3. * relationship to winmisc.c that utils.c does to misc.c, but the
  4. * corresponding name 'winutils.c' was already taken.
  5. */
  6. #include "putty.h"
  7. #ifndef NO_SECUREZEROMEMORY
  8. /*
  9. * Windows implementation of smemclr (see misc.c) using SecureZeroMemory.
  10. */
  11. void smemclr(void *b, size_t n) {
  12. if (b && n > 0)
  13. SecureZeroMemory(b, n);
  14. }
  15. #endif
  16. #ifdef DEBUG
  17. static FILE *debug_fp = NULL;
  18. static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
  19. static int debug_got_console = 0;
  20. void dputs(const char *buf)
  21. {
  22. DWORD dw;
  23. if (!debug_got_console) {
  24. if (AllocConsole()) {
  25. debug_got_console = 1;
  26. debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
  27. }
  28. }
  29. if (!debug_fp) {
  30. debug_fp = fopen("debug.log", "w");
  31. }
  32. if (debug_hdl != INVALID_HANDLE_VALUE) {
  33. WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
  34. }
  35. fputs(buf, debug_fp);
  36. fflush(debug_fp);
  37. }
  38. #endif
  39. #ifdef MINEFIELD
  40. /*
  41. * Minefield - a Windows equivalent for Electric Fence
  42. */
  43. #define PAGESIZE 4096
  44. /*
  45. * Design:
  46. *
  47. * We start by reserving as much virtual address space as Windows
  48. * will sensibly (or not sensibly) let us have. We flag it all as
  49. * invalid memory.
  50. *
  51. * Any allocation attempt is satisfied by committing one or more
  52. * pages, with an uncommitted page on either side. The returned
  53. * memory region is jammed up against the _end_ of the pages.
  54. *
  55. * Freeing anything causes instantaneous decommitment of the pages
  56. * involved, so stale pointers are caught as soon as possible.
  57. */
  58. static int minefield_initialised = 0;
  59. static void *minefield_region = NULL;
  60. static long minefield_size = 0;
  61. static long minefield_npages = 0;
  62. static long minefield_curpos = 0;
  63. static unsigned short *minefield_admin = NULL;
  64. static void *minefield_pages = NULL;
  65. static void minefield_admin_hide(int hide)
  66. {
  67. int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
  68. VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
  69. }
  70. static void minefield_init(void)
  71. {
  72. int size;
  73. int admin_size;
  74. int i;
  75. for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
  76. minefield_region = VirtualAlloc(NULL, size,
  77. MEM_RESERVE, PAGE_NOACCESS);
  78. if (minefield_region)
  79. break;
  80. }
  81. minefield_size = size;
  82. /*
  83. * Firstly, allocate a section of that to be the admin block.
  84. * We'll need a two-byte field for each page.
  85. */
  86. minefield_admin = minefield_region;
  87. minefield_npages = minefield_size / PAGESIZE;
  88. admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
  89. minefield_npages = (minefield_size - admin_size) / PAGESIZE;
  90. minefield_pages = (char *) minefield_region + admin_size;
  91. /*
  92. * Commit the admin region.
  93. */
  94. VirtualAlloc(minefield_admin, minefield_npages * 2,
  95. MEM_COMMIT, PAGE_READWRITE);
  96. /*
  97. * Mark all pages as unused (0xFFFF).
  98. */
  99. for (i = 0; i < minefield_npages; i++)
  100. minefield_admin[i] = 0xFFFF;
  101. /*
  102. * Hide the admin region.
  103. */
  104. minefield_admin_hide(1);
  105. minefield_initialised = 1;
  106. }
  107. static void minefield_bomb(void)
  108. {
  109. div(1, *(int *) minefield_pages);
  110. }
  111. static void *minefield_alloc(int size)
  112. {
  113. int npages;
  114. int pos, lim, region_end, region_start;
  115. int start;
  116. int i;
  117. npages = (size + PAGESIZE - 1) / PAGESIZE;
  118. minefield_admin_hide(0);
  119. /*
  120. * Search from current position until we find a contiguous
  121. * bunch of npages+2 unused pages.
  122. */
  123. pos = minefield_curpos;
  124. lim = minefield_npages;
  125. while (1) {
  126. /* Skip over used pages. */
  127. while (pos < lim && minefield_admin[pos] != 0xFFFF)
  128. pos++;
  129. /* Count unused pages. */
  130. start = pos;
  131. while (pos < lim && pos - start < npages + 2 &&
  132. minefield_admin[pos] == 0xFFFF)
  133. pos++;
  134. if (pos - start == npages + 2)
  135. break;
  136. /* If we've reached the limit, reset the limit or stop. */
  137. if (pos >= lim) {
  138. if (lim == minefield_npages) {
  139. /* go round and start again at zero */
  140. lim = minefield_curpos;
  141. pos = 0;
  142. } else {
  143. minefield_admin_hide(1);
  144. return NULL;
  145. }
  146. }
  147. }
  148. minefield_curpos = pos - 1;
  149. /*
  150. * We have npages+2 unused pages starting at start. We leave
  151. * the first and last of these alone and use the rest.
  152. */
  153. region_end = (start + npages + 1) * PAGESIZE;
  154. region_start = region_end - size;
  155. /* FIXME: could align here if we wanted */
  156. /*
  157. * Update the admin region.
  158. */
  159. for (i = start + 2; i < start + npages + 1; i++)
  160. minefield_admin[i] = 0xFFFE; /* used but no region starts here */
  161. minefield_admin[start + 1] = region_start % PAGESIZE;
  162. minefield_admin_hide(1);
  163. VirtualAlloc((char *) minefield_pages + region_start, size,
  164. MEM_COMMIT, PAGE_READWRITE);
  165. return (char *) minefield_pages + region_start;
  166. }
  167. static void minefield_free(void *ptr)
  168. {
  169. int region_start, i, j;
  170. minefield_admin_hide(0);
  171. region_start = (char *) ptr - (char *) minefield_pages;
  172. i = region_start / PAGESIZE;
  173. if (i < 0 || i >= minefield_npages ||
  174. minefield_admin[i] != region_start % PAGESIZE)
  175. minefield_bomb();
  176. for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
  177. minefield_admin[j] = 0xFFFF;
  178. }
  179. VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
  180. minefield_admin_hide(1);
  181. }
  182. static int minefield_get_size(void *ptr)
  183. {
  184. int region_start, i, j;
  185. minefield_admin_hide(0);
  186. region_start = (char *) ptr - (char *) minefield_pages;
  187. i = region_start / PAGESIZE;
  188. if (i < 0 || i >= minefield_npages ||
  189. minefield_admin[i] != region_start % PAGESIZE)
  190. minefield_bomb();
  191. for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
  192. minefield_admin_hide(1);
  193. return j * PAGESIZE - region_start;
  194. }
  195. void *minefield_c_malloc(size_t size)
  196. {
  197. if (!minefield_initialised)
  198. minefield_init();
  199. return minefield_alloc(size);
  200. }
  201. void minefield_c_free(void *p)
  202. {
  203. if (!minefield_initialised)
  204. minefield_init();
  205. minefield_free(p);
  206. }
  207. /*
  208. * realloc _always_ moves the chunk, for rapid detection of code
  209. * that assumes it won't.
  210. */
  211. void *minefield_c_realloc(void *p, size_t size)
  212. {
  213. size_t oldsize;
  214. void *q;
  215. if (!minefield_initialised)
  216. minefield_init();
  217. q = minefield_alloc(size);
  218. oldsize = minefield_get_size(p);
  219. memcpy(q, p, (oldsize < size ? oldsize : size));
  220. minefield_free(p);
  221. return q;
  222. }
  223. #endif /* MINEFIELD */
  224. #if defined _MSC_VER && _MSC_VER < 1800
  225. /*
  226. * Work around lack of strtoumax in older MSVC libraries
  227. */
  228. uintmax_t strtoumax(const char *nptr, char **endptr, int base)
  229. {
  230. return _strtoui64(nptr, endptr, base);
  231. }
  232. #endif
  233. #if defined _M_ARM || defined _M_ARM64
  234. bool platform_aes_hw_available(void)
  235. {
  236. return IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE);
  237. }
  238. #endif