xref: /aosp_15_r20/external/AFLplusplus/utils/libtokencap/libtokencap.so.c (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1 /*
2 
3    american fuzzy lop++ - extract tokens passed to strcmp / memcmp
4    -------------------------------------------------------------
5 
6    Originally written by Michal Zalewski
7 
8    Copyright 2016 Google Inc. All rights reserved.
9    Copyright 2019-2024 AFLplusplus Project. All rights reserved.
10 
11    Licensed under the Apache License, Version 2.0 (the "License");
12    you may not use this file except in compliance with the License.
13    You may obtain a copy of the License at:
14 
15      http://www.apache.org/licenses/LICENSE-2.0
16 
17    This Linux-only companion library allows you to instrument strcmp(),
18    memcmp(), and related functions to automatically extract tokens.
19    See README.tokencap.md for more info.
20 
21  */
22 
23 #ifndef _GNU_SOURCE
24   #define _GNU_SOURCE
25 #endif
26 #include <stdio.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <stdbool.h>
32 
33 #include "../types.h"
34 #include "../config.h"
35 
36 #include "debug.h"
37 
38 #if !defined __linux__ && !defined __APPLE__ && !defined __FreeBSD__ &&      \
39     !defined __OpenBSD__ && !defined __NetBSD__ && !defined __DragonFly__ && \
40     !defined(__HAIKU__) && !defined(__sun)
41   #error "Sorry, this library is unsupported in this platform for now!"
42 #endif /* !__linux__ && !__APPLE__ && ! __FreeBSD__ && ! __OpenBSD__ && \
43           !__NetBSD__*/
44 
45 #if defined __APPLE__
46   #include <mach/vm_map.h>
47   #include <mach/mach_init.h>
48 #elif defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
49   #include <sys/types.h>
50   #include <sys/sysctl.h>
51   #if !defined __NetBSD__
52     #include <sys/user.h>
53   #endif
54   #include <sys/mman.h>
55 #elif defined __HAIKU__
56   #include <kernel/image.h>
57 #elif defined __sun
58 /* For map addresses the old struct is enough */
59   #include <sys/procfs.h>
60   #include <limits.h>
61 #endif
62 
63 #include <dlfcn.h>
64 
65 #ifdef RTLD_NEXT
66 /* The libc functions are a magnitude faster than our replacements.
67    Use them when RTLD_NEXT is available. */
68 int (*__libc_strcmp)(const char *str1, const char *str2);
69 int (*__libc_strncmp)(const char *str1, const char *str2, size_t len);
70 int (*__libc_strcasecmp)(const char *str1, const char *str2);
71 int (*__libc_strncasecmp)(const char *str1, const char *str2, size_t len);
72 int (*__libc_memcmp)(const void *mem1, const void *mem2, size_t len);
73 int (*__libc_bcmp)(const void *mem1, const void *mem2, size_t len);
74 char *(*__libc_strstr)(const char *haystack, const char *needle);
75 char *(*__libc_strcasestr)(const char *haystack, const char *needle);
76 void *(*__libc_memmem)(const void *haystack, size_t haystack_len,
77                        const void *needle, size_t needle_len);
78 #endif
79 
80 /* Mapping data and such */
81 
82 #define MAX_MAPPINGS 1024
83 
84 static struct mapping {
85 
86   void *st, *en;
87 
88 } __tokencap_ro[MAX_MAPPINGS];
89 
90 static u32   __tokencap_ro_cnt;
91 static u8    __tokencap_ro_loaded;
92 static int   __tokencap_out_file = -1;
93 static pid_t __tokencap_pid = -1;
94 
95 /* Identify read-only regions in memory. Only parameters that fall into these
96    ranges are worth dumping when passed to strcmp() and so on. Read-write
97    regions are far more likely to contain user input instead. */
98 
__tokencap_load_mappings(void)99 static void __tokencap_load_mappings(void) {
100 
101 #if defined __linux__
102 
103   u8    buf[MAX_LINE];
104   FILE *f = fopen("/proc/self/maps", "r");
105 
106   __tokencap_ro_loaded = 1;
107 
108   if (!f) return;
109 
110   while (fgets(buf, MAX_LINE, f)) {
111 
112     u8    rf, wf;
113     void *st, *en;
114 
115     if (sscanf(buf, "%p-%p %c%c", &st, &en, &rf, &wf) != 4) continue;
116     if (wf == 'w' || rf != 'r') continue;
117 
118     __tokencap_ro[__tokencap_ro_cnt].st = (void *)st;
119     __tokencap_ro[__tokencap_ro_cnt].en = (void *)en;
120 
121     if (++__tokencap_ro_cnt == MAX_MAPPINGS) break;
122 
123   }
124 
125   fclose(f);
126 
127 #elif defined __APPLE__
128 
129   struct vm_region_submap_info_64 region;
130   mach_msg_type_number_t          cnt = VM_REGION_SUBMAP_INFO_COUNT_64;
131   vm_address_t                    base = 0;
132   vm_size_t                       size = 0;
133   natural_t                       depth = 0;
134 
135   __tokencap_ro_loaded = 1;
136 
137   while (1) {
138 
139     if (vm_region_recurse_64(mach_task_self(), &base, &size, &depth,
140                              (vm_region_info_64_t)&region,
141                              &cnt) != KERN_SUCCESS)
142       break;
143 
144     if (region.is_submap) {
145 
146       depth++;
147 
148     } else {
149 
150       /* We only care of main map addresses and the read only kinds */
151       if ((region.protection & VM_PROT_READ) &&
152           !(region.protection & VM_PROT_WRITE)) {
153 
154         __tokencap_ro[__tokencap_ro_cnt].st = (void *)base;
155         __tokencap_ro[__tokencap_ro_cnt].en = (void *)(base + size);
156 
157         if (++__tokencap_ro_cnt == MAX_MAPPINGS) break;
158 
159       }
160 
161       base += size;
162       size = 0;
163 
164     }
165 
166   }
167 
168 #elif defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
169 
170   #if defined   __FreeBSD__
171   int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, __tokencap_pid};
172   #elif defined __OpenBSD__
173   int mib[] = {CTL_KERN, KERN_PROC_VMMAP, __tokencap_pid};
174   #elif defined __NetBSD__
175   int mib[] = {CTL_VM, VM_PROC, VM_PROC_MAP, __tokencap_pid,
176                sizeof(struct kinfo_vmentry)};
177   #endif
178   char  *buf, *low, *high;
179   size_t miblen = sizeof(mib) / sizeof(mib[0]);
180   size_t len;
181 
182   if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) return;
183 
184   #if defined __FreeBSD__ || defined __NetBSD__
185   len = len * 4 / 3;
186   #elif defined                      __OpenBSD__
187   len -= len % sizeof(struct kinfo_vmentry);
188   #endif
189 
190   buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
191   if (buf == MAP_FAILED) return;
192 
193   if (sysctl(mib, miblen, buf, &len, NULL, 0) == -1) {
194 
195     munmap(buf, len);
196     return;
197 
198   }
199 
200   low = buf;
201   high = low + len;
202 
203   __tokencap_ro_loaded = 1;
204 
205   while (low < high) {
206 
207     struct kinfo_vmentry *region = (struct kinfo_vmentry *)low;
208 
209   #if defined __FreeBSD__ || defined __NetBSD__
210 
211     #if defined   __FreeBSD__
212     size_t size = region->kve_structsize;
213 
214     if (size == 0) break;
215     #elif defined __NetBSD__
216     size_t size = sizeof(*region);
217     #endif
218 
219     /* We go through the whole mapping of the process and track read-only
220      * addresses */
221     if ((region->kve_protection & KVME_PROT_READ) &&
222         !(region->kve_protection & KVME_PROT_WRITE)) {
223 
224   #elif defined __OpenBSD__
225 
226     size_t size = sizeof(*region);
227 
228     /* We go through the whole mapping of the process and track read-only
229      * addresses */
230     if ((region->kve_protection & KVE_PROT_READ) &&
231         !(region->kve_protection & KVE_PROT_WRITE)) {
232 
233   #endif
234       __tokencap_ro[__tokencap_ro_cnt].st = (void *)region->kve_start;
235       __tokencap_ro[__tokencap_ro_cnt].en = (void *)region->kve_end;
236 
237       if (++__tokencap_ro_cnt == MAX_MAPPINGS) break;
238 
239     }
240 
241     low += size;
242 
243   }
244 
245   munmap(buf, len);
246 #elif defined __HAIKU__
247   image_info ii;
248   int32_t    group = 0;
249 
250   __tokencap_ro_loaded = 1;
251 
252   while (get_next_image_info(0, &group, &ii) == B_OK) {
253 
254     __tokencap_ro[__tokencap_ro_cnt].st = ii.text;
255     __tokencap_ro[__tokencap_ro_cnt].en = ((char *)ii.text) + ii.text_size;
256 
257     if (++__tokencap_ro_cnt == MAX_MAPPINGS) break;
258 
259   }
260 
261 #elif defined __sun
262   prmap_t *c, *map;
263   char     path[PATH_MAX];
264   ssize_t  r;
265   size_t   hint;
266   int      fd;
267 
268   snprintf(path, sizeof(path), "/proc/%ld/map", getpid());
269   fd = open(path, O_RDONLY);
270   hint = (1 << 20);
271   map = malloc(hint);
272 
273   __tokencap_ro_loaded = 1;
274 
275   for (; (r = pread(fd, map, hint, 0)) == hint;) {
276 
277     hint <<= 1;
278     map = realloc(map, hint);
279 
280   }
281 
282   for (c = map; r > 0; c++, r -= sizeof(prmap_t)) {
283 
284     __tokencap_ro[__tokencap_ro_cnt].st = (void *)c->pr_vaddr;
285     __tokencap_ro[__tokencap_ro_cnt].en = (void *)(c->pr_vaddr + c->pr_size);
286 
287     if (++__tokencap_ro_cnt == MAX_MAPPINGS) break;
288 
289   }
290 
291   free(map);
292   close(fd);
293 #endif
294 
295 }
296 
297 /* Check an address against the list of read-only mappings. */
298 
299 static u8 __tokencap_is_ro(const void *ptr) {
300 
301   u32 i;
302 
303   if (!__tokencap_ro_loaded) __tokencap_load_mappings();
304 
305   for (i = 0; i < __tokencap_ro_cnt; i++)
306     if (ptr >= __tokencap_ro[i].st && ptr <= __tokencap_ro[i].en) return 1;
307 
308   return 0;
309 
310 }
311 
312 /* Dump an interesting token to output file, quoting and escaping it
313    properly. */
314 
315 static void __tokencap_dump(const u8 *ptr, size_t len, u8 is_text) {
316 
317   u8  buf[MAX_AUTO_EXTRA * 4 + 1];
318   u32 i;
319   u32 pos = 0;
320 
321   if (len < MIN_AUTO_EXTRA || len > MAX_AUTO_EXTRA || __tokencap_out_file == -1)
322     return;
323 
324   for (i = 0; i < len; i++) {
325 
326     if (is_text && !ptr[i]) break;
327 
328     switch (ptr[i]) {
329 
330       case 0 ... 31:
331       case 127 ... 255:
332       case '\"':
333       case '\\':
334 
335         sprintf(buf + pos, "\\x%02x", ptr[i]);
336         pos += 4;
337         break;
338 
339       default:
340         buf[pos++] = ptr[i];
341 
342     }
343 
344   }
345 
346   buf[pos] = 0;
347 
348   int wrt_ok = (1 == write(__tokencap_out_file, "\"", 1));
349   wrt_ok &= (pos == write(__tokencap_out_file, buf, pos));
350   wrt_ok &= (2 == write(__tokencap_out_file, "\"\n", 2));
351 
352   if (!wrt_ok) { DEBUGF("%s", "writing to the token file failed\n"); }
353 
354 }
355 
356 /* Replacements for strcmp(), memcmp(), and so on. Note that these will be used
357    only if the target is compiled with -fno-builtins and linked dynamically. */
358 
359 #undef strcmp
360 
361 __attribute__((hot)) int strcmp(const char *str1, const char *str2) {
362 
363   if (__tokencap_is_ro(str1)) __tokencap_dump(str1, strlen(str1), 1);
364   if (__tokencap_is_ro(str2)) __tokencap_dump(str2, strlen(str2), 1);
365 
366 #ifdef RTLD_NEXT
367   if (__libc_strcmp) return __libc_strcmp(str1, str2);
368 #endif
369 
370   while (1) {
371 
372     const unsigned char c1 = *str1, c2 = *str2;
373 
374     if (c1 != c2) return (c1 > c2) ? 1 : -1;
375     if (!c1) return 0;
376     str1++;
377     str2++;
378 
379   }
380 
381 }
382 
383 #undef strncmp
384 
385 __attribute__((hot)) int strncmp(const char *str1, const char *str2,
386                                  size_t len) {
387 
388   if (__tokencap_is_ro(str1)) __tokencap_dump(str1, len, 1);
389   if (__tokencap_is_ro(str2)) __tokencap_dump(str2, len, 1);
390 
391 #ifdef RTLD_NEXT
392   if (__libc_strncmp) return __libc_strncmp(str1, str2, len);
393 #endif
394 
395   while (len--) {
396 
397     unsigned char c1 = *str1, c2 = *str2;
398 
399     if (c1 != c2) return (c1 > c2) ? 1 : -1;
400     if (!c1) return 0;
401     str1++;
402     str2++;
403 
404   }
405 
406   return 0;
407 
408 }
409 
410 #undef strcasecmp
411 
412 __attribute__((hot)) int strcasecmp(const char *str1, const char *str2) {
413 
414   if (__tokencap_is_ro(str1)) __tokencap_dump(str1, strlen(str1), 1);
415   if (__tokencap_is_ro(str2)) __tokencap_dump(str2, strlen(str2), 1);
416 
417 #ifdef RTLD_NEXT
418   if (__libc_strcasecmp) return __libc_strcasecmp(str1, str2);
419 #endif
420 
421   while (1) {
422 
423     const unsigned char c1 = tolower((int)*str1), c2 = tolower((int)*str2);
424 
425     if (c1 != c2) return (c1 > c2) ? 1 : -1;
426     if (!c1) return 0;
427     str1++;
428     str2++;
429 
430   }
431 
432 }
433 
434 #undef strncasecmp
435 
436 __attribute__((hot)) int strncasecmp(const char *str1, const char *str2,
437                                      size_t len) {
438 
439   if (__tokencap_is_ro(str1)) __tokencap_dump(str1, len, 1);
440   if (__tokencap_is_ro(str2)) __tokencap_dump(str2, len, 1);
441 
442 #ifdef RTLD_NEXT
443   if (__libc_strncasecmp) return __libc_strncasecmp(str1, str2, len);
444 #endif
445 
446   while (len--) {
447 
448     const unsigned char c1 = tolower((int)*str1), c2 = tolower((int)*str2);
449 
450     if (c1 != c2) return (c1 > c2) ? 1 : -1;
451     if (!c1) return 0;
452     str1++;
453     str2++;
454 
455   }
456 
457   return 0;
458 
459 }
460 
461 #undef memcmp
462 
463 __attribute__((hot)) int memcmp(const void *mem1, const void *mem2,
464                                 size_t len) {
465 
466   if (__tokencap_is_ro(mem1)) __tokencap_dump(mem1, len, 0);
467   if (__tokencap_is_ro(mem2)) __tokencap_dump(mem2, len, 0);
468 
469 #ifdef RTLD_NEXT
470   if (__libc_memcmp) return __libc_memcmp(mem1, mem2, len);
471 #endif
472 
473   const char *strmem1 = (const char *)mem1;
474   const char *strmem2 = (const char *)mem2;
475 
476   while (len--) {
477 
478     const unsigned char c1 = *strmem1, c2 = *strmem2;
479     if (c1 != c2) return (c1 > c2) ? 1 : -1;
480     strmem1++;
481     strmem2++;
482 
483   }
484 
485   return 0;
486 
487 }
488 
489 #undef bcmp
490 
491 __attribute__((hot)) int bcmp(const void *mem1, const void *mem2, size_t len) {
492 
493   if (__tokencap_is_ro(mem1)) __tokencap_dump(mem1, len, 0);
494   if (__tokencap_is_ro(mem2)) __tokencap_dump(mem2, len, 0);
495 
496 #ifdef RTLD_NEXT
497   if (__libc_bcmp) return __libc_bcmp(mem1, mem2, len);
498 #endif
499 
500   const char *strmem1 = (const char *)mem1;
501   const char *strmem2 = (const char *)mem2;
502 
503   while (len--) {
504 
505     int diff = *strmem1 ^ *strmem2;
506     if (diff != 0) return 1;
507     strmem1++;
508     strmem2++;
509 
510   }
511 
512   return 0;
513 
514 }
515 
516 #undef strstr
517 
518 __attribute__((hot)) char *strstr(const char *haystack, const char *needle) {
519 
520   if (__tokencap_is_ro(haystack))
521     __tokencap_dump(haystack, strlen(haystack), 1);
522 
523   if (__tokencap_is_ro(needle)) __tokencap_dump(needle, strlen(needle), 1);
524 
525 #ifdef RTLD_NEXT
526   if (__libc_strstr) return __libc_strstr(haystack, needle);
527 #endif
528 
529   do {
530 
531     const char *n = needle;
532     const char *h = haystack;
533 
534     while (*n && *h && *n == *h)
535       n++, h++;
536 
537     if (!*n) return (char *)haystack;
538 
539   } while (*(haystack++));
540 
541   return 0;
542 
543 }
544 
545 #undef strcasestr
546 
547 __attribute__((hot)) char *strcasestr(const char *haystack,
548                                       const char *needle) {
549 
550   if (__tokencap_is_ro(haystack))
551     __tokencap_dump(haystack, strlen(haystack), 1);
552 
553   if (__tokencap_is_ro(needle)) __tokencap_dump(needle, strlen(needle), 1);
554 
555 #ifdef RTLD_NEXT
556   if (__libc_strcasestr) return __libc_strcasestr(haystack, needle);
557 #endif
558 
559   do {
560 
561     const char *n = needle;
562     const char *h = haystack;
563 
564     while (*n && *h && tolower((int)*n) == tolower((int)*h))
565       n++, h++;
566 
567     if (!*n) return (char *)haystack;
568 
569   } while (*(haystack++));
570 
571   return 0;
572 
573 }
574 
575 #undef memmem
576 
577 __attribute__((hot)) void *memmem(const void *haystack, size_t haystack_len,
578                                   const void *needle, size_t needle_len) {
579 
580   if (__tokencap_is_ro(haystack)) __tokencap_dump(haystack, haystack_len, 1);
581 
582   if (__tokencap_is_ro(needle)) __tokencap_dump(needle, needle_len, 1);
583 
584 #ifdef RTLD_NEXT
585   if (__libc_memmem)
586     return __libc_memmem(haystack, haystack_len, needle, needle_len);
587 #endif
588 
589   const char *n = (const char *)needle;
590   const char *h = (const char *)haystack;
591   if (haystack_len < needle_len) return 0;
592   if (needle_len == 0) return (void *)haystack;
593   if (needle_len == 1) return memchr(haystack, *n, haystack_len);
594 
595   const char *end = h + (haystack_len - needle_len);
596 
597   do {
598 
599     if (*h == *n) {
600 
601       if (memcmp(h, n, needle_len) == 0) return (void *)h;
602 
603     }
604 
605   } while (h++ <= end);
606 
607   return 0;
608 
609 }
610 
611 /* Common libraries wrappers (from honggfuzz) */
612 
613 /*
614  * Apache's httpd wrappers
615  */
616 int ap_cstr_casecmp(const char *s1, const char *s2) {
617 
618   return strcasecmp(s1, s2);
619 
620 }
621 
622 int ap_cstr_casecmpn(const char *s1, const char *s2, size_t n) {
623 
624   return strncasecmp(s1, s2, n);
625 
626 }
627 
628 const char *ap_strcasestr(const char *s1, const char *s2) {
629 
630   return strcasestr(s1, s2);
631 
632 }
633 
634 int apr_cstr_casecmp(const char *s1, const char *s2) {
635 
636   return strcasecmp(s1, s2);
637 
638 }
639 
640 int apr_cstr_casecmpn(const char *s1, const char *s2, size_t n) {
641 
642   return strncasecmp(s1, s2, n);
643 
644 }
645 
646 /*
647  * *SSL wrappers
648  */
649 int CRYPTO_memcmp(const void *m1, const void *m2, size_t len) {
650 
651   return memcmp(m1, m2, len);
652 
653 }
654 
655 int OPENSSL_memcmp(const void *m1, const void *m2, size_t len) {
656 
657   return memcmp(m1, m2, len);
658 
659 }
660 
661 int OPENSSL_strcasecmp(const char *s1, const char *s2) {
662 
663   return strcasecmp(s1, s2);
664 
665 }
666 
667 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t len) {
668 
669   return strncasecmp(s1, s2, len);
670 
671 }
672 
673 int32_t memcmpct(const void *s1, const void *s2, size_t len) {
674 
675   return memcmp(s1, s2, len);
676 
677 }
678 
679 /*
680  * libXML wrappers
681  */
682 int xmlStrncmp(const char *s1, const char *s2, int len) {
683 
684   if (len <= 0) { return 0; }
685   if (s1 == s2) { return 0; }
686   if (s1 == NULL) { return -1; }
687   if (s2 == NULL) { return 1; }
688   return strncmp(s1, s2, (size_t)len);
689 
690 }
691 
692 int xmlStrcmp(const char *s1, const char *s2) {
693 
694   if (s1 == s2) { return 0; }
695   if (s1 == NULL) { return -1; }
696   if (s2 == NULL) { return 1; }
697   return strcmp(s1, s2);
698 
699 }
700 
701 int xmlStrEqual(const char *s1, const char *s2) {
702 
703   if (s1 == s2) { return 1; }
704   if (s1 == NULL) { return 0; }
705   if (s2 == NULL) { return 0; }
706   if (strcmp(s1, s2) == 0) { return 1; }
707   return 0;
708 
709 }
710 
711 int xmlStrcasecmp(const char *s1, const char *s2) {
712 
713   if (s1 == s2) { return 0; }
714   if (s1 == NULL) { return -1; }
715   if (s2 == NULL) { return 1; }
716   return strcasecmp(s1, s2);
717 
718 }
719 
720 int xmlStrncasecmp(const char *s1, const char *s2, int len) {
721 
722   if (len <= 0) { return 0; }
723   if (s1 == s2) { return 0; }
724   if (s1 == NULL) { return -1; }
725   if (s2 == NULL) { return 1; }
726   return strncasecmp(s1, s2, (size_t)len);
727 
728 }
729 
730 const char *xmlStrstr(const char *haystack, const char *needle) {
731 
732   if (haystack == NULL) { return NULL; }
733   if (needle == NULL) { return NULL; }
734   return strstr(haystack, needle);
735 
736 }
737 
738 const char *xmlStrcasestr(const char *haystack, const char *needle) {
739 
740   if (haystack == NULL) { return NULL; }
741   if (needle == NULL) { return NULL; }
742   return strcasestr(haystack, needle);
743 
744 }
745 
746 /*
747  * Samba wrappers
748  */
749 int memcmp_const_time(const void *s1, const void *s2, size_t n) {
750 
751   return memcmp(s1, s2, n);
752 
753 }
754 
755 bool strcsequal(const void *s1, const void *s2) {
756 
757   if (s1 == s2) { return true; }
758   if (!s1 || !s2) { return false; }
759   return (strcmp(s1, s2) == 0);
760 
761 }
762 
763 /* bcmp/memcmp BSD flavors, similar to CRYPTO_memcmp */
764 
765 int timingsafe_bcmp(const void *mem1, const void *mem2, size_t len) {
766 
767   return bcmp(mem1, mem2, len);
768 
769 }
770 
771 int timingsafe_memcmp(const void *mem1, const void *mem2, size_t len) {
772 
773   return memcmp(mem1, mem2, len);
774 
775 }
776 
777 /* Init code to open the output file (or default to stderr). */
778 
779 __attribute__((constructor)) void __tokencap_init(void) {
780 
781   u8 *fn = getenv("AFL_TOKEN_FILE");
782   if (fn) __tokencap_out_file = open(fn, O_RDWR | O_CREAT | O_APPEND, 0655);
783   if (__tokencap_out_file == -1) __tokencap_out_file = STDERR_FILENO;
784   __tokencap_pid = getpid();
785 
786 #ifdef RTLD_NEXT
787   __libc_strcmp = dlsym(RTLD_NEXT, "strcmp");
788   __libc_strncmp = dlsym(RTLD_NEXT, "strncmp");
789   __libc_strcasecmp = dlsym(RTLD_NEXT, "strcasecmp");
790   __libc_strncasecmp = dlsym(RTLD_NEXT, "strncasecmp");
791   __libc_memcmp = dlsym(RTLD_NEXT, "memcmp");
792   __libc_bcmp = dlsym(RTLD_NEXT, "bcmp");
793   __libc_strstr = dlsym(RTLD_NEXT, "strstr");
794   __libc_strcasestr = dlsym(RTLD_NEXT, "strcasestr");
795   __libc_memmem = dlsym(RTLD_NEXT, "memmem");
796 #endif
797 
798 }
799 
800 /* closing as best as we can the tokens file */
801 __attribute__((destructor)) void __tokencap_shutdown(void) {
802 
803   if (__tokencap_out_file != STDERR_FILENO) close(__tokencap_out_file);
804 
805 }
806 
807