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 | */ |
| 16 | |
| 17 | #define LOG_TAG "incidentd" |
| 18 | |
| 19 | #include "IncidentService.h" |
| 20 | |
| 21 | #include "Reporter.h" |
| 22 | |
| 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include <cutils/log.h> |
| 26 | #include <private/android_filesystem_config.h> |
| 27 | #include <utils/Looper.h> |
| 28 | |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | using namespace android; |
| 32 | |
| 33 | enum { |
| 34 | WHAT_RUN_REPORT = 1, |
| 35 | WHAT_SEND_BACKLOG_TO_DROPBOX = 2 |
| 36 | }; |
| 37 | |
| 38 | //#define DEFAULT_BACKLOG_DELAY_NS (1000000000LL * 60 * 5) |
| 39 | #define DEFAULT_BACKLOG_DELAY_NS (1000000000LL) |
| 40 | |
| 41 | // ================================================================================ |
| 42 | String16 const DUMP_PERMISSION("android.permission.DUMP"); |
| 43 | String16 const USAGE_STATS_PERMISSION("android.permission.PACKAGE_USAGE_STATS"); |
| 44 | |
| 45 | static Status |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 46 | checkIncidentPermissions(const IncidentReportArgs& args) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 47 | { |
Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 48 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 49 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 50 | if (callingUid == AID_ROOT || callingUid == AID_SHELL) { |
| 51 | // root doesn't have permission.DUMP if don't do this! |
| 52 | return Status::ok(); |
| 53 | } |
| 54 | |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 55 | // checking calling permission. |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 56 | if (!checkCallingPermission(DUMP_PERMISSION)) { |
| 57 | ALOGW("Calling pid %d and uid %d does not have permission: android.permission.DUMP", |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 58 | callingPid, callingUid); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 59 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 60 | "Calling process does not have permission: android.permission.DUMP"); |
| 61 | } |
| 62 | if (!checkCallingPermission(USAGE_STATS_PERMISSION)) { |
| 63 | ALOGW("Calling pid %d and uid %d does not have permission: android.permission.USAGE_STATS", |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 64 | callingPid, callingUid); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 65 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 66 | "Calling process does not have permission: android.permission.USAGE_STATS"); |
| 67 | } |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 68 | |
| 69 | // checking calling request uid permission. |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 70 | switch (args.dest()) { |
| 71 | case DEST_LOCAL: |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 72 | if (callingUid != AID_SHELL && callingUid != AID_ROOT) { |
| 73 | ALOGW("Calling pid %d and uid %d does not have permission to get local data.", |
| 74 | callingPid, callingUid); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 75 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 76 | "Calling process does not have permission to get local data."); |
| 77 | } |
| 78 | case DEST_EXPLICIT: |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 79 | if (callingUid != AID_SHELL && callingUid != AID_ROOT && |
| 80 | callingUid != AID_STATSD && callingUid != AID_SYSTEM) { |
| 81 | ALOGW("Calling pid %d and uid %d does not have permission to get explicit data.", |
| 82 | callingPid, callingUid); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 83 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 84 | "Calling process does not have permission to get explicit data."); |
| 85 | } |
| 86 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 87 | return Status::ok(); |
| 88 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 89 | // ================================================================================ |
| 90 | ReportRequestQueue::ReportRequestQueue() |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | ReportRequestQueue::~ReportRequestQueue() |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | void |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 99 | ReportRequestQueue::addRequest(const sp<ReportRequest>& request) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 100 | { |
| 101 | unique_lock<mutex> lock(mLock); |
| 102 | mQueue.push_back(request); |
| 103 | } |
| 104 | |
| 105 | sp<ReportRequest> |
| 106 | ReportRequestQueue::getNextRequest() |
| 107 | { |
| 108 | unique_lock<mutex> lock(mLock); |
| 109 | if (mQueue.empty()) { |
| 110 | return NULL; |
| 111 | } else { |
| 112 | sp<ReportRequest> front(mQueue.front()); |
| 113 | mQueue.pop_front(); |
| 114 | return front; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | // ================================================================================ |
| 120 | ReportHandler::ReportHandler(const sp<Looper>& handlerLooper, const sp<ReportRequestQueue>& queue) |
| 121 | :mBacklogDelay(DEFAULT_BACKLOG_DELAY_NS), |
| 122 | mHandlerLooper(handlerLooper), |
| 123 | mQueue(queue) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | ReportHandler::~ReportHandler() |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | ReportHandler::handleMessage(const Message& message) |
| 133 | { |
| 134 | switch (message.what) { |
| 135 | case WHAT_RUN_REPORT: |
| 136 | run_report(); |
| 137 | break; |
| 138 | case WHAT_SEND_BACKLOG_TO_DROPBOX: |
| 139 | send_backlog_to_dropbox(); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | ReportHandler::scheduleRunReport(const sp<ReportRequest>& request) |
| 146 | { |
| 147 | mQueue->addRequest(request); |
| 148 | mHandlerLooper->removeMessages(this, WHAT_RUN_REPORT); |
| 149 | mHandlerLooper->sendMessage(this, Message(WHAT_RUN_REPORT)); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | ReportHandler::scheduleSendBacklogToDropbox() |
| 154 | { |
| 155 | unique_lock<mutex> lock(mLock); |
| 156 | mBacklogDelay = DEFAULT_BACKLOG_DELAY_NS; |
| 157 | schedule_send_backlog_to_dropbox_locked(); |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | ReportHandler::schedule_send_backlog_to_dropbox_locked() |
| 162 | { |
| 163 | mHandlerLooper->removeMessages(this, WHAT_SEND_BACKLOG_TO_DROPBOX); |
| 164 | mHandlerLooper->sendMessageDelayed(mBacklogDelay, this, |
| 165 | Message(WHAT_SEND_BACKLOG_TO_DROPBOX)); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | ReportHandler::run_report() |
| 170 | { |
| 171 | sp<Reporter> reporter = new Reporter(); |
| 172 | |
| 173 | // Merge all of the requests into one that has all of the |
| 174 | // requested fields. |
| 175 | while (true) { |
| 176 | sp<ReportRequest> request = mQueue->getNextRequest(); |
| 177 | if (request == NULL) { |
| 178 | break; |
| 179 | } |
| 180 | reporter->batch.add(request); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Take the report, which might take a while. More requests might queue |
| 184 | // up while we're doing this, and we'll handle them in their next batch. |
| 185 | // TODO: We should further rate-limit the reports to no more than N per time-period. |
| 186 | Reporter::run_report_status_t reportStatus = reporter->runReport(); |
| 187 | if (reportStatus == Reporter::REPORT_NEEDS_DROPBOX) { |
| 188 | unique_lock<mutex> lock(mLock); |
| 189 | schedule_send_backlog_to_dropbox_locked(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | ReportHandler::send_backlog_to_dropbox() |
| 195 | { |
| 196 | if (Reporter::upload_backlog() == Reporter::REPORT_NEEDS_DROPBOX) { |
| 197 | // There was a failure. Exponential backoff. |
| 198 | unique_lock<mutex> lock(mLock); |
| 199 | mBacklogDelay *= 2; |
| 200 | ALOGI("Error sending to dropbox. Trying again in %lld minutes", |
| 201 | (mBacklogDelay / (1000000000LL * 60))); |
| 202 | schedule_send_backlog_to_dropbox_locked(); |
| 203 | } else { |
| 204 | mBacklogDelay = DEFAULT_BACKLOG_DELAY_NS; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // ================================================================================ |
| 209 | IncidentService::IncidentService(const sp<Looper>& handlerLooper) |
| 210 | :mQueue(new ReportRequestQueue()) |
| 211 | { |
| 212 | mHandler = new ReportHandler(handlerLooper, mQueue); |
| 213 | } |
| 214 | |
| 215 | IncidentService::~IncidentService() |
| 216 | { |
| 217 | } |
| 218 | |
| 219 | Status |
| 220 | IncidentService::reportIncident(const IncidentReportArgs& args) |
| 221 | { |
| 222 | ALOGI("reportIncident"); |
| 223 | |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 224 | Status status = checkIncidentPermissions(args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 225 | if (!status.isOk()) { |
| 226 | return status; |
| 227 | } |
| 228 | |
| 229 | mHandler->scheduleRunReport(new ReportRequest(args, NULL, -1)); |
| 230 | |
| 231 | return Status::ok(); |
| 232 | } |
| 233 | |
| 234 | Status |
| 235 | IncidentService::reportIncidentToStream(const IncidentReportArgs& args, |
| 236 | const sp<IIncidentReportStatusListener>& listener, const unique_fd& stream) |
| 237 | { |
| 238 | ALOGI("reportIncidentToStream"); |
| 239 | |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 240 | Status status = checkIncidentPermissions(args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 241 | if (!status.isOk()) { |
| 242 | return status; |
| 243 | } |
| 244 | |
| 245 | int fd = dup(stream.get()); |
| 246 | if (fd < 0) { |
| 247 | return Status::fromStatusT(-errno); |
| 248 | } |
| 249 | |
| 250 | mHandler->scheduleRunReport(new ReportRequest(args, listener, fd)); |
| 251 | |
| 252 | return Status::ok(); |
| 253 | } |
| 254 | |
| 255 | Status |
| 256 | IncidentService::systemRunning() |
| 257 | { |
| 258 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 259 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 260 | "Only system uid can call systemRunning"); |
| 261 | } |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 262 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 263 | // When system_server is up and running, schedule the dropbox task to run. |
| 264 | mHandler->scheduleSendBacklogToDropbox(); |
| 265 | |
| 266 | return Status::ok(); |
| 267 | } |
| 268 | |