blob: 836801deaa082135bbf4c18ab193a54b26004511 [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -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 */
Yi Jin4e843102018-02-14 15:36:18 -080016#define DEBUG false
Yi Jinb592e3b2018-02-01 15:17:04 -080017#include "Log.h"
Joe Onorato1754d742016-11-21 17:51:35 -080018
19#include "IncidentService.h"
20
Yi Jinb592e3b2018-02-01 15:17:04 -080021#include "FdBuffer.h"
Joe Onorato99598ee2019-02-11 15:55:13 +000022#include "PrivacyFilter.h"
Joe Onorato1754d742016-11-21 17:51:35 -080023#include "Reporter.h"
Yi Jinb592e3b2018-02-01 15:17:04 -080024#include "incidentd_util.h"
25#include "section_list.h"
Joe Onorato1754d742016-11-21 17:51:35 -080026
Mike Ma85434ec2018-11-27 10:32:31 -080027#include <android/os/IncidentReportArgs.h>
Joe Onorato1754d742016-11-21 17:51:35 -080028#include <binder/IPCThreadState.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080029#include <binder/IResultReceiver.h>
Joe Onorato1754d742016-11-21 17:51:35 -080030#include <binder/IServiceManager.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080031#include <binder/IShellCallback.h>
Mike Ma35056662018-12-06 13:32:59 -080032#include <log/log.h>
Joe Onorato1754d742016-11-21 17:51:35 -080033#include <private/android_filesystem_config.h>
34#include <utils/Looper.h>
Yao Chencbafce92019-04-01 15:56:44 -070035#include <thread>
Joe Onorato1754d742016-11-21 17:51:35 -080036
37#include <unistd.h>
38
Joe Onorato99598ee2019-02-11 15:55:13 +000039enum {
40 WHAT_TAKE_REPORT = 1,
41 WHAT_SEND_BROADCASTS = 2
42};
Joe Onorato1754d742016-11-21 17:51:35 -080043
Joe Onorato99598ee2019-02-11 15:55:13 +000044#define DEFAULT_DELAY_NS (1000000000LL)
Joe Onorato1754d742016-11-21 17:51:35 -080045
Rafal Slawik1334f1a2022-01-12 17:13:33 +000046#define DEFAULT_BYTES_SIZE_LIMIT (400 * 1024 * 1024) // 400MB
Yi Jin4e843102018-02-14 15:36:18 -080047#define DEFAULT_REFACTORY_PERIOD_MS (24 * 60 * 60 * 1000) // 1 Day
48
Mike Ma5a57d792019-08-21 14:52:46 -070049// Skip these sections (for dumpstate only)
Kevin Jeon4ee5ba59e2022-07-18 20:26:38 +000050// Skip logs (1100 - 1108), traces (1200 - 1202), dumpsys (3000 - 3024, 3027 - 3056, 4000 - 4001)
51// because they are already in the bug report.
Mike Ma5a57d792019-08-21 14:52:46 -070052#define SKIPPED_DUMPSTATE_SECTIONS { \
53 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, /* Logs */ \
Kevin Jeon4ee5ba59e2022-07-18 20:26:38 +000054 1200, 1201, 1202, /* Native, hal, java traces */ \
55 3000, 3001, 3002, 3003, 3004, 3005, 3006, 3007, 3008, 3009, 3010, 3011, 3012, 3013, \
56 3014, 3015, 3016, 3017, 3018, 3019, 3020, 3021, 3022, 3023, 3024, 3027, 3028, 3029, \
57 3030, 3031, 3032, 3033, 3034, 3035, 3036, 3037, 3038, 3039, 3040, 3041, 3042, 3043, \
58 3044, 3045, 3046, 3047, 3048, 3049, 3050, 3051, 3052, 3053, 3054, 3055, 3056, 4000, \
59 4001, /* Dumpsys */ }
Mike Ma85434ec2018-11-27 10:32:31 -080060
Yi Jin6cacbcb2018-03-30 14:04:52 -070061namespace android {
62namespace os {
63namespace incidentd {
64
Joe Onorato99598ee2019-02-11 15:55:13 +000065String16 const APPROVE_INCIDENT_REPORTS("android.permission.APPROVE_INCIDENT_REPORTS");
Joe Onorato1754d742016-11-21 17:51:35 -080066String16 const DUMP_PERMISSION("android.permission.DUMP");
67String16 const USAGE_STATS_PERMISSION("android.permission.PACKAGE_USAGE_STATS");
68
Yi Jinb592e3b2018-02-01 15:17:04 -080069static Status checkIncidentPermissions(const IncidentReportArgs& args) {
Yi Jin437aa6e2018-01-10 11:34:26 -080070 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Yi Jinafb36062018-01-31 19:14:25 -080071 pid_t callingPid = IPCThreadState::self()->getCallingPid();
Yi Jin437aa6e2018-01-10 11:34:26 -080072 if (callingUid == AID_ROOT || callingUid == AID_SHELL) {
Joe Onorato99598ee2019-02-11 15:55:13 +000073 // Root and shell are ok.
74 return Status::ok();
75 }
76
77 if (checkCallingPermission(APPROVE_INCIDENT_REPORTS)) {
78 // Permission controller (this is a singleton permission that is always granted
79 // exactly for PermissionController) is allowed to access incident reports
80 // so it can show the user info about what they are approving.
Yi Jin437aa6e2018-01-10 11:34:26 -080081 return Status::ok();
82 }
83
Yi Jin4bab3a12018-01-10 16:50:59 -080084 // checking calling permission.
Joe Onorato1754d742016-11-21 17:51:35 -080085 if (!checkCallingPermission(DUMP_PERMISSION)) {
86 ALOGW("Calling pid %d and uid %d does not have permission: android.permission.DUMP",
Yi Jinb592e3b2018-02-01 15:17:04 -080087 callingPid, callingUid);
88 return Status::fromExceptionCode(
89 Status::EX_SECURITY,
Joe Onorato1754d742016-11-21 17:51:35 -080090 "Calling process does not have permission: android.permission.DUMP");
91 }
92 if (!checkCallingPermission(USAGE_STATS_PERMISSION)) {
93 ALOGW("Calling pid %d and uid %d does not have permission: android.permission.USAGE_STATS",
Yi Jinb592e3b2018-02-01 15:17:04 -080094 callingPid, callingUid);
95 return Status::fromExceptionCode(
96 Status::EX_SECURITY,
Joe Onorato1754d742016-11-21 17:51:35 -080097 "Calling process does not have permission: android.permission.USAGE_STATS");
98 }
Yi Jin4bab3a12018-01-10 16:50:59 -080099
100 // checking calling request uid permission.
Joe Onorato99598ee2019-02-11 15:55:13 +0000101 switch (args.getPrivacyPolicy()) {
102 case PRIVACY_POLICY_LOCAL:
Yi Jinafb36062018-01-31 19:14:25 -0800103 if (callingUid != AID_SHELL && callingUid != AID_ROOT) {
104 ALOGW("Calling pid %d and uid %d does not have permission to get local data.",
Yi Jinb592e3b2018-02-01 15:17:04 -0800105 callingPid, callingUid);
106 return Status::fromExceptionCode(
107 Status::EX_SECURITY,
108 "Calling process does not have permission to get local data.");
Yi Jin4bab3a12018-01-10 16:50:59 -0800109 }
Bookatzda9b8d02018-11-14 13:14:45 -0800110 break;
Joe Onorato99598ee2019-02-11 15:55:13 +0000111 case PRIVACY_POLICY_EXPLICIT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800112 if (callingUid != AID_SHELL && callingUid != AID_ROOT && callingUid != AID_STATSD &&
Bookatzda9b8d02018-11-14 13:14:45 -0800113 callingUid != AID_SYSTEM) {
Yi Jinafb36062018-01-31 19:14:25 -0800114 ALOGW("Calling pid %d and uid %d does not have permission to get explicit data.",
Yi Jinb592e3b2018-02-01 15:17:04 -0800115 callingPid, callingUid);
116 return Status::fromExceptionCode(
117 Status::EX_SECURITY,
118 "Calling process does not have permission to get explicit data.");
Yi Jin4bab3a12018-01-10 16:50:59 -0800119 }
Bookatzda9b8d02018-11-14 13:14:45 -0800120 break;
Yi Jin4bab3a12018-01-10 16:50:59 -0800121 }
Joe Onorato1754d742016-11-21 17:51:35 -0800122 return Status::ok();
123}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700124
Joe Onorato99598ee2019-02-11 15:55:13 +0000125static string build_uri(const string& pkg, const string& cls, const string& id) {
Yao Chencbafce92019-04-01 15:56:44 -0700126 return "content://android.os.IncidentManager/pending?pkg="
127 + pkg + "&receiver=" + cls + "&r=" + id;
Joe Onorato1754d742016-11-21 17:51:35 -0800128}
129
Joe Onorato1754d742016-11-21 17:51:35 -0800130// ================================================================================
Joe Onorato99598ee2019-02-11 15:55:13 +0000131ReportHandler::ReportHandler(const sp<WorkDirectory>& workDirectory,
Mike Ma643de922019-12-17 10:56:17 -0800132 const sp<Broadcaster>& broadcaster,
133 const sp<Looper>& handlerLooper,
134 const sp<Throttler>& throttler,
135 const vector<BringYourOwnSection*>& registeredSections)
Joe Onorato99598ee2019-02-11 15:55:13 +0000136 :mLock(),
137 mWorkDirectory(workDirectory),
138 mBroadcaster(broadcaster),
139 mHandlerLooper(handlerLooper),
140 mBacklogDelay(DEFAULT_DELAY_NS),
141 mThrottler(throttler),
Mike Ma643de922019-12-17 10:56:17 -0800142 mRegisteredSections(registeredSections),
Joe Onorato99598ee2019-02-11 15:55:13 +0000143 mBatch(new ReportBatch()) {
144}
Joe Onorato1754d742016-11-21 17:51:35 -0800145
Joe Onorato99598ee2019-02-11 15:55:13 +0000146ReportHandler::~ReportHandler() {
147}
Joe Onorato1754d742016-11-21 17:51:35 -0800148
Yi Jinb592e3b2018-02-01 15:17:04 -0800149void ReportHandler::handleMessage(const Message& message) {
Joe Onorato1754d742016-11-21 17:51:35 -0800150 switch (message.what) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000151 case WHAT_TAKE_REPORT:
152 take_report();
Joe Onorato1754d742016-11-21 17:51:35 -0800153 break;
Joe Onorato99598ee2019-02-11 15:55:13 +0000154 case WHAT_SEND_BROADCASTS:
155 send_broadcasts();
Joe Onorato1754d742016-11-21 17:51:35 -0800156 break;
157 }
158}
159
Joe Onorato99598ee2019-02-11 15:55:13 +0000160void ReportHandler::schedulePersistedReport(const IncidentReportArgs& args) {
Mike Mab98050f2020-03-30 13:37:31 -0700161 unique_lock<mutex> lock(mLock);
Joe Onorato99598ee2019-02-11 15:55:13 +0000162 mBatch->addPersistedReport(args);
163 mHandlerLooper->removeMessages(this, WHAT_TAKE_REPORT);
164 mHandlerLooper->sendMessage(this, Message(WHAT_TAKE_REPORT));
Joe Onorato1754d742016-11-21 17:51:35 -0800165}
166
Joe Onorato99598ee2019-02-11 15:55:13 +0000167void ReportHandler::scheduleStreamingReport(const IncidentReportArgs& args,
168 const sp<IIncidentReportStatusListener>& listener, int streamFd) {
Mike Mab98050f2020-03-30 13:37:31 -0700169 unique_lock<mutex> lock(mLock);
Joe Onorato99598ee2019-02-11 15:55:13 +0000170 mBatch->addStreamingReport(args, listener, streamFd);
171 mHandlerLooper->removeMessages(this, WHAT_TAKE_REPORT);
172 mHandlerLooper->sendMessage(this, Message(WHAT_TAKE_REPORT));
173}
174
175void ReportHandler::scheduleSendBacklog() {
Joe Onorato1754d742016-11-21 17:51:35 -0800176 unique_lock<mutex> lock(mLock);
Joe Onorato99598ee2019-02-11 15:55:13 +0000177 mBacklogDelay = DEFAULT_DELAY_NS;
178 schedule_send_broadcasts_locked();
Joe Onorato1754d742016-11-21 17:51:35 -0800179}
180
Joe Onorato99598ee2019-02-11 15:55:13 +0000181void ReportHandler::schedule_send_broadcasts_locked() {
182 mHandlerLooper->removeMessages(this, WHAT_SEND_BROADCASTS);
183 mHandlerLooper->sendMessageDelayed(mBacklogDelay, this, Message(WHAT_SEND_BROADCASTS));
Joe Onorato1754d742016-11-21 17:51:35 -0800184}
185
Joe Onorato99598ee2019-02-11 15:55:13 +0000186void ReportHandler::take_report() {
Joe Onoratoe5472052019-04-24 16:27:33 -0700187 // Cycle the batch and throttle.
Joe Onorato99598ee2019-02-11 15:55:13 +0000188 sp<ReportBatch> batch;
189 {
190 unique_lock<mutex> lock(mLock);
Joe Onoratoe5472052019-04-24 16:27:33 -0700191 batch = mThrottler->filterBatch(mBatch);
Joe Onorato1754d742016-11-21 17:51:35 -0800192 }
193
Joe Onorato99598ee2019-02-11 15:55:13 +0000194 if (batch->empty()) {
195 // Nothing to do.
196 return;
197 }
198
Mike Ma643de922019-12-17 10:56:17 -0800199 sp<Reporter> reporter = new Reporter(mWorkDirectory, batch, mRegisteredSections);
Joe Onorato99598ee2019-02-11 15:55:13 +0000200
Joe Onorato1754d742016-11-21 17:51:35 -0800201 // Take the report, which might take a while. More requests might queue
202 // up while we're doing this, and we'll handle them in their next batch.
203 // TODO: We should further rate-limit the reports to no more than N per time-period.
Joe Onorato99598ee2019-02-11 15:55:13 +0000204 // TODO: Move this inside reporter.
Yi Jin4e843102018-02-14 15:36:18 -0800205 size_t reportByteSize = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000206 reporter->runReport(&reportByteSize);
207
Joe Onoratoe5472052019-04-24 16:27:33 -0700208 // Tell the throttler how big it was, for the next throttling.
209 // TODO: This still isn't ideal. The throttler really should just track the
210 // persisted reqeusts, but changing Reporter::runReport() to track that individually
211 // will be a big change.
212 if (batch->hasPersistedReports()) {
213 mThrottler->addReportSize(reportByteSize);
214 }
Joe Onorato99598ee2019-02-11 15:55:13 +0000215
216 // Kick off the next steps, one of which is to send any new or otherwise remaining
217 // approvals, and one of which is to send any new or remaining broadcasts.
218 {
Joe Onorato1754d742016-11-21 17:51:35 -0800219 unique_lock<mutex> lock(mLock);
Joe Onorato99598ee2019-02-11 15:55:13 +0000220 schedule_send_broadcasts_locked();
Joe Onorato1754d742016-11-21 17:51:35 -0800221 }
222}
223
Joe Onorato99598ee2019-02-11 15:55:13 +0000224void ReportHandler::send_broadcasts() {
225 Broadcaster::broadcast_status_t result = mBroadcaster->sendBroadcasts();
226 if (result == Broadcaster::BROADCASTS_FINISHED) {
227 // We're done.
228 unique_lock<mutex> lock(mLock);
229 mBacklogDelay = DEFAULT_DELAY_NS;
230 } else if (result == Broadcaster::BROADCASTS_REPEAT) {
231 // It worked, but there are more.
232 unique_lock<mutex> lock(mLock);
233 mBacklogDelay = DEFAULT_DELAY_NS;
234 schedule_send_broadcasts_locked();
235 } else if (result == Broadcaster::BROADCASTS_BACKOFF) {
Joe Onorato1754d742016-11-21 17:51:35 -0800236 // There was a failure. Exponential backoff.
237 unique_lock<mutex> lock(mLock);
238 mBacklogDelay *= 2;
239 ALOGI("Error sending to dropbox. Trying again in %lld minutes",
Yi Jinb592e3b2018-02-01 15:17:04 -0800240 (mBacklogDelay / (1000000000LL * 60)));
Joe Onorato99598ee2019-02-11 15:55:13 +0000241 schedule_send_broadcasts_locked();
Joe Onorato1754d742016-11-21 17:51:35 -0800242 }
243}
244
245// ================================================================================
Joe Onorato99598ee2019-02-11 15:55:13 +0000246IncidentService::IncidentService(const sp<Looper>& handlerLooper) {
247 mThrottler = new Throttler(DEFAULT_BYTES_SIZE_LIMIT, DEFAULT_REFACTORY_PERIOD_MS);
248 mWorkDirectory = new WorkDirectory();
249 mBroadcaster = new Broadcaster(mWorkDirectory);
250 mHandler = new ReportHandler(mWorkDirectory, mBroadcaster, handlerLooper,
Mike Ma643de922019-12-17 10:56:17 -0800251 mThrottler, mRegisteredSections);
Joe Onorato99598ee2019-02-11 15:55:13 +0000252 mBroadcaster->setHandler(mHandler);
Joe Onorato1754d742016-11-21 17:51:35 -0800253}
254
Yi Jinb592e3b2018-02-01 15:17:04 -0800255IncidentService::~IncidentService() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800256
Yi Jinb592e3b2018-02-01 15:17:04 -0800257Status IncidentService::reportIncident(const IncidentReportArgs& args) {
Joe Onoratoe5472052019-04-24 16:27:33 -0700258 IncidentReportArgs argsCopy(args);
Joe Onorato1754d742016-11-21 17:51:35 -0800259
Joe Onoratoe5472052019-04-24 16:27:33 -0700260 // Validate that the privacy policy is one of the real ones.
261 // If it isn't, clamp it to the next more restrictive real one.
262 argsCopy.setPrivacyPolicy(cleanup_privacy_policy(args.getPrivacyPolicy()));
Joe Onorato99598ee2019-02-11 15:55:13 +0000263
264 // TODO: Check that the broadcast recevier has the proper permissions
265 // TODO: Maybe we should consider relaxing the permissions if it's going to
266 // dropbox, but definitely not if it's going to the broadcaster.
Yi Jin4bab3a12018-01-10 16:50:59 -0800267 Status status = checkIncidentPermissions(args);
Joe Onorato1754d742016-11-21 17:51:35 -0800268 if (!status.isOk()) {
269 return status;
270 }
271
Joe Onoratoe5472052019-04-24 16:27:33 -0700272 // If they asked for the LOCAL privacy policy, give them EXPLICT. LOCAL has to
273 // be streamed. (This only applies to shell/root, because everyone else would have
274 // been rejected by checkIncidentPermissions()).
275 if (argsCopy.getPrivacyPolicy() < PRIVACY_POLICY_EXPLICIT) {
276 ALOGI("Demoting privacy policy to EXPLICT for persisted report.");
277 argsCopy.setPrivacyPolicy(PRIVACY_POLICY_EXPLICIT);
278 }
279
Joe Onorato99598ee2019-02-11 15:55:13 +0000280 // If they didn't specify a component, use dropbox.
Joe Onorato99598ee2019-02-11 15:55:13 +0000281 if (argsCopy.receiverPkg().length() == 0 && argsCopy.receiverCls().length() == 0) {
282 argsCopy.setReceiverPkg(DROPBOX_SENTINEL.getPackageName());
283 argsCopy.setReceiverCls(DROPBOX_SENTINEL.getClassName());
284 }
285
286 mHandler->schedulePersistedReport(argsCopy);
Joe Onorato1754d742016-11-21 17:51:35 -0800287
288 return Status::ok();
289}
290
Yi Jinb592e3b2018-02-01 15:17:04 -0800291Status IncidentService::reportIncidentToStream(const IncidentReportArgs& args,
292 const sp<IIncidentReportStatusListener>& listener,
Jiyong Parkb8ba2342019-11-25 11:03:38 +0900293 unique_fd stream) {
Joe Onoratoe5472052019-04-24 16:27:33 -0700294 IncidentReportArgs argsCopy(args);
Joe Onorato99598ee2019-02-11 15:55:13 +0000295
296 // Streaming reports can not also be broadcast.
Joe Onorato99598ee2019-02-11 15:55:13 +0000297 argsCopy.setReceiverPkg("");
298 argsCopy.setReceiverCls("");
299
Joe Onoratoe5472052019-04-24 16:27:33 -0700300 // Validate that the privacy policy is one of the real ones.
301 // If it isn't, clamp it to the next more restrictive real one.
302 argsCopy.setPrivacyPolicy(cleanup_privacy_policy(args.getPrivacyPolicy()));
303
Joe Onorato99598ee2019-02-11 15:55:13 +0000304 Status status = checkIncidentPermissions(argsCopy);
Joe Onorato1754d742016-11-21 17:51:35 -0800305 if (!status.isOk()) {
306 return status;
307 }
308
Joe Onorato99598ee2019-02-11 15:55:13 +0000309 // The ReportRequest takes ownership of the fd, so we need to dup it.
Joe Onorato1754d742016-11-21 17:51:35 -0800310 int fd = dup(stream.get());
311 if (fd < 0) {
312 return Status::fromStatusT(-errno);
313 }
314
Joe Onorato99598ee2019-02-11 15:55:13 +0000315 mHandler->scheduleStreamingReport(argsCopy, listener, fd);
Joe Onorato1754d742016-11-21 17:51:35 -0800316
317 return Status::ok();
318}
319
Jiyong Parkb8ba2342019-11-25 11:03:38 +0900320Status IncidentService::reportIncidentToDumpstate(unique_fd stream,
Mike Ma5a57d792019-08-21 14:52:46 -0700321 const sp<IIncidentReportStatusListener>& listener) {
322 uid_t caller = IPCThreadState::self()->getCallingUid();
323 if (caller != AID_ROOT && caller != AID_SHELL) {
324 ALOGW("Calling uid %d does not have permission: only ROOT or SHELL allowed", caller);
325 return Status::fromExceptionCode(Status::EX_SECURITY, "Only ROOT or SHELL allowed");
326 }
327
328 ALOGD("Stream incident report to dumpstate");
329 IncidentReportArgs incidentArgs;
330 // Privacy policy for dumpstate incident reports is always EXPLICIT.
331 incidentArgs.setPrivacyPolicy(PRIVACY_POLICY_EXPLICIT);
332
333 int skipped[] = SKIPPED_DUMPSTATE_SECTIONS;
334 for (const Section** section = SECTION_LIST; *section; section++) {
335 const int id = (*section)->id;
336 if (std::find(std::begin(skipped), std::end(skipped), id) == std::end(skipped)
337 && !section_requires_specific_mention(id)) {
338 incidentArgs.addSection(id);
339 }
340 }
Mike Ma643de922019-12-17 10:56:17 -0800341 for (const Section* section : mRegisteredSections) {
342 if (!section_requires_specific_mention(section->id)) {
343 incidentArgs.addSection(section->id);
344 }
345 }
Mike Ma5a57d792019-08-21 14:52:46 -0700346
347 // The ReportRequest takes ownership of the fd, so we need to dup it.
348 int fd = dup(stream.get());
349 if (fd < 0) {
350 return Status::fromStatusT(-errno);
351 }
352
353 mHandler->scheduleStreamingReport(incidentArgs, listener, fd);
354
355 return Status::ok();
356}
357
Mike Ma643de922019-12-17 10:56:17 -0800358Status IncidentService::registerSection(const int id, const String16& name16,
359 const sp<IIncidentDumpCallback>& callback) {
Wenjie Zhou751c7c92020-05-14 12:49:19 -0700360 const String8 name = String8(name16);
Mike Mac2ab45a2020-01-17 06:11:24 +0000361 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Wenjie Zhou751c7c92020-05-14 12:49:19 -0700362 ALOGI("Uid %d registers section %d '%s'", callingUid, id, name.c_str());
Mike Ma643de922019-12-17 10:56:17 -0800363 if (callback == nullptr) {
364 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
365 }
Mike Ma643de922019-12-17 10:56:17 -0800366 for (int i = 0; i < mRegisteredSections.size(); i++) {
367 if (mRegisteredSections.at(i)->id == id) {
368 if (mRegisteredSections.at(i)->uid != callingUid) {
369 ALOGW("Error registering section %d: calling uid does not match", id);
370 return Status::fromExceptionCode(Status::EX_SECURITY);
371 }
Wenjie Zhou751c7c92020-05-14 12:49:19 -0700372 mRegisteredSections.at(i) = new BringYourOwnSection(id, name.c_str(), callingUid, callback);
Mike Ma643de922019-12-17 10:56:17 -0800373 return Status::ok();
374 }
375 }
Wenjie Zhou751c7c92020-05-14 12:49:19 -0700376 mRegisteredSections.push_back(new BringYourOwnSection(id, name.c_str(), callingUid, callback));
Mike Ma643de922019-12-17 10:56:17 -0800377 return Status::ok();
378}
379
380Status IncidentService::unregisterSection(const int id) {
Mike Ma643de922019-12-17 10:56:17 -0800381 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Mike Mac2ab45a2020-01-17 06:11:24 +0000382 ALOGI("Uid %d unregisters section %d", callingUid, id);
383
Mike Ma643de922019-12-17 10:56:17 -0800384 for (auto it = mRegisteredSections.begin(); it != mRegisteredSections.end(); it++) {
385 if ((*it)->id == id) {
386 if ((*it)->uid != callingUid) {
387 ALOGW("Error unregistering section %d: calling uid does not match", id);
388 return Status::fromExceptionCode(Status::EX_SECURITY);
389 }
390 mRegisteredSections.erase(it);
391 return Status::ok();
392 }
393 }
394 ALOGW("Section %d not found", id);
395 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
396}
397
Yi Jinb592e3b2018-02-01 15:17:04 -0800398Status IncidentService::systemRunning() {
Joe Onorato1754d742016-11-21 17:51:35 -0800399 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
400 return Status::fromExceptionCode(Status::EX_SECURITY,
Yi Jinb592e3b2018-02-01 15:17:04 -0800401 "Only system uid can call systemRunning");
Joe Onorato1754d742016-11-21 17:51:35 -0800402 }
Yi Jinadd11e92017-07-30 16:10:07 -0700403
Joe Onorato1754d742016-11-21 17:51:35 -0800404 // When system_server is up and running, schedule the dropbox task to run.
Joe Onorato99598ee2019-02-11 15:55:13 +0000405 mBroadcaster->reset();
406 mHandler->scheduleSendBacklog();
407
408 return Status::ok();
409}
410
411Status IncidentService::getIncidentReportList(const String16& pkg16, const String16& cls16,
412 vector<String16>* result) {
413 status_t err;
414 const string pkg(String8(pkg16).string());
415 const string cls(String8(cls16).string());
416
417 // List the reports
418 vector<sp<ReportFile>> all;
419 err = mWorkDirectory->getReports(&all, 0);
420 if (err != NO_ERROR) {
421 return Status::fromStatusT(err);
422 }
423
424 // Find the ones that match pkg and cls.
425 for (sp<ReportFile>& file: all) {
426 err = file->loadEnvelope();
427 if (err != NO_ERROR) {
428 continue;
429 }
430 const ReportFileProto& envelope = file->getEnvelope();
431 size_t reportCount = envelope.report_size();
432 for (int reportIndex = 0; reportIndex < reportCount; reportIndex++) {
433 const ReportFileProto_Report& report = envelope.report(reportIndex);
434 if (pkg == report.pkg() && cls == report.cls()) {
435 result->push_back(String16(build_uri(pkg, cls, file->getId()).c_str()));
436 break;
437 }
438 }
439 }
440
441 return Status::ok();
442}
443
444Status IncidentService::getIncidentReport(const String16& pkg16, const String16& cls16,
445 const String16& id16, IncidentManager::IncidentReport* result) {
446 status_t err;
447
448 const string pkg(String8(pkg16).string());
449 const string cls(String8(cls16).string());
450 const string id(String8(id16).string());
451
452 IncidentReportArgs args;
453 sp<ReportFile> file = mWorkDirectory->getReport(pkg, cls, id, &args);
454 if (file != nullptr) {
Yao Chencbafce92019-04-01 15:56:44 -0700455 // Create pipe
456 int fds[2];
457 if (pipe(fds) != 0) {
458 ALOGW("Error opening pipe to filter incident report: %s",
459 file->getDataFileName().c_str());
Joe Onorato99598ee2019-02-11 15:55:13 +0000460 return Status::ok();
461 }
Joe Onorato99598ee2019-02-11 15:55:13 +0000462 result->setTimestampNs(file->getTimestampNs());
463 result->setPrivacyPolicy(file->getEnvelope().privacy_policy());
Yao Chencbafce92019-04-01 15:56:44 -0700464 result->takeFileDescriptor(fds[0]);
465 int writeFd = fds[1];
466 // spawn a thread to write the data. Release the writeFd ownership to the thread.
467 thread th([file, writeFd, args]() { file->startFilteringData(writeFd, args); });
468
469 th.detach();
Joe Onorato99598ee2019-02-11 15:55:13 +0000470 }
471
472 return Status::ok();
473}
474
475Status IncidentService::deleteIncidentReports(const String16& pkg16, const String16& cls16,
476 const String16& id16) {
477 const string pkg(String8(pkg16).string());
478 const string cls(String8(cls16).string());
479 const string id(String8(id16).string());
480
481 sp<ReportFile> file = mWorkDirectory->getReport(pkg, cls, id, nullptr);
482 if (file != nullptr) {
483 mWorkDirectory->commit(file, pkg, cls);
484 }
485 mBroadcaster->clearBroadcasts(pkg, cls, id);
486
487 return Status::ok();
488}
489
490Status IncidentService::deleteAllIncidentReports(const String16& pkg16) {
491 const string pkg(String8(pkg16).string());
492
493 mWorkDirectory->commitAll(pkg);
494 mBroadcaster->clearPackageBroadcasts(pkg);
Joe Onorato1754d742016-11-21 17:51:35 -0800495
496 return Status::ok();
497}
498
Yi Jinb592e3b2018-02-01 15:17:04 -0800499/**
500 * Implement our own because the default binder implementation isn't
501 * properly handling SHELL_COMMAND_TRANSACTION.
502 */
503status_t IncidentService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
504 uint32_t flags) {
505 status_t err;
506
507 switch (code) {
508 case SHELL_COMMAND_TRANSACTION: {
509 int in = data.readFileDescriptor();
510 int out = data.readFileDescriptor();
511 int err = data.readFileDescriptor();
512 int argc = data.readInt32();
513 Vector<String8> args;
514 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
515 args.add(String8(data.readString16()));
516 }
517 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
518 sp<IResultReceiver> resultReceiver =
519 IResultReceiver::asInterface(data.readStrongBinder());
520
521 FILE* fin = fdopen(in, "r");
522 FILE* fout = fdopen(out, "w");
523 FILE* ferr = fdopen(err, "w");
524
525 if (fin == NULL || fout == NULL || ferr == NULL) {
526 resultReceiver->send(NO_MEMORY);
527 } else {
528 err = command(fin, fout, ferr, args);
529 resultReceiver->send(err);
530 }
531
532 if (fin != NULL) {
533 fflush(fin);
534 fclose(fin);
535 }
536 if (fout != NULL) {
537 fflush(fout);
538 fclose(fout);
539 }
540 if (fout != NULL) {
541 fflush(ferr);
542 fclose(ferr);
543 }
544
545 return NO_ERROR;
Bookatzda9b8d02018-11-14 13:14:45 -0800546 } break;
Yi Jinb592e3b2018-02-01 15:17:04 -0800547 default: { return BnIncidentManager::onTransact(code, data, reply, flags); }
548 }
549}
550
551status_t IncidentService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
552 const int argCount = args.size();
553
554 if (argCount >= 1) {
555 if (!args[0].compare(String8("privacy"))) {
556 return cmd_privacy(in, out, err, args);
557 }
Yi Jin4e843102018-02-14 15:36:18 -0800558 if (!args[0].compare(String8("throttler"))) {
559 mThrottler->dump(out);
560 return NO_ERROR;
561 }
Yi Jin908c02f2018-06-22 16:51:40 -0700562 if (!args[0].compare(String8("section"))) {
Howard Ro8ff53152020-08-18 17:13:40 -0700563 if (argCount == 1) {
564 fprintf(out, "Not enough arguments for section\n");
565 return NO_ERROR;
566 }
Yi Jin908c02f2018-06-22 16:51:40 -0700567 int id = atoi(args[1]);
568 int idx = 0;
569 while (SECTION_LIST[idx] != NULL) {
570 const Section* section = SECTION_LIST[idx];
571 if (section->id == id) {
572 fprintf(out, "Section[%d] %s\n", id, section->name.string());
573 break;
574 }
575 idx++;
576 }
577 return NO_ERROR;
578 }
Yi Jinb592e3b2018-02-01 15:17:04 -0800579 }
580 return cmd_help(out);
581}
582
583status_t IncidentService::cmd_help(FILE* out) {
584 fprintf(out, "usage: adb shell cmd incident privacy print <section_id>\n");
585 fprintf(out, "usage: adb shell cmd incident privacy parse <section_id> < proto.txt\n");
Yi Jin908c02f2018-06-22 16:51:40 -0700586 fprintf(out, " Prints/parses for the section id.\n\n");
587 fprintf(out, "usage: adb shell cmd incident section <section_id>\n");
588 fprintf(out, " Prints section id and its name.\n\n");
Yi Jin4e843102018-02-14 15:36:18 -0800589 fprintf(out, "usage: adb shell cmd incident throttler\n");
590 fprintf(out, " Prints the current throttler state\n");
Yi Jinb592e3b2018-02-01 15:17:04 -0800591 return NO_ERROR;
592}
593
594static void printPrivacy(const Privacy* p, FILE* out, String8 indent) {
595 if (p == NULL) return;
Joe Onorato99598ee2019-02-11 15:55:13 +0000596 fprintf(out, "%sid:%d, type:%d, dest:%d\n", indent.string(), p->field_id, p->type, p->policy);
Yi Jinb592e3b2018-02-01 15:17:04 -0800597 if (p->children == NULL) return;
598 for (int i = 0; p->children[i] != NULL; i++) { // NULL-terminated.
599 printPrivacy(p->children[i], out, indent + " ");
600 }
601}
602
603status_t IncidentService::cmd_privacy(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000604 (void)in;
605
Yi Jinb592e3b2018-02-01 15:17:04 -0800606 const int argCount = args.size();
607 if (argCount >= 3) {
608 String8 opt = args[1];
609 int sectionId = atoi(args[2].string());
610
611 const Privacy* p = get_privacy_of_section(sectionId);
612 if (p == NULL) {
613 fprintf(err, "Can't find section id %d\n", sectionId);
614 return NO_ERROR;
615 }
616 fprintf(err, "Get privacy for %d\n", sectionId);
617 if (opt == "print") {
618 printPrivacy(p, out, String8(""));
619 } else if (opt == "parse") {
Joe Onorato99598ee2019-02-11 15:55:13 +0000620 /*
Yi Jinb592e3b2018-02-01 15:17:04 -0800621 FdBuffer buf;
Yi Jine3dab2d2018-03-22 16:56:39 -0700622 status_t error = buf.read(fileno(in), 60000);
Yi Jinb592e3b2018-02-01 15:17:04 -0800623 if (error != NO_ERROR) {
624 fprintf(err, "Error reading from stdin\n");
625 return error;
626 }
627 fprintf(err, "Read %zu bytes\n", buf.size());
Joe Onorato99598ee2019-02-11 15:55:13 +0000628 PrivacyFilter pBuf(p, buf.data());
Yi Jinb592e3b2018-02-01 15:17:04 -0800629
630 PrivacySpec spec = PrivacySpec::new_spec(argCount > 3 ? atoi(args[3]) : -1);
631 error = pBuf.strip(spec);
632 if (error != NO_ERROR) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000633 fprintf(err, "Error strip pii fields with spec %d\n", spec.policy);
Yi Jinb592e3b2018-02-01 15:17:04 -0800634 return error;
635 }
636 return pBuf.flush(fileno(out));
Joe Onorato99598ee2019-02-11 15:55:13 +0000637 */
638 return -1;
Yi Jinb592e3b2018-02-01 15:17:04 -0800639 }
640 } else {
641 return cmd_help(out);
642 }
643 return NO_ERROR;
644}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700645
646} // namespace incidentd
647} // namespace os
648} // namespace android