25
25
#include < sys/uio.h>
26
26
#include < syslog.h>
27
27
28
+ #include < fstream>
29
+ #include < sstream>
30
+
28
31
#include < android-base/macros.h>
32
+ #include < log/log_properties.h>
29
33
#include < private/android_filesystem_config.h>
30
34
#include < private/android_logger.h>
31
35
@@ -138,6 +142,71 @@ bool LogAudit::onDataAvailable(SocketClient* cli) {
138
142
return true ;
139
143
}
140
144
145
+ static inline bool hasMetadata (char * str, int str_len) {
146
+ // need to check and see if str already contains bug metadata from
147
+ // possibility of stuttering if log audit crashes and then reloads kernel
148
+ // messages. Kernel denials that contain metadata will either end in
149
+ // "b/[0-9]+$" or "b/[0-9]+ duplicate messages suppressed$" which will put
150
+ // a '/' character at either 9 or 39 indices away from the end of the str.
151
+ return str_len >= 39 &&
152
+ (str[str_len - 9 ] == ' /' || str[str_len - 39 ] == ' /' );
153
+ }
154
+
155
+ std::map<std::string, std::string> LogAudit::populateDenialMap () {
156
+ std::ifstream bug_file (" /system/etc/selinux/selinux_denial_metadata" );
157
+ std::string line;
158
+ // allocate a map for the static map pointer in logParse to keep track of,
159
+ // this function only runs once
160
+ std::map<std::string, std::string> denial_to_bug;
161
+ if (bug_file.good ()) {
162
+ std::string scontext;
163
+ std::string tcontext;
164
+ std::string tclass;
165
+ std::string bug_num;
166
+ while (std::getline (bug_file, line)) {
167
+ std::stringstream split_line (line);
168
+ split_line >> scontext >> tcontext >> tclass >> bug_num;
169
+ denial_to_bug.emplace (scontext + tcontext + tclass, bug_num);
170
+ }
171
+ }
172
+ return denial_to_bug;
173
+ }
174
+
175
+ std::string LogAudit::denialParse (const std::string& denial, char terminator,
176
+ const std::string& search_term) {
177
+ size_t start_index = denial.find (search_term);
178
+ if (start_index != std::string::npos) {
179
+ start_index += search_term.length ();
180
+ return denial.substr (
181
+ start_index, denial.find (terminator, start_index) - start_index);
182
+ }
183
+ return " " ;
184
+ }
185
+
186
+ void LogAudit::logParse (const std::string& string, std::string* bug_num) {
187
+ if (!__android_log_is_debuggable ()) {
188
+ bug_num->assign (" " );
189
+ return ;
190
+ }
191
+ static std::map<std::string, std::string> denial_to_bug =
192
+ populateDenialMap ();
193
+ std::string scontext = denialParse (string, ' :' , " scontext=u:object_r:" );
194
+ std::string tcontext = denialParse (string, ' :' , " tcontext=u:object_r:" );
195
+ std::string tclass = denialParse (string, ' ' , " tclass=" );
196
+ if (scontext.empty ()) {
197
+ scontext = denialParse (string, ' :' , " scontext=u:r:" );
198
+ }
199
+ if (tcontext.empty ()) {
200
+ tcontext = denialParse (string, ' :' , " tcontext=u:r:" );
201
+ }
202
+ auto search = denial_to_bug.find (scontext + tcontext + tclass);
203
+ if (search != denial_to_bug.end ()) {
204
+ bug_num->assign (" b/" + search->second );
205
+ } else {
206
+ bug_num->assign (" " );
207
+ }
208
+ }
209
+
141
210
int LogAudit::logPrint (const char * fmt, ...) {
142
211
if (fmt == NULL ) {
143
212
return -EINVAL;
@@ -153,7 +222,6 @@ int LogAudit::logPrint(const char* fmt, ...) {
153
222
if (rc < 0 ) {
154
223
return rc;
155
224
}
156
-
157
225
char * cp;
158
226
// Work around kernels missing
159
227
// https://github.com/torvalds/linux/commit/b8f89caafeb55fba75b74bea25adc4e4cd91be67
@@ -165,10 +233,10 @@ int LogAudit::logPrint(const char* fmt, ...) {
165
233
while ((cp = strstr (str, " " ))) {
166
234
memmove (cp, cp + 1 , strlen (cp + 1 ) + 1 );
167
235
}
168
-
169
236
bool info = strstr (str, " permissive=1" ) || strstr (str, " policy loaded " );
237
+ static std::string bug_metadata;
170
238
if ((fdDmesg >= 0 ) && initialized) {
171
- struct iovec iov[3 ];
239
+ struct iovec iov[4 ];
172
240
static const char log_info[] = { KMSG_PRIORITY (LOG_INFO) };
173
241
static const char log_warning[] = { KMSG_PRIORITY (LOG_WARNING) };
174
242
static const char newline[] = " \n " ;
@@ -197,19 +265,20 @@ int LogAudit::logPrint(const char* fmt, ...) {
197
265
}
198
266
if (!skip) {
199
267
static const char resume[] = " duplicate messages suppressed\n " ;
200
-
201
268
iov[0 ].iov_base = last_info ? const_cast <char *>(log_info)
202
269
: const_cast <char *>(log_warning);
203
270
iov[0 ].iov_len =
204
271
last_info ? sizeof (log_info) : sizeof (log_warning);
205
272
iov[1 ].iov_base = last_str;
206
273
iov[1 ].iov_len = strlen (last_str);
274
+ iov[2 ].iov_base = const_cast <char *>(bug_metadata.c_str ());
275
+ iov[2 ].iov_len = bug_metadata.length ();
207
276
if (count > 1 ) {
208
- iov[2 ].iov_base = const_cast <char *>(resume);
209
- iov[2 ].iov_len = strlen (resume);
277
+ iov[3 ].iov_base = const_cast <char *>(resume);
278
+ iov[3 ].iov_len = strlen (resume);
210
279
} else {
211
- iov[2 ].iov_base = const_cast <char *>(newline);
212
- iov[2 ].iov_len = strlen (newline);
280
+ iov[3 ].iov_base = const_cast <char *>(newline);
281
+ iov[3 ].iov_len = strlen (newline);
213
282
}
214
283
215
284
writev (fdDmesg, iov, arraysize (iov));
@@ -223,13 +292,16 @@ int LogAudit::logPrint(const char* fmt, ...) {
223
292
last_info = info;
224
293
}
225
294
if (count == 0 ) {
295
+ logParse (str, &bug_metadata);
226
296
iov[0 ].iov_base = info ? const_cast <char *>(log_info)
227
297
: const_cast <char *>(log_warning);
228
298
iov[0 ].iov_len = info ? sizeof (log_info) : sizeof (log_warning);
229
299
iov[1 ].iov_base = str;
230
300
iov[1 ].iov_len = strlen (str);
231
- iov[2 ].iov_base = const_cast <char *>(newline);
232
- iov[2 ].iov_len = strlen (newline);
301
+ iov[2 ].iov_base = const_cast <char *>(bug_metadata.c_str ());
302
+ iov[2 ].iov_len = bug_metadata.length ();
303
+ iov[3 ].iov_base = const_cast <char *>(newline);
304
+ iov[3 ].iov_len = strlen (newline);
233
305
234
306
writev (fdDmesg, iov, arraysize (iov));
235
307
}
@@ -285,24 +357,32 @@ int LogAudit::logPrint(const char* fmt, ...) {
285
357
286
358
// log to events
287
359
288
- size_t l = strnlen (str, LOGGER_ENTRY_MAX_PAYLOAD);
289
- size_t n = l + sizeof (android_log_event_string_t );
360
+ size_t str_len = strnlen (str, LOGGER_ENTRY_MAX_PAYLOAD);
361
+ if (((fdDmesg < 0 ) || !initialized) && !hasMetadata (str, str_len))
362
+ logParse (str, &bug_metadata);
363
+ str_len = (str_len + bug_metadata.length () <= LOGGER_ENTRY_MAX_PAYLOAD)
364
+ ? str_len + bug_metadata.length ()
365
+ : LOGGER_ENTRY_MAX_PAYLOAD;
366
+ size_t message_len = str_len + sizeof (android_log_event_string_t );
290
367
291
368
bool notify = false ;
292
369
293
370
if (events) { // begin scope for event buffer
294
- uint32_t buffer[(n + sizeof (uint32_t ) - 1 ) / sizeof (uint32_t )];
371
+ uint32_t buffer[(message_len + sizeof (uint32_t ) - 1 ) / sizeof (uint32_t )];
295
372
296
373
android_log_event_string_t * event =
297
374
reinterpret_cast <android_log_event_string_t *>(buffer);
298
375
event->header .tag = htole32 (AUDITD_LOG_TAG);
299
376
event->type = EVENT_TYPE_STRING;
300
- event->length = htole32 (l);
301
- memcpy (event->data , str, l);
302
-
303
- rc = logbuf->log (LOG_ID_EVENTS, now, uid, pid, tid,
304
- reinterpret_cast <char *>(event),
305
- (n <= USHRT_MAX) ? (unsigned short )n : USHRT_MAX);
377
+ event->length = htole32 (message_len);
378
+ memcpy (event->data , str, str_len - bug_metadata.length ());
379
+ memcpy (event->data + str_len - bug_metadata.length (),
380
+ bug_metadata.c_str (), bug_metadata.length ());
381
+
382
+ rc = logbuf->log (
383
+ LOG_ID_EVENTS, now, uid, pid, tid, reinterpret_cast <char *>(event),
384
+ (message_len <= USHRT_MAX) ? (unsigned short )message_len
385
+ : USHRT_MAX);
306
386
if (rc >= 0 ) {
307
387
notify = true ;
308
388
}
@@ -333,28 +413,31 @@ int LogAudit::logPrint(const char* fmt, ...) {
333
413
const char * ecomm = strchr (comm, ' "' );
334
414
if (ecomm) {
335
415
++ecomm;
336
- l = ecomm - comm;
416
+ str_len = ecomm - comm;
337
417
} else {
338
- l = strlen (comm) + 1 ;
418
+ str_len = strlen (comm) + 1 ;
339
419
ecomm = " " ;
340
420
}
341
- size_t b = estr - str;
342
- if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
343
- b = LOGGER_ENTRY_MAX_PAYLOAD;
421
+ size_t prefix_len = estr - str;
422
+ if (prefix_len > LOGGER_ENTRY_MAX_PAYLOAD) {
423
+ prefix_len = LOGGER_ENTRY_MAX_PAYLOAD;
344
424
}
345
- size_t e = strnlen (ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b );
346
- n = b + e + l + 2 ;
425
+ size_t suffix_len = strnlen (ecomm, LOGGER_ENTRY_MAX_PAYLOAD - prefix_len );
426
+ message_len = str_len + prefix_len + suffix_len + bug_metadata. length () + 2 ;
347
427
348
428
if (main) { // begin scope for main buffer
349
- char newstr[n ];
429
+ char newstr[message_len ];
350
430
351
431
*newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
352
- strlcpy (newstr + 1 , comm, l);
353
- strncpy (newstr + 1 + l, str, b);
354
- strncpy (newstr + 1 + l + b, ecomm, e);
432
+ strlcpy (newstr + 1 , comm, str_len);
433
+ strncpy (newstr + 1 + str_len, str, prefix_len);
434
+ strncpy (newstr + 1 + str_len + prefix_len, ecomm, suffix_len);
435
+ strncpy (newstr + 1 + str_len + prefix_len + suffix_len,
436
+ bug_metadata.c_str (), bug_metadata.length ());
355
437
356
438
rc = logbuf->log (LOG_ID_MAIN, now, uid, pid, tid, newstr,
357
- (n <= USHRT_MAX) ? (unsigned short )n : USHRT_MAX);
439
+ (message_len <= USHRT_MAX) ? (unsigned short )message_len
440
+ : USHRT_MAX);
358
441
359
442
if (rc >= 0 ) {
360
443
notify = true ;
@@ -368,7 +451,7 @@ int LogAudit::logPrint(const char* fmt, ...) {
368
451
if (notify) {
369
452
reader->notifyNewLog ();
370
453
if (rc < 0 ) {
371
- rc = n ;
454
+ rc = message_len ;
372
455
}
373
456
}
374
457
0 commit comments