1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Log.h"
#include "Broadcaster.h"
#include "IncidentService.h"
#include <android/os/DropBoxManager.h>
#include <binder/IServiceManager.h>
#include <thread>
namespace android {
namespace os {
namespace incidentd {
using android::os::IIncidentCompanion;
using binder::Status;
// ============================================================
Broadcaster::ConsentListener::ConsentListener(const sp<Broadcaster>& broadcaster,
const ReportId& reportId)
:mBroadcaster(broadcaster),
mId(reportId) {
}
Broadcaster::ConsentListener::~ConsentListener() {
}
Status Broadcaster::ConsentListener::onReportApproved() {
mBroadcaster->report_approved(mId);
return Status::ok();
}
Status Broadcaster::ConsentListener::onReportDenied() {
mBroadcaster->report_denied(mId);
return Status::ok();
}
// ============================================================
Broadcaster::ReportId::ReportId()
:id(),
pkg(),
cls() {
}
Broadcaster::ReportId::ReportId(const ReportId& that)
:id(that.id),
pkg(that.pkg),
cls(that.cls) {
}
Broadcaster::ReportId::ReportId(const string& i, const string& p, const string& c)
:id(i),
pkg(p),
cls(c) {
}
Broadcaster::ReportId::~ReportId() {
}
bool Broadcaster::ReportId::operator<(const ReportId& that) const {
if (id < that.id) {
return true;
}
if (id > that.id) {
return false;
}
if (pkg < that.pkg) {
return true;
}
if (pkg > that.pkg) {
return false;
}
if (cls < that.cls) {
return true;
}
return false;
}
// ============================================================
Broadcaster::ReportStatus::ReportStatus()
:approval_sent(false),
ready_sent(false),
listener(nullptr) {
}
Broadcaster::ReportStatus::ReportStatus(const ReportStatus& that)
:approval_sent(that.approval_sent),
ready_sent(that.ready_sent),
listener(that.listener) {
}
Broadcaster::ReportStatus::~ReportStatus() {
}
// ============================================================
Broadcaster::Broadcaster(const sp<WorkDirectory>& workDirectory)
:mReportHandler(),
mWorkDirectory(workDirectory) {
}
void Broadcaster::setHandler(const sp<ReportHandler>& handler) {
mReportHandler = handler;
}
void Broadcaster::reset() {
unique_lock<mutex> lock(mLock);
mLastSent = 0;
mHistory.clear();
// Could cancel the listeners, but this happens when
// the system process crashes, so don't bother.
}
void Broadcaster::clearBroadcasts(const string& pkg, const string& cls, const string& id) {
unique_lock<mutex> lock(mLock);
map<ReportId,ReportStatus>::const_iterator found = mHistory.find(ReportId(id, pkg, cls));
if (found != mHistory.end()) {
if (found->second.listener != nullptr) {
sp<IIncidentCompanion> ics = get_incident_companion();
if (ics != nullptr) {
ics->cancelAuthorization(found->second.listener);
}
}
mHistory.erase(found);
}
}
void Broadcaster::clearPackageBroadcasts(const string& pkg) {
unique_lock<mutex> lock(mLock);
map<ReportId,ReportStatus>::iterator it = mHistory.begin();
while (it != mHistory.end()) {
if (it->first.pkg == pkg) {
if (it->second.listener != nullptr) {
sp<IIncidentCompanion> ics = get_incident_companion();
if (ics != nullptr) {
ics->cancelAuthorization(it->second.listener);
}
}
it = mHistory.erase(it);
} else {
it++;
}
}
}
Broadcaster::broadcast_status_t Broadcaster::sendBroadcasts() {
int err;
int64_t lastSent = get_last_sent();
vector<sp<ReportFile>> files;
mWorkDirectory->getReports(&files, 0); //lastSent);
// Don't send multiple broadcasts to the same receiver.
set<ReportId> reportReadyBroadcasts;
for (const sp<ReportFile>& file: files) {
err = file->loadEnvelope();
if (err != NO_ERROR) {
ALOGW("Error (%s) loading envelope from %s", strerror(-err),
file->getEnvelopeFileName().c_str());
continue;
}
const ReportFileProto& envelope = file->getEnvelope();
if (!envelope.completed()) {
ALOGI("Incident report not completed skipping it: %s",
file->getEnvelopeFileName().c_str());
continue;
}
// When one of the broadcast functions in this loop fails, it's almost
// certainly because the system process is crashing or has crashed. Rather
// than continuing to pound on the system process and potentially make things
// worse, we bail right away, return BROADCASTS_BACKOFF, and we will try
// again later. In the meantime, if the system process did crash, it might
// clear out mHistory, which means we'll be back here again to send the
// backlog.
size_t reportCount = envelope.report_size();
bool hasApprovalPending = false;
for (int reportIndex = 0; reportIndex < reportCount; reportIndex++) {
const ReportFileProto_Report& report = envelope.report(reportIndex);
status_t err;
if (report.privacy_policy() == PRIVACY_POLICY_AUTOMATIC || report.share_approved()) {
// It's privacy policy is AUTO, or it's been approved,
// so send the actual broadcast.
if (!was_ready_sent(file->getId(), report.pkg(), report.cls())) {
if (report.pkg() == DROPBOX_SENTINEL.getPackageName()
&& report.cls() == DROPBOX_SENTINEL.getClassName()) {
IncidentReportArgs args;
get_args_from_report(&args, report);
err = send_to_dropbox(file, args);
if (err != NO_ERROR) {
return BROADCASTS_BACKOFF;
}
} else {
reportReadyBroadcasts.insert(ReportId(file->getId(), report.pkg(),
report.cls()));
}
}
} else {
// It's not approved yet, so send the approval.
if (!was_approval_sent(file->getId(), report.pkg(), report.cls())) {
err = send_approval_broadcasts(file->getId(), report.pkg(), report.cls());
if (err != NO_ERROR) {
return BROADCASTS_BACKOFF;
}
hasApprovalPending = true;
}
}
}
lastSent = file->getTimestampNs();
if (!hasApprovalPending) {
set_last_sent(lastSent);
}
}
for (const ReportId& report: reportReadyBroadcasts) {
err = send_report_ready_broadcasts(report.id, report.pkg, report.cls);
if (err != NO_ERROR) {
return BROADCASTS_BACKOFF;
}
}
return mWorkDirectory->hasMore(lastSent) ? BROADCASTS_REPEAT : BROADCASTS_FINISHED;
}
void Broadcaster::set_last_sent(int64_t timestamp) {
unique_lock<mutex> lock(mLock);
mLastSent = timestamp;
}
int64_t Broadcaster::get_last_sent() {
unique_lock<mutex> lock(mLock);
return mLastSent;
}
/*
void Broadcaster::printReportStatuses() const {
ALOGD("mHistory {");
for (map<ReportId,ReportStatus>::const_iterator it = mHistory.begin();
it != mHistory.end(); it++) {
ALOGD(" [%s %s] --> [%d %d]", it->first.id.c_str(), it->first.pkg.c_str(),
it->second.approval_sent, it->second.ready_sent);
}
ALOGD("}");
}
*/
bool Broadcaster::was_approval_sent(const string& id, const string& pkg, const string& cls) {
unique_lock<mutex> lock(mLock);
map<ReportId,ReportStatus>::const_iterator found = mHistory.find(ReportId(id, pkg, cls));
if (found != mHistory.end()) {
return found->second.approval_sent;
}
return false;
}
void Broadcaster::set_approval_sent(const string& id, const string& pkg, const string& cls,
const sp<ConsentListener>& listener) {
unique_lock<mutex> lock(mLock);
ReportStatus& reportStatus = mHistory[ReportId(id, pkg, cls)];
reportStatus.approval_sent = true;
reportStatus.listener = listener;
}
bool Broadcaster::was_ready_sent(const string& id, const string& pkg, const string& cls) {
unique_lock<mutex> lock(mLock);
map<ReportId,ReportStatus>::const_iterator found = mHistory.find(ReportId(id, pkg, cls));
if (found != mHistory.end()) {
return found->second.ready_sent;
}
return false;
}
void Broadcaster::set_ready_sent(const string& id, const string& pkg, const string& cls) {
unique_lock<mutex> lock(mLock);
mHistory[ReportId(id, pkg, cls)].ready_sent = true;
}
status_t Broadcaster::send_approval_broadcasts(const string& id, const string& pkg,
const string& cls) {
sp<IIncidentCompanion> ics = get_incident_companion();
if (ics == nullptr) {
return NAME_NOT_FOUND;
}
sp<ConsentListener> listener = new ConsentListener(this, ReportId(id, pkg, cls));
ALOGI("send_approval_broadcasts for %s %s/%s", id.c_str(), pkg.c_str(), cls.c_str());
Status status = ics->authorizeReport(0, String16(pkg.c_str()),
String16(cls.c_str()), String16(id.c_str()), 0, listener);
if (!status.isOk()) {
// authorizeReport is oneway, so any error is a transaction error.
return status.transactionError();
}
set_approval_sent(id, pkg, cls, listener);
return NO_ERROR;
}
void Broadcaster::report_approved(const ReportId& reportId) {
status_t err;
// Kick off broadcaster to do send the ready broadcasts.
ALOGI("The user approved the report, so kicking off another broadcast pass. %s %s/%s",
reportId.id.c_str(), reportId.pkg.c_str(), reportId.cls.c_str());
sp<ReportFile> file = mWorkDirectory->getReport(reportId.pkg, reportId.cls, reportId.id,
nullptr);
if (file != nullptr) {
err = file->loadEnvelope();
if (err != NO_ERROR) {
return;
}
err = file->markApproved(reportId.pkg, reportId.cls);
if (err != NO_ERROR) {
ALOGI("Couldn't find report that was just approved: %s %s/%s",
reportId.id.c_str(), reportId.pkg.c_str(), reportId.cls.c_str());
return;
}
file->saveEnvelope();
if (err != NO_ERROR) {
return;
}
}
mReportHandler->scheduleSendBacklog();
}
void Broadcaster::report_denied(const ReportId& reportId) {
// The user didn't approve the report, so remove it from the WorkDirectory.
ALOGI("The user denied the report, so deleting it. %s %s/%s",
reportId.id.c_str(), reportId.pkg.c_str(), reportId.cls.c_str());
sp<ReportFile> file = mWorkDirectory->getReport(reportId.pkg, reportId.cls, reportId.id,
nullptr);
if (file != nullptr) {
mWorkDirectory->commit(file, reportId.pkg, reportId.cls);
}
}
status_t Broadcaster::send_report_ready_broadcasts(const string& id, const string& pkg,
const string& cls) {
sp<IIncidentCompanion> ics = get_incident_companion();
if (ics == nullptr) {
return NAME_NOT_FOUND;
}
ALOGI("send_report_ready_broadcasts for %s %s/%s", id.c_str(), pkg.c_str(), cls.c_str());
Status status = ics->sendReportReadyBroadcast(String16(pkg.c_str()), String16(cls.c_str()));
if (!status.isOk()) {
// sendReportReadyBroadcast is oneway, so any error is a transaction error.
return status.transactionError();
}
set_ready_sent(id, pkg, cls);
return NO_ERROR;
}
status_t Broadcaster::send_to_dropbox(const sp<ReportFile>& file,
const IncidentReportArgs& args) {
status_t err;
sp<DropBoxManager> dropbox = new DropBoxManager();
if (dropbox == nullptr) {
ALOGW("Can't reach dropbox now, so we won't be able to write the incident report to there");
return NO_ERROR;
}
int fds[2];
if (pipe(fds) != 0) {
ALOGW("Error opening pipe to filter incident report: %s", file->getDataFileName().c_str());
return NO_ERROR;
}
int readFd = fds[0];
int writeFd = fds[1];
// spawn a thread to write the data. Release the writeFd ownership to the thread.
thread th([file, writeFd, args]() { file->startFilteringData(writeFd, args); });
th.detach();
// Takes ownership of readFd.
Status status = dropbox->addFile(String16("incident"), readFd, 0);
if (!status.isOk()) {
// TODO: This may or may not leak the readFd, depending on where it failed.
// Not sure how to fix this given the dropbox API.
ALOGW("Error sending incident report to dropbox.");
return -errno;
}
// On successful write, tell the working directory that this file is done.
mWorkDirectory->commit(file, DROPBOX_SENTINEL.getPackageName(),
DROPBOX_SENTINEL.getClassName());
// Don't need to call set_ready_sent, because we just removed it from the ReportFile,
// so we'll never hear about it again.
return NO_ERROR;
}
sp<IIncidentCompanion> Broadcaster::get_incident_companion() {
sp<IBinder> binder = defaultServiceManager()->getService(String16("incidentcompanion"));
if (binder == nullptr) {
ALOGI("Can not find IIncidentCompanion service to send broadcast. Will try again later.");
return nullptr;
}
sp<IIncidentCompanion> ics = interface_cast<IIncidentCompanion>(binder);
if (ics == nullptr) {
ALOGI("The incidentcompanion service is not an IIncidentCompanion. Will try again later.");
return nullptr;
}
return ics;
}
} // namespace incidentd
} // namespace os
} // namespace android
|