1 /*
2 * Copyright (C) 2016 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 #include <errno.h>
18 #include <fcntl.h>
19 #include <inttypes.h>
20 #include <pthread.h>
21 #include <sched.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/prctl.h>
28 #include <sys/syscall.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32
33 #include "android-base/macros.h"
34
35 #include "PtracerThread.h"
36 #include "log.h"
37
38 namespace android {
39
40 class Stack {
41 public:
Stack(size_t size)42 explicit Stack(size_t size) : size_(size) {
43 int prot = PROT_READ | PROT_WRITE;
44 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
45 page_size_ = getpagesize();
46 // Round up size to page_size
47 size_ = (size_ + (page_size_ - 1)) & ~(page_size_ - 1);
48 size_ += page_size_ * 2; // guard pages
49 base_ = mmap(NULL, size_, prot, flags, -1, 0);
50 if (base_ == MAP_FAILED) {
51 base_ = NULL;
52 size_ = 0;
53 return;
54 }
55 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, base_, size_, "libmemunreachable stack");
56 mprotect(base_, page_size_, PROT_NONE);
57 mprotect(top(), page_size_, PROT_NONE);
58 };
~Stack()59 ~Stack() { munmap(base_, size_); };
top()60 void* top() {
61 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(base_) + size_ - page_size_);
62 };
63
64 private:
65 DISALLOW_COPY_AND_ASSIGN(Stack);
66
67 void* base_;
68 size_t size_;
69 size_t page_size_;
70 };
71
PtracerThread(const std::function<int ()> & func)72 PtracerThread::PtracerThread(const std::function<int()>& func) : child_pid_(0) {
73 stack_ = std::make_unique<Stack>(512 * 1024); // 512 kB
74 if (stack_->top() == nullptr) {
75 MEM_LOG_ALWAYS_FATAL("failed to mmap child stack: %s", strerror(errno));
76 }
77
78 func_ = std::function<int()>{[&, func]() -> int {
79 // In the child thread, lock and unlock the mutex to wait for the parent
80 // to finish setting up for the child thread
81 std::unique_lock<std::mutex> lk(m_);
82 lk.unlock();
83 _exit(func());
84 }};
85 }
86
~PtracerThread()87 PtracerThread::~PtracerThread() {
88 Kill();
89 Join();
90 ClearTracer();
91 stack_ = nullptr;
92 }
93
Start()94 bool PtracerThread::Start() {
95 std::unique_lock<std::mutex> lk(m_);
96
97 // Convert from void(*)(void*) to lambda with captures
98 auto proxy = [](void* arg) -> int {
99 prctl(PR_SET_NAME, "libmemunreachable ptrace thread");
100 return (*reinterpret_cast<std::function<int()>*>(arg))();
101 };
102
103 // See README.md for why we create the child process this way
104 child_pid_ = clone(proxy, stack_->top(), CLONE_VM | CLONE_FS | CLONE_FILES /*|CLONE_UNTRACED*/,
105 reinterpret_cast<void*>(&func_));
106 if (child_pid_ < 0) {
107 MEM_ALOGE("failed to clone child: %s", strerror(errno));
108 return false;
109 }
110
111 SetTracer(child_pid_);
112
113 lk.unlock();
114
115 return true;
116 }
117
Join()118 int PtracerThread::Join() {
119 if (child_pid_ == -1) {
120 return -1;
121 }
122 int status;
123 int ret = TEMP_FAILURE_RETRY(waitpid(child_pid_, &status, __WALL));
124 if (ret < 0) {
125 MEM_ALOGE("waitpid %d failed: %s", child_pid_, strerror(errno));
126 return -1;
127 }
128
129 child_pid_ = -1;
130
131 if (WIFEXITED(status)) {
132 return WEXITSTATUS(status);
133 } else if (WIFSIGNALED(status)) {
134 return -WTERMSIG(status);
135 } else {
136 MEM_ALOGE("unexpected status %x", status);
137 return -1;
138 }
139 }
140
Kill()141 void PtracerThread::Kill() {
142 if (child_pid_ == -1) {
143 return;
144 }
145
146 syscall(SYS_tkill, child_pid_, SIGKILL);
147 }
148
SetTracer(pid_t tracer_pid)149 void PtracerThread::SetTracer(pid_t tracer_pid) {
150 prctl(PR_SET_PTRACER, tracer_pid);
151 }
152
ClearTracer()153 void PtracerThread::ClearTracer() {
154 prctl(PR_SET_PTRACER, 0);
155 }
156
157 } // namespace android
158