| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 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> | 
| Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 22 | #include "BionicDeathTest.h" | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 23 |  | 
|  | 24 | #include <pthread.h> | 
|  | 25 | #include <stdint.h> | 
|  | 26 | #include <stdio.h> | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 27 | #include <unistd.h> | 
|  | 28 | #include <set> | 
|  | 29 |  | 
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 30 | extern "C" pid_t gettid(); | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 31 |  | 
| Elliott Hughes | fb7eb5e | 2013-02-14 14:37:34 -0800 | [diff] [blame] | 32 | // For x86, bionic and glibc have per-thread stack guard values (all identical). | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 33 | #if defined(__i386__) | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 34 | static uint32_t GetGuardFromTls() { | 
|  | 35 | uint32_t guard; | 
|  | 36 | asm ("mov %%gs:0x14, %0": "=d" (guard)); | 
|  | 37 | return guard; | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | struct stack_protector_checker { | 
|  | 41 | std::set<pid_t> tids; | 
|  | 42 | std::set<uint32_t> guards; | 
|  | 43 |  | 
|  | 44 | void Check() { | 
|  | 45 | pid_t tid = gettid(); | 
|  | 46 | uint32_t guard = GetGuardFromTls(); | 
|  | 47 |  | 
|  | 48 | printf("[thread %d] %%gs:0x14 = 0x%08x\n", tid, guard); | 
|  | 49 |  | 
|  | 50 | // Duplicate tid. gettid(2) bug? Seeing this would be very upsetting. | 
|  | 51 | ASSERT_TRUE(tids.find(tid) == tids.end()); | 
| Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 52 |  | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 53 | // Uninitialized guard. Our bug. Note this is potentially flaky; we _could_ get | 
|  | 54 | // four random zero bytes, but it should be vanishingly unlikely. | 
|  | 55 | ASSERT_NE(guard, 0U); | 
|  | 56 |  | 
|  | 57 | tids.insert(tid); | 
|  | 58 | guards.insert(guard); | 
|  | 59 | } | 
|  | 60 | }; | 
|  | 61 |  | 
|  | 62 | static void* ThreadGuardHelper(void* arg) { | 
|  | 63 | stack_protector_checker* checker = reinterpret_cast<stack_protector_checker*>(arg); | 
|  | 64 | checker->Check(); | 
|  | 65 | return NULL; | 
|  | 66 | } | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 67 | #endif // __i386__ | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 68 |  | 
| Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 69 | TEST(stack_protector, same_guard_per_thread) { | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 70 | #if defined(__i386__) | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 71 | stack_protector_checker checker; | 
|  | 72 | size_t thread_count = 10; | 
|  | 73 | for (size_t i = 0; i < thread_count; ++i) { | 
|  | 74 | pthread_t t; | 
|  | 75 | ASSERT_EQ(0, pthread_create(&t, NULL, ThreadGuardHelper, &checker)); | 
|  | 76 | void* result; | 
|  | 77 | ASSERT_EQ(0, pthread_join(t, &result)); | 
|  | 78 | ASSERT_EQ(NULL, result); | 
|  | 79 | } | 
|  | 80 | ASSERT_EQ(thread_count, checker.tids.size()); | 
|  | 81 |  | 
| Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 82 | // bionic and glibc use the same guard for every thread. | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 83 | ASSERT_EQ(1U, checker.guards.size()); | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 84 | #else // __i386__ | 
|  | 85 | GTEST_LOG_(INFO) << "This test does nothing.\n"; | 
|  | 86 | #endif // __i386__ | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 89 | // For ARM and MIPS, glibc has a global stack check guard value. | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 90 | #if defined(__BIONIC__) || defined(__arm__) || defined(__mips__) | 
|  | 91 | #define TEST_STACK_CHK_GUARD | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 92 |  | 
|  | 93 | // Bionic has the global for x86 too, to support binaries that can run on | 
|  | 94 | // Android releases that didn't implement the TLS guard value. | 
| Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 95 | extern "C" uintptr_t __stack_chk_guard; | 
| Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 96 |  | 
| Nick Kralevich | dcab1b2 | 2013-01-10 17:12:29 -0800 | [diff] [blame] | 97 | /* | 
|  | 98 | * When this function returns, the stack canary will be inconsistent | 
|  | 99 | * with the previous value, which will generate a call to __stack_chk_fail(), | 
|  | 100 | * eventually resulting in a SIGABRT. | 
|  | 101 | * | 
|  | 102 | * This must be marked with "__attribute__ ((noinline))", to ensure the | 
|  | 103 | * compiler generates the proper stack guards around this function. | 
|  | 104 | */ | 
| Yabin Cui | 562ae1e | 2014-12-11 14:35:05 -0800 | [diff] [blame] | 105 | static char* dummy_buf; | 
|  | 106 |  | 
| Nick Kralevich | dcab1b2 | 2013-01-10 17:12:29 -0800 | [diff] [blame] | 107 | __attribute__ ((noinline)) | 
|  | 108 | static void do_modify_stack_chk_guard() { | 
| Yabin Cui | 562ae1e | 2014-12-11 14:35:05 -0800 | [diff] [blame] | 109 | char buf[128]; | 
|  | 110 | // Store local array's address to global variable to force compiler to generate stack guards. | 
|  | 111 | dummy_buf = buf; | 
| Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 112 | __stack_chk_guard = 0x12345678; | 
| Nick Kralevich | dcab1b2 | 2013-01-10 17:12:29 -0800 | [diff] [blame] | 113 | } | 
| Yabin Cui | 562ae1e | 2014-12-11 14:35:05 -0800 | [diff] [blame] | 114 |  | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 115 | #endif | 
| Nick Kralevich | dcab1b2 | 2013-01-10 17:12:29 -0800 | [diff] [blame] | 116 |  | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 117 | TEST(stack_protector, global_guard) { | 
|  | 118 | #if defined(TEST_STACK_CHK_GUARD) | 
|  | 119 | ASSERT_NE(0, gettid()); | 
|  | 120 | ASSERT_NE(0U, __stack_chk_guard); | 
|  | 121 | #else // TEST_STACK_CHK_GUARD | 
|  | 122 | GTEST_LOG_(INFO) << "This test does nothing.\n"; | 
|  | 123 | #endif // TEST_STACK_CHK_GUARD | 
| Nick Kralevich | dcab1b2 | 2013-01-10 17:12:29 -0800 | [diff] [blame] | 124 | } | 
|  | 125 |  | 
| Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 126 | class stack_protector_DeathTest : public BionicDeathTest {}; | 
|  | 127 |  | 
|  | 128 | TEST_F(stack_protector_DeathTest, modify_stack_protector) { | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 129 | #if defined(TEST_STACK_CHK_GUARD) | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 130 | ASSERT_EXIT(do_modify_stack_chk_guard(), testing::KilledBySignal(SIGABRT), ""); | 
|  | 131 | #else // TEST_STACK_CHK_GUARD | 
|  | 132 | GTEST_LOG_(INFO) << "This test does nothing.\n"; | 
|  | 133 | #endif // TEST_STACK_CHK_GUARD | 
|  | 134 | } |