xref: /aosp_15_r20/bionic/tests/stack_protector_test.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  *
4*8d67ca89SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker  *
8*8d67ca89SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker  *
10*8d67ca89SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker  * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker  */
16*8d67ca89SAndroid Build Coastguard Worker 
17*8d67ca89SAndroid Build Coastguard Worker /*
18*8d67ca89SAndroid Build Coastguard Worker  * Contributed by: Intel Corporation
19*8d67ca89SAndroid Build Coastguard Worker  */
20*8d67ca89SAndroid Build Coastguard Worker 
21*8d67ca89SAndroid Build Coastguard Worker #include <gtest/gtest.h>
22*8d67ca89SAndroid Build Coastguard Worker 
23*8d67ca89SAndroid Build Coastguard Worker #include <pthread.h>
24*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
25*8d67ca89SAndroid Build Coastguard Worker #include <stdio.h>
26*8d67ca89SAndroid Build Coastguard Worker #include <unistd.h>
27*8d67ca89SAndroid Build Coastguard Worker #include <set>
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #include <android-base/silent_death_test.h>
30*8d67ca89SAndroid Build Coastguard Worker 
31*8d67ca89SAndroid Build Coastguard Worker #include "platform/bionic/mte.h"
32*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_tls.h"
33*8d67ca89SAndroid Build Coastguard Worker 
34*8d67ca89SAndroid Build Coastguard Worker extern "C" pid_t gettid(); // glibc defines this but doesn't declare it anywhere.
35*8d67ca89SAndroid Build Coastguard Worker 
36*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
37*8d67ca89SAndroid Build Coastguard Worker extern uintptr_t __stack_chk_guard;
38*8d67ca89SAndroid Build Coastguard Worker #endif
39*8d67ca89SAndroid Build Coastguard Worker 
40*8d67ca89SAndroid Build Coastguard Worker struct stack_protector_checker {
41*8d67ca89SAndroid Build Coastguard Worker   std::set<pid_t> tids;
42*8d67ca89SAndroid Build Coastguard Worker   std::set<void*> guards;
43*8d67ca89SAndroid Build Coastguard Worker 
Checkstack_protector_checker44*8d67ca89SAndroid Build Coastguard Worker   void Check() {
45*8d67ca89SAndroid Build Coastguard Worker     pid_t tid = gettid();
46*8d67ca89SAndroid Build Coastguard Worker     void* guard = __get_tls()[TLS_SLOT_STACK_GUARD];
47*8d67ca89SAndroid Build Coastguard Worker 
48*8d67ca89SAndroid Build Coastguard Worker     printf("[thread %d] TLS stack guard = %p\n", tid, guard);
49*8d67ca89SAndroid Build Coastguard Worker 
50*8d67ca89SAndroid Build Coastguard Worker     // Duplicate tid. gettid(2) bug? Seeing this would be very upsetting.
51*8d67ca89SAndroid Build Coastguard Worker     ASSERT_FALSE(tids.contains(tid));
52*8d67ca89SAndroid Build Coastguard Worker 
53*8d67ca89SAndroid Build Coastguard Worker     // Uninitialized guard. Our bug. Note this is potentially flaky; we _could_
54*8d67ca89SAndroid Build Coastguard Worker     // get four random zero bytes, but it should be vanishingly unlikely.
55*8d67ca89SAndroid Build Coastguard Worker     ASSERT_NE(guard, nullptr);
56*8d67ca89SAndroid Build Coastguard Worker 
57*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
58*8d67ca89SAndroid Build Coastguard Worker     // bionic always has the global too.
59*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(__stack_chk_guard, reinterpret_cast<uintptr_t>(guard));
60*8d67ca89SAndroid Build Coastguard Worker #endif
61*8d67ca89SAndroid Build Coastguard Worker 
62*8d67ca89SAndroid Build Coastguard Worker     tids.insert(tid);
63*8d67ca89SAndroid Build Coastguard Worker     guards.insert(guard);
64*8d67ca89SAndroid Build Coastguard Worker   }
65*8d67ca89SAndroid Build Coastguard Worker };
66*8d67ca89SAndroid Build Coastguard Worker 
TEST(stack_protector,same_guard_per_thread)67*8d67ca89SAndroid Build Coastguard Worker TEST(stack_protector, same_guard_per_thread) {
68*8d67ca89SAndroid Build Coastguard Worker   // Everyone has the TLS slot set, even if their stack protector
69*8d67ca89SAndroid Build Coastguard Worker   // implementation doesn't yet use it.
70*8d67ca89SAndroid Build Coastguard Worker   stack_protector_checker checker;
71*8d67ca89SAndroid Build Coastguard Worker 
72*8d67ca89SAndroid Build Coastguard Worker   // Check the main thread.
73*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(getpid(), gettid()); // We are the main thread, right?
74*8d67ca89SAndroid Build Coastguard Worker   checker.Check();
75*8d67ca89SAndroid Build Coastguard Worker 
76*8d67ca89SAndroid Build Coastguard Worker   size_t thread_count = 9;
77*8d67ca89SAndroid Build Coastguard Worker   for (size_t i = 1; i < thread_count; ++i) {
78*8d67ca89SAndroid Build Coastguard Worker     pthread_t t;
79*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(0, pthread_create(&t, nullptr, [](void* arg) -> void* {
80*8d67ca89SAndroid Build Coastguard Worker       stack_protector_checker* checker = reinterpret_cast<stack_protector_checker*>(arg);
81*8d67ca89SAndroid Build Coastguard Worker       checker->Check();
82*8d67ca89SAndroid Build Coastguard Worker       return nullptr;
83*8d67ca89SAndroid Build Coastguard Worker     }, &checker));
84*8d67ca89SAndroid Build Coastguard Worker     void* result;
85*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(0, pthread_join(t, &result));
86*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EQ(nullptr, result);
87*8d67ca89SAndroid Build Coastguard Worker   }
88*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(thread_count, checker.tids.size());
89*8d67ca89SAndroid Build Coastguard Worker 
90*8d67ca89SAndroid Build Coastguard Worker   // Both bionic and glibc use the same guard for every thread.
91*8d67ca89SAndroid Build Coastguard Worker   ASSERT_EQ(1U, checker.guards.size());
92*8d67ca89SAndroid Build Coastguard Worker }
93*8d67ca89SAndroid Build Coastguard Worker 
TEST(stack_protector,global_guard)94*8d67ca89SAndroid Build Coastguard Worker TEST(stack_protector, global_guard) {
95*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
96*8d67ca89SAndroid Build Coastguard Worker   // Bionic always has a global, even if it's using TLS.
97*8d67ca89SAndroid Build Coastguard Worker   ASSERT_NE(0, gettid());
98*8d67ca89SAndroid Build Coastguard Worker   ASSERT_NE(0U, __stack_chk_guard);
99*8d67ca89SAndroid Build Coastguard Worker #else
100*8d67ca89SAndroid Build Coastguard Worker   GTEST_SKIP() << "glibc doesn't have a global __stack_chk_guard";
101*8d67ca89SAndroid Build Coastguard Worker #endif
102*8d67ca89SAndroid Build Coastguard Worker }
103*8d67ca89SAndroid Build Coastguard Worker 
104*8d67ca89SAndroid Build Coastguard Worker // Make sure that a stack variable (`*p`) is tagged under MTE, by forcing the
105*8d67ca89SAndroid Build Coastguard Worker // stack safety analysis to fail.
106*8d67ca89SAndroid Build Coastguard Worker int z;
escape_stack_safety_analysis(int * p)107*8d67ca89SAndroid Build Coastguard Worker __attribute__((noinline)) void escape_stack_safety_analysis(int* p) {
108*8d67ca89SAndroid Build Coastguard Worker   *p = z;
109*8d67ca89SAndroid Build Coastguard Worker }
110*8d67ca89SAndroid Build Coastguard Worker 
stack_mte_enabled()111*8d67ca89SAndroid Build Coastguard Worker bool stack_mte_enabled() {
112*8d67ca89SAndroid Build Coastguard Worker   if (!mte_supported()) return false;
113*8d67ca89SAndroid Build Coastguard Worker   int stack_variable;
114*8d67ca89SAndroid Build Coastguard Worker   escape_stack_safety_analysis(&stack_variable);
115*8d67ca89SAndroid Build Coastguard Worker #if defined(__aarch64__)
116*8d67ca89SAndroid Build Coastguard Worker   return reinterpret_cast<uintptr_t>(&stack_variable) & (0xfull << 56);
117*8d67ca89SAndroid Build Coastguard Worker #else   // !defined(__aarch64__)
118*8d67ca89SAndroid Build Coastguard Worker   return false;
119*8d67ca89SAndroid Build Coastguard Worker #endif  // defined(__aarch64__)
120*8d67ca89SAndroid Build Coastguard Worker }
121*8d67ca89SAndroid Build Coastguard Worker 
hwasan_enabled()122*8d67ca89SAndroid Build Coastguard Worker bool hwasan_enabled() {
123*8d67ca89SAndroid Build Coastguard Worker #if __has_feature(hwaddress_sanitizer)
124*8d67ca89SAndroid Build Coastguard Worker   return true;
125*8d67ca89SAndroid Build Coastguard Worker #else
126*8d67ca89SAndroid Build Coastguard Worker   return false;
127*8d67ca89SAndroid Build Coastguard Worker #endif  // __has_feature(hwaddress_sanitizer)
128*8d67ca89SAndroid Build Coastguard Worker }
129*8d67ca89SAndroid Build Coastguard Worker 
130*8d67ca89SAndroid Build Coastguard Worker using stack_protector_DeathTest = SilentDeathTest;
131*8d67ca89SAndroid Build Coastguard Worker 
TEST_F(stack_protector_DeathTest,modify_stack_protector)132*8d67ca89SAndroid Build Coastguard Worker TEST_F(stack_protector_DeathTest, modify_stack_protector) {
133*8d67ca89SAndroid Build Coastguard Worker   // In another file to prevent inlining, which removes stack protection.
134*8d67ca89SAndroid Build Coastguard Worker   extern void modify_stack_protector_test();
135*8d67ca89SAndroid Build Coastguard Worker 
136*8d67ca89SAndroid Build Coastguard Worker   if (stack_mte_enabled()) {
137*8d67ca89SAndroid Build Coastguard Worker     GTEST_SKIP() << "Stack MTE is enabled, stack protector is not available";
138*8d67ca89SAndroid Build Coastguard Worker   } else if (hwasan_enabled()) {
139*8d67ca89SAndroid Build Coastguard Worker     GTEST_SKIP() << "HWASan is enabled, stack protector is not testable";
140*8d67ca89SAndroid Build Coastguard Worker   } else {
141*8d67ca89SAndroid Build Coastguard Worker     ASSERT_EXIT(modify_stack_protector_test(), testing::KilledBySignal(SIGABRT),
142*8d67ca89SAndroid Build Coastguard Worker                 "stack corruption detected");
143*8d67ca89SAndroid Build Coastguard Worker   }
144*8d67ca89SAndroid Build Coastguard Worker }
145