1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "dumpstate"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #include "DumpstateInternal.h"
20*38e8c45fSAndroid Build Coastguard Worker
21*38e8c45fSAndroid Build Coastguard Worker #include <errno.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <grp.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <poll.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <pwd.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <stdint.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <stdio.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <string.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <sys/capability.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <sys/prctl.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <sys/stat.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <sys/types.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <unistd.h>
33*38e8c45fSAndroid Build Coastguard Worker
34*38e8c45fSAndroid Build Coastguard Worker #include <cstdint>
35*38e8c45fSAndroid Build Coastguard Worker #include <string>
36*38e8c45fSAndroid Build Coastguard Worker #include <vector>
37*38e8c45fSAndroid Build Coastguard Worker
38*38e8c45fSAndroid Build Coastguard Worker #include <android-base/file.h>
39*38e8c45fSAndroid Build Coastguard Worker #include <android-base/macros.h>
40*38e8c45fSAndroid Build Coastguard Worker #include <log/log.h>
41*38e8c45fSAndroid Build Coastguard Worker
Nanotime()42*38e8c45fSAndroid Build Coastguard Worker uint64_t Nanotime() {
43*38e8c45fSAndroid Build Coastguard Worker timespec ts;
44*38e8c45fSAndroid Build Coastguard Worker clock_gettime(CLOCK_MONOTONIC, &ts);
45*38e8c45fSAndroid Build Coastguard Worker return static_cast<uint64_t>(ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec);
46*38e8c45fSAndroid Build Coastguard Worker }
47*38e8c45fSAndroid Build Coastguard Worker
48*38e8c45fSAndroid Build Coastguard Worker // Switches to non-root user and group.
DropRootUser()49*38e8c45fSAndroid Build Coastguard Worker bool DropRootUser() {
50*38e8c45fSAndroid Build Coastguard Worker struct group* grp = getgrnam("shell");
51*38e8c45fSAndroid Build Coastguard Worker gid_t shell_gid = grp != nullptr ? grp->gr_gid : 0;
52*38e8c45fSAndroid Build Coastguard Worker struct passwd* pwd = getpwnam("shell");
53*38e8c45fSAndroid Build Coastguard Worker uid_t shell_uid = pwd != nullptr ? pwd->pw_uid : 0;
54*38e8c45fSAndroid Build Coastguard Worker
55*38e8c45fSAndroid Build Coastguard Worker if (!shell_gid || !shell_uid) {
56*38e8c45fSAndroid Build Coastguard Worker MYLOGE("Unable to get AID_SHELL: %s\n", strerror(errno));
57*38e8c45fSAndroid Build Coastguard Worker return false;
58*38e8c45fSAndroid Build Coastguard Worker }
59*38e8c45fSAndroid Build Coastguard Worker
60*38e8c45fSAndroid Build Coastguard Worker if (getgid() == shell_gid && getuid() == shell_uid) {
61*38e8c45fSAndroid Build Coastguard Worker MYLOGD("drop_root_user(): already running as Shell\n");
62*38e8c45fSAndroid Build Coastguard Worker return true;
63*38e8c45fSAndroid Build Coastguard Worker }
64*38e8c45fSAndroid Build Coastguard Worker /* ensure we will keep capabilities when we drop root */
65*38e8c45fSAndroid Build Coastguard Worker if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
66*38e8c45fSAndroid Build Coastguard Worker MYLOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
67*38e8c45fSAndroid Build Coastguard Worker return false;
68*38e8c45fSAndroid Build Coastguard Worker }
69*38e8c45fSAndroid Build Coastguard Worker
70*38e8c45fSAndroid Build Coastguard Worker static const std::vector<std::string> group_names{
71*38e8c45fSAndroid Build Coastguard Worker "log", "sdcard_r", "sdcard_rw", "mount", "inet", "net_bw_stats",
72*38e8c45fSAndroid Build Coastguard Worker "readproc", "bluetooth", "wakelock", "nfc"};
73*38e8c45fSAndroid Build Coastguard Worker std::vector<gid_t> groups(group_names.size(), 0);
74*38e8c45fSAndroid Build Coastguard Worker for (size_t i = 0; i < group_names.size(); ++i) {
75*38e8c45fSAndroid Build Coastguard Worker grp = getgrnam(group_names[i].c_str());
76*38e8c45fSAndroid Build Coastguard Worker groups[i] = grp != nullptr ? grp->gr_gid : 0;
77*38e8c45fSAndroid Build Coastguard Worker if (groups[i] == 0) {
78*38e8c45fSAndroid Build Coastguard Worker MYLOGE("Unable to get required gid '%s': %s\n", group_names[i].c_str(),
79*38e8c45fSAndroid Build Coastguard Worker strerror(errno));
80*38e8c45fSAndroid Build Coastguard Worker return false;
81*38e8c45fSAndroid Build Coastguard Worker }
82*38e8c45fSAndroid Build Coastguard Worker }
83*38e8c45fSAndroid Build Coastguard Worker
84*38e8c45fSAndroid Build Coastguard Worker if (setgroups(groups.size(), groups.data()) != 0) {
85*38e8c45fSAndroid Build Coastguard Worker MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
86*38e8c45fSAndroid Build Coastguard Worker return false;
87*38e8c45fSAndroid Build Coastguard Worker }
88*38e8c45fSAndroid Build Coastguard Worker if (setgid(shell_gid) != 0) {
89*38e8c45fSAndroid Build Coastguard Worker MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno));
90*38e8c45fSAndroid Build Coastguard Worker return false;
91*38e8c45fSAndroid Build Coastguard Worker }
92*38e8c45fSAndroid Build Coastguard Worker if (setuid(shell_uid) != 0) {
93*38e8c45fSAndroid Build Coastguard Worker MYLOGE("Unable to setuid, aborting: %s\n", strerror(errno));
94*38e8c45fSAndroid Build Coastguard Worker return false;
95*38e8c45fSAndroid Build Coastguard Worker }
96*38e8c45fSAndroid Build Coastguard Worker
97*38e8c45fSAndroid Build Coastguard Worker struct __user_cap_header_struct capheader;
98*38e8c45fSAndroid Build Coastguard Worker struct __user_cap_data_struct capdata[2];
99*38e8c45fSAndroid Build Coastguard Worker memset(&capheader, 0, sizeof(capheader));
100*38e8c45fSAndroid Build Coastguard Worker memset(&capdata, 0, sizeof(capdata));
101*38e8c45fSAndroid Build Coastguard Worker capheader.version = _LINUX_CAPABILITY_VERSION_3;
102*38e8c45fSAndroid Build Coastguard Worker capheader.pid = 0;
103*38e8c45fSAndroid Build Coastguard Worker
104*38e8c45fSAndroid Build Coastguard Worker if (capget(&capheader, &capdata[0]) != 0) {
105*38e8c45fSAndroid Build Coastguard Worker MYLOGE("capget failed: %s\n", strerror(errno));
106*38e8c45fSAndroid Build Coastguard Worker return false;
107*38e8c45fSAndroid Build Coastguard Worker }
108*38e8c45fSAndroid Build Coastguard Worker
109*38e8c45fSAndroid Build Coastguard Worker const uint32_t cap_syslog_mask = CAP_TO_MASK(CAP_SYSLOG);
110*38e8c45fSAndroid Build Coastguard Worker const uint32_t cap_syslog_index = CAP_TO_INDEX(CAP_SYSLOG);
111*38e8c45fSAndroid Build Coastguard Worker bool has_cap_syslog = (capdata[cap_syslog_index].permitted & cap_syslog_mask) != 0;
112*38e8c45fSAndroid Build Coastguard Worker
113*38e8c45fSAndroid Build Coastguard Worker memset(&capdata, 0, sizeof(capdata));
114*38e8c45fSAndroid Build Coastguard Worker if (has_cap_syslog) {
115*38e8c45fSAndroid Build Coastguard Worker // Only attempt to keep CAP_SYSLOG if it was present to begin with.
116*38e8c45fSAndroid Build Coastguard Worker capdata[cap_syslog_index].permitted |= cap_syslog_mask;
117*38e8c45fSAndroid Build Coastguard Worker capdata[cap_syslog_index].effective |= cap_syslog_mask;
118*38e8c45fSAndroid Build Coastguard Worker }
119*38e8c45fSAndroid Build Coastguard Worker
120*38e8c45fSAndroid Build Coastguard Worker const uint32_t cap_block_suspend_mask = CAP_TO_MASK(CAP_BLOCK_SUSPEND);
121*38e8c45fSAndroid Build Coastguard Worker const uint32_t cap_block_suspend_index = CAP_TO_INDEX(CAP_BLOCK_SUSPEND);
122*38e8c45fSAndroid Build Coastguard Worker capdata[cap_block_suspend_index].permitted |= cap_block_suspend_mask;
123*38e8c45fSAndroid Build Coastguard Worker capdata[cap_block_suspend_index].effective |= cap_block_suspend_mask;
124*38e8c45fSAndroid Build Coastguard Worker
125*38e8c45fSAndroid Build Coastguard Worker if (capset(&capheader, &capdata[0]) != 0) {
126*38e8c45fSAndroid Build Coastguard Worker MYLOGE("capset({%#x, %#x}) failed: %s\n", capdata[0].effective,
127*38e8c45fSAndroid Build Coastguard Worker capdata[1].effective, strerror(errno));
128*38e8c45fSAndroid Build Coastguard Worker return false;
129*38e8c45fSAndroid Build Coastguard Worker }
130*38e8c45fSAndroid Build Coastguard Worker
131*38e8c45fSAndroid Build Coastguard Worker return true;
132*38e8c45fSAndroid Build Coastguard Worker }
133*38e8c45fSAndroid Build Coastguard Worker
DumpFileFromFdToFd(const std::string & title,const std::string & path_string,int fd,int out_fd,bool dry_run)134*38e8c45fSAndroid Build Coastguard Worker int DumpFileFromFdToFd(const std::string& title, const std::string& path_string, int fd, int out_fd,
135*38e8c45fSAndroid Build Coastguard Worker bool dry_run) {
136*38e8c45fSAndroid Build Coastguard Worker const char* path = path_string.c_str();
137*38e8c45fSAndroid Build Coastguard Worker if (!title.empty()) {
138*38e8c45fSAndroid Build Coastguard Worker dprintf(out_fd, "------ %s (%s", title.c_str(), path);
139*38e8c45fSAndroid Build Coastguard Worker
140*38e8c45fSAndroid Build Coastguard Worker struct stat st;
141*38e8c45fSAndroid Build Coastguard Worker // Only show the modification time of non-device files.
142*38e8c45fSAndroid Build Coastguard Worker size_t path_len = strlen(path);
143*38e8c45fSAndroid Build Coastguard Worker if ((path_len < 6 || memcmp(path, "/proc/", 6)) &&
144*38e8c45fSAndroid Build Coastguard Worker (path_len < 5 || memcmp(path, "/sys/", 5)) &&
145*38e8c45fSAndroid Build Coastguard Worker (path_len < 3 || memcmp(path, "/d/", 3)) && !fstat(fd, &st)) {
146*38e8c45fSAndroid Build Coastguard Worker char stamp[80];
147*38e8c45fSAndroid Build Coastguard Worker time_t mtime = st.st_mtime;
148*38e8c45fSAndroid Build Coastguard Worker strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
149*38e8c45fSAndroid Build Coastguard Worker dprintf(out_fd, ": %s", stamp);
150*38e8c45fSAndroid Build Coastguard Worker }
151*38e8c45fSAndroid Build Coastguard Worker dprintf(out_fd, ") ------\n");
152*38e8c45fSAndroid Build Coastguard Worker fsync(out_fd);
153*38e8c45fSAndroid Build Coastguard Worker }
154*38e8c45fSAndroid Build Coastguard Worker if (dry_run) {
155*38e8c45fSAndroid Build Coastguard Worker if (out_fd != STDOUT_FILENO) {
156*38e8c45fSAndroid Build Coastguard Worker // There is no title, but we should still print a dry-run message
157*38e8c45fSAndroid Build Coastguard Worker dprintf(out_fd, "%s: skipped on dry run\n", path);
158*38e8c45fSAndroid Build Coastguard Worker } else if (!title.empty()) {
159*38e8c45fSAndroid Build Coastguard Worker dprintf(out_fd, "\t(skipped on dry run)\n");
160*38e8c45fSAndroid Build Coastguard Worker }
161*38e8c45fSAndroid Build Coastguard Worker fsync(out_fd);
162*38e8c45fSAndroid Build Coastguard Worker return 0;
163*38e8c45fSAndroid Build Coastguard Worker }
164*38e8c45fSAndroid Build Coastguard Worker bool newline = false;
165*38e8c45fSAndroid Build Coastguard Worker int poll_timeout_ms = 30 * 1000;
166*38e8c45fSAndroid Build Coastguard Worker while (true) {
167*38e8c45fSAndroid Build Coastguard Worker pollfd fds[] = { { .fd = fd, .events = POLLIN } };
168*38e8c45fSAndroid Build Coastguard Worker int ret = TEMP_FAILURE_RETRY(poll(fds, arraysize(fds), poll_timeout_ms));
169*38e8c45fSAndroid Build Coastguard Worker if (ret == -1) {
170*38e8c45fSAndroid Build Coastguard Worker dprintf(out_fd, "*** %s: poll failed: %s\n", path, strerror(errno));
171*38e8c45fSAndroid Build Coastguard Worker newline = true;
172*38e8c45fSAndroid Build Coastguard Worker break;
173*38e8c45fSAndroid Build Coastguard Worker } else if (ret == 0 && poll_timeout_ms != 0) {
174*38e8c45fSAndroid Build Coastguard Worker dprintf(out_fd, "*** %s: Timed out after %ds\n", path, poll_timeout_ms / 1000 );
175*38e8c45fSAndroid Build Coastguard Worker newline = true;
176*38e8c45fSAndroid Build Coastguard Worker break;
177*38e8c45fSAndroid Build Coastguard Worker } else {
178*38e8c45fSAndroid Build Coastguard Worker char buffer[65536];
179*38e8c45fSAndroid Build Coastguard Worker ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
180*38e8c45fSAndroid Build Coastguard Worker if (bytes_read > 0) {
181*38e8c45fSAndroid Build Coastguard Worker android::base::WriteFully(out_fd, buffer, bytes_read);
182*38e8c45fSAndroid Build Coastguard Worker newline = (buffer[bytes_read - 1] == '\n');
183*38e8c45fSAndroid Build Coastguard Worker } else {
184*38e8c45fSAndroid Build Coastguard Worker if (bytes_read == -1) {
185*38e8c45fSAndroid Build Coastguard Worker dprintf(out_fd, "*** %s: Failed to read from fd: %s", path, strerror(errno));
186*38e8c45fSAndroid Build Coastguard Worker newline = true;
187*38e8c45fSAndroid Build Coastguard Worker }
188*38e8c45fSAndroid Build Coastguard Worker break;
189*38e8c45fSAndroid Build Coastguard Worker }
190*38e8c45fSAndroid Build Coastguard Worker }
191*38e8c45fSAndroid Build Coastguard Worker poll_timeout_ms = 0;
192*38e8c45fSAndroid Build Coastguard Worker }
193*38e8c45fSAndroid Build Coastguard Worker
194*38e8c45fSAndroid Build Coastguard Worker if (!newline) dprintf(out_fd, "\n");
195*38e8c45fSAndroid Build Coastguard Worker if (!title.empty()) dprintf(out_fd, "\n");
196*38e8c45fSAndroid Build Coastguard Worker return 0;
197*38e8c45fSAndroid Build Coastguard Worker }
198