Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

679 lines
21KB

  1. /*
  2. Copyright (c) 2005-2021, Troy D. Hanson http://troydhanson.github.com/uthash/
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  10. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  11. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  12. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  15. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  16. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  17. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19. */
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <inttypes.h>
  27. #include <sys/types.h> /* on OSX, must come before ptrace.h */
  28. #include <sys/ptrace.h>
  29. #include <unistd.h>
  30. #include <sys/wait.h>
  31. #include <assert.h>
  32. #ifdef __FreeBSD__
  33. #include <sys/param.h> /* MAXPATHLEN */
  34. #include <vm/vm.h> /* VM_PROT_* flags */
  35. #endif
  36. #if defined(PT_ATTACH) && !defined(PTRACE_ATTACH)
  37. #define PTRACE_ATTACH PT_ATTACH
  38. #define PTRACE_DETACH PT_DETACH
  39. #endif
  40. /* need this defined so offsetof can give us bloom offsets in UT_hash_table */
  41. #define HASH_BLOOM 16
  42. #include "uthash.h"
  43. #ifdef __FreeBSD__
  44. typedef struct {
  45. void *start;
  46. void *end;
  47. } vma_t;
  48. #else
  49. typedef struct {
  50. off_t start;
  51. off_t end;
  52. char perms[4]; /* rwxp */
  53. char device[5]; /* fd:01 or 00:00 */
  54. } vma_t;
  55. #endif
  56. const uint32_t sig = HASH_SIGNATURE;
  57. int verbose=0;
  58. int getkeys=0;
  59. #define vv(...) do {if (verbose>0) printf(__VA_ARGS__);} while(0)
  60. #define vvv(...) do {if (verbose>1) printf(__VA_ARGS__);} while(0)
  61. /* these id's are arbitrary, only meaningful within this file */
  62. #define JEN 1
  63. #define BER 2
  64. #define SFH 3
  65. #define SAX 4
  66. #define FNV 5
  67. #define OAT 6
  68. #define NUM_HASH_FUNCS 7 /* includes id 0, the non-function */
  69. const char *hash_fcns[] = {"???","JEN","BER","SFH","SAX","FNV","OAT"};
  70. /* given a peer key/len/hashv, reverse engineer its hash function */
  71. static int infer_hash_function(char *key, size_t keylen, uint32_t hashv)
  72. {
  73. uint32_t ohashv;
  74. /* BER SAX FNV OAT JEN SFH */
  75. HASH_JEN(key,keylen,ohashv);
  76. if (ohashv == hashv) {
  77. return JEN;
  78. }
  79. HASH_BER(key,keylen,ohashv);
  80. if (ohashv == hashv) {
  81. return BER;
  82. }
  83. HASH_SFH(key,keylen,ohashv);
  84. if (ohashv == hashv) {
  85. return SFH;
  86. }
  87. HASH_SAX(key,keylen,ohashv);
  88. if (ohashv == hashv) {
  89. return SAX;
  90. }
  91. HASH_FNV(key,keylen,ohashv);
  92. if (ohashv == hashv) {
  93. return FNV;
  94. }
  95. HASH_OAT(key,keylen,ohashv);
  96. if (ohashv == hashv) {
  97. return OAT;
  98. }
  99. return 0;
  100. }
  101. /* read peer's memory from addr for len bytes, store into our dst */
  102. #ifdef __FreeBSD__
  103. static int read_mem(void *dst, pid_t pid, void *start, size_t len)
  104. {
  105. struct ptrace_io_desc io_desc;
  106. int ret;
  107. io_desc.piod_op = PIOD_READ_D;
  108. io_desc.piod_offs = start;
  109. io_desc.piod_addr = dst;
  110. io_desc.piod_len = len;
  111. ret = ptrace(PT_IO, pid, (void *) &io_desc, 0);
  112. if (ret) {
  113. vv("read_mem: ptrace failed: %s\n", strerror(errno));
  114. return -1;
  115. } else if (io_desc.piod_len != len) {
  116. vv("read_mem: short read!\n");
  117. return -1;
  118. }
  119. return 0;
  120. }
  121. #else
  122. static int read_mem(void *dst, int fd, off_t start, size_t len)
  123. {
  124. int rc;
  125. size_t bytes_read=0;
  126. if (lseek(fd, start, SEEK_SET) == (off_t)-1) {
  127. fprintf(stderr, "lseek failed: %s\n", strerror(errno));
  128. return -1;
  129. }
  130. while ( len && ((rc=read(fd, (char*)dst+bytes_read, len)) > 0)) {
  131. len -= rc;
  132. bytes_read += rc;
  133. }
  134. if (rc==-1) {
  135. vv("read_mem failed (%s)\n",strerror(errno));
  136. }
  137. if ((len != 0 && rc >= 0)) {
  138. vv("INTERNAL ERROR\n");
  139. }
  140. return (rc == -1) ? -1 : 0;
  141. }
  142. #endif
  143. /* later compensate for possible presence of bloom filter */
  144. static char *tbl_from_sig_addr(char *sig)
  145. {
  146. return (sig - offsetof(UT_hash_table,signature));
  147. }
  148. #define HS_BIT_TEST(v,i) (v[i/8] & (1U << (i%8)))
  149. static void found(int fd, char* peer_sig, pid_t pid)
  150. {
  151. UT_hash_table *tbl=NULL;
  152. UT_hash_bucket *bkts=NULL;
  153. UT_hash_handle hh;
  154. size_t i, bloom_len, bloom_bitlen, bloom_on_bits=0,bloom_off_bits=0;
  155. char *peer_tbl, *peer_bloom_sig, *peer_bloom_nbits, *peer_bloombv_ptr,
  156. *peer_bloombv, *peer_bkts, *peer_hh, *key=NULL;
  157. const char *peer_key;
  158. const char *hash_fcn = NULL;
  159. unsigned char *bloombv=NULL;
  160. static int fileno=0;
  161. char keyfile[50];
  162. unsigned char bloom_nbits=0;
  163. int keyfd=-1, mode=S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
  164. hash_fcn_hits[NUM_HASH_FUNCS], hash_fcn_winner;
  165. unsigned max_chain=0;
  166. uint32_t bloomsig;
  167. int has_bloom_filter_fields = 0;
  168. for(i=0; i < NUM_HASH_FUNCS; i++) {
  169. hash_fcn_hits[i]=0;
  170. }
  171. if (getkeys) {
  172. snprintf(keyfile, sizeof(keyfile), "/tmp/%u-%u.key", (unsigned)pid,fileno++);
  173. if ( (keyfd = open(keyfile, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
  174. fprintf(stderr, "can't open %s: %s\n", keyfile, strerror(errno));
  175. exit(-1);
  176. }
  177. }
  178. vv("found signature at peer %p\n", (void*)peer_sig);
  179. peer_tbl = tbl_from_sig_addr(peer_sig);
  180. vvv("reading table at peer %p\n", (void*)peer_tbl);
  181. if ( (tbl = (UT_hash_table*)malloc(sizeof(UT_hash_table))) == NULL) {
  182. fprintf(stderr, "out of memory\n");
  183. exit(-1);
  184. }
  185. #ifdef __FreeBSD__
  186. if (read_mem(tbl, pid, (void *)peer_tbl, sizeof(UT_hash_table)) != 0) {
  187. #else
  188. if (read_mem(tbl, fd, (off_t)peer_tbl, sizeof(UT_hash_table)) != 0) {
  189. #endif
  190. fprintf(stderr, "failed to read peer memory\n");
  191. goto done;
  192. }
  193. /* got the table. how about the buckets */
  194. peer_bkts = (char*)tbl->buckets;
  195. vvv("reading %u buckets at peer %p\n", tbl->num_buckets, (void*)peer_bkts);
  196. bkts = (UT_hash_bucket*)malloc(sizeof(UT_hash_bucket)*tbl->num_buckets);
  197. if (bkts == NULL) {
  198. fprintf(stderr, "out of memory\n");
  199. goto done;
  200. }
  201. #ifdef __FreeBSD__
  202. if (read_mem(bkts, pid, (void *)peer_bkts, sizeof(UT_hash_bucket)*tbl->num_buckets) != 0) {
  203. #else
  204. if (read_mem(bkts, fd, (off_t)peer_bkts, sizeof(UT_hash_bucket)*tbl->num_buckets) != 0) {
  205. #endif
  206. fprintf(stderr, "failed to read peer memory\n");
  207. goto done;
  208. }
  209. vvv("scanning %u peer buckets\n", tbl->num_buckets);
  210. for(i=0; i < tbl->num_buckets; i++) {
  211. vvv("bucket %u has %u items\n", (unsigned)i, (unsigned)(bkts[i].count));
  212. if (bkts[i].count > max_chain) {
  213. max_chain = bkts[i].count;
  214. }
  215. if (bkts[i].expand_mult) {
  216. vvv(" bucket %u has expand_mult %u\n", (unsigned)i, (unsigned)(bkts[i].expand_mult));
  217. }
  218. vvv("scanning bucket %u chain:\n", (unsigned)i);
  219. peer_hh = (char*)bkts[i].hh_head;
  220. while(peer_hh) {
  221. #ifdef __FreeBSD__
  222. if (read_mem(&hh, pid, (void *)peer_hh, sizeof(hh)) != 0) {
  223. #else
  224. if (read_mem(&hh, fd, (off_t)peer_hh, sizeof(hh)) != 0) {
  225. #endif
  226. fprintf(stderr, "failed to read peer memory\n");
  227. goto done;
  228. }
  229. if ((char*)hh.tbl != peer_tbl) {
  230. goto done;
  231. }
  232. peer_hh = (char*)hh.hh_next;
  233. peer_key = (const char*)(hh.key);
  234. /* malloc space to read the key, and read it */
  235. if ( (key = (char*)malloc(sizeof(hh.keylen))) == NULL) {
  236. fprintf(stderr, "out of memory\n");
  237. exit(-1);
  238. }
  239. #ifdef __FreeBSD__
  240. if (read_mem(key, pid, (void*)peer_key, hh.keylen) != 0) {
  241. #else
  242. if (read_mem(key, fd, (off_t)peer_key, hh.keylen) != 0) {
  243. #endif
  244. fprintf(stderr, "failed to read peer memory\n");
  245. goto done;
  246. }
  247. hash_fcn_hits[infer_hash_function(key,hh.keylen,hh.hashv)]++;
  248. /* write the key if requested */
  249. if (getkeys) {
  250. write(keyfd, &hh.keylen, sizeof(unsigned));
  251. write(keyfd, key, hh.keylen);
  252. }
  253. free(key);
  254. key=NULL;
  255. }
  256. }
  257. /* does it have a bloom filter? */
  258. peer_bloom_sig = peer_tbl + offsetof(UT_hash_table, bloom_sig);
  259. peer_bloombv_ptr = peer_tbl + offsetof(UT_hash_table, bloom_bv);
  260. peer_bloom_nbits = peer_tbl + offsetof(UT_hash_table, bloom_nbits);
  261. vvv("looking for bloom signature at peer %p\n", (void*)peer_bloom_sig);
  262. #ifdef __FreeBSD__
  263. if ((read_mem(&bloomsig, pid, (void *)peer_bloom_sig, sizeof(uint32_t)) == 0) &&
  264. (bloomsig == HASH_BLOOM_SIGNATURE)) {
  265. #else
  266. if ((read_mem(&bloomsig, fd, (off_t)peer_bloom_sig, sizeof(uint32_t)) == 0) &&
  267. (bloomsig == HASH_BLOOM_SIGNATURE)) {
  268. #endif
  269. vvv("bloom signature (%x) found\n",bloomsig);
  270. /* bloom found. get at bv, nbits */
  271. #ifdef __FreeBSD__
  272. if (read_mem(&bloom_nbits, pid, (void *)peer_bloom_nbits, sizeof(char)) == 0) {
  273. #else
  274. if (read_mem(&bloom_nbits, fd, (off_t)peer_bloom_nbits, sizeof(char)) == 0) {
  275. #endif
  276. /* scan bloom filter, calculate saturation */
  277. bloom_bitlen = (1ULL << bloom_nbits);
  278. bloom_len = (bloom_bitlen / 8) + ((bloom_bitlen % 8) ? 1 : 0);
  279. vvv("bloom bitlen is %u, bloom_bytelen is %u\n", (unsigned)bloom_bitlen, (unsigned)bloom_len);
  280. if ( (bloombv = (unsigned char*)malloc(bloom_len)) == NULL) {
  281. fprintf(stderr, "out of memory\n");
  282. exit(-1);
  283. }
  284. /* read the address of the bitvector in the peer, then read the bv itself */
  285. #ifdef __FreeBSD__
  286. if ((read_mem(&peer_bloombv, pid, (void *)peer_bloombv_ptr, sizeof(void*)) == 0) &&
  287. (read_mem(bloombv, pid, (void *)peer_bloombv, bloom_len) == 0)) {
  288. #else
  289. if ((read_mem(&peer_bloombv, fd, (off_t)peer_bloombv_ptr, sizeof(void*)) == 0) &&
  290. (read_mem(bloombv, fd, (off_t)peer_bloombv, bloom_len) == 0)) {
  291. #endif
  292. /* calculate saturation */
  293. vvv("read peer bloom bitvector from %p (%u bytes)\n", (void*)peer_bloombv, (unsigned)bloom_len);
  294. for(i=0; i < bloom_bitlen; i++) {
  295. if (HS_BIT_TEST(bloombv,(unsigned)i)) {
  296. /* vvv("bit %u set\n",(unsigned)i); */
  297. bloom_on_bits++;
  298. } else {
  299. bloom_off_bits++;
  300. }
  301. }
  302. has_bloom_filter_fields = 1;
  303. vvv("there were %u on_bits among %u total bits\n", (unsigned)bloom_on_bits, (unsigned)bloom_bitlen);
  304. }
  305. }
  306. }
  307. /* choose apparent hash function */
  308. hash_fcn_winner=0;
  309. for(i=0; i<NUM_HASH_FUNCS; i++) {
  310. if (hash_fcn_hits[i] > hash_fcn_hits[hash_fcn_winner]) {
  311. hash_fcn_winner=i;
  312. }
  313. }
  314. hash_fcn = hash_fcns[hash_fcn_winner];
  315. /*
  316. Address ideal items buckets mc fl bloom sat fcn keys saved to
  317. ------------------ ----- -------- -------- -- -- ----- ----- --- -------------
  318. 0x10aa4090 98% 10000000 32000000 10 ok BER /tmp/9110-0.key
  319. 0x10abcdef 100% 10000000 32000000 9 NX 27 12% BER /tmp/9110-1.key
  320. */
  321. printf("Address ideal items buckets mc fl bloom sat fcn keys saved to\n");
  322. printf("------------------ ----- -------- -------- -- -- ----- ----- --- -------------\n");
  323. if (has_bloom_filter_fields) {
  324. printf("%-18p %4.0f%% %8u %8u %2u %2s %5u %4.0f%c %3s %s\n",
  325. (void*)peer_tbl,
  326. (tbl->num_items - tbl->nonideal_items) * 100.0 / tbl->num_items,
  327. tbl->num_items,
  328. tbl->num_buckets,
  329. max_chain,
  330. tbl->noexpand ? "NX" : "ok",
  331. bloom_nbits,
  332. bloom_on_bits * 100.0 / bloom_bitlen, '%',
  333. hash_fcn,
  334. (getkeys ? keyfile : ""));
  335. } else {
  336. printf("%-18p %4.0f%% %8u %8u %2u %2s %5s %4s%c %3s %s\n",
  337. (void*)peer_tbl,
  338. (tbl->num_items - tbl->nonideal_items) * 100.0 / tbl->num_items,
  339. tbl->num_items,
  340. tbl->num_buckets,
  341. max_chain,
  342. tbl->noexpand ? "NX" : "ok",
  343. "",
  344. "", ' ',
  345. hash_fcn,
  346. (getkeys ? keyfile : ""));
  347. }
  348. #if 0
  349. printf("read peer tbl:\n");
  350. printf("num_buckets: %u\n", tbl->num_buckets);
  351. printf("num_items: %u\n", tbl->num_items);
  352. printf("nonideal_items: %u (%.2f%%)\n", tbl->nonideal_items,
  353. tbl->nonideal_items*100.0/tbl->num_items);
  354. printf("expand: %s\n", tbl->noexpand ? "inhibited": "normal");
  355. if (getkeys) {
  356. printf("keys written to %s\n", keyfile);
  357. }
  358. #endif
  359. done:
  360. if (bkts) {
  361. free(bkts);
  362. }
  363. if (tbl) {
  364. free(tbl);
  365. }
  366. if (key) {
  367. free(key);
  368. }
  369. if (keyfd != -1) {
  370. close(keyfd);
  371. }
  372. if (bloombv) {
  373. free(bloombv);
  374. }
  375. }
  376. #ifdef __FreeBSD__
  377. static void sigscan(pid_t pid, void *start, void *end, uint32_t sig)
  378. {
  379. struct ptrace_io_desc io_desc;
  380. int page_size = getpagesize();
  381. char *buf;
  382. char *pos;
  383. /* make sure page_size is a multiple of the signature size, code below assumes this */
  384. assert(page_size % sizeof(sig) == 0);
  385. buf = malloc(page_size);
  386. if (buf == NULL) {
  387. fprintf(stderr, "malloc failed in sigscan()\n");
  388. return;
  389. }
  390. io_desc.piod_op = PIOD_READ_D;
  391. io_desc.piod_offs = start;
  392. io_desc.piod_addr = buf;
  393. io_desc.piod_len = page_size;
  394. /* read in one page after another and search sig */
  395. while(!ptrace(PT_IO, pid, (void *) &io_desc, 0)) {
  396. if (io_desc.piod_len != page_size) {
  397. fprintf(stderr, "PT_IO returned less than page size in sigscan()\n");
  398. return;
  399. }
  400. /* iterate over the the page using the signature size and look for the sig */
  401. for (pos = buf; pos < (buf + page_size); pos += sizeof(sig)) {
  402. if (*(uint32_t *) pos == sig) {
  403. found(pid, (char *) io_desc.piod_offs + (pos - buf), pid);
  404. }
  405. }
  406. /*
  407. * 'end' is inclusive (the address of the last valid byte), so if the current offset
  408. * plus a page is beyond 'end', we're already done. since all vm map entries consist
  409. * of entire pages and 'end' is inclusive, current offset plus one page should point
  410. * exactly one byte beyond 'end'. this is assert()ed below to be on the safe side.
  411. */
  412. if (io_desc.piod_offs + page_size > end) {
  413. assert(io_desc.piod_offs + page_size == (end + 1));
  414. break;
  415. }
  416. /* advance to the next page */
  417. io_desc.piod_offs += page_size;
  418. }
  419. }
  420. #else
  421. static void sigscan(int fd, off_t start, off_t end, uint32_t sig, pid_t pid)
  422. {
  423. int rlen;
  424. uint32_t u;
  425. off_t at=0;
  426. if (lseek(fd, start, SEEK_SET) == (off_t)-1) {
  427. fprintf(stderr, "lseek failed: %s\n", strerror(errno));
  428. return;
  429. }
  430. while ( (rlen = read(fd,&u,sizeof(u))) == sizeof(u)) {
  431. if (!memcmp(&u,&sig,sizeof(u))) {
  432. found(fd, (char*)(start+at),pid);
  433. }
  434. at += sizeof(u);
  435. if ((off_t)(at + sizeof(u)) > end-start) {
  436. break;
  437. }
  438. }
  439. if (rlen == -1) {
  440. //fprintf(stderr,"read failed: %s\n", strerror(errno));
  441. //exit(-1);
  442. }
  443. }
  444. #endif
  445. #ifdef __FreeBSD__
  446. static int scan(pid_t pid)
  447. {
  448. vma_t *vmas=NULL, vma;
  449. unsigned i, num_vmas = 0;
  450. int ret;
  451. struct ptrace_vm_entry vm_entry;
  452. char path[MAXPATHLEN];
  453. vv("attaching to peer\n");
  454. if (ptrace(PT_ATTACH,pid,NULL,0) == -1) {
  455. fprintf(stderr,"failed to attach to %u: %s\n", (unsigned)pid, strerror(errno));
  456. exit(EXIT_FAILURE);
  457. }
  458. vv("waiting for peer to suspend temporarily\n");
  459. if (waitpid(pid,NULL,0) != pid) {
  460. fprintf(stderr,"failed to wait for pid %u: %s\n",(unsigned)pid, strerror(errno));
  461. goto die;
  462. }
  463. /* read memory map using ptrace */
  464. vv("listing peer virtual memory areas\n");
  465. vm_entry.pve_entry = 0;
  466. vm_entry.pve_path = path; /* not used but required to make vm_entry.pve_pathlen work */
  467. while(1) {
  468. /* set pve_pathlen every turn, it gets overwritten by ptrace */
  469. vm_entry.pve_pathlen = MAXPATHLEN;
  470. errno = 0;
  471. ret = ptrace(PT_VM_ENTRY, pid, (void *) &vm_entry, 0);
  472. if (ret) {
  473. if (errno == ENOENT) {
  474. /* we've reached the last entry */
  475. break;
  476. }
  477. fprintf(stderr, "fetching vm map entry failed: %s (%i)\n", strerror(errno), errno);
  478. goto die;
  479. }
  480. vvv("vmmap entry: start: %p, end: %p", (void *) vm_entry.pve_start, (void *) vm_entry.pve_end);
  481. /* skip unreadable or vnode-backed entries */
  482. if (!(vm_entry.pve_prot & VM_PROT_READ) || vm_entry.pve_pathlen > 0) {
  483. vvv(" -> skipped (not readable or vnode-backed)\n");
  484. vm_entry.pve_path[0] = 0;
  485. continue;
  486. }
  487. /* useful entry, add to list */
  488. vvv(" -> will be scanned\n");
  489. vma.start = (void *)vm_entry.pve_start;
  490. vma.end = (void *)vm_entry.pve_end;
  491. vmas = (vma_t *) realloc(vmas, (num_vmas + 1) * sizeof(vma_t));
  492. if (vmas == NULL) {
  493. exit(-1);
  494. }
  495. vmas[num_vmas++] = vma;
  496. }
  497. vv("peer has %u virtual memory areas\n", num_vmas);
  498. /* look for the hash signature */
  499. vv("scanning peer memory for hash table signatures\n");
  500. for(i=0; i<num_vmas; i++) {
  501. vma = vmas[i];
  502. sigscan(pid, vma.start, vma.end, sig);
  503. }
  504. die:
  505. vv("detaching and resuming peer\n");
  506. if (ptrace(PT_DETACH, pid, NULL, 0) == -1) {
  507. fprintf(stderr,"failed to detach from %u: %s\n", (unsigned)pid, strerror(errno));
  508. }
  509. return 0;
  510. }
  511. # else
  512. static int scan(pid_t pid)
  513. {
  514. FILE *mapf;
  515. char mapfile[30], memfile[30], line[100];
  516. vma_t *vmas=NULL, vma;
  517. unsigned i, num_vmas = 0;
  518. int memfd;
  519. void *pstart, *pend, *unused;
  520. /* attach to the target process and wait for it to suspend */
  521. vv("attaching to peer\n");
  522. if (ptrace(PTRACE_ATTACH, pid, NULL, 0) == -1) {
  523. fprintf(stderr,"failed to attach to %u: %s\n", (unsigned)pid, strerror(errno));
  524. exit(-1);
  525. }
  526. vv("waiting for peer to suspend temporarily\n");
  527. if (waitpid(pid,NULL,0) != pid) {
  528. fprintf(stderr,"failed to wait for pid %u: %s\n",(unsigned)pid, strerror(errno));
  529. goto die;
  530. }
  531. /* get ready to open its memory map. this gives us its valid memory areas */
  532. snprintf(mapfile,sizeof(mapfile),"/proc/%u/maps",(unsigned)pid);
  533. snprintf(memfile,sizeof(memfile),"/proc/%u/mem", (unsigned)pid);
  534. vv("opening peer memory map [%s]\n", mapfile);
  535. if ( (mapf = fopen(mapfile,"r")) == NULL) {
  536. fprintf(stderr,"failed to open %s: %s\n", mapfile, strerror(errno));
  537. goto die;
  538. }
  539. vv("listing peer virtual memory areas\n");
  540. while(fgets(line,sizeof(line),mapf)) {
  541. if (sscanf(line, "%p-%p %4c %p %5c", &pstart, &pend, vma.perms,
  542. &unused, vma.device) == 5) {
  543. vma.start = (off_t)pstart;
  544. vma.end = (off_t)pend;
  545. if (vma.perms[0] != 'r') {
  546. continue; /* only readable vma's */
  547. }
  548. if (memcmp(vma.device,"fd",2)==0) {
  549. continue; /* skip mapped files */
  550. }
  551. vmas = (vma_t*)realloc(vmas, (num_vmas+1) * sizeof(vma_t));
  552. if (vmas == NULL) {
  553. exit(-1);
  554. }
  555. vmas[num_vmas++] = vma;
  556. }
  557. }
  558. vv("peer has %u virtual memory areas\n",num_vmas);
  559. fclose(mapf);
  560. /* ok, open up its memory and start looking around in there */
  561. vv("opening peer memory\n");
  562. if ( (memfd=open(memfile,O_RDONLY)) == -1) {
  563. fprintf(stderr,"failed to open %s: %s\n", memfile, strerror(errno));
  564. goto die;
  565. }
  566. /* look for the hash signature */
  567. vv("scanning peer memory for hash table signatures\n");
  568. for(i=0; i<num_vmas; i++) {
  569. vma = vmas[i];
  570. pstart = (void*)vma.start;
  571. pend = (void*)vma.end;
  572. /*fprintf(stderr,"scanning %p-%p %.4s %.5s\n", pstart, pend,
  573. vma.perms, vma.device);*/
  574. sigscan(memfd, vma.start, vma.end, sig, pid);
  575. }
  576. /* done. close memory and detach. this resumes the target process */
  577. close(memfd);
  578. die:
  579. vv("detaching and resuming peer\n");
  580. if (ptrace(PTRACE_DETACH, pid, NULL, 0) == -1) {
  581. fprintf(stderr,"failed to detach from %u: %s\n", (unsigned)pid, strerror(errno));
  582. }
  583. return 0;
  584. }
  585. #endif
  586. static int usage(const char *prog)
  587. {
  588. fprintf(stderr,"usage: %s [-v] [-k] <pid>\n", prog);
  589. return -1;
  590. }
  591. int main(int argc, char *argv[])
  592. {
  593. int opt;
  594. while ( (opt = getopt(argc, argv, "kv")) != -1) {
  595. switch (opt) {
  596. case 'v':
  597. verbose++;
  598. break;
  599. case 'k':
  600. getkeys++;
  601. break;
  602. default:
  603. return usage(argv[0]);
  604. }
  605. }
  606. if (optind < argc) {
  607. pid_t pid = atoi(argv[optind++]);
  608. return scan(pid);
  609. } else {
  610. return usage(argv[0]);
  611. }
  612. }