• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 
17 /*
18  * Contributed by: Intel Corporation
19  */
20 
21 #include <gtest/gtest.h>
22 
23 #include <pthread.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <set>
28 
29 #include <android-base/silent_death_test.h>
30 
31 #include "private/bionic_tls.h"
32 
33 extern "C" pid_t gettid(); // glibc defines this but doesn't declare it anywhere.
34 
35 #if defined(__BIONIC__)
36 extern uintptr_t __stack_chk_guard;
37 #endif
38 
39 struct stack_protector_checker {
40   std::set<pid_t> tids;
41   std::set<void*> guards;
42 
Checkstack_protector_checker43   void Check() {
44     pid_t tid = gettid();
45     void* guard = __get_tls()[TLS_SLOT_STACK_GUARD];
46 
47     printf("[thread %d] TLS stack guard = %p\n", tid, guard);
48 
49     // Duplicate tid. gettid(2) bug? Seeing this would be very upsetting.
50     ASSERT_TRUE(tids.find(tid) == tids.end());
51 
52     // Uninitialized guard. Our bug. Note this is potentially flaky; we _could_
53     // get four random zero bytes, but it should be vanishingly unlikely.
54     ASSERT_NE(guard, nullptr);
55 
56 #if defined(__BIONIC__)
57     // bionic always has the global too.
58     ASSERT_EQ(__stack_chk_guard, reinterpret_cast<uintptr_t>(guard));
59 #endif
60 
61     tids.insert(tid);
62     guards.insert(guard);
63   }
64 };
65 
TEST(stack_protector,same_guard_per_thread)66 TEST(stack_protector, same_guard_per_thread) {
67   // Everyone has the TLS slot set, even if their stack protector
68   // implementation doesn't yet use it.
69   stack_protector_checker checker;
70 
71   // Check the main thread.
72   ASSERT_EQ(getpid(), gettid()); // We are the main thread, right?
73   checker.Check();
74 
75   size_t thread_count = 9;
76   for (size_t i = 1; i < thread_count; ++i) {
77     pthread_t t;
78     ASSERT_EQ(0, pthread_create(&t, nullptr, [](void* arg) -> void* {
79       stack_protector_checker* checker = reinterpret_cast<stack_protector_checker*>(arg);
80       checker->Check();
81       return nullptr;
82     }, &checker));
83     void* result;
84     ASSERT_EQ(0, pthread_join(t, &result));
85     ASSERT_EQ(nullptr, result);
86   }
87   ASSERT_EQ(thread_count, checker.tids.size());
88 
89   // Both bionic and glibc use the same guard for every thread.
90   ASSERT_EQ(1U, checker.guards.size());
91 }
92 
TEST(stack_protector,global_guard)93 TEST(stack_protector, global_guard) {
94 #if defined(__BIONIC__)
95   // Bionic always has a global, even if it's using TLS.
96   ASSERT_NE(0, gettid());
97   ASSERT_NE(0U, __stack_chk_guard);
98 #else
99   GTEST_SKIP() << "glibc doesn't have a global __stack_chk_guard";
100 #endif
101 }
102 
103 using stack_protector_DeathTest = SilentDeathTest;
104 
TEST_F(stack_protector_DeathTest,modify_stack_protector)105 TEST_F(stack_protector_DeathTest, modify_stack_protector) {
106   // In another file to prevent inlining, which removes stack protection.
107   extern void modify_stack_protector_test();
108 #if __has_feature(hwaddress_sanitizer)
109   ASSERT_EXIT(modify_stack_protector_test(),
110               testing::KilledBySignal(SIGABRT), "tag-mismatch");
111 #else
112   ASSERT_EXIT(modify_stack_protector_test(),
113               testing::KilledBySignal(SIGABRT), "stack corruption detected");
114 #endif
115 }
116