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> |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
| 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 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 23 | #include "BionicDeathTest.h" |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 24 | #include "gtest_globals.h" |
| 25 | #include "utils.h" |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 26 | |
Evgenii Stepanov | 1dfd76a | 2017-09-18 17:51:48 -0700 | [diff] [blame] | 27 | #if defined(__BIONIC__) |
| 28 | #include "private/CFIShadow.h" |
| 29 | #endif |
| 30 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 31 | // Private libdl interface. |
| 32 | extern "C" { |
| 33 | void __cfi_slowpath(uint64_t CallSiteTypeId, void* Ptr); |
| 34 | void __cfi_slowpath_diag(uint64_t CallSiteTypeId, void* Ptr, void* DiagData); |
Evgenii Stepanov | 97c16f8 | 2017-08-02 16:34:44 -0700 | [diff] [blame] | 35 | size_t __cfi_shadow_size(); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 38 | using cfi_test_DeathTest = BionicDeathTest; |
| 39 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 40 | static void f() {} |
| 41 | |
Christopher Ferris | f322483 | 2020-04-01 16:59:57 -0700 | [diff] [blame] | 42 | static void test_cfi_slowpath_with_alloc() { |
| 43 | std::vector<void*> allocs; |
| 44 | for (size_t i = 0; i < 1000; i++) { |
| 45 | allocs.push_back(malloc(4096)); |
| 46 | __cfi_slowpath(46, allocs.back()); |
| 47 | } |
| 48 | } |
| 49 | |
Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 50 | TEST_F(cfi_test_DeathTest, basic) { |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 51 | #if defined(__BIONIC__) |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 52 | void* handle; |
| 53 | handle = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL); |
| 54 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 55 | |
Evgenii Stepanov | 97c16f8 | 2017-08-02 16:34:44 -0700 | [diff] [blame] | 56 | EXPECT_NE(0U, __cfi_shadow_size()); |
| 57 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 58 | #define SYM(type, name) auto name = reinterpret_cast<type>(dlsym(handle, #name)) |
Evgenii Stepanov | 1dfd76a | 2017-09-18 17:51:48 -0700 | [diff] [blame] | 59 | SYM(size_t (*)(), get_count); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 60 | SYM(uint64_t(*)(), get_last_type_id); |
| 61 | SYM(void* (*)(), get_last_address); |
| 62 | SYM(void* (*)(), get_last_diag); |
| 63 | SYM(void* (*)(), get_global_address); |
| 64 | SYM(void (*)(uint64_t, void*, void*), __cfi_check); |
Evgenii Stepanov | 1dfd76a | 2017-09-18 17:51:48 -0700 | [diff] [blame] | 65 | SYM(char*, bss); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 66 | #undef SYM |
| 67 | |
Evgenii Stepanov | 1dfd76a | 2017-09-18 17:51:48 -0700 | [diff] [blame] | 68 | size_t c = get_count(); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 69 | |
| 70 | // CFI check for code inside the DSO. Can't use just any function address - this is only |
| 71 | // guaranteed to work for code addresses above __cfi_check. |
| 72 | void* code_ptr = reinterpret_cast<char*>(__cfi_check) + 1234; |
| 73 | void* diag_ptr = reinterpret_cast<void*>(5678); |
| 74 | __cfi_slowpath_diag(42, code_ptr, diag_ptr); |
| 75 | EXPECT_EQ(42U, get_last_type_id()); |
| 76 | EXPECT_EQ(code_ptr, get_last_address()); |
| 77 | EXPECT_EQ(diag_ptr, get_last_diag()); |
| 78 | EXPECT_EQ(++c, get_count()); |
| 79 | |
| 80 | // __cfi_slowpath passes nullptr for the Diag argument. |
| 81 | __cfi_slowpath(42, code_ptr); |
| 82 | EXPECT_EQ(42U, get_last_type_id()); |
| 83 | EXPECT_EQ(code_ptr, get_last_address()); |
| 84 | EXPECT_EQ(nullptr, get_last_diag()); |
| 85 | EXPECT_EQ(++c, get_count()); |
| 86 | |
| 87 | // CFI check for a data address inside the DSO. |
| 88 | __cfi_slowpath(43, get_global_address()); |
| 89 | EXPECT_EQ(43U, get_last_type_id()); |
| 90 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 91 | EXPECT_EQ(++c, get_count()); |
| 92 | |
| 93 | // CFI check for a function inside _this_ DSO. It either goes to this DSO's __cfi_check, |
| 94 | // or (if missing) is simply ignored. Any way, it does not affect the test lib's counters. |
| 95 | __cfi_slowpath(44, reinterpret_cast<void*>(&f)); |
| 96 | EXPECT_EQ(43U, get_last_type_id()); |
| 97 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 98 | EXPECT_EQ(c, get_count()); |
| 99 | |
Christopher Ferris | f322483 | 2020-04-01 16:59:57 -0700 | [diff] [blame] | 100 | // CFI check for a heap address. |
| 101 | // It's possible that this allocation could wind up in the same CFI granule as |
| 102 | // an unchecked library, which means the below might not crash. To force a |
| 103 | // crash keep allocating up to a max until there is a crash. |
| 104 | EXPECT_DEATH(test_cfi_slowpath_with_alloc(), ""); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 105 | |
Evgenii Stepanov | 1dfd76a | 2017-09-18 17:51:48 -0700 | [diff] [blame] | 106 | // Check all the addresses. |
| 107 | const size_t bss_size = 1024 * 1024; |
| 108 | static_assert(bss_size >= kLibraryAlignment * 2, "test range not big enough"); |
| 109 | for (size_t i = 0; i < bss_size; ++i) { |
| 110 | __cfi_slowpath(47, bss + i); |
| 111 | EXPECT_EQ(++c, get_count()); |
| 112 | } |
| 113 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 114 | // Load the same library again. |
| 115 | void* handle2 = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL); |
| 116 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 117 | EXPECT_EQ(handle2, handle); |
| 118 | |
| 119 | // Check that it is still there. |
| 120 | __cfi_slowpath(43, get_global_address()); |
| 121 | EXPECT_EQ(43U, get_last_type_id()); |
| 122 | EXPECT_EQ(get_global_address(), get_last_address()); |
| 123 | EXPECT_EQ(++c, get_count()); |
| 124 | |
| 125 | dlclose(handle); |
| 126 | dlclose(handle2); |
| 127 | |
| 128 | // CFI check for a function inside the unloaded DSO. This is always invalid and gets the process |
| 129 | // killed. |
| 130 | EXPECT_DEATH(__cfi_slowpath(45, reinterpret_cast<void*>(code_ptr)), ""); |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 131 | #endif |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | TEST(cfi_test, invalid) { |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 135 | #if defined(__BIONIC__) |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 136 | void* handle; |
| 137 | handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL); |
| 138 | ASSERT_FALSE(handle != nullptr) << dlerror(); |
| 139 | |
| 140 | handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL); |
| 141 | ASSERT_FALSE(handle != nullptr) << dlerror(); |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 142 | #endif |
| 143 | } |
| 144 | |
| 145 | // cfi_test_helper exports __cfi_check, which triggers CFI initialization at startup. |
| 146 | TEST(cfi_test, early_init) { |
| 147 | #if defined(__BIONIC__) |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 148 | std::string helper = GetTestlibRoot() + "/cfi_test_helper/cfi_test_helper"; |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 149 | chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607 |
| 150 | ExecTestHelper eth; |
| 151 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 152 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
| 153 | #endif |
| 154 | } |
| 155 | |
| 156 | // cfi_test_helper2 depends on a library that exports __cfi_check, which triggers CFI initialization |
| 157 | // at startup. |
| 158 | TEST(cfi_test, early_init2) { |
| 159 | #if defined(__BIONIC__) |
Christopher Ferris | 6d2c0bd | 2018-08-21 18:13:10 -0700 | [diff] [blame] | 160 | std::string helper = GetTestlibRoot() + "/cfi_test_helper2/cfi_test_helper2"; |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame] | 161 | chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607 |
| 162 | ExecTestHelper eth; |
| 163 | eth.SetArgs({ helper.c_str(), nullptr }); |
| 164 | eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr); |
| 165 | #endif |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 166 | } |