2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/util/minielf.h"
|
|
|
|
|
|
|
|
#include <elf.h>
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <memory>
|
2021-11-18 18:15:53 +08:00
|
|
|
#include <type_traits>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include "absl/base/internal/endian.h"
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-10-16 04:25:02 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/match.h"
|
|
|
|
#include "absl/strings/str_cat.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/util.h"
|
2021-07-27 19:09:37 +08:00
|
|
|
#include "sandboxed_api/util/os_error.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace host_cpu = ::sapi::host_cpu;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
namespace sandbox2 {
|
|
|
|
|
2020-12-17 01:17:53 +08:00
|
|
|
using ElfEhdr = std::conditional_t<host_cpu::Is64Bit(), Elf64_Ehdr, Elf32_Ehdr>;
|
|
|
|
using ElfShdr = std::conditional_t<host_cpu::Is64Bit(), Elf64_Shdr, Elf32_Shdr>;
|
|
|
|
using ElfPhdr = std::conditional_t<host_cpu::Is64Bit(), Elf64_Phdr, Elf32_Phdr>;
|
|
|
|
using ElfDyn = std::conditional_t<host_cpu::Is64Bit(), Elf64_Dyn, Elf32_Dyn>;
|
|
|
|
using ElfSym = std::conditional_t<host_cpu::Is64Bit(), Elf64_Sym, Elf32_Sym>;
|
|
|
|
|
|
|
|
constexpr int kElfHeaderSize = sizeof(ElfEhdr); // Maximum size for binaries
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
constexpr char kElfMagic[] =
|
|
|
|
"\x7F"
|
|
|
|
"ELF";
|
|
|
|
|
|
|
|
constexpr int kEiClassOffset = 0x04;
|
2020-12-17 01:17:53 +08:00
|
|
|
constexpr int kEiClass = host_cpu::Is64Bit() ? ELFCLASS64 : ELFCLASS32;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
constexpr int kEiDataOffset = 0x05;
|
|
|
|
constexpr int kEiDataLittle = 1; // Little Endian
|
|
|
|
constexpr int kEiDataBig = 2; // Big Endian
|
|
|
|
|
|
|
|
constexpr int kEiVersionOffset = 0x06;
|
|
|
|
constexpr int kEvCurrent = 1; // ELF version
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// NOLINTNEXTLINE
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status CheckedFSeek(FILE* f, long offset, int whence) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (fseek(f, offset, whence)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2021-07-27 19:09:37 +08:00
|
|
|
sapi::OsErrorMessage(errno, "Fseek on ELF failed"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status CheckedFRead(void* dst, size_t size, size_t nmemb, FILE* f) {
|
2021-04-28 22:48:48 +08:00
|
|
|
if (std::fread(dst, size, nmemb, f) == nmemb) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2021-07-27 19:09:37 +08:00
|
|
|
sapi::OsErrorMessage(errno, "Reading ELF data failed"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status CheckedRead(std::string* s, FILE* f) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return CheckedFRead(&(*s)[0], 1, s->size(), f);
|
|
|
|
}
|
|
|
|
|
|
|
|
absl::string_view ReadName(uint32_t offset, absl::string_view strtab) {
|
|
|
|
auto name = strtab.substr(offset);
|
|
|
|
return name.substr(0, name.find('\0'));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#define LOAD_MEMBER(data_struct, member, src) \
|
|
|
|
Load(&(data_struct).member, \
|
|
|
|
&src[offsetof(std::remove_reference<decltype(data_struct)>::type, \
|
|
|
|
member)])
|
|
|
|
|
|
|
|
class ElfParser {
|
|
|
|
public:
|
|
|
|
// Arbitrary cut-off values, so we can parse safely.
|
|
|
|
static constexpr int kMaxProgramHeaderEntries = 500;
|
|
|
|
static constexpr int kMaxSectionHeaderEntries = 500;
|
2020-08-06 21:03:46 +08:00
|
|
|
static constexpr size_t kMaxSectionSize = 200 * 1024 * 1024;
|
2019-03-19 00:21:48 +08:00
|
|
|
static constexpr size_t kMaxStrtabSize = 500 * 1024 * 1024;
|
|
|
|
static constexpr size_t kMaxLibPathSize = 1024;
|
2020-08-06 21:03:46 +08:00
|
|
|
static constexpr int kMaxSymbolEntries = 2 * 1000 * 1000;
|
2019-03-19 00:21:48 +08:00
|
|
|
static constexpr int kMaxDynamicEntries = 10000;
|
|
|
|
static constexpr size_t kMaxInterpreterSize = 1000;
|
|
|
|
|
2021-04-28 22:48:48 +08:00
|
|
|
static absl::StatusOr<ElfFile> Parse(const std::string& filename,
|
|
|
|
uint32_t features);
|
|
|
|
|
|
|
|
~ElfParser() {
|
|
|
|
if (elf_) {
|
|
|
|
std::fclose(elf_);
|
|
|
|
}
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
private:
|
2021-04-28 22:48:48 +08:00
|
|
|
ElfParser() = default;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Endianess support functions
|
|
|
|
uint16_t Load16(const void* src) {
|
|
|
|
return elf_little_ ? absl::little_endian::Load16(src)
|
|
|
|
: absl::big_endian::Load16(src);
|
|
|
|
}
|
|
|
|
uint32_t Load32(const void* src) {
|
|
|
|
return elf_little_ ? absl::little_endian::Load32(src)
|
|
|
|
: absl::big_endian::Load32(src);
|
|
|
|
}
|
|
|
|
uint64_t Load64(const void* src) {
|
|
|
|
return elf_little_ ? absl::little_endian::Load64(src)
|
|
|
|
: absl::big_endian::Load64(src);
|
|
|
|
}
|
2021-04-28 22:48:48 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
template <size_t N>
|
|
|
|
void Load(unsigned char (*dst)[N], const void* src) {
|
|
|
|
memcpy(dst, src, N);
|
|
|
|
}
|
2021-11-18 18:15:53 +08:00
|
|
|
|
|
|
|
template <typename IntT>
|
|
|
|
std::enable_if_t<std::is_integral_v<IntT>, void> Load(IntT* dst,
|
|
|
|
const void* src) {
|
|
|
|
switch (sizeof(IntT)) {
|
|
|
|
case 1:
|
|
|
|
*dst = *reinterpret_cast<const char*>(src);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*dst = Load16(src);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*dst = Load32(src);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
*dst = Load64(src);
|
|
|
|
break;
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 22:48:48 +08:00
|
|
|
// Reads ELF file size.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ReadFileSize();
|
2021-04-28 22:48:48 +08:00
|
|
|
// Reads ELF header.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ReadFileHeader();
|
2021-04-28 22:48:48 +08:00
|
|
|
// Reads a single ELF program header.
|
2020-12-17 01:17:53 +08:00
|
|
|
absl::StatusOr<ElfPhdr> ReadProgramHeader(absl::string_view src);
|
2021-04-28 22:48:48 +08:00
|
|
|
// Reads all ELF program headers.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ReadProgramHeaders();
|
2021-04-28 22:48:48 +08:00
|
|
|
// Reads a single ELF section header.
|
2020-12-17 01:17:53 +08:00
|
|
|
absl::StatusOr<ElfShdr> ReadSectionHeader(absl::string_view src);
|
2021-04-28 22:48:48 +08:00
|
|
|
// Reads all ELF section headers.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ReadSectionHeaders();
|
2021-04-28 22:48:48 +08:00
|
|
|
// Reads contents of an ELF section.
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::string> ReadSectionContents(int idx);
|
|
|
|
absl::StatusOr<std::string> ReadSectionContents(
|
2020-12-17 01:17:53 +08:00
|
|
|
const ElfShdr& section_header);
|
2019-03-19 00:21:48 +08:00
|
|
|
// Reads all symbols from symtab section.
|
2020-12-17 01:17:53 +08:00
|
|
|
absl::Status ReadSymbolsFromSymtab(const ElfShdr& symtab);
|
2019-03-19 00:21:48 +08:00
|
|
|
// Reads all imported libraries from dynamic section.
|
2020-12-17 01:17:53 +08:00
|
|
|
absl::Status ReadImportedLibrariesFromDynamic(const ElfShdr& dynamic);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
ElfFile result_;
|
|
|
|
FILE* elf_ = nullptr;
|
|
|
|
size_t file_size_ = 0;
|
|
|
|
bool elf_little_ = false;
|
2020-12-17 01:17:53 +08:00
|
|
|
ElfEhdr file_header_;
|
|
|
|
std::vector<ElfPhdr> program_headers_;
|
|
|
|
std::vector<ElfShdr> section_headers_;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
int symbol_entries_read = 0;
|
|
|
|
int dynamic_entries_read = 0;
|
|
|
|
};
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ElfParser::ReadFileSize() {
|
2021-04-28 22:48:48 +08:00
|
|
|
std::fseek(elf_, 0, SEEK_END);
|
|
|
|
file_size_ = std::ftell(elf_);
|
2019-03-19 00:21:48 +08:00
|
|
|
if (file_size_ < kElfHeaderSize) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("file too small: ", file_size_, " bytes, at least ",
|
|
|
|
kElfHeaderSize, " bytes expected"));
|
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ElfParser::ReadFileHeader() {
|
2019-03-19 00:21:48 +08:00
|
|
|
std::string header(kElfHeaderSize, '\0');
|
|
|
|
SAPI_RETURN_IF_ERROR(CheckedFSeek(elf_, 0, SEEK_SET));
|
|
|
|
SAPI_RETURN_IF_ERROR(CheckedRead(&header, elf_));
|
|
|
|
|
|
|
|
if (!absl::StartsWith(header, kElfMagic)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("magic not found, not an ELF");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-12-17 01:17:53 +08:00
|
|
|
if (header[kEiClassOffset] != kEiClass) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("invalid ELF class");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
const auto elf_data = header[kEiDataOffset];
|
|
|
|
elf_little_ = elf_data == kEiDataLittle;
|
|
|
|
if (!elf_little_ && elf_data != kEiDataBig) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("invalid endianness");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (header[kEiVersionOffset] != kEvCurrent) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("invalid ELF version");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
LOAD_MEMBER(file_header_, e_ident, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_type, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_machine, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_version, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_entry, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_phoff, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_shoff, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_flags, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_ehsize, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_phentsize, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_phnum, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_shentsize, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_shnum, header.data());
|
|
|
|
LOAD_MEMBER(file_header_, e_shstrndx, header.data());
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-12-17 01:17:53 +08:00
|
|
|
absl::StatusOr<ElfShdr> ElfParser::ReadSectionHeader(absl::string_view src) {
|
|
|
|
if (src.size() < sizeof(ElfShdr)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid section header data: got ", src.size(),
|
2020-12-17 01:17:53 +08:00
|
|
|
" bytes, ", sizeof(ElfShdr), " bytes expected."));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-12-17 01:17:53 +08:00
|
|
|
ElfShdr rv;
|
2019-03-19 00:21:48 +08:00
|
|
|
LOAD_MEMBER(rv, sh_name, src.data());
|
|
|
|
LOAD_MEMBER(rv, sh_type, src.data());
|
|
|
|
LOAD_MEMBER(rv, sh_flags, src.data());
|
|
|
|
LOAD_MEMBER(rv, sh_addr, src.data());
|
|
|
|
LOAD_MEMBER(rv, sh_offset, src.data());
|
|
|
|
LOAD_MEMBER(rv, sh_size, src.data());
|
|
|
|
LOAD_MEMBER(rv, sh_link, src.data());
|
|
|
|
LOAD_MEMBER(rv, sh_info, src.data());
|
|
|
|
LOAD_MEMBER(rv, sh_addralign, src.data());
|
|
|
|
LOAD_MEMBER(rv, sh_entsize, src.data());
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ElfParser::ReadSectionHeaders() {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (file_header_.e_shoff > file_size_) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid section header offset: ", file_header_.e_shoff));
|
|
|
|
}
|
2020-12-17 01:17:53 +08:00
|
|
|
if (file_header_.e_shentsize != sizeof(ElfShdr)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(absl::StrCat(
|
2019-03-19 00:21:48 +08:00
|
|
|
"section header entry size incorrect: ", file_header_.e_shentsize,
|
2020-12-17 01:17:53 +08:00
|
|
|
" bytes, ", sizeof(ElfShdr), " expected."));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (file_header_.e_shnum > kMaxSectionHeaderEntries) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("too many section header entries: ", file_header_.e_shnum,
|
|
|
|
" limit: ", kMaxSectionHeaderEntries));
|
|
|
|
}
|
|
|
|
std::string headers(file_header_.e_shentsize * file_header_.e_shnum, '\0');
|
|
|
|
SAPI_RETURN_IF_ERROR(CheckedFSeek(elf_, file_header_.e_shoff, SEEK_SET));
|
|
|
|
SAPI_RETURN_IF_ERROR(CheckedRead(&headers, elf_));
|
|
|
|
section_headers_.resize(file_header_.e_shnum);
|
|
|
|
absl::string_view src = headers;
|
|
|
|
for (int i = 0; i < file_header_.e_shnum; ++i) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(section_headers_[i], ReadSectionHeader(src));
|
|
|
|
src = src.substr(file_header_.e_shentsize);
|
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::string> ElfParser::ReadSectionContents(int idx) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (idx < 0 || idx >= section_headers_.size()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid section header index: ", idx));
|
|
|
|
}
|
|
|
|
return ReadSectionContents(section_headers_.at(idx));
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::string> ElfParser::ReadSectionContents(
|
2020-12-17 01:17:53 +08:00
|
|
|
const ElfShdr& section_header) {
|
2019-03-19 00:21:48 +08:00
|
|
|
auto offset = section_header.sh_offset;
|
|
|
|
if (offset > file_size_) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid section offset: ", offset));
|
|
|
|
}
|
|
|
|
auto size = section_header.sh_size;
|
|
|
|
if (size > kMaxSectionSize) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("section too big: ", size, " limit: ", kMaxSectionSize));
|
|
|
|
}
|
|
|
|
std::string rv(size, '\0');
|
|
|
|
SAPI_RETURN_IF_ERROR(CheckedFSeek(elf_, offset, SEEK_SET));
|
|
|
|
SAPI_RETURN_IF_ERROR(CheckedRead(&rv, elf_));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2020-12-17 01:17:53 +08:00
|
|
|
absl::StatusOr<ElfPhdr> ElfParser::ReadProgramHeader(absl::string_view src) {
|
|
|
|
if (src.size() < sizeof(ElfPhdr)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid program header data: got ", src.size(),
|
2020-12-17 01:17:53 +08:00
|
|
|
" bytes, ", sizeof(ElfPhdr), " bytes expected."));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-12-17 01:17:53 +08:00
|
|
|
ElfPhdr rv;
|
2019-03-19 00:21:48 +08:00
|
|
|
LOAD_MEMBER(rv, p_type, src.data());
|
|
|
|
LOAD_MEMBER(rv, p_flags, src.data());
|
|
|
|
LOAD_MEMBER(rv, p_offset, src.data());
|
|
|
|
LOAD_MEMBER(rv, p_vaddr, src.data());
|
|
|
|
LOAD_MEMBER(rv, p_paddr, src.data());
|
|
|
|
LOAD_MEMBER(rv, p_filesz, src.data());
|
|
|
|
LOAD_MEMBER(rv, p_memsz, src.data());
|
|
|
|
LOAD_MEMBER(rv, p_align, src.data());
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ElfParser::ReadProgramHeaders() {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (file_header_.e_phoff > file_size_) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid program header offset: ", file_header_.e_phoff));
|
|
|
|
}
|
2020-12-17 01:17:53 +08:00
|
|
|
if (file_header_.e_phentsize != sizeof(ElfPhdr)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(absl::StrCat(
|
2019-03-19 00:21:48 +08:00
|
|
|
"section header entry size incorrect: ", file_header_.e_phentsize,
|
2020-12-17 01:17:53 +08:00
|
|
|
" bytes, ", sizeof(ElfPhdr), " expected."));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (file_header_.e_phnum > kMaxProgramHeaderEntries) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("too many program header entries: ", file_header_.e_phnum,
|
|
|
|
" limit: ", kMaxProgramHeaderEntries));
|
|
|
|
}
|
|
|
|
std::string headers(file_header_.e_phentsize * file_header_.e_phnum, '\0');
|
|
|
|
SAPI_RETURN_IF_ERROR(CheckedFSeek(elf_, file_header_.e_phoff, SEEK_SET));
|
|
|
|
SAPI_RETURN_IF_ERROR(CheckedRead(&headers, elf_));
|
|
|
|
program_headers_.resize(file_header_.e_phnum);
|
|
|
|
absl::string_view src = headers;
|
|
|
|
for (int i = 0; i < file_header_.e_phnum; ++i) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(program_headers_[i], ReadProgramHeader(src));
|
|
|
|
src = src.substr(file_header_.e_phentsize);
|
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-12-17 01:17:53 +08:00
|
|
|
absl::Status ElfParser::ReadSymbolsFromSymtab(const ElfShdr& symtab) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (symtab.sh_type != SHT_SYMTAB) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("invalid symtab type");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-12-17 01:17:53 +08:00
|
|
|
if (symtab.sh_entsize != sizeof(ElfSym)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid symbol entry size: ", symtab.sh_entsize));
|
|
|
|
}
|
|
|
|
if ((symtab.sh_size % symtab.sh_entsize) != 0) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid symbol table size: ", symtab.sh_size));
|
|
|
|
}
|
|
|
|
size_t symbol_entries = symtab.sh_size / symtab.sh_entsize;
|
|
|
|
if (symbol_entries > kMaxSymbolEntries - symbol_entries_read) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("too many symbols: ", symbol_entries));
|
|
|
|
}
|
|
|
|
symbol_entries_read += symbol_entries;
|
|
|
|
if (symtab.sh_link >= section_headers_.size()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid symtab's strtab reference: ", symtab.sh_link));
|
|
|
|
}
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(1, "Symbol table with %zu entries found", symbol_entries);
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(std::string strtab,
|
|
|
|
ReadSectionContents(symtab.sh_link));
|
2019-03-19 00:21:48 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(std::string symbols, ReadSectionContents(symtab));
|
|
|
|
result_.symbols_.reserve(result_.symbols_.size() + symbol_entries);
|
|
|
|
for (absl::string_view src = symbols; !src.empty();
|
|
|
|
src = src.substr(symtab.sh_entsize)) {
|
2020-12-17 01:17:53 +08:00
|
|
|
ElfSym symbol;
|
2019-03-19 00:21:48 +08:00
|
|
|
LOAD_MEMBER(symbol, st_name, src.data());
|
|
|
|
LOAD_MEMBER(symbol, st_info, src.data());
|
|
|
|
LOAD_MEMBER(symbol, st_other, src.data());
|
|
|
|
LOAD_MEMBER(symbol, st_shndx, src.data());
|
|
|
|
LOAD_MEMBER(symbol, st_value, src.data());
|
|
|
|
LOAD_MEMBER(symbol, st_size, src.data());
|
|
|
|
if (symbol.st_shndx == SHN_UNDEF) {
|
|
|
|
// External symbol, not supported.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (symbol.st_shndx == SHN_ABS) {
|
|
|
|
// Absolute value, not supported.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (symbol.st_shndx >= section_headers_.size()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(absl::StrCat(
|
2019-03-19 00:21:48 +08:00
|
|
|
"invalid symbol data: section index: ", symbol.st_shndx));
|
|
|
|
}
|
|
|
|
if (symbol.st_name >= strtab.size()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid name reference: REL", symbol.st_value));
|
|
|
|
}
|
|
|
|
result_.symbols_.push_back(
|
|
|
|
{symbol.st_value, std::string(ReadName(symbol.st_name, strtab))});
|
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ElfParser::ReadImportedLibrariesFromDynamic(
|
2020-12-17 01:17:53 +08:00
|
|
|
const ElfShdr& dynamic) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (dynamic.sh_type != SHT_DYNAMIC) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("invalid dynamic type");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-12-17 01:17:53 +08:00
|
|
|
if (dynamic.sh_entsize != sizeof(ElfDyn)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid dynamic entry size: ", dynamic.sh_entsize));
|
|
|
|
}
|
|
|
|
if ((dynamic.sh_size % dynamic.sh_entsize) != 0) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid dynamic table size: ", dynamic.sh_size));
|
|
|
|
}
|
|
|
|
size_t entries = dynamic.sh_size / dynamic.sh_entsize;
|
|
|
|
if (entries > kMaxDynamicEntries - dynamic_entries_read) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("too many dynamic entries: ", entries));
|
|
|
|
}
|
|
|
|
dynamic_entries_read += entries;
|
|
|
|
if (dynamic.sh_link >= section_headers_.size()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid dynamic's strtab reference: ", dynamic.sh_link));
|
|
|
|
}
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(1, "Dynamic section with %zu entries found", entries);
|
2019-03-19 00:21:48 +08:00
|
|
|
// strtab may be shared with symbols and therefore huge
|
|
|
|
const auto& strtab_section = section_headers_.at(dynamic.sh_link);
|
|
|
|
if (strtab_section.sh_offset > file_size_) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(absl::StrCat(
|
2019-03-19 00:21:48 +08:00
|
|
|
"invalid symtab's strtab section offset: ", strtab_section.sh_offset));
|
|
|
|
}
|
|
|
|
if (strtab_section.sh_size >= kMaxStrtabSize ||
|
|
|
|
strtab_section.sh_size >= file_size_ ||
|
|
|
|
strtab_section.sh_offset >= file_size_ - strtab_section.sh_size) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("symtab's strtab too big: ", strtab_section.sh_size));
|
|
|
|
}
|
|
|
|
auto strtab_end = strtab_section.sh_offset + strtab_section.sh_size;
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(std::string dynamic_entries,
|
|
|
|
ReadSectionContents(dynamic));
|
2019-03-19 00:21:48 +08:00
|
|
|
for (absl::string_view src = dynamic_entries; !src.empty();
|
|
|
|
src = src.substr(dynamic.sh_entsize)) {
|
2020-12-17 01:17:53 +08:00
|
|
|
ElfDyn dyn;
|
2019-03-19 00:21:48 +08:00
|
|
|
LOAD_MEMBER(dyn, d_tag, src.data());
|
|
|
|
LOAD_MEMBER(dyn, d_un.d_val, src.data());
|
|
|
|
if (dyn.d_tag != DT_NEEDED) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (dyn.d_un.d_val >= strtab_section.sh_size) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("invalid name reference"));
|
|
|
|
}
|
|
|
|
auto offset = strtab_section.sh_offset + dyn.d_un.d_val;
|
|
|
|
SAPI_RETURN_IF_ERROR(CheckedFSeek(elf_, offset, SEEK_SET));
|
2021-11-18 18:15:53 +08:00
|
|
|
std::string path(
|
|
|
|
std::min(kMaxLibPathSize, static_cast<size_t>(strtab_end - offset)),
|
|
|
|
'\0');
|
2021-04-28 22:48:48 +08:00
|
|
|
size_t size = std::fread(&path[0], 1, path.size(), elf_);
|
2019-03-19 00:21:48 +08:00
|
|
|
path.resize(size);
|
|
|
|
result_.imported_libraries_.push_back(path.substr(0, path.find('\0')));
|
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 22:48:48 +08:00
|
|
|
absl::StatusOr<ElfFile> ElfParser::Parse(const std::string& filename,
|
|
|
|
uint32_t features) {
|
|
|
|
ElfParser parser;
|
|
|
|
if (parser.elf_ = std::fopen(filename.c_str(), "r"); !parser.elf_) {
|
2021-07-27 19:09:37 +08:00
|
|
|
return absl::UnknownError(sapi::OsErrorMessage(
|
|
|
|
errno, absl::StrCat("cannot open file: ", filename)));
|
2021-04-28 22:48:48 +08:00
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Basic sanity check.
|
|
|
|
if (features & ~(ElfFile::kAll)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError("Unknown feature flags specified");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-04-28 22:48:48 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(parser.ReadFileSize());
|
|
|
|
SAPI_RETURN_IF_ERROR(parser.ReadFileHeader());
|
|
|
|
switch (parser.file_header_.e_type) {
|
2019-03-19 00:21:48 +08:00
|
|
|
case ET_EXEC:
|
2021-04-28 22:48:48 +08:00
|
|
|
parser.result_.position_independent_ = false;
|
2019-03-19 00:21:48 +08:00
|
|
|
break;
|
|
|
|
case ET_DYN:
|
2021-04-28 22:48:48 +08:00
|
|
|
parser.result_.position_independent_ = true;
|
2019-03-19 00:21:48 +08:00
|
|
|
break;
|
|
|
|
default:
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("not an executable: ");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (features & ElfFile::kGetInterpreter) {
|
2021-04-28 22:48:48 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(parser.ReadProgramHeaders());
|
2019-03-19 00:21:48 +08:00
|
|
|
std::string interpreter;
|
|
|
|
auto it = std::find_if(
|
2021-04-28 22:48:48 +08:00
|
|
|
parser.program_headers_.begin(), parser.program_headers_.end(),
|
2020-12-17 01:17:53 +08:00
|
|
|
[](const ElfPhdr& hdr) { return hdr.p_type == PT_INTERP; });
|
2019-03-19 00:21:48 +08:00
|
|
|
// No interpreter usually means that the executable was statically linked.
|
2021-04-28 22:48:48 +08:00
|
|
|
if (it != parser.program_headers_.end()) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (it->p_filesz > kMaxInterpreterSize) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("program interpeter path too long: ", it->p_filesz));
|
|
|
|
}
|
2021-04-28 22:48:48 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(CheckedFSeek(parser.elf_, it->p_offset, SEEK_SET));
|
2019-03-19 00:21:48 +08:00
|
|
|
interpreter.resize(it->p_filesz, '\0');
|
2021-04-28 22:48:48 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(CheckedRead(&interpreter, parser.elf_));
|
2019-03-19 00:21:48 +08:00
|
|
|
auto first_nul = interpreter.find_first_of('\0');
|
|
|
|
if (first_nul != std::string::npos) {
|
|
|
|
interpreter.erase(first_nul);
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 22:48:48 +08:00
|
|
|
parser.result_.interpreter_ = std::move(interpreter);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (features & (ElfFile::kLoadSymbols | ElfFile::kLoadImportedLibraries)) {
|
2021-04-28 22:48:48 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(parser.ReadSectionHeaders());
|
|
|
|
for (const auto& hdr : parser.section_headers_) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (hdr.sh_type == SHT_SYMTAB && features & ElfFile::kLoadSymbols) {
|
2021-04-28 22:48:48 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(parser.ReadSymbolsFromSymtab(hdr));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (hdr.sh_type == SHT_DYNAMIC &&
|
|
|
|
features & ElfFile::kLoadImportedLibraries) {
|
2021-04-28 22:48:48 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(parser.ReadImportedLibrariesFromDynamic(hdr));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 22:48:48 +08:00
|
|
|
return std::move(parser.result_);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<ElfFile> ElfFile::ParseFromFile(const std::string& filename,
|
2020-08-28 19:49:15 +08:00
|
|
|
uint32_t features) {
|
2021-04-28 22:48:48 +08:00
|
|
|
return ElfParser::Parse(filename, features);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sandbox2
|