Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/nacl/loader/nonsfi/elf_loader.h" | |
| 6 | |
| 7 #include <elf.h> | |
| 8 #include <link.h> | |
| 9 | |
| 10 #include <cstring> | |
| 11 #include <string> | |
| 12 #include <sys/mman.h> | |
| 13 | |
| 14 #include "base/logging.h" | |
| 15 #include "base/strings/string_number_conversions.h" | |
| 16 #include "native_client/src/include/portability.h" | |
| 17 #include "native_client/src/shared/platform/nacl_host_desc.h" | |
| 18 #include "native_client/src/trusted/desc/nacl_desc_base.h" | |
| 19 #include "native_client/src/trusted/desc/nacl_desc_effector_trusted_mem.h" | |
| 20 | |
| 21 // Extracted from native_client/src/trusted/service_runtime/nacl_config.h. | |
| 22 // Also, here we define EM_386 also for x86-64 architecture. | |
| 23 // Currently, on 64bit architecture, we expect the code can be compiled, but | |
| 24 // don't expect that the code runs correctly. We should revisit here, when we | |
|
Mark Seaborn
2013/12/10 19:56:44
I would like to be able to run this on x86-64 to t
hidehiko
2013/12/11 07:56:14
Done.
| |
| 25 // start to implement non-sfi mode on 64bit architecure. Also, we should add | |
| 26 // ARM support, too. | |
| 27 // TODO(hidehiko): Support ARM and 64bit architecture. | |
| 28 #if NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 | |
| 29 # define NACL_ELF_E_MACHINE EM_386 | |
|
Mark Seaborn
2013/12/10 19:56:44
Since you don't define NACL_ELF_E_MACHINE on non-x
hidehiko
2013/12/11 07:56:14
Done, and sorry for my less test.
Ran trybot. If m
| |
| 30 #endif | |
| 31 | |
| 32 // Copied from native_client/src/trusted/service_runtime/include/bits/mman.h | |
|
Mark Seaborn
2013/12/10 19:56:44
This is one header from service_runtime that it's
hidehiko
2013/12/11 07:56:14
I see. Done. Should the file be moved eventually?
| |
| 33 #define NACL_ABI_PROT_READ 0x1 // Page can be read. | |
| 34 #define NACL_ABI_PROT_WRITE 0x2 // Page can be written. | |
| 35 #define NACL_ABI_PROT_EXEC 0x4 // Page can be executed. | |
| 36 #define NACL_ABI_PROT_NONE 0x0 // Page can not be accessed. | |
| 37 #define NACL_ABI_MAP_PRIVATE 0x02 // Changes are private. | |
| 38 #define NACL_ABI_MAP_FIXED 0x10 // Interpret addr exactly. | |
| 39 | |
| 40 namespace nacl { | |
| 41 namespace nonsfi { | |
| 42 namespace { | |
| 43 | |
| 44 // Page size for non-SFI Mode. | |
| 45 const ElfW(Addr) kNonSfiPageSize = 4096; | |
| 46 const ElfW(Addr) kNonSfiPageMask = kNonSfiPageSize - 1; | |
| 47 | |
| 48 void DumpElfHeader(const ElfW(Ehdr)& ehdr) { | |
| 49 #define DUMP(member) \ | |
| 50 #member << " = 0x" << base::HexEncode(&ehdr.member, sizeof(ehdr.member)) | |
| 51 // For expanding and stringify ElfW(Ehdr). | |
| 52 #define QUOTE(name) QUOTE_1(name) | |
| 53 #define QUOTE_1(name) #name | |
| 54 | |
| 55 VLOG(2) << "\n" << | |
| 56 "=================================================\n" | |
| 57 "Elf header\n" | |
| 58 "==================================================\n" << | |
| 59 std::string( | |
| 60 reinterpret_cast<const char*>(ehdr.e_ident + 1), 3) << "\n" << | |
| 61 DUMP(e_type) << "\n" << | |
| 62 DUMP(e_machine) << "\n" << | |
| 63 DUMP(e_version) << "\n" << | |
| 64 DUMP(e_entry) << "\n" << | |
| 65 DUMP(e_phoff) << "\n" << | |
| 66 DUMP(e_shoff) << "\n" << | |
| 67 DUMP(e_flags) << "\n" << | |
| 68 DUMP(e_ehsize) << "\n" << | |
| 69 DUMP(e_phentsize) << "\n" << | |
| 70 DUMP(e_phnum) << "\n" << | |
| 71 DUMP(e_shentsize) << "\n" << | |
| 72 DUMP(e_shnum) << "\n" << | |
| 73 DUMP(e_shstrndx) << "\n" << | |
| 74 "sizeof(" << QUOTE(ElfW(Ehdr)) << ") = " << sizeof(ElfW(Ehdr)); | |
| 75 #undef QUOTE_1 | |
| 76 #undef QUOTE | |
| 77 #undef DUMP | |
| 78 } | |
| 79 | |
| 80 void DumpElfProgramHeader(const ElfW(Phdr)& phdr) { | |
| 81 #define DUMP(member) \ | |
| 82 #member << " = 0x" << base::HexEncode(&phdr.member, sizeof(phdr.member)) | |
| 83 | |
| 84 VLOG(2) << | |
| 85 DUMP(p_type) << "\n" << | |
| 86 DUMP(p_offset) << "\n" << | |
| 87 DUMP(p_vaddr) << "\n" << | |
| 88 DUMP(p_paddr) << "\n" << | |
| 89 DUMP(p_filesz) << "\n" << | |
| 90 DUMP(p_memsz) << "\n" << | |
| 91 DUMP(p_flags) << "\n" << | |
| 92 " (" << ((phdr.p_flags & PF_R) ? "PF_R" : "") << " " | |
| 93 << ((phdr.p_flags & PF_W) ? "PF_W" : "") << " " | |
| 94 << ((phdr.p_flags & PF_W) ? "PF_X" : "") << ")\n" << | |
| 95 DUMP(p_align) << "\n\n"; | |
| 96 #undef DUMP | |
| 97 } | |
| 98 | |
| 99 NonSfiErrorCode ValidateElfHeader(const ElfW(Ehdr)& ehdr) { | |
| 100 if (std::memcmp(ehdr.e_ident, ELFMAG, SELFMAG)) { | |
| 101 LOG(ERROR) << "Bad elf magic"; | |
| 102 return LOAD_BAD_ELF_MAGIC; | |
| 103 } | |
| 104 | |
| 105 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) { | |
| 106 LOG(ERROR) << "Bad elf class"; | |
| 107 return LOAD_NOT_32_BIT; | |
| 108 } | |
| 109 | |
| 110 if (ehdr.e_type != ET_DYN) { | |
| 111 LOG(ERROR) << "Non executable"; | |
|
Mark Seaborn
2013/12/10 19:56:44
Nit: should be something like "Not a relocatable (
hidehiko
2013/12/11 07:56:14
Done.
| |
| 112 return LOAD_NOT_EXEC; | |
| 113 } | |
| 114 | |
| 115 if (ehdr.e_machine != NACL_ELF_E_MACHINE) { | |
| 116 LOG(ERROR) << "Bad machine: " | |
| 117 << base::HexEncode(&ehdr.e_machine, sizeof(ehdr.e_machine)); | |
| 118 return LOAD_BAD_MACHINE; | |
| 119 } | |
| 120 | |
| 121 if (ehdr.e_version != EV_CURRENT) { | |
| 122 LOG(ERROR) << "Bad elf version: " | |
| 123 << base::HexEncode(&ehdr.e_version, sizeof(ehdr.e_version)); | |
| 124 } | |
| 125 | |
| 126 return LOAD_OK; | |
| 127 } | |
| 128 | |
| 129 // Returns the address of the page starting at address 'addr' for non-SFI mode. | |
| 130 ElfW(Addr) GetPageStart(ElfW(Addr) addr) { | |
| 131 return addr & ~kNonSfiPageMask; | |
| 132 } | |
| 133 | |
| 134 // Returns the offset of address 'addr' in its memory page. In other words, | |
| 135 // this equals to 'addr' - GetPageStart(addr). | |
| 136 ElfW(Addr) GetPageOffset(ElfW(Addr) addr) { | |
| 137 return addr & kNonSfiPageMask; | |
| 138 } | |
| 139 | |
| 140 // Returns the address of the next page after address 'addr', unless 'addr' is | |
| 141 // at the start of a page. This equals to: | |
| 142 // addr == GetPageStart(addr) ? addr : GetPageStart(addr) + kNonSfiPageSize | |
| 143 ElfW(Addr) GetPageEnd(ElfW(Addr) addr) { | |
| 144 return GetPageStart(addr + kNonSfiPageSize - 1); | |
| 145 } | |
| 146 | |
| 147 // Converts the pflags (in phdr) to mmap's prot flags. | |
| 148 int PFlagsToProt(int pflags) { | |
| 149 return ((pflags & PF_X) ? PROT_EXEC : 0) | | |
| 150 ((pflags & PF_R) ? PROT_READ : 0) | | |
| 151 ((pflags & PF_W) ? PROT_WRITE : 0); | |
| 152 } | |
| 153 | |
| 154 // Converts the pflags (in phdr) to NaCl ABI's prot flags. | |
| 155 int PFlagsToNaClProt(int pflags) { | |
| 156 return ((pflags & PF_X) ? NACL_ABI_PROT_EXEC : 0) | | |
| 157 ((pflags & PF_R) ? NACL_ABI_PROT_READ : 0) | | |
| 158 ((pflags & PF_W) ? NACL_ABI_PROT_WRITE : 0); | |
| 159 } | |
| 160 | |
| 161 // Returns the load size for the given phdrs, or 0 on error. | |
| 162 ElfW(Addr) GetLoadSize(const ElfW(Phdr)* phdrs, int phnum) { | |
| 163 ElfW(Addr) begin = 0xFFFFFFFFU; | |
| 164 ElfW(Addr) end = 0; | |
| 165 | |
| 166 VLOG(4) << "GetLoadSize: phnum=" << phnum; | |
| 167 for (int i = 0; i < phnum; ++i) { | |
| 168 const ElfW(Phdr)& phdr = phdrs[i]; | |
| 169 if (phdr.p_type != PT_LOAD) { | |
| 170 // Do nothing for non PT_LOAD header. | |
| 171 continue; | |
| 172 } | |
| 173 | |
| 174 begin = std::min(begin, phdr.p_vaddr); | |
| 175 end = std::max(end, phdr.p_vaddr + phdr.p_memsz); | |
| 176 } | |
| 177 | |
| 178 if (begin > end) { | |
| 179 // The end address looks overflowing. | |
| 180 return 0; | |
| 181 } | |
| 182 | |
| 183 return GetPageEnd(end) - GetPageStart(begin); | |
| 184 } | |
| 185 | |
| 186 // Reserves the memory for the given phdrs, and stores the memory address, | |
| 187 // its size and bias to the load_start, load_size and load_bias. | |
| 188 NonSfiErrorCode ReserveMemory(const ElfW(Phdr)* phdrs, | |
| 189 int phnum, | |
| 190 ElfW(Addr)* load_bias) { | |
| 191 VLOG(4) << "ReserveMemory"; | |
| 192 | |
| 193 ElfW(Addr) size = GetLoadSize(phdrs, phnum); | |
| 194 if (size == 0) { | |
| 195 LOG(ERROR) << "ReserveMemory failed to calculate size"; | |
| 196 return LOAD_UNLOADABLE; | |
| 197 } | |
| 198 VLOG(4) << "ReserveMemory: size=" << size; | |
| 199 | |
| 200 // Make sure that the given program headers represents PIE binary. | |
| 201 for (int i = 0; i < phnum; ++i) { | |
| 202 if (phdrs[i].p_type == PT_LOAD) { | |
| 203 // Here, phdrs[i] is the first loadable segment. | |
| 204 if (phdrs[i].p_vaddr != 0) { | |
| 205 // The binary is not PIE (i.e. needs to be loaded onto fixed addressed | |
| 206 // memory. We don't support such a case. | |
| 207 LOG(ERROR) | |
| 208 << "Reservememory: Non-PIE binary loading is not supported."; | |
| 209 return LOAD_UNLOADABLE; | |
| 210 } | |
| 211 break; | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 void* start = mmap(0, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
| 216 if (start == MAP_FAILED) { | |
| 217 LOG(ERROR) << "ReserveMemory: failed to mmap."; | |
| 218 return LOAD_NO_MEMORY; | |
| 219 } | |
| 220 | |
| 221 *load_bias = reinterpret_cast<ElfW(Addr)>(start); | |
| 222 VLOG(4) << "ReserveMemory: success"; | |
| 223 return LOAD_OK; | |
| 224 } | |
| 225 | |
| 226 NonSfiErrorCode LoadSegments( | |
| 227 const ElfW(Phdr)* phdrs, int phnum, ElfW(Addr) load_bias, | |
| 228 struct NaClDesc* descriptor) { | |
| 229 for (int i = 0; i < phnum; ++i) { | |
| 230 const ElfW(Phdr)& phdr = phdrs[i]; | |
| 231 if (phdr.p_type != PT_LOAD) { | |
| 232 // Not a load target. | |
| 233 VLOG(4) << "LoadSegments: [" << i << "] Skipped"; | |
| 234 continue; | |
| 235 } | |
| 236 | |
| 237 VLOG(4) << "LoadSegments: [" << i << "] Loading..."; | |
| 238 | |
| 239 // Addresses on the memory. | |
| 240 ElfW(Addr) seg_start = phdr.p_vaddr + load_bias; | |
| 241 ElfW(Addr) seg_end = seg_start + phdr.p_memsz; | |
| 242 ElfW(Addr) seg_page_start = GetPageStart(seg_start); | |
| 243 ElfW(Addr) seg_page_end = GetPageEnd(seg_end); | |
| 244 ElfW(Addr) seg_file_end = seg_start + phdr.p_filesz; | |
| 245 | |
| 246 // Addresses on the file content. | |
| 247 ElfW(Addr) file_start = phdr.p_offset; | |
| 248 ElfW(Addr) file_end = file_start + phdr.p_filesz; | |
| 249 ElfW(Addr) file_page_start = GetPageStart(file_start); | |
| 250 | |
| 251 uintptr_t seg_addr = (*NACL_VTBL(NaClDesc, descriptor)->Map)( | |
| 252 descriptor, | |
| 253 NaClDescEffectorTrustedMem(), | |
| 254 reinterpret_cast<void *>(seg_page_start), | |
| 255 file_end - file_page_start, | |
| 256 PFlagsToNaClProt(phdr.p_flags), | |
| 257 NACL_ABI_MAP_PRIVATE | NACL_ABI_MAP_FIXED, | |
| 258 file_page_start); | |
| 259 if (NaClPtrIsNegErrno(&seg_addr)) { | |
| 260 LOG(ERROR) << "LoadSegments: [" << i << "] mmap failed, " << seg_addr; | |
| 261 return LOAD_NO_MEMORY; | |
| 262 } | |
| 263 | |
| 264 // Fill Zero between the segment end and the page boundary if necessary | |
| 265 // (i.e. if the segment doesn't end on a page boundary). | |
| 266 ElfW(Addr) seg_file_end_offset = GetPageOffset(seg_file_end); | |
| 267 if ((phdr.p_flags & PF_W) && seg_file_end_offset > 0) { | |
| 268 memset(reinterpret_cast<void *>(seg_file_end), 0, | |
| 269 kNonSfiPageSize - seg_file_end_offset); | |
| 270 } | |
| 271 | |
| 272 // Hereafter, seg_file_end is now the first page address after the file | |
| 273 // content. If seg_end is larger, we need to zero anything between them. | |
| 274 // This is done by using a private anonymous mmap for all extra pages. | |
| 275 seg_file_end = GetPageEnd(seg_file_end); | |
| 276 if (seg_page_end > seg_file_end) { | |
| 277 void* zeromap = mmap(reinterpret_cast<void *>(seg_file_end), | |
| 278 seg_page_end - seg_file_end, | |
| 279 PFlagsToProt(phdr.p_flags), | |
| 280 MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, | |
| 281 -1, 0); | |
| 282 if (zeromap == MAP_FAILED) { | |
| 283 LOG(ERROR) << "LoadSegments: [" << i << "] Failed to zeromap."; | |
| 284 return LOAD_NO_MEMORY; | |
| 285 } | |
| 286 } | |
| 287 } | |
| 288 return LOAD_OK; | |
| 289 } | |
| 290 | |
| 291 } // namespace | |
| 292 | |
| 293 ElfImage::ElfImage() { | |
| 294 } | |
| 295 | |
| 296 ElfImage::~ElfImage() { | |
| 297 } | |
| 298 | |
| 299 NonSfiErrorCode ElfImage::Read(struct NaClDesc* descriptor) { | |
| 300 // Read elf header. | |
| 301 ssize_t read_ret = (*NACL_VTBL(NaClDesc, descriptor)->PRead)( | |
| 302 descriptor, &ehdr_, sizeof(ehdr_), 0); | |
| 303 if (NaClSSizeIsNegErrno(&read_ret) || | |
| 304 static_cast<size_t>(read_ret) != sizeof(ehdr_)) { | |
| 305 LOG(ERROR) << "Could not load elf headers."; | |
| 306 return LOAD_READ_ERROR; | |
| 307 } | |
| 308 | |
| 309 DumpElfHeader(ehdr_); | |
| 310 NonSfiErrorCode error_code = ValidateElfHeader(ehdr_); | |
| 311 if (error_code != LOAD_OK) | |
| 312 return error_code; | |
| 313 | |
| 314 // Read program headers. | |
| 315 if (ehdr_.e_phnum > MAX_PROGRAM_HEADERS) { | |
| 316 LOG(ERROR) << "Too many program headers"; | |
| 317 return LOAD_TOO_MANY_PROG_HDRS; | |
| 318 } | |
| 319 | |
| 320 if (ehdr_.e_phentsize != sizeof(phdrs_[0])) { | |
| 321 LOG(ERROR) << "Bad program headers size\n" | |
| 322 << " ehdr_.e_phentsize = " << ehdr_.e_phentsize << "\n" | |
| 323 << " sizeof phdrs_[0] = " << sizeof(phdrs_[0]); | |
| 324 return LOAD_BAD_PHENTSIZE; | |
| 325 } | |
| 326 | |
| 327 size_t read_size = ehdr_.e_phnum * ehdr_.e_phentsize; | |
| 328 read_ret = (*NACL_VTBL(NaClDesc, descriptor)->PRead)( | |
| 329 descriptor, phdrs_, read_size, ehdr_.e_phoff); | |
| 330 | |
| 331 if (NaClSSizeIsNegErrno(&read_ret) || | |
| 332 static_cast<size_t>(read_ret) != read_size) { | |
| 333 LOG(ERROR) << "Cannot load prog headers"; | |
| 334 return LOAD_READ_ERROR; | |
| 335 } | |
| 336 | |
| 337 VLOG(2) << "\n" << | |
| 338 "=================================================\n" | |
| 339 "Elf Program headers\n" | |
| 340 "==================================================\n"; | |
| 341 for (int i = 0; i < ehdr_.e_phnum; ++i) { | |
| 342 DumpElfProgramHeader(phdrs_[i]); | |
| 343 } | |
| 344 | |
| 345 return LOAD_OK; | |
| 346 } | |
| 347 | |
| 348 NonSfiErrorCode ElfImage::Load(struct NaClDesc* descriptor) { | |
| 349 VLOG(3) << "ElfImage::Load"; | |
| 350 | |
| 351 NonSfiErrorCode error = ReserveMemory(phdrs_, ehdr_.e_phnum, &load_bias_); | |
| 352 if (error != LOAD_OK) { | |
| 353 LOG(ERROR) << "ElfImage::Load: Failed to allocate memory."; | |
| 354 return error; | |
| 355 } | |
| 356 VLOG(3) << "ElfImage::Load: Loader maps the program to 0x" | |
| 357 << base::HexEncode(&load_bias_, sizeof(load_bias_)); | |
| 358 | |
| 359 error = LoadSegments(phdrs_, ehdr_.e_phnum, load_bias_, descriptor); | |
| 360 if (error != LOAD_OK) { | |
| 361 LOG(ERROR) << "ElfImage::Load: Failed to load segments"; | |
| 362 return error; | |
| 363 } | |
| 364 | |
| 365 return LOAD_OK; | |
| 366 } | |
| 367 | |
| 368 } // namespace nonsfi | |
| 369 } // namespace nacl | |
| OLD | NEW |