Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -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 | */ |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 16 | #define DEBUG false |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 17 | #include "Log.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 18 | |
| 19 | #include "IncidentService.h" |
| 20 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 21 | #include "FdBuffer.h" |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 22 | #include "PrivacyFilter.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 23 | #include "Reporter.h" |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 24 | #include "incidentd_util.h" |
| 25 | #include "section_list.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 26 | |
Mike Ma | 85434ec | 2018-11-27 10:32:31 -0800 | [diff] [blame] | 27 | #include <android/os/IncidentReportArgs.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 28 | #include <binder/IPCThreadState.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 29 | #include <binder/IResultReceiver.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 30 | #include <binder/IServiceManager.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 31 | #include <binder/IShellCallback.h> |
Mike Ma | 3505666 | 2018-12-06 13:32:59 -0800 | [diff] [blame] | 32 | #include <log/log.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 33 | #include <private/android_filesystem_config.h> |
| 34 | #include <utils/Looper.h> |
Yao Chen | cbafce9 | 2019-04-01 15:56:44 -0700 | [diff] [blame] | 35 | #include <thread> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 36 | |
| 37 | #include <unistd.h> |
| 38 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 39 | enum { |
| 40 | WHAT_TAKE_REPORT = 1, |
| 41 | WHAT_SEND_BROADCASTS = 2 |
| 42 | }; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 43 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 44 | #define DEFAULT_DELAY_NS (1000000000LL) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 45 | |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 46 | #define DEFAULT_BYTES_SIZE_LIMIT (20 * 1024 * 1024) // 20MB |
| 47 | #define DEFAULT_REFACTORY_PERIOD_MS (24 * 60 * 60 * 1000) // 1 Day |
| 48 | |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 49 | // Skip these sections for dumpstate only. Dumpstate allows 10s max for each service to dump. |
| 50 | // Skip logs (1100 - 1108) and traces (1200 - 1202) because they are already in the bug report. |
| 51 | // Skip 3018 because it takes too long. |
Mike Ma | 85434ec | 2018-11-27 10:32:31 -0800 | [diff] [blame] | 52 | #define SKIPPED_SECTIONS { 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, /* Logs */ \ |
| 53 | 1200, 1201, 1202, /* Native, hal, java traces */ \ |
Mike Ma | 85434ec | 2018-11-27 10:32:31 -0800 | [diff] [blame] | 54 | 3018 /* "meminfo -a --proto" */ } |
| 55 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 56 | namespace android { |
| 57 | namespace os { |
| 58 | namespace incidentd { |
| 59 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 60 | String16 const APPROVE_INCIDENT_REPORTS("android.permission.APPROVE_INCIDENT_REPORTS"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 61 | String16 const DUMP_PERMISSION("android.permission.DUMP"); |
| 62 | String16 const USAGE_STATS_PERMISSION("android.permission.PACKAGE_USAGE_STATS"); |
| 63 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 64 | static Status checkIncidentPermissions(const IncidentReportArgs& args) { |
Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 65 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 66 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 67 | if (callingUid == AID_ROOT || callingUid == AID_SHELL) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 68 | // Root and shell are ok. |
| 69 | return Status::ok(); |
| 70 | } |
| 71 | |
| 72 | if (checkCallingPermission(APPROVE_INCIDENT_REPORTS)) { |
| 73 | // Permission controller (this is a singleton permission that is always granted |
| 74 | // exactly for PermissionController) is allowed to access incident reports |
| 75 | // so it can show the user info about what they are approving. |
Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 76 | return Status::ok(); |
| 77 | } |
| 78 | |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 79 | // checking calling permission. |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 80 | if (!checkCallingPermission(DUMP_PERMISSION)) { |
| 81 | ALOGW("Calling pid %d and uid %d does not have permission: android.permission.DUMP", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 82 | callingPid, callingUid); |
| 83 | return Status::fromExceptionCode( |
| 84 | Status::EX_SECURITY, |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 85 | "Calling process does not have permission: android.permission.DUMP"); |
| 86 | } |
| 87 | if (!checkCallingPermission(USAGE_STATS_PERMISSION)) { |
| 88 | ALOGW("Calling pid %d and uid %d does not have permission: android.permission.USAGE_STATS", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 89 | callingPid, callingUid); |
| 90 | return Status::fromExceptionCode( |
| 91 | Status::EX_SECURITY, |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 92 | "Calling process does not have permission: android.permission.USAGE_STATS"); |
| 93 | } |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 94 | |
| 95 | // checking calling request uid permission. |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 96 | switch (args.getPrivacyPolicy()) { |
| 97 | case PRIVACY_POLICY_LOCAL: |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 98 | if (callingUid != AID_SHELL && callingUid != AID_ROOT) { |
| 99 | ALOGW("Calling pid %d and uid %d does not have permission to get local data.", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 100 | callingPid, callingUid); |
| 101 | return Status::fromExceptionCode( |
| 102 | Status::EX_SECURITY, |
| 103 | "Calling process does not have permission to get local data."); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 104 | } |
Bookatz | da9b8d0 | 2018-11-14 13:14:45 -0800 | [diff] [blame] | 105 | break; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 106 | case PRIVACY_POLICY_EXPLICIT: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 107 | if (callingUid != AID_SHELL && callingUid != AID_ROOT && callingUid != AID_STATSD && |
Bookatz | da9b8d0 | 2018-11-14 13:14:45 -0800 | [diff] [blame] | 108 | callingUid != AID_SYSTEM) { |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 109 | ALOGW("Calling pid %d and uid %d does not have permission to get explicit data.", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 110 | callingPid, callingUid); |
| 111 | return Status::fromExceptionCode( |
| 112 | Status::EX_SECURITY, |
| 113 | "Calling process does not have permission to get explicit data."); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 114 | } |
Bookatz | da9b8d0 | 2018-11-14 13:14:45 -0800 | [diff] [blame] | 115 | break; |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 116 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 117 | return Status::ok(); |
| 118 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 119 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 120 | static string build_uri(const string& pkg, const string& cls, const string& id) { |
Yao Chen | cbafce9 | 2019-04-01 15:56:44 -0700 | [diff] [blame] | 121 | return "content://android.os.IncidentManager/pending?pkg=" |
| 122 | + pkg + "&receiver=" + cls + "&r=" + id; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 125 | // ================================================================================ |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 126 | ReportHandler::ReportHandler(const sp<WorkDirectory>& workDirectory, |
| 127 | const sp<Broadcaster>& broadcaster, const sp<Looper>& handlerLooper, |
| 128 | const sp<Throttler>& throttler) |
| 129 | :mLock(), |
| 130 | mWorkDirectory(workDirectory), |
| 131 | mBroadcaster(broadcaster), |
| 132 | mHandlerLooper(handlerLooper), |
| 133 | mBacklogDelay(DEFAULT_DELAY_NS), |
| 134 | mThrottler(throttler), |
| 135 | mBatch(new ReportBatch()) { |
| 136 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 137 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 138 | ReportHandler::~ReportHandler() { |
| 139 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 140 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 141 | void ReportHandler::handleMessage(const Message& message) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 142 | switch (message.what) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 143 | case WHAT_TAKE_REPORT: |
| 144 | take_report(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 145 | break; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 146 | case WHAT_SEND_BROADCASTS: |
| 147 | send_broadcasts(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 152 | void ReportHandler::schedulePersistedReport(const IncidentReportArgs& args) { |
| 153 | mBatch->addPersistedReport(args); |
| 154 | mHandlerLooper->removeMessages(this, WHAT_TAKE_REPORT); |
| 155 | mHandlerLooper->sendMessage(this, Message(WHAT_TAKE_REPORT)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 158 | void ReportHandler::scheduleStreamingReport(const IncidentReportArgs& args, |
| 159 | const sp<IIncidentReportStatusListener>& listener, int streamFd) { |
| 160 | mBatch->addStreamingReport(args, listener, streamFd); |
| 161 | mHandlerLooper->removeMessages(this, WHAT_TAKE_REPORT); |
| 162 | mHandlerLooper->sendMessage(this, Message(WHAT_TAKE_REPORT)); |
| 163 | } |
| 164 | |
| 165 | void ReportHandler::scheduleSendBacklog() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 166 | unique_lock<mutex> lock(mLock); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 167 | mBacklogDelay = DEFAULT_DELAY_NS; |
| 168 | schedule_send_broadcasts_locked(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 171 | void ReportHandler::schedule_send_broadcasts_locked() { |
| 172 | mHandlerLooper->removeMessages(this, WHAT_SEND_BROADCASTS); |
| 173 | mHandlerLooper->sendMessageDelayed(mBacklogDelay, this, Message(WHAT_SEND_BROADCASTS)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 176 | void ReportHandler::take_report() { |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame^] | 177 | // Cycle the batch and throttle. |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 178 | sp<ReportBatch> batch; |
| 179 | { |
| 180 | unique_lock<mutex> lock(mLock); |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame^] | 181 | batch = mThrottler->filterBatch(mBatch); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 184 | if (batch->empty()) { |
| 185 | // Nothing to do. |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | sp<Reporter> reporter = new Reporter(mWorkDirectory, batch); |
| 190 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 191 | // Take the report, which might take a while. More requests might queue |
| 192 | // up while we're doing this, and we'll handle them in their next batch. |
| 193 | // TODO: We should further rate-limit the reports to no more than N per time-period. |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 194 | // TODO: Move this inside reporter. |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 195 | size_t reportByteSize = 0; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 196 | reporter->runReport(&reportByteSize); |
| 197 | |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame^] | 198 | // Tell the throttler how big it was, for the next throttling. |
| 199 | // TODO: This still isn't ideal. The throttler really should just track the |
| 200 | // persisted reqeusts, but changing Reporter::runReport() to track that individually |
| 201 | // will be a big change. |
| 202 | if (batch->hasPersistedReports()) { |
| 203 | mThrottler->addReportSize(reportByteSize); |
| 204 | } |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 205 | |
| 206 | // Kick off the next steps, one of which is to send any new or otherwise remaining |
| 207 | // approvals, and one of which is to send any new or remaining broadcasts. |
| 208 | { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 209 | unique_lock<mutex> lock(mLock); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 210 | schedule_send_broadcasts_locked(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 214 | void ReportHandler::send_broadcasts() { |
| 215 | Broadcaster::broadcast_status_t result = mBroadcaster->sendBroadcasts(); |
| 216 | if (result == Broadcaster::BROADCASTS_FINISHED) { |
| 217 | // We're done. |
| 218 | unique_lock<mutex> lock(mLock); |
| 219 | mBacklogDelay = DEFAULT_DELAY_NS; |
| 220 | } else if (result == Broadcaster::BROADCASTS_REPEAT) { |
| 221 | // It worked, but there are more. |
| 222 | unique_lock<mutex> lock(mLock); |
| 223 | mBacklogDelay = DEFAULT_DELAY_NS; |
| 224 | schedule_send_broadcasts_locked(); |
| 225 | } else if (result == Broadcaster::BROADCASTS_BACKOFF) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 226 | // There was a failure. Exponential backoff. |
| 227 | unique_lock<mutex> lock(mLock); |
| 228 | mBacklogDelay *= 2; |
| 229 | ALOGI("Error sending to dropbox. Trying again in %lld minutes", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 230 | (mBacklogDelay / (1000000000LL * 60))); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 231 | schedule_send_broadcasts_locked(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | // ================================================================================ |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 236 | IncidentService::IncidentService(const sp<Looper>& handlerLooper) { |
| 237 | mThrottler = new Throttler(DEFAULT_BYTES_SIZE_LIMIT, DEFAULT_REFACTORY_PERIOD_MS); |
| 238 | mWorkDirectory = new WorkDirectory(); |
| 239 | mBroadcaster = new Broadcaster(mWorkDirectory); |
| 240 | mHandler = new ReportHandler(mWorkDirectory, mBroadcaster, handlerLooper, |
| 241 | mThrottler); |
| 242 | mBroadcaster->setHandler(mHandler); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 245 | IncidentService::~IncidentService() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 246 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 247 | Status IncidentService::reportIncident(const IncidentReportArgs& args) { |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame^] | 248 | IncidentReportArgs argsCopy(args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 249 | |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame^] | 250 | // Validate that the privacy policy is one of the real ones. |
| 251 | // If it isn't, clamp it to the next more restrictive real one. |
| 252 | argsCopy.setPrivacyPolicy(cleanup_privacy_policy(args.getPrivacyPolicy())); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 253 | |
| 254 | // TODO: Check that the broadcast recevier has the proper permissions |
| 255 | // TODO: Maybe we should consider relaxing the permissions if it's going to |
| 256 | // dropbox, but definitely not if it's going to the broadcaster. |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 257 | Status status = checkIncidentPermissions(args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 258 | if (!status.isOk()) { |
| 259 | return status; |
| 260 | } |
| 261 | |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame^] | 262 | // If they asked for the LOCAL privacy policy, give them EXPLICT. LOCAL has to |
| 263 | // be streamed. (This only applies to shell/root, because everyone else would have |
| 264 | // been rejected by checkIncidentPermissions()). |
| 265 | if (argsCopy.getPrivacyPolicy() < PRIVACY_POLICY_EXPLICIT) { |
| 266 | ALOGI("Demoting privacy policy to EXPLICT for persisted report."); |
| 267 | argsCopy.setPrivacyPolicy(PRIVACY_POLICY_EXPLICIT); |
| 268 | } |
| 269 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 270 | // If they didn't specify a component, use dropbox. |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 271 | if (argsCopy.receiverPkg().length() == 0 && argsCopy.receiverCls().length() == 0) { |
| 272 | argsCopy.setReceiverPkg(DROPBOX_SENTINEL.getPackageName()); |
| 273 | argsCopy.setReceiverCls(DROPBOX_SENTINEL.getClassName()); |
| 274 | } |
| 275 | |
| 276 | mHandler->schedulePersistedReport(argsCopy); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 277 | |
| 278 | return Status::ok(); |
| 279 | } |
| 280 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 281 | Status IncidentService::reportIncidentToStream(const IncidentReportArgs& args, |
| 282 | const sp<IIncidentReportStatusListener>& listener, |
| 283 | const unique_fd& stream) { |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame^] | 284 | IncidentReportArgs argsCopy(args); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 285 | |
| 286 | // Streaming reports can not also be broadcast. |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 287 | argsCopy.setReceiverPkg(""); |
| 288 | argsCopy.setReceiverCls(""); |
| 289 | |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame^] | 290 | // Validate that the privacy policy is one of the real ones. |
| 291 | // If it isn't, clamp it to the next more restrictive real one. |
| 292 | argsCopy.setPrivacyPolicy(cleanup_privacy_policy(args.getPrivacyPolicy())); |
| 293 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 294 | Status status = checkIncidentPermissions(argsCopy); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 295 | if (!status.isOk()) { |
| 296 | return status; |
| 297 | } |
| 298 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 299 | // The ReportRequest takes ownership of the fd, so we need to dup it. |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 300 | int fd = dup(stream.get()); |
| 301 | if (fd < 0) { |
| 302 | return Status::fromStatusT(-errno); |
| 303 | } |
| 304 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 305 | mHandler->scheduleStreamingReport(argsCopy, listener, fd); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 306 | |
| 307 | return Status::ok(); |
| 308 | } |
| 309 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 310 | Status IncidentService::systemRunning() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 311 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 312 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 313 | "Only system uid can call systemRunning"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 314 | } |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 315 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 316 | // When system_server is up and running, schedule the dropbox task to run. |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 317 | mBroadcaster->reset(); |
| 318 | mHandler->scheduleSendBacklog(); |
| 319 | |
| 320 | return Status::ok(); |
| 321 | } |
| 322 | |
| 323 | Status IncidentService::getIncidentReportList(const String16& pkg16, const String16& cls16, |
| 324 | vector<String16>* result) { |
| 325 | status_t err; |
| 326 | const string pkg(String8(pkg16).string()); |
| 327 | const string cls(String8(cls16).string()); |
| 328 | |
| 329 | // List the reports |
| 330 | vector<sp<ReportFile>> all; |
| 331 | err = mWorkDirectory->getReports(&all, 0); |
| 332 | if (err != NO_ERROR) { |
| 333 | return Status::fromStatusT(err); |
| 334 | } |
| 335 | |
| 336 | // Find the ones that match pkg and cls. |
| 337 | for (sp<ReportFile>& file: all) { |
| 338 | err = file->loadEnvelope(); |
| 339 | if (err != NO_ERROR) { |
| 340 | continue; |
| 341 | } |
| 342 | const ReportFileProto& envelope = file->getEnvelope(); |
| 343 | size_t reportCount = envelope.report_size(); |
| 344 | for (int reportIndex = 0; reportIndex < reportCount; reportIndex++) { |
| 345 | const ReportFileProto_Report& report = envelope.report(reportIndex); |
| 346 | if (pkg == report.pkg() && cls == report.cls()) { |
| 347 | result->push_back(String16(build_uri(pkg, cls, file->getId()).c_str())); |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | return Status::ok(); |
| 354 | } |
| 355 | |
| 356 | Status IncidentService::getIncidentReport(const String16& pkg16, const String16& cls16, |
| 357 | const String16& id16, IncidentManager::IncidentReport* result) { |
| 358 | status_t err; |
| 359 | |
| 360 | const string pkg(String8(pkg16).string()); |
| 361 | const string cls(String8(cls16).string()); |
| 362 | const string id(String8(id16).string()); |
| 363 | |
| 364 | IncidentReportArgs args; |
| 365 | sp<ReportFile> file = mWorkDirectory->getReport(pkg, cls, id, &args); |
| 366 | if (file != nullptr) { |
Yao Chen | cbafce9 | 2019-04-01 15:56:44 -0700 | [diff] [blame] | 367 | // Create pipe |
| 368 | int fds[2]; |
| 369 | if (pipe(fds) != 0) { |
| 370 | ALOGW("Error opening pipe to filter incident report: %s", |
| 371 | file->getDataFileName().c_str()); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 372 | return Status::ok(); |
| 373 | } |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 374 | result->setTimestampNs(file->getTimestampNs()); |
| 375 | result->setPrivacyPolicy(file->getEnvelope().privacy_policy()); |
Yao Chen | cbafce9 | 2019-04-01 15:56:44 -0700 | [diff] [blame] | 376 | result->takeFileDescriptor(fds[0]); |
| 377 | int writeFd = fds[1]; |
| 378 | // spawn a thread to write the data. Release the writeFd ownership to the thread. |
| 379 | thread th([file, writeFd, args]() { file->startFilteringData(writeFd, args); }); |
| 380 | |
| 381 | th.detach(); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | return Status::ok(); |
| 385 | } |
| 386 | |
| 387 | Status IncidentService::deleteIncidentReports(const String16& pkg16, const String16& cls16, |
| 388 | const String16& id16) { |
| 389 | const string pkg(String8(pkg16).string()); |
| 390 | const string cls(String8(cls16).string()); |
| 391 | const string id(String8(id16).string()); |
| 392 | |
| 393 | sp<ReportFile> file = mWorkDirectory->getReport(pkg, cls, id, nullptr); |
| 394 | if (file != nullptr) { |
| 395 | mWorkDirectory->commit(file, pkg, cls); |
| 396 | } |
| 397 | mBroadcaster->clearBroadcasts(pkg, cls, id); |
| 398 | |
| 399 | return Status::ok(); |
| 400 | } |
| 401 | |
| 402 | Status IncidentService::deleteAllIncidentReports(const String16& pkg16) { |
| 403 | const string pkg(String8(pkg16).string()); |
| 404 | |
| 405 | mWorkDirectory->commitAll(pkg); |
| 406 | mBroadcaster->clearPackageBroadcasts(pkg); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 407 | |
| 408 | return Status::ok(); |
| 409 | } |
| 410 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 411 | /** |
| 412 | * Implement our own because the default binder implementation isn't |
| 413 | * properly handling SHELL_COMMAND_TRANSACTION. |
| 414 | */ |
| 415 | status_t IncidentService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 416 | uint32_t flags) { |
| 417 | status_t err; |
| 418 | |
| 419 | switch (code) { |
| 420 | case SHELL_COMMAND_TRANSACTION: { |
| 421 | int in = data.readFileDescriptor(); |
| 422 | int out = data.readFileDescriptor(); |
| 423 | int err = data.readFileDescriptor(); |
| 424 | int argc = data.readInt32(); |
| 425 | Vector<String8> args; |
| 426 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 427 | args.add(String8(data.readString16())); |
| 428 | } |
| 429 | sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder()); |
| 430 | sp<IResultReceiver> resultReceiver = |
| 431 | IResultReceiver::asInterface(data.readStrongBinder()); |
| 432 | |
| 433 | FILE* fin = fdopen(in, "r"); |
| 434 | FILE* fout = fdopen(out, "w"); |
| 435 | FILE* ferr = fdopen(err, "w"); |
| 436 | |
| 437 | if (fin == NULL || fout == NULL || ferr == NULL) { |
| 438 | resultReceiver->send(NO_MEMORY); |
| 439 | } else { |
| 440 | err = command(fin, fout, ferr, args); |
| 441 | resultReceiver->send(err); |
| 442 | } |
| 443 | |
| 444 | if (fin != NULL) { |
| 445 | fflush(fin); |
| 446 | fclose(fin); |
| 447 | } |
| 448 | if (fout != NULL) { |
| 449 | fflush(fout); |
| 450 | fclose(fout); |
| 451 | } |
| 452 | if (fout != NULL) { |
| 453 | fflush(ferr); |
| 454 | fclose(ferr); |
| 455 | } |
| 456 | |
| 457 | return NO_ERROR; |
Bookatz | da9b8d0 | 2018-11-14 13:14:45 -0800 | [diff] [blame] | 458 | } break; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 459 | default: { return BnIncidentManager::onTransact(code, data, reply, flags); } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | status_t IncidentService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
| 464 | const int argCount = args.size(); |
| 465 | |
| 466 | if (argCount >= 1) { |
| 467 | if (!args[0].compare(String8("privacy"))) { |
| 468 | return cmd_privacy(in, out, err, args); |
| 469 | } |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 470 | if (!args[0].compare(String8("throttler"))) { |
| 471 | mThrottler->dump(out); |
| 472 | return NO_ERROR; |
| 473 | } |
Yi Jin | 908c02f | 2018-06-22 16:51:40 -0700 | [diff] [blame] | 474 | if (!args[0].compare(String8("section"))) { |
| 475 | int id = atoi(args[1]); |
| 476 | int idx = 0; |
| 477 | while (SECTION_LIST[idx] != NULL) { |
| 478 | const Section* section = SECTION_LIST[idx]; |
| 479 | if (section->id == id) { |
| 480 | fprintf(out, "Section[%d] %s\n", id, section->name.string()); |
| 481 | break; |
| 482 | } |
| 483 | idx++; |
| 484 | } |
| 485 | return NO_ERROR; |
| 486 | } |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 487 | } |
| 488 | return cmd_help(out); |
| 489 | } |
| 490 | |
| 491 | status_t IncidentService::cmd_help(FILE* out) { |
| 492 | fprintf(out, "usage: adb shell cmd incident privacy print <section_id>\n"); |
| 493 | fprintf(out, "usage: adb shell cmd incident privacy parse <section_id> < proto.txt\n"); |
Yi Jin | 908c02f | 2018-06-22 16:51:40 -0700 | [diff] [blame] | 494 | fprintf(out, " Prints/parses for the section id.\n\n"); |
| 495 | fprintf(out, "usage: adb shell cmd incident section <section_id>\n"); |
| 496 | fprintf(out, " Prints section id and its name.\n\n"); |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 497 | fprintf(out, "usage: adb shell cmd incident throttler\n"); |
| 498 | fprintf(out, " Prints the current throttler state\n"); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 499 | return NO_ERROR; |
| 500 | } |
| 501 | |
| 502 | static void printPrivacy(const Privacy* p, FILE* out, String8 indent) { |
| 503 | if (p == NULL) return; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 504 | fprintf(out, "%sid:%d, type:%d, dest:%d\n", indent.string(), p->field_id, p->type, p->policy); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 505 | if (p->children == NULL) return; |
| 506 | for (int i = 0; p->children[i] != NULL; i++) { // NULL-terminated. |
| 507 | printPrivacy(p->children[i], out, indent + " "); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | status_t IncidentService::cmd_privacy(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 512 | (void)in; |
| 513 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 514 | const int argCount = args.size(); |
| 515 | if (argCount >= 3) { |
| 516 | String8 opt = args[1]; |
| 517 | int sectionId = atoi(args[2].string()); |
| 518 | |
| 519 | const Privacy* p = get_privacy_of_section(sectionId); |
| 520 | if (p == NULL) { |
| 521 | fprintf(err, "Can't find section id %d\n", sectionId); |
| 522 | return NO_ERROR; |
| 523 | } |
| 524 | fprintf(err, "Get privacy for %d\n", sectionId); |
| 525 | if (opt == "print") { |
| 526 | printPrivacy(p, out, String8("")); |
| 527 | } else if (opt == "parse") { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 528 | /* |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 529 | FdBuffer buf; |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 530 | status_t error = buf.read(fileno(in), 60000); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 531 | if (error != NO_ERROR) { |
| 532 | fprintf(err, "Error reading from stdin\n"); |
| 533 | return error; |
| 534 | } |
| 535 | fprintf(err, "Read %zu bytes\n", buf.size()); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 536 | PrivacyFilter pBuf(p, buf.data()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 537 | |
| 538 | PrivacySpec spec = PrivacySpec::new_spec(argCount > 3 ? atoi(args[3]) : -1); |
| 539 | error = pBuf.strip(spec); |
| 540 | if (error != NO_ERROR) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 541 | fprintf(err, "Error strip pii fields with spec %d\n", spec.policy); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 542 | return error; |
| 543 | } |
| 544 | return pBuf.flush(fileno(out)); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 545 | */ |
| 546 | return -1; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 547 | } |
| 548 | } else { |
| 549 | return cmd_help(out); |
| 550 | } |
| 551 | return NO_ERROR; |
| 552 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 553 | |
Mike Ma | 85434ec | 2018-11-27 10:32:31 -0800 | [diff] [blame] | 554 | status_t IncidentService::dump(int fd, const Vector<String16>& args) { |
| 555 | if (std::find(args.begin(), args.end(), String16("--proto")) == args.end()) { |
| 556 | ALOGD("Skip dumping incident. Only proto format is supported."); |
| 557 | dprintf(fd, "Incident dump only supports proto version.\n"); |
| 558 | return NO_ERROR; |
| 559 | } |
| 560 | |
| 561 | ALOGD("Dump incident proto"); |
| 562 | IncidentReportArgs incidentArgs; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 563 | incidentArgs.setPrivacyPolicy(PRIVACY_POLICY_EXPLICIT); |
Mike Ma | 85434ec | 2018-11-27 10:32:31 -0800 | [diff] [blame] | 564 | int skipped[] = SKIPPED_SECTIONS; |
| 565 | for (const Section** section = SECTION_LIST; *section; section++) { |
| 566 | const int id = (*section)->id; |
| 567 | if (std::find(std::begin(skipped), std::end(skipped), id) == std::end(skipped)) { |
| 568 | incidentArgs.addSection(id); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if (!checkIncidentPermissions(incidentArgs).isOk()) { |
| 573 | return PERMISSION_DENIED; |
| 574 | } |
| 575 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 576 | // The ReportRequest takes ownership of the fd, so we need to dup it. |
Mike Ma | 85434ec | 2018-11-27 10:32:31 -0800 | [diff] [blame] | 577 | int fd1 = dup(fd); |
| 578 | if (fd1 < 0) { |
| 579 | return -errno; |
| 580 | } |
| 581 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 582 | // TODO: Remove this. Someone even dumpstate, wanting to get an incident report |
| 583 | // should use the API. That will take making dumpstated call the API, which is a |
| 584 | // good thing. It also means it won't be subject to the timeout. |
| 585 | mHandler->scheduleStreamingReport(incidentArgs, NULL, fd1); |
Mike Ma | 85434ec | 2018-11-27 10:32:31 -0800 | [diff] [blame] | 586 | |
| 587 | return NO_ERROR; |
| 588 | } |
| 589 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 590 | } // namespace incidentd |
| 591 | } // namespace os |
| 592 | } // namespace android |