2012-10-25 03:37:21 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contributed by: Intel Corporation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2014-11-06 03:01:01 +01:00
|
|
|
#include "BionicDeathTest.h"
|
2012-10-25 03:37:21 +02:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <set>
|
|
|
|
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__GLIBC__)
|
2012-10-25 03:37:21 +02:00
|
|
|
// glibc doesn't expose gettid(2).
|
|
|
|
pid_t gettid() { return syscall(__NR_gettid); }
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __GLIBC__
|
2012-10-25 03:37:21 +02:00
|
|
|
|
2013-02-14 23:37:34 +01:00
|
|
|
// For x86, bionic and glibc have per-thread stack guard values (all identical).
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__i386__)
|
2012-10-25 03:37:21 +02:00
|
|
|
static uint32_t GetGuardFromTls() {
|
|
|
|
uint32_t guard;
|
|
|
|
asm ("mov %%gs:0x14, %0": "=d" (guard));
|
|
|
|
return guard;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stack_protector_checker {
|
|
|
|
std::set<pid_t> tids;
|
|
|
|
std::set<uint32_t> guards;
|
|
|
|
|
|
|
|
void Check() {
|
|
|
|
pid_t tid = gettid();
|
|
|
|
uint32_t guard = GetGuardFromTls();
|
|
|
|
|
|
|
|
printf("[thread %d] %%gs:0x14 = 0x%08x\n", tid, guard);
|
|
|
|
|
|
|
|
// Duplicate tid. gettid(2) bug? Seeing this would be very upsetting.
|
|
|
|
ASSERT_TRUE(tids.find(tid) == tids.end());
|
2013-02-08 03:39:34 +01:00
|
|
|
|
2012-10-25 03:37:21 +02:00
|
|
|
// Uninitialized guard. Our bug. Note this is potentially flaky; we _could_ get
|
|
|
|
// four random zero bytes, but it should be vanishingly unlikely.
|
|
|
|
ASSERT_NE(guard, 0U);
|
|
|
|
|
|
|
|
tids.insert(tid);
|
|
|
|
guards.insert(guard);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void* ThreadGuardHelper(void* arg) {
|
|
|
|
stack_protector_checker* checker = reinterpret_cast<stack_protector_checker*>(arg);
|
|
|
|
checker->Check();
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __i386__
|
2012-10-25 03:37:21 +02:00
|
|
|
|
2013-02-08 03:39:34 +01:00
|
|
|
TEST(stack_protector, same_guard_per_thread) {
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__i386__)
|
2012-10-25 03:37:21 +02:00
|
|
|
stack_protector_checker checker;
|
|
|
|
size_t thread_count = 10;
|
|
|
|
for (size_t i = 0; i < thread_count; ++i) {
|
|
|
|
pthread_t t;
|
|
|
|
ASSERT_EQ(0, pthread_create(&t, NULL, ThreadGuardHelper, &checker));
|
|
|
|
void* result;
|
|
|
|
ASSERT_EQ(0, pthread_join(t, &result));
|
|
|
|
ASSERT_EQ(NULL, result);
|
|
|
|
}
|
|
|
|
ASSERT_EQ(thread_count, checker.tids.size());
|
|
|
|
|
2013-02-08 03:39:34 +01:00
|
|
|
// bionic and glibc use the same guard for every thread.
|
2012-10-25 03:37:21 +02:00
|
|
|
ASSERT_EQ(1U, checker.guards.size());
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __i386__
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing.\n";
|
|
|
|
#endif // __i386__
|
2012-10-25 03:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// For ARM and MIPS, glibc has a global stack check guard value.
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__BIONIC__) || defined(__arm__) || defined(__mips__)
|
|
|
|
#define TEST_STACK_CHK_GUARD
|
2012-10-25 03:37:21 +02:00
|
|
|
|
|
|
|
// Bionic has the global for x86 too, to support binaries that can run on
|
|
|
|
// Android releases that didn't implement the TLS guard value.
|
2013-02-08 03:39:34 +01:00
|
|
|
extern "C" uintptr_t __stack_chk_guard;
|
2012-10-25 03:37:21 +02:00
|
|
|
|
2013-01-11 02:12:29 +01:00
|
|
|
/*
|
|
|
|
* When this function returns, the stack canary will be inconsistent
|
|
|
|
* with the previous value, which will generate a call to __stack_chk_fail(),
|
|
|
|
* eventually resulting in a SIGABRT.
|
|
|
|
*
|
|
|
|
* This must be marked with "__attribute__ ((noinline))", to ensure the
|
|
|
|
* compiler generates the proper stack guards around this function.
|
|
|
|
*/
|
2014-12-11 23:35:05 +01:00
|
|
|
static char* dummy_buf;
|
|
|
|
|
2013-01-11 02:12:29 +01:00
|
|
|
__attribute__ ((noinline))
|
|
|
|
static void do_modify_stack_chk_guard() {
|
2014-12-11 23:35:05 +01:00
|
|
|
char buf[128];
|
|
|
|
// Store local array's address to global variable to force compiler to generate stack guards.
|
|
|
|
dummy_buf = buf;
|
2013-02-08 03:39:34 +01:00
|
|
|
__stack_chk_guard = 0x12345678;
|
2013-01-11 02:12:29 +01:00
|
|
|
}
|
2014-12-11 23:35:05 +01:00
|
|
|
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST(stack_protector, global_guard) {
|
|
|
|
#if defined(TEST_STACK_CHK_GUARD)
|
|
|
|
ASSERT_NE(0, gettid());
|
|
|
|
ASSERT_NE(0U, __stack_chk_guard);
|
|
|
|
#else // TEST_STACK_CHK_GUARD
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing.\n";
|
|
|
|
#endif // TEST_STACK_CHK_GUARD
|
|
|
|
}
|
2013-01-11 02:12:29 +01:00
|
|
|
|
2014-11-06 03:01:01 +01:00
|
|
|
class stack_protector_DeathTest : public BionicDeathTest {};
|
|
|
|
|
|
|
|
TEST_F(stack_protector_DeathTest, modify_stack_protector) {
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(TEST_STACK_CHK_GUARD)
|
2013-06-12 00:45:23 +02:00
|
|
|
ASSERT_EXIT(do_modify_stack_chk_guard(), testing::KilledBySignal(SIGABRT), "");
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // TEST_STACK_CHK_GUARD
|
|
|
|
GTEST_LOG_(INFO) << "This test does nothing.\n";
|
|
|
|
#endif // TEST_STACK_CHK_GUARD
|
2013-01-11 02:12:29 +01:00
|
|
|
}
|