blob: dd65a812ca11de7d9581e5b2c8567bcd272f3ee2 [file] [log] [blame]
Evgenii Stepanov68ecec12017-01-31 13:19:30 -08001/*
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 Stepanovbeb3eb12017-01-31 17:10:03 -080017#include <dlfcn.h>
Evgenii Stepanov68ecec12017-01-31 13:19:30 -080018#include <sys/stat.h>
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070019
Christopher Ferrisf3224832020-04-01 16:59:57 -070020#include <vector>
21
Elliott Hughes141b9172021-04-09 17:13:09 -070022#include <android-base/silent_death_test.h>
23#include <gtest/gtest.h>
24
Evgenii Stepanov68ecec12017-01-31 13:19:30 -080025#include "gtest_globals.h"
26#include "utils.h"
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070027
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070028#if defined(__BIONIC__)
29#include "private/CFIShadow.h"
30#endif
31
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070032// Private libdl interface.
33extern "C" {
34void __cfi_slowpath(uint64_t CallSiteTypeId, void* Ptr);
35void __cfi_slowpath_diag(uint64_t CallSiteTypeId, void* Ptr, void* DiagData);
Evgenii Stepanov97c16f82017-08-02 16:34:44 -070036size_t __cfi_shadow_size();
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070037}
38
Elliott Hughes141b9172021-04-09 17:13:09 -070039using cfi_test_DeathTest = SilentDeathTest;
Elliott Hughese657eb42021-02-18 17:11:56 -080040
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070041static void f() {}
42
Christopher Ferrisf3224832020-04-01 16:59:57 -070043static void test_cfi_slowpath_with_alloc() {
44 std::vector<void*> allocs;
45 for (size_t i = 0; i < 1000; i++) {
46 allocs.push_back(malloc(4096));
47 __cfi_slowpath(46, allocs.back());
48 }
49}
50
Elliott Hughese657eb42021-02-18 17:11:56 -080051TEST_F(cfi_test_DeathTest, basic) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -080052#if defined(__BIONIC__)
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070053 void* handle;
54 handle = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL);
55 ASSERT_TRUE(handle != nullptr) << dlerror();
56
Evgenii Stepanov97c16f82017-08-02 16:34:44 -070057 EXPECT_NE(0U, __cfi_shadow_size());
58
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070059#define SYM(type, name) auto name = reinterpret_cast<type>(dlsym(handle, #name))
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070060 SYM(size_t (*)(), get_count);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070061 SYM(uint64_t(*)(), get_last_type_id);
62 SYM(void* (*)(), get_last_address);
63 SYM(void* (*)(), get_last_diag);
64 SYM(void* (*)(), get_global_address);
65 SYM(void (*)(uint64_t, void*, void*), __cfi_check);
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070066 SYM(char*, bss);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070067#undef SYM
68
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -070069 size_t c = get_count();
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070070
71 // CFI check for code inside the DSO. Can't use just any function address - this is only
72 // guaranteed to work for code addresses above __cfi_check.
73 void* code_ptr = reinterpret_cast<char*>(__cfi_check) + 1234;
74 void* diag_ptr = reinterpret_cast<void*>(5678);
75 __cfi_slowpath_diag(42, code_ptr, diag_ptr);
76 EXPECT_EQ(42U, get_last_type_id());
77 EXPECT_EQ(code_ptr, get_last_address());
78 EXPECT_EQ(diag_ptr, get_last_diag());
79 EXPECT_EQ(++c, get_count());
80
81 // __cfi_slowpath passes nullptr for the Diag argument.
82 __cfi_slowpath(42, code_ptr);
83 EXPECT_EQ(42U, get_last_type_id());
84 EXPECT_EQ(code_ptr, get_last_address());
85 EXPECT_EQ(nullptr, get_last_diag());
86 EXPECT_EQ(++c, get_count());
87
88 // CFI check for a data address inside the DSO.
89 __cfi_slowpath(43, get_global_address());
90 EXPECT_EQ(43U, get_last_type_id());
91 EXPECT_EQ(get_global_address(), get_last_address());
92 EXPECT_EQ(++c, get_count());
93
94 // CFI check for a function inside _this_ DSO. It either goes to this DSO's __cfi_check,
95 // or (if missing) is simply ignored. Any way, it does not affect the test lib's counters.
96 __cfi_slowpath(44, reinterpret_cast<void*>(&f));
97 EXPECT_EQ(43U, get_last_type_id());
98 EXPECT_EQ(get_global_address(), get_last_address());
99 EXPECT_EQ(c, get_count());
100
Christopher Ferrisf3224832020-04-01 16:59:57 -0700101 // CFI check for a heap address.
102 // It's possible that this allocation could wind up in the same CFI granule as
103 // an unchecked library, which means the below might not crash. To force a
104 // crash keep allocating up to a max until there is a crash.
105 EXPECT_DEATH(test_cfi_slowpath_with_alloc(), "");
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700106
Evgenii Stepanov1dfd76a2017-09-18 17:51:48 -0700107 // Check all the addresses.
108 const size_t bss_size = 1024 * 1024;
109 static_assert(bss_size >= kLibraryAlignment * 2, "test range not big enough");
110 for (size_t i = 0; i < bss_size; ++i) {
111 __cfi_slowpath(47, bss + i);
112 EXPECT_EQ(++c, get_count());
113 }
114
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700115 // Load the same library again.
116 void* handle2 = dlopen("libcfi-test.so", RTLD_NOW | RTLD_LOCAL);
117 ASSERT_TRUE(handle2 != nullptr) << dlerror();
118 EXPECT_EQ(handle2, handle);
119
120 // Check that it is still there.
121 __cfi_slowpath(43, get_global_address());
122 EXPECT_EQ(43U, get_last_type_id());
123 EXPECT_EQ(get_global_address(), get_last_address());
124 EXPECT_EQ(++c, get_count());
125
126 dlclose(handle);
127 dlclose(handle2);
128
129 // CFI check for a function inside the unloaded DSO. This is always invalid and gets the process
130 // killed.
131 EXPECT_DEATH(__cfi_slowpath(45, reinterpret_cast<void*>(code_ptr)), "");
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800132#endif
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700133}
134
135TEST(cfi_test, invalid) {
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800136#if defined(__BIONIC__)
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700137 void* handle;
138 handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL);
139 ASSERT_FALSE(handle != nullptr) << dlerror();
140
141 handle = dlopen("libcfi-test-bad.so", RTLD_NOW | RTLD_LOCAL);
142 ASSERT_FALSE(handle != nullptr) << dlerror();
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800143#endif
144}
145
146// cfi_test_helper exports __cfi_check, which triggers CFI initialization at startup.
147TEST(cfi_test, early_init) {
148#if defined(__BIONIC__)
Colin Crossbadcb382021-09-24 17:49:58 -0700149 std::string helper = GetTestlibRoot() + "/cfi_test_helper";
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800150 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
151 ExecTestHelper eth;
152 eth.SetArgs({ helper.c_str(), nullptr });
153 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
154#endif
155}
156
157// cfi_test_helper2 depends on a library that exports __cfi_check, which triggers CFI initialization
158// at startup.
159TEST(cfi_test, early_init2) {
160#if defined(__BIONIC__)
Colin Crossbadcb382021-09-24 17:49:58 -0700161 std::string helper = GetTestlibRoot() + "/cfi_test_helper2";
Evgenii Stepanov68ecec12017-01-31 13:19:30 -0800162 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
163 ExecTestHelper eth;
164 eth.SetArgs({ helper.c_str(), nullptr });
165 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
166#endif
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700167}