/[gentoo-projects]/pax-utils/scanelf.c
Gentoo

Diff of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.226 Revision 1.227
1/* 1/*
2 * Copyright 2003-2007 Gentoo Foundation 2 * Copyright 2003-2007 Gentoo Foundation
3 * Distributed under the terms of the GNU General Public License v2 3 * Distributed under the terms of the GNU General Public License v2
4 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.226 2011/09/27 18:37:22 vapier Exp $ 4 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.227 2011/09/27 19:20:51 vapier Exp $
5 * 5 *
6 * Copyright 2003-2007 Ned Ludd - <solar@gentoo.org> 6 * Copyright 2003-2007 Ned Ludd - <solar@gentoo.org>
7 * Copyright 2004-2007 Mike Frysinger - <vapier@gentoo.org> 7 * Copyright 2004-2007 Mike Frysinger - <vapier@gentoo.org>
8 */ 8 */
9 9
10static const char *rcsid = "$Id: scanelf.c,v 1.226 2011/09/27 18:37:22 vapier Exp $"; 10static const char *rcsid = "$Id: scanelf.c,v 1.227 2011/09/27 19:20:51 vapier Exp $";
11const char argv0[] = "scanelf"; 11const char argv0[] = "scanelf";
12 12
13#include "paxinc.h" 13#include "paxinc.h"
14 14
15#define IS_MODIFIER(c) (c == '%' || c == '#' || c == '+') 15#define IS_MODIFIER(c) (c == '%' || c == '#' || c == '+')
28static void parseenv(void); 28static void parseenv(void);
29static int parseargs(int argc, char *argv[]); 29static int parseargs(int argc, char *argv[]);
30 30
31/* variables to control behavior */ 31/* variables to control behavior */
32static char match_etypes[126] = ""; 32static char match_etypes[126] = "";
33static char *ldpaths[256]; 33static array_t _ldpaths = array_init_decl, *ldpaths = &_ldpaths;
34static char scan_ldpath = 0; 34static char scan_ldpath = 0;
35static char scan_envpath = 0; 35static char scan_envpath = 0;
36static char scan_symlink = 1; 36static char scan_symlink = 1;
37static char scan_archives = 0; 37static char scan_archives = 0;
38static char dir_recurse = 0; 38static char dir_recurse = 0;
532 break; 532 break;
533 } 533 }
534} 534}
535static void scanelf_file_rpath(elfobj *elf, char *found_rpath, char **ret, size_t *ret_len) 535static void scanelf_file_rpath(elfobj *elf, char *found_rpath, char **ret, size_t *ret_len)
536{ 536{
537 unsigned long i, s; 537 unsigned long i;
538 char *rpath, *runpath, **r; 538 char *rpath, *runpath, **r;
539 void *strtbl_void; 539 void *strtbl_void;
540 540
541 if (!show_rpath) return; 541 if (!show_rpath) return;
542 542
587 /* scan each path in : delimited list */ \ 587 /* scan each path in : delimited list */ \
588 while (start) { \ 588 while (start) { \
589 rpath_security_checks(elf, start, get_elfdtype(word)); \ 589 rpath_security_checks(elf, start, get_elfdtype(word)); \
590 end = strchr(start, ':'); \ 590 end = strchr(start, ':'); \
591 len = (end ? abs(end - start) : strlen(start)); \ 591 len = (end ? abs(end - start) : strlen(start)); \
592 if (use_ldcache) \ 592 if (use_ldcache) { \
593 for (s = 0; ldpaths[s]; ++s) \ 593 size_t n; \
594 const char *ldpath; \
595 array_for_each(ldpaths, n, ldpath) \
594 if (!strncmp(ldpaths[s], start, len) && !ldpaths[s][len]) { \ 596 if (!strncmp(ldpath, start, len) && !ldpath[len]) { \
595 *r = end; \ 597 *r = end; \
596 /* corner case ... if RPATH reads "/usr/lib:", we want \ 598 /* corner case ... if RPATH reads "/usr/lib:", we want \
597 * to show ':' rather than '' */ \ 599 * to show ':' rather than '' */ \
598 if (end && end[1] != '\0') \ 600 if (end && end[1] != '\0') \
599 (*r)++; \ 601 (*r)++; \
600 break; \ 602 break; \
601 } \ 603 } \
604 } \
602 if (!*r || !end) \ 605 if (!*r || !end) \
603 break; \ 606 break; \
604 else \ 607 else \
605 start = start + len + 1; \ 608 start = start + len + 1; \
606 } \ 609 } \
777#elif defined(__NetBSD__) 780#elif defined(__NetBSD__)
778static char *lookup_cache_lib(elfobj *elf, char *fname) 781static char *lookup_cache_lib(elfobj *elf, char *fname)
779{ 782{
780 static char buf[__PAX_UTILS_PATH_MAX] = ""; 783 static char buf[__PAX_UTILS_PATH_MAX] = "";
781 static struct stat st; 784 static struct stat st;
782 785 size_t n;
783 char **ldpath; 786 char *ldpath;
784 for (ldpath = ldpaths; *ldpath != NULL; ldpath++) { 787
788 array_for_each(ldpath, n, ldpath) {
785 if ((unsigned) snprintf(buf, sizeof(buf), "%s/%s", *ldpath, fname) >= sizeof(buf)) 789 if ((unsigned) snprintf(buf, sizeof(buf), "%s/%s", ldpath, fname) >= sizeof(buf))
786 continue; /* if the pathname is too long, or something went wrong, ignore */ 790 continue; /* if the pathname is too long, or something went wrong, ignore */
787 791
788 if (stat(buf, &st) != 0) 792 if (stat(buf, &st) != 0)
789 continue; /* if the lib doesn't exist in *ldpath, look further */ 793 continue; /* if the lib doesn't exist in *ldpath, look further */
790 794
1625 char path[__PAX_UTILS_PATH_MAX]; 1629 char path[__PAX_UTILS_PATH_MAX];
1626 char _fname[__PAX_UTILS_PATH_MAX]; 1630 char _fname[__PAX_UTILS_PATH_MAX];
1627 1631
1628 fname = maybe_add_root(fname, _fname); 1632 fname = maybe_add_root(fname, _fname);
1629 1633
1630 if (i + 1 == ARRAY_SIZE(ldpaths))
1631 return i;
1632
1633 if ((fp = fopen(fname, "r")) == NULL) 1634 if ((fp = fopen(fname, "r")) == NULL)
1634 return i; 1635 return i;
1635 1636
1636 while ((fgets(path, __PAX_UTILS_PATH_MAX, fp)) != NULL) { 1637 while ((fgets(path, __PAX_UTILS_PATH_MAX, fp)) != NULL) {
1637 if ((p = strrchr(path, '\r')) != NULL) 1638 if ((p = strrchr(path, '\r')) != NULL)
1658 for (x = 0; x < gl.gl_pathc; ++x) { 1659 for (x = 0; x < gl.gl_pathc; ++x) {
1659 /* try to avoid direct loops */ 1660 /* try to avoid direct loops */
1660 if (strcmp(gl.gl_pathv[x], fname) == 0) 1661 if (strcmp(gl.gl_pathv[x], fname) == 0)
1661 continue; 1662 continue;
1662 i = load_ld_cache_config(i, gl.gl_pathv[x]); 1663 i = load_ld_cache_config(i, gl.gl_pathv[x]);
1663 if (i + 1 >= ARRAY_SIZE(ldpaths)) {
1664 globfree(&gl);
1665 return i;
1666 }
1667 } 1664 }
1668 globfree(&gl); 1665 globfree(&gl);
1669 continue; 1666 continue;
1670 } 1667 }
1671 } 1668 }
1672 1669
1673 if (*path != '/') 1670 if (*path != '/')
1674 continue; 1671 continue;
1675 1672
1676 ldpaths[i++] = xstrdup(path); 1673 xarraypush(ldpaths, path, strlen(path));
1677
1678 if (i + 1 == ARRAY_SIZE(ldpaths))
1679 break;
1680 } 1674 }
1681 ldpaths[i] = NULL;
1682 1675
1683 fclose(fp); 1676 fclose(fp);
1684 return i; 1677 return i;
1685} 1678}
1686 1679
1692 char *b = NULL, *p; 1685 char *b = NULL, *p;
1693 struct elfhints_hdr hdr; 1686 struct elfhints_hdr hdr;
1694 char _fname[__PAX_UTILS_PATH_MAX]; 1687 char _fname[__PAX_UTILS_PATH_MAX];
1695 1688
1696 fname = maybe_add_root(fname, _fname); 1689 fname = maybe_add_root(fname, _fname);
1697
1698 if (i + 1 == ARRAY_SIZE(ldpaths))
1699 return i;
1700 1690
1701 if ((fp = fopen(fname, "r")) == NULL) 1691 if ((fp = fopen(fname, "r")) == NULL)
1702 return i; 1692 return i;
1703 1693
1704 if (fread(&hdr, 1, sizeof(hdr), fp) != sizeof(hdr) || 1694 if (fread(&hdr, 1, sizeof(hdr), fp) != sizeof(hdr) ||
1715 free(b); 1705 free(b);
1716 return i; 1706 return i;
1717 } 1707 }
1718 1708
1719 while ((p = strsep(&b, ":"))) { 1709 while ((p = strsep(&b, ":"))) {
1720 if (*p == '\0') continue; 1710 if (*p == '\0')
1721 ldpaths[i++] = xstrdup(p); 1711 continue;
1722 1712 xarraypush(ldpaths, p, strlen(p));
1723 if (i + 1 == ARRAY_SIZE(ldpaths))
1724 break;
1725 } 1713 }
1726 ldpaths[i] = NULL;
1727 1714
1728 free(b); 1715 free(b);
1729 fclose(fp); 1716 fclose(fp);
1730 return i; 1717 return i;
1731} 1718}
1734#ifdef __ELF__ 1721#ifdef __ELF__
1735#warning Cache config support not implemented for your target 1722#warning Cache config support not implemented for your target
1736#endif 1723#endif
1737static int load_ld_cache_config(int i, const char *fname) 1724static int load_ld_cache_config(int i, const char *fname)
1738{ 1725{
1739 memset(ldpaths, 0x00, sizeof(ldpaths));
1740 return 0; 1726 return 0;
1741} 1727}
1742#endif 1728#endif
1743 1729
1744/* scan /etc/ld.so.conf for paths */ 1730/* scan /etc/ld.so.conf for paths */
1745static void scanelf_ldpath(void) 1731static void scanelf_ldpath(void)
1746{ 1732{
1747 char scan_l, scan_ul, scan_ull; 1733 char scan_l, scan_ul, scan_ull;
1734 size_t n;
1735 const char *ldpath;
1748 int i = 0; 1736 int i = 0;
1749 1737
1750 if (!ldpaths[0]) 1738 if (array_cnt(ldpaths) == 0)
1751 err("Unable to load any paths from ld.so.conf"); 1739 err("Unable to load any paths from ld.so.conf");
1752 1740
1753 scan_l = scan_ul = scan_ull = 0; 1741 scan_l = scan_ul = scan_ull = 0;
1754 1742
1755 while (ldpaths[i]) { 1743 array_for_each(ldpaths, n, ldpath) {
1756 if (!scan_l && !strcmp(ldpaths[i], "/lib")) scan_l = 1; 1744 if (!scan_l && !strcmp(ldpath, "/lib")) scan_l = 1;
1757 if (!scan_ul && !strcmp(ldpaths[i], "/usr/lib")) scan_ul = 1; 1745 if (!scan_ul && !strcmp(ldpath, "/usr/lib")) scan_ul = 1;
1758 if (!scan_ull && !strcmp(ldpaths[i], "/usr/local/lib")) scan_ull = 1; 1746 if (!scan_ull && !strcmp(ldpath, "/usr/local/lib")) scan_ull = 1;
1759 scanelf_dir(ldpaths[i]); 1747 scanelf_dir(ldpath);
1760 ++i; 1748 ++i;
1761 } 1749 }
1762 1750
1763 if (!scan_l) scanelf_dir("/lib"); 1751 if (!scan_l) scanelf_dir("/lib");
1764 if (!scan_ul) scanelf_dir("/usr/lib"); 1752 if (!scan_ul) scanelf_dir("/usr/lib");
2139 search_path = argv[optind++]; 2127 search_path = argv[optind++];
2140 ret = scanelf_dir(search_path); 2128 ret = scanelf_dir(search_path);
2141 } 2129 }
2142 2130
2143 /* clean up */ 2131 /* clean up */
2144 for (i = 0; ldpaths[i]; ++i)
2145 free(ldpaths[i]); 2132 xarrayfree(ldpaths);
2133 xarrayfree(find_lib_arr);
2134 xarrayfree(find_section_arr);
2146 free(find_lib); 2135 free(find_lib);
2147 free(find_section); 2136 free(find_section);
2148 2137
2149 if (ldcache != 0) 2138 if (ldcache != 0)
2150 munmap(ldcache, ldcache_size); 2139 munmap(ldcache, ldcache_size);

Legend:
Removed from v.1.226  
changed lines
  Added in v.1.227

  ViewVC Help
Powered by ViewVC 1.1.20