static int read_callback (void *vstorage, int argc, char **argv, char **col_names) { storage_t *storage = (storage_t *) vstorage; storage_add (storage, argv[0], atoi (argv[1]), atoi (argv[2])); return 0; }
// setup() and loop(): void setup(void) { storage_start(); storage_add(16, "SOME LOCATION"); storage_add(16, "ANOTHER LOCATION"); //bluetooth_start(); communication_start(); gpio_set_mode(GPIOB, 8, GPIO_INPUT_PD); exti_attach_interrupt(AFIO_EXTI_8, AFIO_EXTI_PB, transmit_interrupt, EXTI_RISING); //Onboard LED gpio_set_mode(GPIOB, 1, GPIO_OUTPUT_PP); gpio_write_bit(GPIOB, 1, 1); }
void load_blacklist(void) { if (blacklist_loaded) return; // open filesystem log if (!orig_fopen) orig_fopen = (orig_fopen_t)dlsym(RTLD_NEXT, "fopen"); FILE *fp = orig_fopen(RUN_FSLOGGER_FILE, "r"); if (!fp) return; // extract blacklists char buf[MAXBUF]; int cnt = 0; while (fgets(buf, MAXBUF, fp)) { if (strncmp(buf, "sandbox pid: ", 13) == 0) { char *ptr = strchr(buf, '\n'); if (ptr) *ptr = '\0'; sandbox_pid_str = strdup(buf + 13); } else if (strncmp(buf, "sandbox name: ", 14) == 0) { char *ptr = strchr(buf, '\n'); if (ptr) *ptr = '\0'; sandbox_name_str = strdup(buf + 14); } else if (strncmp(buf, "blacklist ", 10) == 0) { char *ptr = strchr(buf, '\n'); if (ptr) *ptr = '\0'; storage_add(buf + 10); cnt++; } } fclose(fp); blacklist_loaded = 1; #ifdef DEBUG printf("Monitoring %d blacklists\n", cnt); { int i; for (i = 0; i <= HMASK; i++) { int cnt = 0; ListElem *ptr = storage[i]; while (ptr) { cnt++; ptr = ptr->next; } if ((i % 16) == 0) printf("\n"); printf("%02d ", cnt); } printf("\n"); } #endif }
static void copy_libs_for_lib(const char *lib) { Storage *lib_path; for (lib_path = lib_paths; lib_path; lib_path = lib_path->next) { char *fname; if (asprintf(&fname, "%s/%s", lib_path->name, lib) == -1) errExit("asprintf"); if (access(fname, R_OK) == 0 && is_lib_64(fname)) { if (!storage_find(libs, fname)) { storage_add(&libs, fname); // libs may need other libs parse_elf(fname); } free(fname); return; } free(fname); } // log a warning and continue if (!arg_quiet) fprintf(stderr, "Warning fldd: cannot find %s, skipping...\n", lib); }
static void parse_elf(const char *exe) { int f; f = open(exe, O_RDONLY); if (f < 0) { if (!arg_quiet) fprintf(stderr, "Warning fldd: cannot open %s, skipping...\n", exe); return; } struct stat s; char *base = NULL, *end; if (fstat(f, &s) == -1) goto error_close; base = mmap(0, s.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, f, 0); if (base == MAP_FAILED) goto error_close; end = base + s.st_size; Elf_Ehdr *ebuf = (Elf_Ehdr *)base; if (strncmp((const char *)ebuf->e_ident, ELFMAG, SELFMAG) != 0) { if (!arg_quiet) 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")) { switch (pbuf->p_type) { case PT_INTERP: // dynamic loader ld-linux.so if (!ptr_ok(base + pbuf->p_offset, base, end, "base + pbuf->p_offset")) goto close; storage_add(&libs, base + pbuf->p_offset); break; } pbuf++; } Elf_Shdr *sbuf = (Elf_Shdr *)(base + ebuf->e_shoff); if (!ptr_ok(sbuf, base, end, "sbuf")) goto close; // Find strings section char *strbase = NULL; int sections = ebuf->e_shnum; while (sections-- > 0 && ptr_ok(sbuf, base, end, "sbuf")) { if (sbuf->sh_type == SHT_STRTAB) { strbase = base + sbuf->sh_offset; if (!ptr_ok(strbase, base, end, "strbase")) goto close; break; } sbuf++; } if (strbase == NULL) goto error_close; // Find dynamic section sections = ebuf->e_shnum; while (sections-- > 0 && ptr_ok(sbuf, base, end, "sbuf")) { // TODO: running fldd on large gui programs (fldd /usr/bin/transmission-qt) // crash on accessing memory location sbuf->sh_type if sbuf->sh_type in the previous section was 0 (SHT_NULL) // for now we just exit the while loop - this is probably incorrect // printf("sbuf %p #%s#, sections %d, type %u\n", sbuf, exe, sections, sbuf->sh_type); if (!ptr_ok(sbuf, base, end, "sbuf")) goto close; if (sbuf->sh_type == SHT_NULL) break; if (sbuf->sh_type == SHT_DYNAMIC) { Elf_Dyn *dbuf = (Elf_Dyn *)(base + sbuf->sh_offset); if (!ptr_ok(dbuf, base, end, "dbuf")) goto close; // Find DT_RPATH/DT_RUNPATH tags first unsigned long size = sbuf->sh_size; while (size >= sizeof(*dbuf) && ptr_ok(dbuf, base, end, "dbuf")) { if (dbuf->d_tag == DT_RPATH || dbuf->d_tag == DT_RUNPATH) { const char *searchpath = strbase + dbuf->d_un.d_ptr; if (!ptr_ok(searchpath, base, end, "searchpath")) goto close; storage_add(&lib_paths, searchpath); } size -= sizeof(*dbuf); dbuf++; } // Find DT_NEEDED tags dbuf = (Elf_Dyn *)(base + sbuf->sh_offset); size = sbuf->sh_size; while (size >= sizeof(*dbuf) && ptr_ok(dbuf, base, end, "dbuf")) { if (dbuf->d_tag == DT_NEEDED) { const char *lib = strbase + dbuf->d_un.d_ptr; if (!ptr_ok(lib, base, end, "lib")) goto close; copy_libs_for_lib(lib); } size -= sizeof(*dbuf); dbuf++; } } sbuf++; } goto close; error_close: perror("copy libs"); close: if (base) munmap(base, s.st_size); close(f); }
static void lib_paths_init(void) { int i; for (i = 0; default_lib_paths[i]; i++) storage_add(&lib_paths, default_lib_paths[i]); }