| Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| Evgenii Stepanov | beb3eb1 | 2017-01-31 17:10:03 -0800 | [diff] [blame] | 17 | #include <dlfcn.h> |
| hangl | fda4c10 | 2025-04-30 12:13:05 -0700 | [diff] [blame] | 18 | #include <link.h> |
| Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 19 | #include <sys/stat.h> |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 20 | |
| Christopher Ferris | f322483 | 2020-04-01 16:59:57 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
| Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
| 24 | |
| Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 25 | #include "gtest_globals.h" |
| 26 | #include "utils.h" |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 27 | |
| Evgenii Stepanov | 1dfd76a | 2017-09-18 17:51:48 -0700 | [diff] [blame] | 28 | #if defined(__BIONIC__) |
| 29 | #include "private/CFIShadow.h" |
| 30 | #endif |
| 31 | |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 32 | // Private libdl interface. |
| 33 | extern "C" { |
| 34 | void __cfi_slowpath(uint64_t CallSiteTypeId, void* Ptr); |
| 35 | void __cfi_slowpath_diag(uint64_t CallSiteTypeId, void* Ptr, void* DiagData); |
| Evgenii Stepanov | 97c16f8 | 2017-08-02 16:34:44 -0700 | [diff] [blame] | 36 | size_t __cfi_shadow_size(); |
| hangl | fda4c10 | 2025-04-30 12:13:05 -0700 | [diff] [blame] | 37 | uintptr_t __cfi_shadow_load(void* p); |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| Elliott Hughes | 4411b94 | 2022-02-09 15:56:27 -0800 | [diff] [blame] | 40 | // Disables debuggerd stack traces to speed up death tests, make them less |
| 41 | // noisy in logcat, and avoid expected deaths from showing up in stability |
| 42 | // metrics. |
| 43 | // We don't use the usual libbase class because (a) we don't care about most |
| 44 | // of the signals it blocks but (b) we do need to block SIGILL, which normal |
| 45 | // death tests shouldn't ever hit. (It's possible that a design where a |
| 46 | // deathtest always declares its expected signals up front is a better one, |
| 47 | // and maybe that's an interesting future direction for libbase.) |
| Elliott Hughes | 82e24a5 | 2022-02-15 10:12:23 -0800 | [diff] [blame] | 48 | // |
| 49 | // We include SIGSEGV because there's a test that passes heap addresses to |
| 50 | // __cfi_slowpath and we only map the executable code shadow as readable. |
| 51 | // We don't always get SIGSEGV there though: if the heap allocation happens |
| 52 | // to be close enough to an executable mapping that its shadow is in the |
| 53 | // same page as the executable shadow, we'll get SIGILL/SIGTRAP. |
| Elliott Hughes | 4411b94 | 2022-02-09 15:56:27 -0800 | [diff] [blame] | 54 | class cfi_test_DeathTest : public testing::Test { |
| 55 | protected: |
| 56 | void SetUp() override { |
| 57 | struct sigaction64 action = {.sa_handler = SIG_DFL}; |
| Elliott Hughes | 4411b94 | 2022-02-09 15:56:27 -0800 | [diff] [blame] | 58 | sigaction64(SIGILL, &action, &previous_sigill_); |
| Elliott Hughes | 82e24a5 | 2022-02-15 10:12:23 -0800 | [diff] [blame] | 59 | sigaction64(SIGSEGV, &action, &previous_sigsegv_); |
| 60 | sigaction64(SIGTRAP, &action, &previous_sigtrap_); |
| Elliott Hughes | 4411b94 | 2022-02-09 15:56:27 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void TearDown() override { |
| Elliott Hughes | 82e24a5 | 2022-02-15 10:12:23 -0800 | [diff] [blame] | 64 | sigaction64(SIGTRAP, &previous_sigtrap_, nullptr); |
| Elliott Hughes | 4411b94 | 2022-02-09 15:56:27 -0800 | [diff] [blame] | 65 | sigaction64(SIGSEGV, &previous_sigsegv_, nullptr); |
| Elliott Hughes | 82e24a5 | 2022-02-15 10:12:23 -0800 | [diff] [blame] | 66 | sigaction64(SIGILL, &previous_sigill_, nullptr); |
| Elliott Hughes | 4411b94 | 2022-02-09 15:56:27 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private: |
| Elliott Hughes | 4411b94 | 2022-02-09 15:56:27 -0800 | [diff] [blame] | 70 | struct sigaction64 previous_sigill_; |
| Elliott Hughes | 82e24a5 | 2022-02-15 10:12:23 -0800 | [diff] [blame] | 71 | struct sigaction64 previous_sigsegv_; |
| 72 | struct sigaction64 previous_sigtrap_; |
| Elliott Hughes | 4411b94 | 2022-02-09 15:56:27 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 75 | static void f() {} |
| 76 | |
| Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 77 | TEST_F(cfi_test_DeathTest, basic) { |
| Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 78 | #if defined(__BIONIC__) |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 79 | void* handle; |
| 80 | handle = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL); |
| 81 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 82 | |
| Evgenii Stepanov | 97c16f8 | 2017-08-02 16:34:44 -0700 | [diff] [blame] | 83 | EXPECT_NE(0U, __cfi_shadow_size()); |
| 84 | |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 85 | #define SYM(type, name) auto name = reinterpret_cast<type>(dlsym(handle, #name)) |
| Evgenii Stepanov | 1dfd76a | 2017-09-18 17:51:48 -0700 | [diff] [blame] | 86 | SYM(size_t (*)(), get_count); |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 87 | SYM(uint64_t(*)(), get_last_type_id); |
| 88 | SYM(void* (*)(), get_last_address); |
| 89 | SYM(void* (*)(), get_last_diag); |
| 90 | SYM(void* (*)(), get_global_address); |
| 91 | SYM(void (*)(uint64_t, void*, void*), __cfi_check); |
| Evgenii Stepanov | 1dfd76a | 2017-09-18 17:51:48 -0700 | [diff] [blame] | 92 | SYM(char*, bss); |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 93 | #undef SYM |
| 94 | |
| Evgenii Stepanov | 1dfd76a | 2017-09-18 17:51:48 -0700 | [diff] [blame] | 95 | size_t c = get_count(); |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 96 | |
| 97 | // CFI check for code inside the DSO. Can't use just any function address - this is only |
| 98 | // guaranteed to work for code addresses above __cfi_check. |
| 99 | void* code_ptr = reinterpret_cast<char*>(__cfi_check) + 1234; |
| 100 | void* diag_ptr = reinterpret_cast<void*>(5678); |
| 101 | __cfi_slowpath_diag(42, code_ptr, diag_ptr); |
| 102 | EXPECT_EQ(42U, get_last_type_id()); |
| 103 | EXPECT_EQ(code_ptr, get_last_address()); |
| 104 | EXPECT_EQ(diag_ptr, get_last_diag()); |
| 105 | EXPECT_EQ(++c, get_count()); |
| 106 | |
| 107 | // __cfi_slowpath passes nullptr for the Diag argument. |
| 108 | __cfi_slowpath(42, code_ptr); |
| 109 | EXPECT_EQ(42U, get_last_type_id()); |
| 110 | EXPECT_EQ(code_ptr, get_last_address()); |
| 111 | EXPECT_EQ(nullptr, get_last_diag()); |
| 112 | EXPECT_EQ(++c, get_count()); |
| 113 | |
| 114 | // CFI check for a data address inside the DSO. |
| 115 | __cfi_slowpath(43, get_global_address()); |
| 116 | EXPECT_EQ(43U, get_last_type_id()); |
| 117 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 118 | EXPECT_EQ(++c, get_count()); |
| 119 | |
| 120 | // CFI check for a function inside _this_ DSO. It either goes to this DSO's __cfi_check, |
| 121 | // or (if missing) is simply ignored. Any way, it does not affect the test lib's counters. |
| 122 | __cfi_slowpath(44, reinterpret_cast<void*>(&f)); |
| 123 | EXPECT_EQ(43U, get_last_type_id()); |
| 124 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 125 | EXPECT_EQ(c, get_count()); |
| 126 | |
| Evgenii Stepanov | 1dfd76a | 2017-09-18 17:51:48 -0700 | [diff] [blame] | 127 | // Check all the addresses. |
| 128 | const size_t bss_size = 1024 * 1024; |
| 129 | static_assert(bss_size >= kLibraryAlignment * 2, "test range not big enough"); |
| 130 | for (size_t i = 0; i < bss_size; ++i) { |
| 131 | __cfi_slowpath(47, bss + i); |
| 132 | EXPECT_EQ(++c, get_count()); |
| 133 | } |
| 134 | |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 135 | // Load the same library again. |
| 136 | void* handle2 = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL); |
| 137 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 138 | EXPECT_EQ(handle2, handle); |
| 139 | |
| 140 | // Check that it is still there. |
| 141 | __cfi_slowpath(43, get_global_address()); |
| 142 | EXPECT_EQ(43U, get_last_type_id()); |
| 143 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 144 | EXPECT_EQ(++c, get_count()); |
| 145 | |
| 146 | dlclose(handle); |
| 147 | dlclose(handle2); |
| Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 148 | #endif |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | TEST(cfi_test, invalid) { |
| Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 152 | #if defined(__BIONIC__) |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 153 | void* handle; |
| 154 | handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL); |
| 155 | ASSERT_FALSE(handle != nullptr) << dlerror(); |
| 156 | |
| 157 | handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL); |
| 158 | ASSERT_FALSE(handle != nullptr) << dlerror(); |
| Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 159 | #endif |
| 160 | } |
| 161 | |
| 162 | // cfi_test_helper exports __cfi_check, which triggers CFI initialization at startup. |
| 163 | TEST(cfi_test, early_init) { |
| 164 | #if defined(__BIONIC__) |
| Elliott Hughes | 8d8138a | 2024-03-12 22:37:13 +0000 | [diff] [blame] | 165 | std::string helper = GetTestLibRoot() + "/cfi_test_helper"; |
| Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 166 | ExecTestHelper eth; |
| 167 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 168 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
| 169 | #endif |
| 170 | } |
| 171 | |
| 172 | // cfi_test_helper2 depends on a library that exports __cfi_check, which triggers CFI initialization |
| 173 | // at startup. |
| 174 | TEST(cfi_test, early_init2) { |
| 175 | #if defined(__BIONIC__) |
| Elliott Hughes | 8d8138a | 2024-03-12 22:37:13 +0000 | [diff] [blame] | 176 | std::string helper = GetTestLibRoot() + "/cfi_test_helper2"; |
| Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 177 | ExecTestHelper eth; |
| 178 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 179 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
| 180 | #endif |
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 181 | } |
| hangl | fda4c10 | 2025-04-30 12:13:05 -0700 | [diff] [blame] | 182 | |
| 183 | TEST(cfi_test, every_loaded_dso_has_valid_shadow) { |
| 184 | #if defined(__BIONIC__) |
| 185 | if (__cfi_shadow_size() == 0) GTEST_SKIP(); |
| 186 | |
| 187 | auto callback = [](dl_phdr_info* info, size_t, void*) { |
| 188 | if (info->dlpi_phnum == 0 || !info->dlpi_phdr) return 0; |
| 189 | for (ElfW(Half) i = 0; i < info->dlpi_phnum; ++i) { |
| 190 | auto& ph = info->dlpi_phdr[i]; |
| 191 | if (ph.p_type != PT_LOAD || !(ph.p_flags & PF_X)) continue; |
| 192 | uintptr_t sample_addr = info->dlpi_addr + ph.p_vaddr; |
| 193 | uint16_t v = __cfi_shadow_load(reinterpret_cast<void*>(sample_addr)); |
| 194 | |
| 195 | EXPECT_NE(uint16_t(CFIShadow::kInvalidShadow), v) |
| 196 | << "ERROR: cfi shadow value is invalid, " << info->dlpi_name |
| 197 | << " @ 0x" << std::hex << sample_addr |
| 198 | << " → shadow=0x" << v |
| 199 | << " (" << std::dec << v << ")"; |
| 200 | } |
| 201 | return 0; |
| 202 | }; |
| 203 | |
| 204 | dl_iterate_phdr(callback, nullptr); |
| 205 | #endif |
| 206 | } |