1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "sandboxed_api/sandbox2/util/minielf.h"
16
17 #include <cstdint>
18 #include <string>
19 #include <vector>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "absl/algorithm/container.h"
24 #include "sandboxed_api/sandbox2/util/maps_parser.h"
25 #include "sandboxed_api/testing.h"
26 #include "sandboxed_api/util/file_helpers.h"
27 #include "sandboxed_api/util/status_matchers.h"
28
ExportedFunction()29 extern "C" void ExportedFunction() {
30 // Don't do anything - used to generate a symbol.
31 }
32
33 namespace file = ::sapi::file;
34 using ::sapi::GetTestSourcePath;
35 using ::sapi::IsOk;
36 using ::testing::ElementsAre;
37 using ::testing::Eq;
38 using ::testing::IsTrue;
39 using ::testing::Ne;
40 using ::testing::StrEq;
41
42 namespace sandbox2 {
43 namespace {
44
TEST(MinielfTest,Chrome70)45 TEST(MinielfTest, Chrome70) {
46 SAPI_ASSERT_OK_AND_ASSIGN(
47 ElfFile elf,
48 ElfFile::ParseFromFile(
49 GetTestSourcePath("sandbox2/util/testdata/chrome_grte_header"),
50 ElfFile::kGetInterpreter));
51 EXPECT_THAT(elf.interpreter(), StrEq("/usr/grte/v4/ld64"));
52 }
53
TEST(MinielfTest,SymbolResolutionWorks)54 TEST(MinielfTest, SymbolResolutionWorks) {
55 SAPI_ASSERT_OK_AND_ASSIGN(
56 ElfFile elf,
57 ElfFile::ParseFromFile("/proc/self/exe", ElfFile::kLoadSymbols));
58 ASSERT_THAT(elf.position_independent(), IsTrue());
59
60 // Load /proc/self/maps to take ASLR into account.
61 std::string maps_buffer;
62 ASSERT_THAT(
63 file::GetContents("/proc/self/maps", &maps_buffer, file::Defaults()),
64 IsOk());
65 SAPI_ASSERT_OK_AND_ASSIGN(std::vector<MapsEntry> maps,
66 ParseProcMaps(maps_buffer));
67
68 // Find maps entry that covers this entry.
69 uint64_t function_address = reinterpret_cast<uint64_t>(&ExportedFunction);
70 auto entry =
71 absl::c_find_if(maps, [function_address](const MapsEntry& entry) {
72 return entry.start <= function_address && entry.end > function_address;
73 });
74 ASSERT_THAT(entry, Ne(maps.end()));
75
76 auto function_symbol =
77 absl::c_find_if(elf.symbols(), [](const ElfFile::Symbol& symbol) {
78 return symbol.name == "ExportedFunction";
79 });
80 ASSERT_THAT(function_symbol, Ne(elf.symbols().end()));
81
82 function_address -= entry->start - entry->pgoff;
83 EXPECT_THAT(function_symbol->address, Eq(function_address));
84 }
85
TEST(MinielfTest,ImportedLibraries)86 TEST(MinielfTest, ImportedLibraries) {
87 SAPI_ASSERT_OK_AND_ASSIGN(
88 ElfFile elf, ElfFile::ParseFromFile(
89 GetTestSourcePath("sandbox2/util/testdata/hello_world"),
90 ElfFile::kLoadImportedLibraries));
91 EXPECT_THAT(elf.imported_libraries(), ElementsAre("libc.so.6"));
92 }
93
94 } // namespace
95 } // namespace sandbox2
96