Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 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 "DumpstateInternal.h" |
| 20 | |
Dan Albert | 3c9c33a | 2017-10-11 12:42:46 -0700 | [diff] [blame] | 21 | #include <errno.h> |
Yifan Hong | dc3cb64 | 2017-07-26 10:47:53 -0700 | [diff] [blame^] | 22 | #include <grp.h> |
| 23 | #include <pwd.h> |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 24 | #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 Park | 522ae9a | 2017-06-23 21:23:16 +0900 | [diff] [blame] | 31 | #include <unistd.h> |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 32 | |
| 33 | #include <cstdint> |
| 34 | #include <string> |
| 35 | #include <vector> |
| 36 | |
| 37 | #include <android-base/file.h> |
Jiyong Park | 522ae9a | 2017-06-23 21:23:16 +0900 | [diff] [blame] | 38 | #include <log/log.h> |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 39 | |
| 40 | uint64_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. |
| 47 | bool DropRootUser() { |
Yifan Hong | dc3cb64 | 2017-07-26 10:47:53 -0700 | [diff] [blame^] | 48 | 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 Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 59 | 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 Hong | dc3cb64 | 2017-07-26 10:47:53 -0700 | [diff] [blame^] | 68 | 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 Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 82 | MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno)); |
| 83 | return false; |
| 84 | } |
Yifan Hong | dc3cb64 | 2017-07-26 10:47:53 -0700 | [diff] [blame^] | 85 | if (setgid(shell_gid) != 0) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 86 | MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno)); |
| 87 | return false; |
| 88 | } |
Yifan Hong | dc3cb64 | 2017-07-26 10:47:53 -0700 | [diff] [blame^] | 89 | if (setuid(shell_uid) != 0) { |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 90 | 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 | |
Felipe Leme | 6ae5c4f | 2017-01-10 14:13:22 -0800 | [diff] [blame] | 101 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG); |
| 102 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG); |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 103 | capdata[0].inheritable = 0; |
| 104 | capdata[1].inheritable = 0; |
| 105 | |
| 106 | if (capset(&capheader, &capdata[0]) < 0) { |
| 107 | MYLOGE("capset failed: %s\n", strerror(errno)); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | int DumpFileFromFdToFd(const std::string& title, const std::string& path_string, int fd, int out_fd, |
| 115 | bool dry_run) { |
| 116 | const char* path = path_string.c_str(); |
| 117 | if (!title.empty()) { |
| 118 | dprintf(out_fd, "------ %s (%s", title.c_str(), path); |
| 119 | |
| 120 | struct stat st; |
| 121 | // Only show the modification time of non-device files. |
| 122 | size_t path_len = strlen(path); |
| 123 | if ((path_len < 6 || memcmp(path, "/proc/", 6)) && |
| 124 | (path_len < 5 || memcmp(path, "/sys/", 5)) && |
| 125 | (path_len < 3 || memcmp(path, "/d/", 3)) && !fstat(fd, &st)) { |
| 126 | char stamp[80]; |
| 127 | time_t mtime = st.st_mtime; |
| 128 | strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime)); |
| 129 | dprintf(out_fd, ": %s", stamp); |
| 130 | } |
| 131 | dprintf(out_fd, ") ------\n"); |
| 132 | fsync(out_fd); |
| 133 | } |
| 134 | if (dry_run) { |
| 135 | if (out_fd != STDOUT_FILENO) { |
| 136 | // There is no title, but we should still print a dry-run message |
| 137 | dprintf(out_fd, "%s: skipped on dry run\n", path); |
| 138 | } else if (!title.empty()) { |
| 139 | dprintf(out_fd, "\t(skipped on dry run)\n"); |
| 140 | } |
| 141 | fsync(out_fd); |
| 142 | return 0; |
| 143 | } |
| 144 | bool newline = false; |
| 145 | fd_set read_set; |
| 146 | timeval tm; |
| 147 | while (true) { |
| 148 | FD_ZERO(&read_set); |
| 149 | FD_SET(fd, &read_set); |
| 150 | /* Timeout if no data is read for 30 seconds. */ |
| 151 | tm.tv_sec = 30; |
| 152 | tm.tv_usec = 0; |
| 153 | uint64_t elapsed = Nanotime(); |
| 154 | int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, nullptr, nullptr, &tm)); |
| 155 | if (ret == -1) { |
| 156 | dprintf(out_fd, "*** %s: select failed: %s\n", path, strerror(errno)); |
| 157 | newline = true; |
| 158 | break; |
| 159 | } else if (ret == 0) { |
| 160 | elapsed = Nanotime() - elapsed; |
| 161 | dprintf(out_fd, "*** %s: Timed out after %.3fs\n", path, (float)elapsed / NANOS_PER_SEC); |
| 162 | newline = true; |
| 163 | break; |
| 164 | } else { |
| 165 | char buffer[65536]; |
| 166 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 167 | if (bytes_read > 0) { |
| 168 | android::base::WriteFully(out_fd, buffer, bytes_read); |
| 169 | newline = (buffer[bytes_read - 1] == '\n'); |
| 170 | } else { |
| 171 | if (bytes_read == -1) { |
| 172 | dprintf(out_fd, "*** %s: Failed to read from fd: %s", path, strerror(errno)); |
| 173 | newline = true; |
| 174 | } |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | } |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 179 | |
| 180 | if (!newline) dprintf(out_fd, "\n"); |
| 181 | if (!title.empty()) dprintf(out_fd, "\n"); |
| 182 | return 0; |
| 183 | } |