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 <unistd.h>
|
|
|
|
#include <set>
|
|
|
|
|
2016-03-04 20:53:09 +01:00
|
|
|
#include "private/bionic_tls.h"
|
|
|
|
|
|
|
|
extern "C" pid_t gettid(); // glibc defines this but doesn't declare it anywhere.
|
|
|
|
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
extern uintptr_t __stack_chk_guard;
|
|
|
|
#endif
|
2012-10-25 03:37:21 +02:00
|
|
|
|
|
|
|
struct stack_protector_checker {
|
|
|
|
std::set<pid_t> tids;
|
2016-03-04 20:53:09 +01:00
|
|
|
std::set<void*> guards;
|
2012-10-25 03:37:21 +02:00
|
|
|
|
|
|
|
void Check() {
|
|
|
|
pid_t tid = gettid();
|
2016-03-04 20:53:09 +01:00
|
|
|
void* guard = __get_tls()[TLS_SLOT_STACK_GUARD];
|
2012-10-25 03:37:21 +02:00
|
|
|
|
2016-03-04 20:53:09 +01:00
|
|
|
printf("[thread %d] TLS stack guard = %p\n", tid, guard);
|
2012-10-25 03:37:21 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2016-03-04 20:53:09 +01: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, nullptr);
|
|
|
|
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
// bionic always has the global too.
|
|
|
|
ASSERT_EQ(__stack_chk_guard, reinterpret_cast<uintptr_t>(guard));
|
|
|
|
#endif
|
2012-10-25 03:37:21 +02:00
|
|
|
|
|
|
|
tids.insert(tid);
|
|
|
|
guards.insert(guard);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-08 03:39:34 +01:00
|
|
|
TEST(stack_protector, same_guard_per_thread) {
|
2016-03-04 20:53:09 +01:00
|
|
|
// Everyone has the TLS slot set, even if their stack protector
|
|
|
|
// implementation doesn't yet use it.
|
2012-10-25 03:37:21 +02:00
|
|
|
stack_protector_checker checker;
|
2016-03-04 20:53:09 +01:00
|
|
|
|
|
|
|
// Check the main thread.
|
|
|
|
ASSERT_EQ(getpid(), gettid()); // We are the main thread, right?
|
|
|
|
checker.Check();
|
|
|
|
|
|
|
|
size_t thread_count = 9;
|
|
|
|
for (size_t i = 1; i < thread_count; ++i) {
|
2012-10-25 03:37:21 +02:00
|
|
|
pthread_t t;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0, pthread_create(&t, nullptr, [](void* arg) -> void* {
|
2016-03-04 20:53:09 +01:00
|
|
|
stack_protector_checker* checker = reinterpret_cast<stack_protector_checker*>(arg);
|
|
|
|
checker->Check();
|
|
|
|
return nullptr;
|
|
|
|
}, &checker));
|
2012-10-25 03:37:21 +02:00
|
|
|
void* result;
|
|
|
|
ASSERT_EQ(0, pthread_join(t, &result));
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(nullptr, result);
|
2012-10-25 03:37:21 +02:00
|
|
|
}
|
|
|
|
ASSERT_EQ(thread_count, checker.tids.size());
|
|
|
|
|
2016-03-04 20:53:09 +01:00
|
|
|
// Both 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
|
|
|
TEST(stack_protector, global_guard) {
|
2016-03-04 20:53:09 +01:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
// Bionic always has a global, even if it's using TLS.
|
2013-12-21 03:43:21 +01:00
|
|
|
ASSERT_NE(0, gettid());
|
|
|
|
ASSERT_NE(0U, __stack_chk_guard);
|
2016-03-04 20:53:09 +01:00
|
|
|
#else
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "glibc doesn't have a global __stack_chk_guard";
|
2016-03-04 20:53:09 +01:00
|
|
|
#endif
|
2013-12-21 03:43:21 +01:00
|
|
|
}
|
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) {
|
2016-03-04 20:53:09 +01:00
|
|
|
// In another file to prevent inlining, which removes stack protection.
|
|
|
|
extern void modify_stack_protector_test();
|
2019-02-06 03:43:34 +01:00
|
|
|
#if __has_feature(hwaddress_sanitizer)
|
|
|
|
ASSERT_EXIT(modify_stack_protector_test(),
|
|
|
|
testing::KilledBySignal(SIGABRT), "tag-mismatch");
|
|
|
|
#else
|
2016-03-04 20:53:09 +01:00
|
|
|
ASSERT_EXIT(modify_stack_protector_test(),
|
|
|
|
testing::KilledBySignal(SIGABRT), "stack corruption detected");
|
2019-02-06 03:43:34 +01:00
|
|
|
#endif
|
2013-01-11 02:12:29 +01:00
|
|
|
}
|