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 #define LOG_TAG "dumpstate"
18 
19 #include "DumpstateUtil.h"
20 
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <sys/prctl.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26 
27 #include <vector>
28 
29 #include <android-base/file.h>
30 #include <android-base/properties.h>
31 #include <android-base/stringprintf.h>
32 #include <android-base/strings.h>
33 #include <android-base/unique_fd.h>
34 #include <log/log.h>
35 
36 #include "DumpstateInternal.h"
37 
38 namespace android {
39 namespace os {
40 namespace dumpstate {
41 
42 namespace {
43 
44 static constexpr const char* kSuPath = "/system/xbin/su";
45 
waitpid_with_timeout(pid_t pid,int timeout_ms,int * status)46 static bool waitpid_with_timeout(pid_t pid, int timeout_ms, int* status) {
47     sigset_t child_mask, old_mask;
48     sigemptyset(&child_mask);
49     sigaddset(&child_mask, SIGCHLD);
50 
51     if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) {
52         printf("*** sigprocmask failed: %s\n", strerror(errno));
53         return false;
54     }
55 
56     timespec ts;
57     ts.tv_sec = MSEC_TO_SEC(timeout_ms);
58     ts.tv_nsec = (timeout_ms % 1000) * 1000000;
59     int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, nullptr, &ts));
60     int saved_errno = errno;
61 
62     // Set the signals back the way they were.
63     if (sigprocmask(SIG_SETMASK, &old_mask, nullptr) == -1) {
64         printf("*** sigprocmask failed: %s\n", strerror(errno));
65         if (ret == 0) {
66             return false;
67         }
68     }
69     if (ret == -1) {
70         errno = saved_errno;
71         if (errno == EAGAIN) {
72             errno = ETIMEDOUT;
73         } else {
74             printf("*** sigtimedwait failed: %s\n", strerror(errno));
75         }
76         return false;
77     }
78 
79     pid_t child_pid = waitpid(pid, status, WNOHANG);
80     if (child_pid != pid) {
81         if (child_pid != -1) {
82             printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid);
83         } else {
84             printf("*** waitpid failed: %s\n", strerror(errno));
85         }
86         return false;
87     }
88     return true;
89 }
90 }  // unnamed namespace
91 
92 CommandOptions CommandOptions::DEFAULT = CommandOptions::WithTimeout(10).Build();
93 CommandOptions CommandOptions::AS_ROOT = CommandOptions::WithTimeout(10).AsRoot().Build();
94 
CommandOptionsBuilder(int64_t timeout_ms)95 CommandOptions::CommandOptionsBuilder::CommandOptionsBuilder(int64_t timeout_ms) : values(timeout_ms) {
96 }
97 
Always()98 CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::Always() {
99     values.always_ = true;
100     return *this;
101 }
102 
AsRoot()103 CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::AsRoot() {
104     if (!PropertiesHelper::IsUnroot()) {
105         values.account_mode_ = SU_ROOT;
106     }
107     return *this;
108 }
109 
AsRootIfAvailable()110 CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::AsRootIfAvailable() {
111     if (!PropertiesHelper::IsUserBuild()) {
112         return AsRoot();
113     }
114     return *this;
115 }
116 
DropRoot()117 CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::DropRoot() {
118     values.account_mode_ = DROP_ROOT;
119     return *this;
120 }
121 
RedirectStderr()122 CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::RedirectStderr() {
123     values.output_mode_ = REDIRECT_TO_STDERR;
124     return *this;
125 }
126 
Log(const std::string & message)127 CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::Log(
128     const std::string& message) {
129     values.logging_message_ = message;
130     return *this;
131 }
132 
Build()133 CommandOptions CommandOptions::CommandOptionsBuilder::Build() {
134     return CommandOptions(values);
135 }
136 
CommandOptionsValues(int64_t timeout_ms)137 CommandOptions::CommandOptionsValues::CommandOptionsValues(int64_t timeout_ms)
138     : timeout_ms_(timeout_ms),
139       always_(false),
140       account_mode_(DONT_DROP_ROOT),
141       output_mode_(NORMAL_OUTPUT),
142       logging_message_("") {
143 }
144 
CommandOptions(const CommandOptionsValues & values)145 CommandOptions::CommandOptions(const CommandOptionsValues& values) : values(values) {
146 }
147 
Timeout() const148 int64_t CommandOptions::Timeout() const {
149     return MSEC_TO_SEC(values.timeout_ms_);
150 }
151 
TimeoutInMs() const152 int64_t CommandOptions::TimeoutInMs() const {
153     return values.timeout_ms_;
154 }
155 
Always() const156 bool CommandOptions::Always() const {
157     return values.always_;
158 }
159 
PrivilegeMode() const160 PrivilegeMode CommandOptions::PrivilegeMode() const {
161     return values.account_mode_;
162 }
163 
OutputMode() const164 OutputMode CommandOptions::OutputMode() const {
165     return values.output_mode_;
166 }
167 
LoggingMessage() const168 std::string CommandOptions::LoggingMessage() const {
169     return values.logging_message_;
170 }
171 
WithTimeout(int64_t timeout_sec)172 CommandOptions::CommandOptionsBuilder CommandOptions::WithTimeout(int64_t timeout_sec) {
173     return CommandOptions::CommandOptionsBuilder(SEC_TO_MSEC(timeout_sec));
174 }
175 
WithTimeoutInMs(int64_t timeout_ms)176 CommandOptions::CommandOptionsBuilder CommandOptions::WithTimeoutInMs(int64_t timeout_ms) {
177     return CommandOptions::CommandOptionsBuilder(timeout_ms);
178 }
179 
180 std::string PropertiesHelper::build_type_ = "";
181 int PropertiesHelper::dry_run_ = -1;
182 int PropertiesHelper::unroot_ = -1;
183 
IsUserBuild()184 bool PropertiesHelper::IsUserBuild() {
185     if (build_type_.empty()) {
186         build_type_ = android::base::GetProperty("ro.build.type", "user");
187     }
188     return "user" == build_type_;
189 }
190 
IsDryRun()191 bool PropertiesHelper::IsDryRun() {
192     if (dry_run_ == -1) {
193         dry_run_ = android::base::GetBoolProperty("dumpstate.dry_run", false) ? 1 : 0;
194     }
195     return dry_run_ == 1;
196 }
197 
IsUnroot()198 bool PropertiesHelper::IsUnroot() {
199     if (unroot_ == -1) {
200         unroot_ = android::base::GetBoolProperty("dumpstate.unroot", false) ? 1 : 0;
201     }
202     return unroot_ == 1;
203 }
204 
DumpFileToFd(int out_fd,const std::string & title,const std::string & path)205 int DumpFileToFd(int out_fd, const std::string& title, const std::string& path) {
206     android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
207     if (fd.get() < 0) {
208         int err = errno;
209         if (title.empty()) {
210             dprintf(out_fd, "*** Error dumping %s: %s\n", path.c_str(), strerror(err));
211         } else {
212             dprintf(out_fd, "*** Error dumping %s (%s): %s\n", path.c_str(), title.c_str(),
213                     strerror(err));
214         }
215         fsync(out_fd);
216         return -1;
217     }
218     return DumpFileFromFdToFd(title, path, fd.get(), out_fd, PropertiesHelper::IsDryRun());
219 }
220 
RunCommandToFd(int fd,const std::string & title,const std::vector<std::string> & full_command,const CommandOptions & options)221 int RunCommandToFd(int fd, const std::string& title, const std::vector<std::string>& full_command,
222                    const CommandOptions& options) {
223     if (full_command.empty()) {
224         MYLOGE("No arguments on RunCommandToFd(%s)\n", title.c_str());
225         return -1;
226     }
227 
228     int size = full_command.size() + 1;  // null terminated
229     int starting_index = 0;
230     if (options.PrivilegeMode() == SU_ROOT) {
231         starting_index = 2;  // "su" "root"
232         size += starting_index;
233     }
234 
235     std::vector<const char*> args;
236     args.resize(size);
237 
238     std::string command_string;
239     if (options.PrivilegeMode() == SU_ROOT) {
240         args[0] = kSuPath;
241         command_string += kSuPath;
242         args[1] = "root";
243         command_string += " root ";
244     }
245     for (size_t i = 0; i < full_command.size(); i++) {
246         args[i + starting_index] = full_command[i].data();
247         command_string += args[i + starting_index];
248         if (i != full_command.size() - 1) {
249             command_string += " ";
250         }
251     }
252     args[size - 1] = nullptr;
253 
254     const char* command = command_string.c_str();
255 
256     if (options.PrivilegeMode() == SU_ROOT && PropertiesHelper::IsUserBuild()) {
257         dprintf(fd, "Skipping '%s' on user build.\n", command);
258         return 0;
259     }
260 
261     if (!title.empty()) {
262         dprintf(fd, "------ %s (%s) ------\n", title.c_str(), command);
263         fsync(fd);
264     }
265 
266     const std::string& logging_message = options.LoggingMessage();
267     if (!logging_message.empty()) {
268         MYLOGI(logging_message.c_str(), command_string.c_str());
269     }
270 
271     bool silent = (options.OutputMode() == REDIRECT_TO_STDERR);
272     bool redirecting_to_fd = STDOUT_FILENO != fd;
273 
274     if (PropertiesHelper::IsDryRun() && !options.Always()) {
275         if (!title.empty()) {
276             dprintf(fd, "\t(skipped on dry run)\n");
277         } else if (redirecting_to_fd) {
278             // There is no title, but we should still print a dry-run message
279             dprintf(fd, "%s: skipped on dry run\n", command_string.c_str());
280         }
281         fsync(fd);
282         return 0;
283     }
284 
285     const char* path = args[0];
286 
287     uint64_t start = Nanotime();
288     pid_t pid = fork();
289 
290     /* handle error case */
291     if (pid < 0) {
292         if (!silent) dprintf(fd, "*** fork: %s\n", strerror(errno));
293         MYLOGE("*** fork: %s\n", strerror(errno));
294         return pid;
295     }
296 
297     /* handle child case */
298     if (pid == 0) {
299         if (options.PrivilegeMode() == DROP_ROOT && !DropRootUser()) {
300             if (!silent) {
301                 dprintf(fd, "*** failed to drop root before running %s: %s\n", command,
302                         strerror(errno));
303             }
304             MYLOGE("*** could not drop root before running %s: %s\n", command, strerror(errno));
305             return -1;
306         }
307 
308         if (silent) {
309             // Redirects stdout to stderr
310             TEMP_FAILURE_RETRY(dup2(STDERR_FILENO, STDOUT_FILENO));
311         } else if (redirecting_to_fd) {
312             // Redirect stdout to fd
313             TEMP_FAILURE_RETRY(dup2(fd, STDOUT_FILENO));
314             close(fd);
315         }
316 
317         /* make sure the child dies when dumpstate dies */
318         prctl(PR_SET_PDEATHSIG, SIGKILL);
319 
320         /* just ignore SIGPIPE, will go down with parent's */
321         struct sigaction sigact;
322         memset(&sigact, 0, sizeof(sigact));
323         sigact.sa_handler = SIG_IGN;
324         sigaction(SIGPIPE, &sigact, nullptr);
325 
326         execvp(path, (char**)args.data());
327         // execvp's result will be handled after waitpid_with_timeout() below, but
328         // if it failed, it's safer to exit dumpstate.
329         MYLOGD("execvp on command '%s' failed (error: %s)\n", command, strerror(errno));
330         // Must call _exit (instead of exit), otherwise it will corrupt the zip
331         // file.
332         _exit(EXIT_FAILURE);
333     }
334 
335     /* handle parent case */
336     int status;
337     bool ret = waitpid_with_timeout(pid, options.TimeoutInMs(), &status);
338     fsync(fd);
339 
340     uint64_t elapsed = Nanotime() - start;
341     if (!ret) {
342         if (errno == ETIMEDOUT) {
343             if (!silent)
344                 dprintf(fd, "*** command '%s' timed out after %.3fs (killing pid %d)\n", command,
345                         static_cast<float>(elapsed) / NANOS_PER_SEC, pid);
346             MYLOGE("*** command '%s' timed out after %.3fs (killing pid %d)\n", command,
347                    static_cast<float>(elapsed) / NANOS_PER_SEC, pid);
348         } else {
349             if (!silent)
350                 dprintf(fd, "*** command '%s': Error after %.4fs (killing pid %d)\n", command,
351                         static_cast<float>(elapsed) / NANOS_PER_SEC, pid);
352             MYLOGE("command '%s': Error after %.4fs (killing pid %d)\n", command,
353                    static_cast<float>(elapsed) / NANOS_PER_SEC, pid);
354         }
355         kill(pid, SIGTERM);
356         if (!waitpid_with_timeout(pid, 5000, nullptr)) {
357             kill(pid, SIGKILL);
358             if (!waitpid_with_timeout(pid, 5000, nullptr)) {
359                 if (!silent)
360                     dprintf(fd, "could not kill command '%s' (pid %d) even with SIGKILL.\n",
361                             command, pid);
362                 MYLOGE("could not kill command '%s' (pid %d) even with SIGKILL.\n", command, pid);
363             }
364         }
365         return -1;
366     }
367 
368     if (WIFSIGNALED(status)) {
369         if (!silent)
370             dprintf(fd, "*** command '%s' failed: killed by signal %d\n", command, WTERMSIG(status));
371         MYLOGE("*** command '%s' failed: killed by signal %d\n", command, WTERMSIG(status));
372     } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
373         status = WEXITSTATUS(status);
374         if (!silent) dprintf(fd, "*** command '%s' failed: exit code %d\n", command, status);
375         MYLOGE("*** command '%s' failed: exit code %d\n", command, status);
376     }
377 
378     return status;
379 }
380 
381 }  // namespace dumpstate
382 }  // namespace os
383 }  // namespace android
384