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