Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include <string> |
michael@0 | 6 | #include <fstream> |
michael@0 | 7 | #include <errno.h> |
michael@0 | 8 | #include <fcntl.h> |
michael@0 | 9 | #include <stdlib.h> |
michael@0 | 10 | #include <stdio.h> |
michael@0 | 11 | #include <unistd.h> |
michael@0 | 12 | #include <sys/stat.h> |
michael@0 | 13 | |
michael@0 | 14 | #include "elf_symbol_table.h" |
michael@0 | 15 | #include "elf_utils.h" |
michael@0 | 16 | |
michael@0 | 17 | int |
michael@0 | 18 | elf_symbol_table::init(const char *name) |
michael@0 | 19 | { |
michael@0 | 20 | // Open the file readonly. |
michael@0 | 21 | m_fd = open(name, O_RDONLY); |
michael@0 | 22 | if (m_fd < 0) { |
michael@0 | 23 | perror(name); |
michael@0 | 24 | return m_fd; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | // Get its size. |
michael@0 | 28 | struct stat statbuf; |
michael@0 | 29 | if (fstat(m_fd, &statbuf) < 0) { |
michael@0 | 30 | perror(name); |
michael@0 | 31 | return -1; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | m_size = statbuf.st_size; |
michael@0 | 35 | |
michael@0 | 36 | // Memory map it. |
michael@0 | 37 | m_mapping = mmap(0, m_size, PROT_READ, MAP_SHARED, m_fd, 0); |
michael@0 | 38 | if (m_mapping == MAP_FAILED) { |
michael@0 | 39 | perror(name); |
michael@0 | 40 | return -1; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | // Make sure it's an ELF header. |
michael@0 | 44 | const Elf32_Ehdr *ehdr = reinterpret_cast<const Elf32_Ehdr *>(m_mapping); |
michael@0 | 45 | if (elf_verify_header(ehdr) < 0) |
michael@0 | 46 | return -1; |
michael@0 | 47 | |
michael@0 | 48 | const char *mapping = reinterpret_cast<const char *>(m_mapping); |
michael@0 | 49 | |
michael@0 | 50 | // Find the section headers |
michael@0 | 51 | const Elf32_Shdr *shdrs |
michael@0 | 52 | = reinterpret_cast<const Elf32_Shdr *>(mapping + ehdr->e_shoff); |
michael@0 | 53 | |
michael@0 | 54 | // find the section header string table, .shstrtab |
michael@0 | 55 | const Elf32_Shdr *shstrtabsh = shdrs + ehdr->e_shstrndx; |
michael@0 | 56 | const char *shstrtab = mapping + shstrtabsh->sh_offset; |
michael@0 | 57 | |
michael@0 | 58 | // parse the sections we care about |
michael@0 | 59 | int shndx = 0; |
michael@0 | 60 | const Elf32_Shdr *shlimit = shdrs + ehdr->e_shnum; |
michael@0 | 61 | for (const Elf32_Shdr *shdr = shdrs; shdr < shlimit; ++shdr, ++shndx) { |
michael@0 | 62 | basic_string<char> name(shstrtab + shdr->sh_name); |
michael@0 | 63 | if (name == ".symtab") { |
michael@0 | 64 | m_symbols = reinterpret_cast<const Elf32_Sym *>(mapping + shdr->sh_offset); |
michael@0 | 65 | m_nsymbols = shdr->sh_size / sizeof(Elf32_Sym); |
michael@0 | 66 | } |
michael@0 | 67 | else if (name == ".strtab") { |
michael@0 | 68 | m_strtab = mapping + shdr->sh_offset; |
michael@0 | 69 | } |
michael@0 | 70 | else if (name == ".text") { |
michael@0 | 71 | m_text_shndx = shndx; |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | // Parse the symbol table |
michael@0 | 76 | const Elf32_Sym *limit = m_symbols + m_nsymbols; |
michael@0 | 77 | for (const Elf32_Sym *sym = m_symbols; sym < limit; ++sym) { |
michael@0 | 78 | if (is_function(sym)) { |
michael@0 | 79 | #ifdef DEBUG |
michael@0 | 80 | hex(cout); |
michael@0 | 81 | cout << sym->st_value << endl; |
michael@0 | 82 | #endif |
michael@0 | 83 | m_rsymtab.put(sym->st_value, sym->st_value + sym->st_size, sym); |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | return 0; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | int |
michael@0 | 91 | elf_symbol_table::finish() |
michael@0 | 92 | { |
michael@0 | 93 | if (m_mapping != MAP_FAILED) { |
michael@0 | 94 | munmap(m_mapping, m_size); |
michael@0 | 95 | m_mapping = MAP_FAILED; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | if (m_fd >= 0) { |
michael@0 | 99 | close(m_fd); |
michael@0 | 100 | m_fd = -1; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | return 0; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | const Elf32_Sym * |
michael@0 | 107 | elf_symbol_table::lookup(unsigned int addr) const |
michael@0 | 108 | { |
michael@0 | 109 | rsymtab_t::const_iterator result = m_rsymtab.get(addr); |
michael@0 | 110 | return result != m_rsymtab.end() ? reinterpret_cast<const Elf32_Sym *>(*result) : 0; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | const char * |
michael@0 | 114 | elf_symbol_table::get_symbol_name(const Elf32_Sym *sym) const |
michael@0 | 115 | { |
michael@0 | 116 | return m_strtab + sym->st_name; |
michael@0 | 117 | } |