ptr_valid.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // Licensed under BSD-MIT: See LICENSE.
  2. #include "ptr_valid.h"
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <ccan/noerr/noerr.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <assert.h>
  11. #include <string.h>
  12. #if HAVE_PROC_SELF_MAPS
  13. static char *grab(const char *filename)
  14. {
  15. int ret, fd;
  16. size_t max = 16384, s = 0;
  17. char *buffer;
  18. fd = open(filename, O_RDONLY);
  19. if (fd < 0)
  20. return NULL;
  21. buffer = malloc(max+1);
  22. if (!buffer)
  23. goto close;
  24. while ((ret = read(fd, buffer + s, max - s)) > 0) {
  25. s += ret;
  26. if (s == max) {
  27. buffer = realloc(buffer, max*2+1);
  28. if (!buffer)
  29. goto close;
  30. max *= 2;
  31. }
  32. }
  33. if (ret < 0)
  34. goto free;
  35. close(fd);
  36. buffer[s] = '\0';
  37. return buffer;
  38. free:
  39. free(buffer);
  40. close:
  41. close_noerr(fd);
  42. return NULL;
  43. }
  44. static char *skip_line(char *p)
  45. {
  46. char *nl = strchr(p, '\n');
  47. if (!nl)
  48. return NULL;
  49. return nl + 1;
  50. }
  51. static struct ptr_valid_map *add_map(struct ptr_valid_map *map,
  52. unsigned int *num,
  53. unsigned int *max,
  54. unsigned long start, unsigned long end, bool is_write)
  55. {
  56. if (*num == *max) {
  57. *max *= 2;
  58. map = realloc(map, sizeof(*map) * *max);
  59. if (!map)
  60. return NULL;
  61. }
  62. map[*num].start = (void *)start;
  63. map[*num].end = (void *)end;
  64. map[*num].is_write = is_write;
  65. (*num)++;
  66. return map;
  67. }
  68. static struct ptr_valid_map *get_proc_maps(unsigned int *num)
  69. {
  70. char *buf, *p;
  71. struct ptr_valid_map *map;
  72. unsigned int max = 16;
  73. buf = grab("/proc/self/maps");
  74. if (!buf) {
  75. *num = 0;
  76. return NULL;
  77. }
  78. map = malloc(sizeof(*map) * max);
  79. if (!map)
  80. goto free_buf;
  81. *num = 0;
  82. for (p = buf; p && *p; p = skip_line(p)) {
  83. unsigned long start, end;
  84. char *endp;
  85. /* Expect '<start-in-hex>-<end-in-hex> rw... */
  86. start = strtoul(p, &endp, 16);
  87. if (*endp != '-')
  88. goto malformed;
  89. end = strtoul(endp+1, &endp, 16);
  90. if (*endp != ' ')
  91. goto malformed;
  92. endp++;
  93. if (endp[0] != 'r' && endp[0] != '-')
  94. goto malformed;
  95. if (endp[1] != 'w' && endp[1] != '-')
  96. goto malformed;
  97. /* We only add readable mappings. */
  98. if (endp[0] == 'r') {
  99. map = add_map(map, num, &max, start, end,
  100. endp[1] == 'w');
  101. if (!map)
  102. goto free_buf;
  103. }
  104. }
  105. free(buf);
  106. return map;
  107. malformed:
  108. free(map);
  109. free_buf:
  110. free(buf);
  111. *num = 0;
  112. return NULL;
  113. }
  114. #else
  115. static struct ptr_valid_map *get_proc_maps(unsigned int *num)
  116. {
  117. *num = 0;
  118. return NULL;
  119. }
  120. #endif
  121. static bool check_with_maps(struct ptr_valid_batch *batch,
  122. const char *p, size_t size, bool is_write)
  123. {
  124. unsigned int i;
  125. for (i = 0; i < batch->num_maps; i++) {
  126. if (p >= batch->maps[i].start && p < batch->maps[i].end) {
  127. /* Overlap into other maps? Recurse with remainder. */
  128. if (p + size > batch->maps[i].end) {
  129. size_t len = p + size - batch->maps[i].end;
  130. if (!check_with_maps(batch, batch->maps[i].end,
  131. len, is_write))
  132. return false;
  133. }
  134. return !is_write || batch->maps[i].is_write;
  135. }
  136. }
  137. return false;
  138. }
  139. static void finish_child(struct ptr_valid_batch *batch)
  140. {
  141. close(batch->to_child);
  142. close(batch->from_child);
  143. waitpid(batch->child_pid, NULL, 0);
  144. batch->child_pid = 0;
  145. }
  146. static bool child_alive(struct ptr_valid_batch *batch)
  147. {
  148. return batch->child_pid != 0;
  149. }
  150. static void run_child(int infd, int outfd)
  151. {
  152. volatile char *p;
  153. /* This is how we expect to exit. */
  154. while (read(infd, &p, sizeof(p)) == sizeof(p)) {
  155. size_t i, size;
  156. bool is_write;
  157. char ret = 0;
  158. /* This is weird. */
  159. if (read(infd, &size, sizeof(size)) != sizeof(size))
  160. exit(1);
  161. if (read(infd, &is_write, sizeof(is_write)) != sizeof(is_write))
  162. exit(2);
  163. for (i = 0; i < size; i++) {
  164. ret = p[i];
  165. if (is_write)
  166. p[i] = ret;
  167. }
  168. /* If we're still here, the answer is "yes". */
  169. if (write(outfd, &ret, 1) != 1)
  170. exit(3);
  171. }
  172. exit(0);
  173. }
  174. static bool create_child(struct ptr_valid_batch *batch)
  175. {
  176. int outpipe[2], inpipe[2];
  177. if (pipe(outpipe) != 0)
  178. return false;
  179. if (pipe(inpipe) != 0)
  180. goto close_outpipe;
  181. fflush(stdout);
  182. batch->child_pid = fork();
  183. if (batch->child_pid == 0) {
  184. close(outpipe[1]);
  185. close(inpipe[0]);
  186. run_child(outpipe[0], inpipe[1]);
  187. }
  188. if (batch->child_pid == -1)
  189. goto cleanup_pid;
  190. close(outpipe[0]);
  191. close(inpipe[1]);
  192. batch->to_child = outpipe[1];
  193. batch->from_child = inpipe[0];
  194. return true;
  195. cleanup_pid:
  196. batch->child_pid = 0;
  197. close_noerr(inpipe[0]);
  198. close_noerr(inpipe[1]);
  199. close_outpipe:
  200. close_noerr(outpipe[0]);
  201. close_noerr(outpipe[1]);
  202. return false;
  203. }
  204. static bool check_with_child(struct ptr_valid_batch *batch,
  205. const void *p, size_t size, bool is_write)
  206. {
  207. char ret;
  208. if (!child_alive(batch)) {
  209. if (!create_child(batch))
  210. return false;
  211. }
  212. write(batch->to_child, &p, sizeof(p));
  213. write(batch->to_child, &size, sizeof(size));
  214. write(batch->to_child, &is_write, sizeof(is_write));
  215. if (read(batch->from_child, &ret, sizeof(ret)) != sizeof(ret)) {
  216. finish_child(batch);
  217. errno = EFAULT;
  218. return false;
  219. }
  220. return true;
  221. }
  222. /* msync seems most well-defined test, but page could be mapped with
  223. * no permissions, and can't distiguish readonly from writable. */
  224. bool ptr_valid_batch(struct ptr_valid_batch *batch,
  225. const void *p, size_t alignment, size_t size, bool write)
  226. {
  227. char *start, *end;
  228. bool ret;
  229. if ((intptr_t)p & (alignment - 1))
  230. return false;
  231. start = (void *)((intptr_t)p & ~(getpagesize() - 1));
  232. end = (void *)(((intptr_t)p + size - 1) & ~(getpagesize() - 1));
  233. /* We cache single page hits. */
  234. if (start == end) {
  235. if (batch->last && batch->last == start)
  236. return batch->last_ok;
  237. }
  238. if (batch->num_maps)
  239. ret = check_with_maps(batch, p, size, write);
  240. else
  241. ret = check_with_child(batch, p, size, write);
  242. if (start == end) {
  243. batch->last = start;
  244. batch->last_ok = ret;
  245. }
  246. return ret;
  247. }
  248. bool ptr_valid_batch_string(struct ptr_valid_batch *batch, const char *p)
  249. {
  250. while (ptr_valid_batch(batch, p, 1, 1, false)) {
  251. if (*p == '\0')
  252. return true;
  253. p++;
  254. }
  255. return false;
  256. }
  257. bool ptr_valid(const void *p, size_t alignment, size_t size, bool write)
  258. {
  259. bool ret;
  260. struct ptr_valid_batch batch;
  261. if (!ptr_valid_batch_start(&batch))
  262. return false;
  263. ret = ptr_valid_batch(&batch, p, alignment, size, write);
  264. ptr_valid_batch_end(&batch);
  265. return ret;
  266. }
  267. bool ptr_valid_string(const char *p)
  268. {
  269. bool ret;
  270. struct ptr_valid_batch batch;
  271. if (!ptr_valid_batch_start(&batch))
  272. return false;
  273. ret = ptr_valid_batch_string(&batch, p);
  274. ptr_valid_batch_end(&batch);
  275. return ret;
  276. }
  277. bool ptr_valid_batch_start(struct ptr_valid_batch *batch)
  278. {
  279. batch->child_pid = 0;
  280. batch->maps = get_proc_maps(&batch->num_maps);
  281. batch->last = NULL;
  282. return true;
  283. }
  284. void ptr_valid_batch_end(struct ptr_valid_batch *batch)
  285. {
  286. if (child_alive(batch))
  287. finish_child(batch);
  288. free(batch->maps);
  289. }