From 14b5746d8fba392c02733ce4c90befc32a93fb15 Mon Sep 17 00:00:00 2001 From: startx2017 Date: Mon, 12 Mar 2018 08:41:01 -0400 Subject: private-lib bug: 32 bit libraries being copied instead of 64 bit versions; splitting common code for firejail and fldd in a common static library --- src/fldd/main.c | 55 +++++++++++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 34 deletions(-) (limited to 'src/fldd/main.c') diff --git a/src/fldd/main.c b/src/fldd/main.c index a0530c235..be4500d2a 100644 --- a/src/fldd/main.c +++ b/src/fldd/main.c @@ -19,8 +19,8 @@ */ #include "../include/common.h" +#include "../include/ldd_utils.h" -#include #include #include #include @@ -29,36 +29,10 @@ #include #include -#ifdef __LP64__ -#define Elf_Ehdr Elf64_Ehdr -#define Elf_Phdr Elf64_Phdr -#define Elf_Shdr Elf64_Shdr -#define Elf_Dyn Elf64_Dyn -#else -#define Elf_Ehdr Elf32_Ehdr -#define Elf_Phdr Elf32_Phdr -#define Elf_Shdr Elf32_Shdr -#define Elf_Dyn Elf32_Dyn -#endif static int arg_quiet = 0; static void copy_libs_for_lib(const char *lib); -static const char * const default_lib_paths[] = { - "/lib", - "/lib/x86_64-linux-gnu", - "/lib64", - "/usr/lib", - "/usr/lib/x86_64-linux-gnu", - LIBDIR, - "/usr/local/lib", - "/usr/lib/x86_64-linux-gnu/mesa", // libGL.so is sometimes a symlink into this directory - "/usr/lib/x86_64-linux-gnu/mesa-egl", // libGL.so is sometimes a symlink into this directory -// "/usr/lib/x86_64-linux-gnu/plasma-discover", - NULL -}; - - typedef struct storage_t { struct storage_t *next; const char *name; @@ -107,7 +81,8 @@ static bool ptr_ok(const void *ptr, const void *base, const void *end, const cha return r; } -static void copy_libs_for_exe(const char *exe) { + +static void parse_elf(const char *exe) { int f; f = open(exe, O_RDONLY); if (f < 0) { @@ -132,6 +107,12 @@ static void copy_libs_for_exe(const char *exe) { fprintf(stderr, "Warning fldd: %s is not an ELF executable or library\n", exe); goto close; } +//unsigned char elfclass = ebuf->e_ident[EI_CLASS]; +//if (elfclass == ELFCLASS32) +//printf("%s 32bit\n", exe); +//else if (elfclass == ELFCLASS64) +//printf("%s 64bit\n", exe); + Elf_Phdr *pbuf = (Elf_Phdr *)(base + sizeof(*ebuf)); while (ebuf->e_phnum-- > 0 && ptr_ok(pbuf, base, end, "pbuf")) { @@ -227,11 +208,11 @@ static void copy_libs_for_lib(const char *lib) { char *fname; if (asprintf(&fname, "%s/%s", lib_path->name, lib) == -1) errExit("asprintf"); - if (access(fname, R_OK) == 0) { + if (access(fname, R_OK) == 0 && is_lib_64(fname)) { if (!storage_find(libs, fname)) { storage_add(&libs, fname); // libs may need other libs - copy_libs_for_exe(fname); + parse_elf(fname); } free(fname); return; @@ -270,9 +251,9 @@ static void walk_directory(const char *dirname) { // check regular so library char *ptr = strstr(entry->d_name, ".so"); - if (ptr) { + if (ptr && is_lib_64(path)) { if (*(ptr + 3) == '\0' || *(ptr + 3) == '.') { - copy_libs_for_exe(path); + parse_elf(path); free(path); continue; } @@ -356,8 +337,14 @@ printf("\n"); errExit("stat"); if (S_ISDIR(s.st_mode)) walk_directory(argv[1]); - else - copy_libs_for_exe(argv[1]); + else { + if (is_lib_64(argv[1])) + parse_elf(argv[1]); + else { + fprintf(stderr, "Error fldd: %s is not a 64bit program/library\n", argv[1]); + exit(1); + } + } // print libraries and exit -- cgit v1.2.3-54-g00ecf