blob: 819d5b91f5e65006a4ef11f33fab6fe341a122c2 [file] [log] [blame]
Felipe Lemef0292972016-11-22 13:57:05 -08001/*
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 "DumpstateInternal.h"
20
Dan Albert3c9c33a2017-10-11 12:42:46 -070021#include <errno.h>
Yifan Hongdc3cb642017-07-26 10:47:53 -070022#include <grp.h>
23#include <pwd.h>
Felipe Lemef0292972016-11-22 13:57:05 -080024#include <stdint.h>
25#include <stdio.h>
26#include <string.h>
27#include <sys/capability.h>
28#include <sys/prctl.h>
29#include <sys/stat.h>
30#include <sys/types.h>
Jiyong Park522ae9a2017-06-23 21:23:16 +090031#include <unistd.h>
Felipe Lemef0292972016-11-22 13:57:05 -080032
33#include <cstdint>
34#include <string>
35#include <vector>
36
37#include <android-base/file.h>
Jiyong Park522ae9a2017-06-23 21:23:16 +090038#include <log/log.h>
Felipe Lemef0292972016-11-22 13:57:05 -080039
40uint64_t Nanotime() {
41 timespec ts;
42 clock_gettime(CLOCK_MONOTONIC, &ts);
43 return static_cast<uint64_t>(ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec);
44}
45
46// Switches to non-root user and group.
47bool DropRootUser() {
Yifan Hongdc3cb642017-07-26 10:47:53 -070048 struct group* grp = getgrnam("shell");
49 gid_t shell_gid = grp != nullptr ? grp->gr_gid : 0;
50 struct passwd* pwd = getpwnam("shell");
51 uid_t shell_uid = pwd != nullptr ? pwd->pw_uid : 0;
52
53 if (!shell_gid || !shell_uid) {
54 MYLOGE("Unable to get AID_SHELL: %s\n", strerror(errno));
55 return false;
56 }
57
58 if (getgid() == shell_gid && getuid() == shell_uid) {
Felipe Lemef0292972016-11-22 13:57:05 -080059 MYLOGD("drop_root_user(): already running as Shell\n");
60 return true;
61 }
62 /* ensure we will keep capabilities when we drop root */
63 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
64 MYLOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
65 return false;
66 }
67
Yifan Hongdc3cb642017-07-26 10:47:53 -070068 static const std::vector<std::string> group_names{
69 "log", "sdcard_r", "sdcard_rw", "mount", "inet", "net_bw_stats", "readproc", "bluetooth"};
70 std::vector<gid_t> groups(group_names.size(), 0);
71 for (size_t i = 0; i < group_names.size(); ++i) {
72 grp = getgrnam(group_names[i].c_str());
73 groups[i] = grp != nullptr ? grp->gr_gid : 0;
74 if (groups[i] == 0) {
75 MYLOGE("Unable to get required gid '%s': %s\n", group_names[i].c_str(),
76 strerror(errno));
77 return false;
78 }
79 }
80
81 if (setgroups(groups.size(), groups.data()) != 0) {
Felipe Lemef0292972016-11-22 13:57:05 -080082 MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
83 return false;
84 }
Yifan Hongdc3cb642017-07-26 10:47:53 -070085 if (setgid(shell_gid) != 0) {
Felipe Lemef0292972016-11-22 13:57:05 -080086 MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno));
87 return false;
88 }
Yifan Hongdc3cb642017-07-26 10:47:53 -070089 if (setuid(shell_uid) != 0) {
Felipe Lemef0292972016-11-22 13:57:05 -080090 MYLOGE("Unable to setuid, aborting: %s\n", strerror(errno));
91 return false;
92 }
93
94 struct __user_cap_header_struct capheader;
95 struct __user_cap_data_struct capdata[2];
96 memset(&capheader, 0, sizeof(capheader));
97 memset(&capdata, 0, sizeof(capdata));
98 capheader.version = _LINUX_CAPABILITY_VERSION_3;
99 capheader.pid = 0;
100
Luis Hector Chavezd7feeaa2018-03-14 12:47:25 -0700101 if (capget(&capheader, &capdata[0]) != 0) {
102 MYLOGE("capget failed: %s\n", strerror(errno));
103 return false;
104 }
Felipe Lemef0292972016-11-22 13:57:05 -0800105
Luis Hector Chavezd7feeaa2018-03-14 12:47:25 -0700106 const uint32_t cap_syslog_mask = CAP_TO_MASK(CAP_SYSLOG);
107 const uint32_t cap_syslog_index = CAP_TO_INDEX(CAP_SYSLOG);
108 bool has_cap_syslog = (capdata[cap_syslog_index].effective & cap_syslog_mask) != 0;
109
110 memset(&capdata, 0, sizeof(capdata));
111 if (has_cap_syslog) {
112 // Only attempt to keep CAP_SYSLOG if it was present to begin with.
113 capdata[cap_syslog_index].permitted |= cap_syslog_mask;
114 capdata[cap_syslog_index].effective |= cap_syslog_mask;
115 }
116
117 if (capset(&capheader, &capdata[0]) != 0) {
118 MYLOGE("capset({%#x, %#x}) failed: %s\n", capdata[0].effective,
119 capdata[1].effective, strerror(errno));
Felipe Lemef0292972016-11-22 13:57:05 -0800120 return false;
121 }
122
123 return true;
124}
125
126int DumpFileFromFdToFd(const std::string& title, const std::string& path_string, int fd, int out_fd,
127 bool dry_run) {
128 const char* path = path_string.c_str();
129 if (!title.empty()) {
130 dprintf(out_fd, "------ %s (%s", title.c_str(), path);
131
132 struct stat st;
133 // Only show the modification time of non-device files.
134 size_t path_len = strlen(path);
135 if ((path_len < 6 || memcmp(path, "/proc/", 6)) &&
136 (path_len < 5 || memcmp(path, "/sys/", 5)) &&
137 (path_len < 3 || memcmp(path, "/d/", 3)) && !fstat(fd, &st)) {
138 char stamp[80];
139 time_t mtime = st.st_mtime;
140 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
141 dprintf(out_fd, ": %s", stamp);
142 }
143 dprintf(out_fd, ") ------\n");
144 fsync(out_fd);
145 }
146 if (dry_run) {
147 if (out_fd != STDOUT_FILENO) {
148 // There is no title, but we should still print a dry-run message
149 dprintf(out_fd, "%s: skipped on dry run\n", path);
150 } else if (!title.empty()) {
151 dprintf(out_fd, "\t(skipped on dry run)\n");
152 }
153 fsync(out_fd);
154 return 0;
155 }
156 bool newline = false;
157 fd_set read_set;
158 timeval tm;
159 while (true) {
160 FD_ZERO(&read_set);
161 FD_SET(fd, &read_set);
162 /* Timeout if no data is read for 30 seconds. */
163 tm.tv_sec = 30;
164 tm.tv_usec = 0;
165 uint64_t elapsed = Nanotime();
166 int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, nullptr, nullptr, &tm));
167 if (ret == -1) {
168 dprintf(out_fd, "*** %s: select failed: %s\n", path, strerror(errno));
169 newline = true;
170 break;
171 } else if (ret == 0) {
172 elapsed = Nanotime() - elapsed;
173 dprintf(out_fd, "*** %s: Timed out after %.3fs\n", path, (float)elapsed / NANOS_PER_SEC);
174 newline = true;
175 break;
176 } else {
177 char buffer[65536];
178 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
179 if (bytes_read > 0) {
180 android::base::WriteFully(out_fd, buffer, bytes_read);
181 newline = (buffer[bytes_read - 1] == '\n');
182 } else {
183 if (bytes_read == -1) {
184 dprintf(out_fd, "*** %s: Failed to read from fd: %s", path, strerror(errno));
185 newline = true;
186 }
187 break;
188 }
189 }
190 }
Felipe Lemef0292972016-11-22 13:57:05 -0800191
192 if (!newline) dprintf(out_fd, "\n");
193 if (!title.empty()) dprintf(out_fd, "\n");
194 return 0;
195}