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 #include "native_client/src/trusted/service_runtime/include/bits/mman.h" | |
| 21 | |
| 22 // Extracted from native_client/src/trusted/service_runtime/nacl_config.h. | |
| 23 #if NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 | |
| 24 # if NACL_BUILD_SUBARCH == 64 | |
| 25 # define NACL_ELF_E_MACHINE EM_X86_64 | |
| 26 # elif NACL_BUILD_SUBARCH == 32 | |
| 27 # define NACL_ELF_E_MACHINE EM_386 | |
| 28 # else | |
| 29 # error Unknown platform. | |
| 30 # endif | |
| 31 #elif NACL_ARCH(NACL_BUILD_ARCH) == NACL_arm | |
| 32 # define NACL_ELF_E_MACHINE EM_ARM | |
| 33 #elif NACL_ARCH(NACL_BUILD_ARCH) == NACL_mips | |
| 34 # define NACL_ELF_E_MACHINE EM_MIPS | |
| 35 #else | |
| 36 # error Unknown platform. | |
| 37 #endif | |
| 38 | |
| 39 namespace nacl { | |
| 40 namespace nonsfi { | |
| 41 namespace { | |
| 42 | |
| 43 // Page size for non-SFI Mode. | |
| 44 const ElfW(Addr) kNonSfiPageSize = 4096; | |
| 45 const ElfW(Addr) kNonSfiPageMask = kNonSfiPageSize - 1; | |
| 46 | |
| 47 void DumpElfHeader(const ElfW(Ehdr)& ehdr) { | |
|
Mark Seaborn
2013/12/12 04:49:48
Can you remove this DumpElfHeader logging, please?
hidehiko
2013/12/12 09:26:04
Done.
| |
| 48 #define DUMP(member) \ | |
| 49 #member << " = 0x" << base::HexEncode(&ehdr.member, sizeof(ehdr.member)) | |
| 50 // For expanding and stringify ElfW(Ehdr). | |
| 51 #define QUOTE(name) QUOTE_1(name) | |
| 52 #define QUOTE_1(name) #name | |
| 53 | |
| 54 VLOG(2) << "\n" << | |
| 55 "=================================================\n" | |
| 56 "Elf header\n" | |
| 57 "==================================================\n" << | |
| 58 std::string( | |
| 59 reinterpret_cast<const char*>(ehdr.e_ident + 1), 3) << "\n" << | |
| 60 DUMP(e_type) << "\n" << | |
| 61 DUMP(e_machine) << "\n" << | |
| 62 DUMP(e_version) << "\n" << | |
| 63 DUMP(e_entry) << "\n" << | |
| 64 DUMP(e_phoff) << "\n" << | |
| 65 DUMP(e_shoff) << "\n" << | |
| 66 DUMP(e_flags) << "\n" << | |
| 67 DUMP(e_ehsize) << "\n" << | |
| 68 DUMP(e_phentsize) << "\n" << | |
| 69 DUMP(e_phnum) << "\n" << | |
| 70 DUMP(e_shentsize) << "\n" << | |
| 71 DUMP(e_shnum) << "\n" << | |
| 72 DUMP(e_shstrndx) << "\n" << | |
| 73 "sizeof(" << QUOTE(ElfW(Ehdr)) << ") = " << sizeof(ElfW(Ehdr)); | |
| 74 #undef QUOTE_1 | |
| 75 #undef QUOTE | |
| 76 #undef DUMP | |
| 77 } | |
| 78 | |
| 79 void DumpElfProgramHeader(const ElfW(Phdr)& phdr) { | |
| 80 #define DUMP(member) \ | |
| 81 #member << " = 0x" << base::HexEncode(&phdr.member, sizeof(phdr.member)) | |
| 82 | |
| 83 VLOG(2) << | |
| 84 DUMP(p_type) << "\n" << | |
| 85 DUMP(p_offset) << "\n" << | |
| 86 DUMP(p_vaddr) << "\n" << | |
| 87 DUMP(p_paddr) << "\n" << | |
| 88 DUMP(p_filesz) << "\n" << | |
| 89 DUMP(p_memsz) << "\n" << | |
| 90 DUMP(p_flags) << "\n" << | |
| 91 " (" << ((phdr.p_flags & PF_R) ? "PF_R" : "") << " " | |
| 92 << ((phdr.p_flags & PF_W) ? "PF_W" : "") << " " | |
| 93 << ((phdr.p_flags & PF_W) ? "PF_X" : "") << ")\n" << | |
| 94 DUMP(p_align) << "\n\n"; | |
| 95 #undef DUMP | |
| 96 } | |
| 97 | |
| 98 NonSfiErrorCode ValidateElfHeader(const ElfW(Ehdr)& ehdr) { | |
| 99 if (std::memcmp(ehdr.e_ident, ELFMAG, SELFMAG)) { | |
| 100 LOG(ERROR) << "Bad elf magic"; | |
| 101 return LOAD_BAD_ELF_MAGIC; | |
| 102 } | |
| 103 | |
| 104 #if NACL_BUILD_SUBARCH == 32 | |
| 105 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) { | |
| 106 LOG(ERROR) << "Bad elf class"; | |
| 107 return LOAD_NOT_32_BIT; | |
| 108 } | |
| 109 #elif NACL_BUILD_SUBARCH == 64 | |
| 110 if (ehdr.e_ident[EI_CLASS] != ELFCLASS64) { | |
| 111 LOG(ERROR) << "Bad elf class"; | |
| 112 return LOAD_NOT_64_BIT; | |
| 113 } | |
| 114 #else | |
| 115 # error Unknown platform. | |
| 116 #endif | |
| 117 | |
| 118 if (ehdr.e_type != ET_DYN) { | |
| 119 LOG(ERROR) << "Not a relocatable ELF object (not ET_DYN)"; | |
| 120 return LOAD_NOT_EXEC; | |
| 121 } | |
| 122 | |
| 123 if (ehdr.e_machine != NACL_ELF_E_MACHINE) { | |
| 124 LOG(ERROR) << "Bad machine: " | |
| 125 << base::HexEncode(&ehdr.e_machine, sizeof(ehdr.e_machine)); | |
| 126 return LOAD_BAD_MACHINE; | |
| 127 } | |
| 128 | |
| 129 if (ehdr.e_version != EV_CURRENT) { | |
| 130 LOG(ERROR) << "Bad elf version: " | |
| 131 << base::HexEncode(&ehdr.e_version, sizeof(ehdr.e_version)); | |
| 132 } | |
| 133 | |
| 134 return LOAD_OK; | |
| 135 } | |
| 136 | |
| 137 // Returns the address of the page starting at address 'addr' for non-SFI mode. | |
| 138 ElfW(Addr) GetPageStart(ElfW(Addr) addr) { | |
| 139 return addr & ~kNonSfiPageMask; | |
| 140 } | |
| 141 | |
| 142 // Returns the offset of address 'addr' in its memory page. In other words, | |
| 143 // this equals to 'addr' - GetPageStart(addr). | |
| 144 ElfW(Addr) GetPageOffset(ElfW(Addr) addr) { | |
| 145 return addr & kNonSfiPageMask; | |
| 146 } | |
| 147 | |
| 148 // Returns the address of the next page after address 'addr', unless 'addr' is | |
| 149 // at the start of a page. This equals to: | |
| 150 // addr == GetPageStart(addr) ? addr : GetPageStart(addr) + kNonSfiPageSize | |
| 151 ElfW(Addr) GetPageEnd(ElfW(Addr) addr) { | |
| 152 return GetPageStart(addr + kNonSfiPageSize - 1); | |
| 153 } | |
| 154 | |
| 155 // Converts the pflags (in phdr) to mmap's prot flags. | |
| 156 int PFlagsToProt(int pflags) { | |
| 157 return ((pflags & PF_X) ? PROT_EXEC : 0) | | |
| 158 ((pflags & PF_R) ? PROT_READ : 0) | | |
| 159 ((pflags & PF_W) ? PROT_WRITE : 0); | |
| 160 } | |
| 161 | |
| 162 // Converts the pflags (in phdr) to NaCl ABI's prot flags. | |
| 163 int PFlagsToNaClProt(int pflags) { | |
| 164 return ((pflags & PF_X) ? NACL_ABI_PROT_EXEC : 0) | | |
| 165 ((pflags & PF_R) ? NACL_ABI_PROT_READ : 0) | | |
| 166 ((pflags & PF_W) ? NACL_ABI_PROT_WRITE : 0); | |
| 167 } | |
| 168 | |
| 169 // Returns the load size for the given phdrs, or 0 on error. | |
| 170 ElfW(Addr) GetLoadSize(const ElfW(Phdr)* phdrs, int phnum) { | |
| 171 ElfW(Addr) begin = 0xFFFFFFFFU; | |
| 172 ElfW(Addr) end = 0; | |
| 173 | |
| 174 VLOG(4) << "GetLoadSize: phnum=" << phnum; | |
|
Mark Seaborn
2013/12/12 04:49:48
Can you remove the VLOGs for brevity, please? It'
hidehiko
2013/12/12 09:26:04
Done.
| |
| 175 for (int i = 0; i < phnum; ++i) { | |
| 176 const ElfW(Phdr)& phdr = phdrs[i]; | |
| 177 if (phdr.p_type != PT_LOAD) { | |
| 178 // Do nothing for non PT_LOAD header. | |
| 179 continue; | |
| 180 } | |
| 181 | |
| 182 begin = std::min(begin, phdr.p_vaddr); | |
| 183 end = std::max(end, phdr.p_vaddr + phdr.p_memsz); | |
| 184 } | |
| 185 | |
| 186 if (begin > end) { | |
| 187 // The end address looks overflowing. | |
| 188 return 0; | |
| 189 } | |
| 190 | |
| 191 return GetPageEnd(end) - GetPageStart(begin); | |
| 192 } | |
| 193 | |
| 194 // Reserves the memory for the given phdrs, and stores the memory address, | |
| 195 // its size and bias to the load_start, load_size and load_bias. | |
| 196 NonSfiErrorCode ReserveMemory(const ElfW(Phdr)* phdrs, | |
| 197 int phnum, | |
| 198 ElfW(Addr)* load_bias) { | |
| 199 VLOG(4) << "ReserveMemory"; | |
| 200 | |
| 201 ElfW(Addr) size = GetLoadSize(phdrs, phnum); | |
| 202 if (size == 0) { | |
| 203 LOG(ERROR) << "ReserveMemory failed to calculate size"; | |
| 204 return LOAD_UNLOADABLE; | |
| 205 } | |
| 206 VLOG(4) << "ReserveMemory: size=" << size; | |
| 207 | |
| 208 // Make sure that the given program headers represents PIE binary. | |
| 209 for (int i = 0; i < phnum; ++i) { | |
| 210 if (phdrs[i].p_type == PT_LOAD) { | |
| 211 // Here, phdrs[i] is the first loadable segment. | |
| 212 if (phdrs[i].p_vaddr != 0) { | |
| 213 // The binary is not PIE (i.e. needs to be loaded onto fixed addressed | |
| 214 // memory. We don't support such a case. | |
| 215 LOG(ERROR) | |
| 216 << "Reservememory: Non-PIE binary loading is not supported."; | |
| 217 return LOAD_UNLOADABLE; | |
| 218 } | |
| 219 break; | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 void* start = mmap(0, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
| 224 if (start == MAP_FAILED) { | |
| 225 LOG(ERROR) << "ReserveMemory: failed to mmap."; | |
| 226 return LOAD_NO_MEMORY; | |
| 227 } | |
| 228 | |
| 229 *load_bias = reinterpret_cast<ElfW(Addr)>(start); | |
| 230 VLOG(4) << "ReserveMemory: success"; | |
| 231 return LOAD_OK; | |
| 232 } | |
| 233 | |
| 234 NonSfiErrorCode LoadSegments( | |
| 235 const ElfW(Phdr)* phdrs, int phnum, ElfW(Addr) load_bias, | |
| 236 struct NaClDesc* descriptor) { | |
| 237 for (int i = 0; i < phnum; ++i) { | |
| 238 const ElfW(Phdr)& phdr = phdrs[i]; | |
| 239 if (phdr.p_type != PT_LOAD) { | |
| 240 // Not a load target. | |
| 241 VLOG(4) << "LoadSegments: [" << i << "] Skipped"; | |
| 242 continue; | |
| 243 } | |
| 244 | |
| 245 VLOG(4) << "LoadSegments: [" << i << "] Loading..."; | |
| 246 | |
| 247 // Addresses on the memory. | |
| 248 ElfW(Addr) seg_start = phdr.p_vaddr + load_bias; | |
| 249 ElfW(Addr) seg_end = seg_start + phdr.p_memsz; | |
| 250 ElfW(Addr) seg_page_start = GetPageStart(seg_start); | |
| 251 ElfW(Addr) seg_page_end = GetPageEnd(seg_end); | |
| 252 ElfW(Addr) seg_file_end = seg_start + phdr.p_filesz; | |
| 253 | |
| 254 // Addresses on the file content. | |
| 255 ElfW(Addr) file_start = phdr.p_offset; | |
| 256 ElfW(Addr) file_end = file_start + phdr.p_filesz; | |
| 257 ElfW(Addr) file_page_start = GetPageStart(file_start); | |
| 258 | |
| 259 uintptr_t seg_addr = (*NACL_VTBL(NaClDesc, descriptor)->Map)( | |
| 260 descriptor, | |
| 261 NaClDescEffectorTrustedMem(), | |
| 262 reinterpret_cast<void *>(seg_page_start), | |
| 263 file_end - file_page_start, | |
| 264 PFlagsToNaClProt(phdr.p_flags), | |
| 265 NACL_ABI_MAP_PRIVATE | NACL_ABI_MAP_FIXED, | |
| 266 file_page_start); | |
| 267 if (NaClPtrIsNegErrno(&seg_addr)) { | |
| 268 LOG(ERROR) << "LoadSegments: [" << i << "] mmap failed, " << seg_addr; | |
| 269 return LOAD_NO_MEMORY; | |
| 270 } | |
| 271 | |
| 272 // Fill Zero between the segment end and the page boundary if necessary | |
| 273 // (i.e. if the segment doesn't end on a page boundary). | |
| 274 ElfW(Addr) seg_file_end_offset = GetPageOffset(seg_file_end); | |
| 275 if ((phdr.p_flags & PF_W) && seg_file_end_offset > 0) { | |
| 276 memset(reinterpret_cast<void *>(seg_file_end), 0, | |
| 277 kNonSfiPageSize - seg_file_end_offset); | |
| 278 } | |
| 279 | |
| 280 // Hereafter, seg_file_end is now the first page address after the file | |
| 281 // content. If seg_end is larger, we need to zero anything between them. | |
| 282 // This is done by using a private anonymous mmap for all extra pages. | |
| 283 seg_file_end = GetPageEnd(seg_file_end); | |
| 284 if (seg_page_end > seg_file_end) { | |
| 285 void* zeromap = mmap(reinterpret_cast<void *>(seg_file_end), | |
| 286 seg_page_end - seg_file_end, | |
| 287 PFlagsToProt(phdr.p_flags), | |
| 288 MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, | |
| 289 -1, 0); | |
| 290 if (zeromap == MAP_FAILED) { | |
| 291 LOG(ERROR) << "LoadSegments: [" << i << "] Failed to zeromap."; | |
| 292 return LOAD_NO_MEMORY; | |
| 293 } | |
| 294 } | |
| 295 } | |
| 296 return LOAD_OK; | |
| 297 } | |
| 298 | |
| 299 } // namespace | |
| 300 | |
| 301 struct ElfImage::Data { | |
| 302 // Limit of elf program headers allowed. | |
| 303 enum { | |
| 304 MAX_PROGRAM_HEADERS = 128 | |
| 305 }; | |
| 306 | |
| 307 ElfW(Ehdr) ehdr; | |
| 308 ElfW(Phdr) phdrs[MAX_PROGRAM_HEADERS]; | |
| 309 ElfW(Addr) load_bias; | |
| 310 }; | |
| 311 | |
| 312 ElfImage::ElfImage() { | |
| 313 } | |
| 314 | |
| 315 ElfImage::~ElfImage() { | |
| 316 } | |
| 317 | |
| 318 uintptr_t ElfImage::entry_point() const { | |
| 319 if (!data_) { | |
| 320 LOG(DFATAL) << "entry_point must be called after Read()."; | |
| 321 return 0; | |
| 322 } | |
| 323 return data_->ehdr.e_entry + data_->load_bias; | |
| 324 } | |
| 325 | |
| 326 NonSfiErrorCode ElfImage::Read(struct NaClDesc* descriptor) { | |
| 327 VLOG(3) << "ElfImage::Read"; | |
| 328 DCHECK(!data_); | |
| 329 | |
| 330 ::scoped_ptr<Data> data(new Data); | |
| 331 | |
| 332 // Read elf header. | |
| 333 ssize_t read_ret = (*NACL_VTBL(NaClDesc, descriptor)->PRead)( | |
| 334 descriptor, &data->ehdr, sizeof(data->ehdr), 0); | |
| 335 if (NaClSSizeIsNegErrno(&read_ret) || | |
| 336 static_cast<size_t>(read_ret) != sizeof(data->ehdr)) { | |
| 337 LOG(ERROR) << "Could not load elf headers."; | |
| 338 return LOAD_READ_ERROR; | |
| 339 } | |
| 340 | |
| 341 DumpElfHeader(data->ehdr); | |
| 342 NonSfiErrorCode error_code = ValidateElfHeader(data->ehdr); | |
| 343 if (error_code != LOAD_OK) | |
| 344 return error_code; | |
| 345 | |
| 346 // Read program headers. | |
| 347 if (data->ehdr.e_phnum > Data::MAX_PROGRAM_HEADERS) { | |
| 348 LOG(ERROR) << "Too many program headers"; | |
| 349 return LOAD_TOO_MANY_PROG_HDRS; | |
| 350 } | |
| 351 | |
| 352 if (data->ehdr.e_phentsize != sizeof(data->phdrs[0])) { | |
| 353 LOG(ERROR) << "Bad program headers size\n" | |
| 354 << " ehdr_.e_phentsize = " << data->ehdr.e_phentsize << "\n" | |
| 355 << " sizeof phdrs[0] = " << sizeof(data->phdrs[0]); | |
| 356 return LOAD_BAD_PHENTSIZE; | |
| 357 } | |
| 358 | |
| 359 size_t read_size = data->ehdr.e_phnum * data->ehdr.e_phentsize; | |
| 360 read_ret = (*NACL_VTBL(NaClDesc, descriptor)->PRead)( | |
| 361 descriptor, data->phdrs, read_size, data->ehdr.e_phoff); | |
| 362 | |
| 363 if (NaClSSizeIsNegErrno(&read_ret) || | |
| 364 static_cast<size_t>(read_ret) != read_size) { | |
| 365 LOG(ERROR) << "Cannot load prog headers"; | |
| 366 return LOAD_READ_ERROR; | |
| 367 } | |
| 368 | |
| 369 VLOG(2) << "\n" << | |
| 370 "=================================================\n" | |
| 371 "Elf Program headers\n" | |
| 372 "==================================================\n"; | |
| 373 for (int i = 0; i < data->ehdr.e_phnum; ++i) { | |
| 374 DumpElfProgramHeader(data->phdrs[i]); | |
| 375 } | |
| 376 | |
| 377 data_.swap(data); | |
| 378 return LOAD_OK; | |
| 379 } | |
| 380 | |
| 381 NonSfiErrorCode ElfImage::Load(struct NaClDesc* descriptor) { | |
| 382 VLOG(3) << "ElfImage::Load"; | |
| 383 if (!data_) { | |
| 384 LOG(DFATAL) << "ElfImage::Load() must be called after Read()"; | |
| 385 return LOAD_INTERNAL; | |
| 386 } | |
| 387 | |
| 388 NonSfiErrorCode error = | |
| 389 ReserveMemory(data_->phdrs, data_->ehdr.e_phnum, &data_->load_bias); | |
| 390 if (error != LOAD_OK) { | |
| 391 LOG(ERROR) << "ElfImage::Load: Failed to allocate memory."; | |
| 392 return error; | |
| 393 } | |
| 394 VLOG(3) << "ElfImage::Load: Loader maps the program to 0x" | |
| 395 << base::HexEncode(&data_->load_bias, sizeof(data_->load_bias)); | |
| 396 | |
| 397 error = LoadSegments( | |
| 398 data_->phdrs, data_->ehdr.e_phnum, data_->load_bias, descriptor); | |
| 399 if (error != LOAD_OK) { | |
| 400 LOG(ERROR) << "ElfImage::Load: Failed to load segments"; | |
| 401 return error; | |
| 402 } | |
| 403 | |
| 404 return LOAD_OK; | |
| 405 } | |
| 406 | |
| 407 } // namespace nonsfi | |
| 408 } // namespace nacl | |
| OLD | NEW |