blob: ee38d1c1a53ece47305c657816ee57027d7d9a21 [file] [log] [blame]
William Roberts29d238d2013-02-08 09:45:26 +09001/*
2 * Copyright (C) 2014 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#include <ctype.h>
Mark Salyzyn29eb5702015-03-03 16:21:27 -080018#include <endian.h>
William Roberts29d238d2013-02-08 09:45:26 +090019#include <errno.h>
Mark Salyzyne0fa2912014-04-28 16:39:04 -070020#include <limits.h>
William Roberts29d238d2013-02-08 09:45:26 +090021#include <stdarg.h>
22#include <stdlib.h>
Mark Salyzyn317bfb92016-02-23 08:55:43 -080023#include <string.h>
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070024#include <sys/prctl.h>
Mark Salyzyne9bebd02014-04-03 09:55:26 -070025#include <sys/uio.h>
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070026#include <syslog.h>
William Roberts29d238d2013-02-08 09:45:26 +090027
Mark Salyzync8d31942016-11-03 10:29:23 -070028#include <android-base/macros.h>
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070029#include <private/android_filesystem_config.h>
Mark Salyzyn29eb5702015-03-03 16:21:27 -080030#include <private/android_logger.h>
31
William Roberts29d238d2013-02-08 09:45:26 +090032#include "LogAudit.h"
Mark Salyzyn317bfb92016-02-23 08:55:43 -080033#include "LogBuffer.h"
Mark Salyzynae4d9282014-10-15 08:49:39 -070034#include "LogKlog.h"
Mark Salyzyn317bfb92016-02-23 08:55:43 -080035#include "LogReader.h"
Mark Salyzyna2c02222016-12-13 10:31:29 -080036#include "LogUtils.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080037#include "libaudit.h"
William Roberts29d238d2013-02-08 09:45:26 +090038
Mark Salyzyn501c3732017-03-10 14:31:54 -080039#define KMSG_PRIORITY(PRI) \
40 '<', '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
41 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, '>'
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070042
Mark Salyzyn501c3732017-03-10 14:31:54 -080043LogAudit::LogAudit(LogBuffer* buf, LogReader* reader, int fdDmesg)
44 : SocketListener(mSock = getLogSocket(), false),
45 logbuf(buf),
46 reader(reader),
47 fdDmesg(fdDmesg),
48 main(__android_logger_property_get_bool("ro.logd.auditd.main",
49 BOOL_DEFAULT_TRUE)),
50 events(__android_logger_property_get_bool("ro.logd.auditd.events",
Mark Salyzynce80da32016-12-29 15:16:06 -080051 BOOL_DEFAULT_TRUE)),
Mark Salyzyn501c3732017-03-10 14:31:54 -080052 initialized(false),
53 tooFast(false) {
Sami Tolvanena742d102016-06-14 18:04:43 +000054 static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
Mark Salyzyn501c3732017-03-10 14:31:54 -080055 'l',
56 'o',
57 'g',
58 'd',
59 '.',
60 'a',
61 'u',
62 'd',
63 'i',
64 't',
65 'd',
66 ':',
67 ' ',
68 's',
69 't',
70 'a',
71 'r',
72 't',
73 '\n' };
Sami Tolvanena742d102016-06-14 18:04:43 +000074 write(fdDmesg, auditd_message, sizeof(auditd_message));
William Roberts29d238d2013-02-08 09:45:26 +090075}
76
Mark Salyzyn247d6822017-01-03 14:00:19 -080077void LogAudit::checkRateLimit() {
Mark Salyzyn247d6822017-01-03 14:00:19 -080078 // trim list for AUDIT_RATE_LIMIT_BURST_DURATION of history
79 log_time oldest(AUDIT_RATE_LIMIT_BURST_DURATION, 0);
80 bucket.emplace(android_log_clockid());
81 oldest = bucket.back() - oldest;
82 while (bucket.front() < oldest) bucket.pop();
83
84 static const size_t upperThreshold =
85 ((AUDIT_RATE_LIMIT_BURST_DURATION *
Mark Salyzyn501c3732017-03-10 14:31:54 -080086 (AUDIT_RATE_LIMIT_DEFAULT + AUDIT_RATE_LIMIT_MAX)) +
87 1) /
88 2;
Mark Salyzyn247d6822017-01-03 14:00:19 -080089 if (bucket.size() >= upperThreshold) {
90 // Hit peak, slow down source
91 if (!tooFast) {
92 tooFast = true;
93 audit_rate_limit(mSock, AUDIT_RATE_LIMIT_MAX);
94 }
95
96 // We do not need to hold on to the full set of timing data history,
97 // let's ensure it does not grow without bounds. This also ensures
98 // that std::dequeue underneath behaves almost like a ring buffer.
99 do {
100 bucket.pop();
101 } while (bucket.size() >= upperThreshold);
102 return;
103 }
104
105 if (!tooFast) return;
106
Mark Salyzyn501c3732017-03-10 14:31:54 -0800107 static const size_t lowerThreshold =
108 AUDIT_RATE_LIMIT_BURST_DURATION * AUDIT_RATE_LIMIT_MAX;
Mark Salyzyn247d6822017-01-03 14:00:19 -0800109
110 if (bucket.size() >= lowerThreshold) return;
111
112 tooFast = false;
113 // Went below max sustained rate, allow source to speed up
114 audit_rate_limit(mSock, AUDIT_RATE_LIMIT_DEFAULT);
115}
116
Mark Salyzyn501c3732017-03-10 14:31:54 -0800117bool LogAudit::onDataAvailable(SocketClient* cli) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700118 if (!initialized) {
119 prctl(PR_SET_NAME, "logd.auditd");
120 initialized = true;
121 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -0700122
Mark Salyzyn247d6822017-01-03 14:00:19 -0800123 checkRateLimit();
124
William Roberts29d238d2013-02-08 09:45:26 +0900125 struct audit_message rep;
126
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700127 rep.nlh.nlmsg_type = 0;
128 rep.nlh.nlmsg_len = 0;
129 rep.data[0] = '\0';
130
William Roberts29d238d2013-02-08 09:45:26 +0900131 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
132 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
133 return false;
134 }
135
Mark Salyzyn247d6822017-01-03 14:00:19 -0800136 logPrint("type=%d %.*s", rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts29d238d2013-02-08 09:45:26 +0900137
138 return true;
139}
140
Mark Salyzyn501c3732017-03-10 14:31:54 -0800141int LogAudit::logPrint(const char* fmt, ...) {
William Roberts29d238d2013-02-08 09:45:26 +0900142 if (fmt == NULL) {
143 return -EINVAL;
144 }
145
146 va_list args;
147
Mark Salyzyn501c3732017-03-10 14:31:54 -0800148 char* str = NULL;
William Roberts29d238d2013-02-08 09:45:26 +0900149 va_start(args, fmt);
150 int rc = vasprintf(&str, fmt, args);
151 va_end(args);
152
153 if (rc < 0) {
154 return rc;
155 }
156
Mark Salyzyn501c3732017-03-10 14:31:54 -0800157 char* cp;
Nick Kralevich2e588672017-01-03 10:35:34 -0800158 // Work around kernels missing
159 // https://github.com/torvalds/linux/commit/b8f89caafeb55fba75b74bea25adc4e4cd91be67
160 // Such kernels improperly add newlines inside audit messages.
161 while ((cp = strchr(str, '\n'))) {
162 *cp = ' ';
163 }
164
Mark Salyzyne4369d62014-05-27 10:06:34 -0700165 while ((cp = strstr(str, " "))) {
166 memmove(cp, cp + 1, strlen(cp + 1) + 1);
167 }
168
Sami Tolvanena742d102016-06-14 18:04:43 +0000169 bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
Mark Salyzyneb06de72014-10-13 09:59:37 -0700170 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700171 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700172 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
173 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700174 static const char newline[] = "\n";
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700175
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700176 // Dedupe messages, checking for identical messages starting with avc:
177 static unsigned count;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800178 static char* last_str;
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700179 static bool last_info;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700180
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700181 if (last_str != NULL) {
182 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800183 char* avcl = strstr(last_str, avc);
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700184 bool skip = false;
185
186 if (avcl) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800187 char* avcr = strstr(str, avc);
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700188
Mark Salyzyn501c3732017-03-10 14:31:54 -0800189 skip = avcr &&
190 !fastcmp<strcmp>(avcl + strlen(avc), avcr + strlen(avc));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700191 if (skip) {
192 ++count;
193 free(last_str);
194 last_str = strdup(str);
195 last_info = info;
196 }
197 }
198 if (!skip) {
199 static const char resume[] = " duplicate messages suppressed\n";
200
Mark Salyzyn501c3732017-03-10 14:31:54 -0800201 iov[0].iov_base = last_info ? const_cast<char*>(log_info)
202 : const_cast<char*>(log_warning);
203 iov[0].iov_len =
204 last_info ? sizeof(log_info) : sizeof(log_warning);
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700205 iov[1].iov_base = last_str;
206 iov[1].iov_len = strlen(last_str);
207 if (count > 1) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800208 iov[2].iov_base = const_cast<char*>(resume);
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700209 iov[2].iov_len = strlen(resume);
210 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800211 iov[2].iov_base = const_cast<char*>(newline);
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700212 iov[2].iov_len = strlen(newline);
213 }
214
Mark Salyzync8d31942016-11-03 10:29:23 -0700215 writev(fdDmesg, iov, arraysize(iov));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700216 free(last_str);
217 last_str = NULL;
218 }
219 }
220 if (last_str == NULL) {
221 count = 0;
222 last_str = strdup(str);
223 last_info = info;
224 }
225 if (count == 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800226 iov[0].iov_base = info ? const_cast<char*>(log_info)
227 : const_cast<char*>(log_warning);
228 iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700229 iov[1].iov_base = str;
230 iov[1].iov_len = strlen(str);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800231 iov[2].iov_base = const_cast<char*>(newline);
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700232 iov[2].iov_len = strlen(newline);
233
Mark Salyzync8d31942016-11-03 10:29:23 -0700234 writev(fdDmesg, iov, arraysize(iov));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700235 }
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700236 }
237
Mark Salyzynce80da32016-12-29 15:16:06 -0800238 if (!main && !events) {
239 free(str);
240 return 0;
241 }
242
William Roberts29d238d2013-02-08 09:45:26 +0900243 pid_t pid = getpid();
244 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700245 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900246 log_time now;
247
248 static const char audit_str[] = " audit(";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800249 char* timeptr = strstr(str, audit_str);
250 if (timeptr &&
251 ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q"))) &&
252 (*cp == ':')) {
William Roberts29d238d2013-02-08 09:45:26 +0900253 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700254 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700255 if (!isMonotonic()) {
256 if (android::isMonotonic(now)) {
257 LogKlog::convertMonotonicToReal(now);
258 }
259 } else {
260 if (!android::isMonotonic(now)) {
261 LogKlog::convertRealToMonotonic(now);
262 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700263 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700264 } else if (isMonotonic()) {
265 now = log_time(CLOCK_MONOTONIC);
William Roberts29d238d2013-02-08 09:45:26 +0900266 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700267 now = log_time(CLOCK_REALTIME);
William Roberts29d238d2013-02-08 09:45:26 +0900268 }
269
270 static const char pid_str[] = " pid=";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800271 char* pidptr = strstr(str, pid_str);
William Roberts29d238d2013-02-08 09:45:26 +0900272 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
273 cp = pidptr + sizeof(pid_str) - 1;
274 pid = 0;
275 while (isdigit(*cp)) {
276 pid = (pid * 10) + (*cp - '0');
277 ++cp;
278 }
279 tid = pid;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700280 logbuf->wrlock();
William Roberts29d238d2013-02-08 09:45:26 +0900281 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700282 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700283 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900284 }
285
Mark Salyzyne4369d62014-05-27 10:06:34 -0700286 // log to events
287
Mark Salyzynddda2122015-10-02 09:22:52 -0700288 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800289 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700290
291 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900292
Mark Salyzyn501c3732017-03-10 14:31:54 -0800293 if (events) { // begin scope for event buffer
Mark Salyzynddda2122015-10-02 09:22:52 -0700294 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
295
Mark Salyzyn501c3732017-03-10 14:31:54 -0800296 android_log_event_string_t* event =
297 reinterpret_cast<android_log_event_string_t*>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800298 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700299 event->type = EVENT_TYPE_STRING;
300 event->length = htole32(l);
301 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700302
Mark Salyzyn202e1532015-02-09 08:21:05 -0800303 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
Mark Salyzyn501c3732017-03-10 14:31:54 -0800304 reinterpret_cast<char*>(event),
305 (n <= USHRT_MAX) ? (unsigned short)n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800306 if (rc >= 0) {
307 notify = true;
308 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700309 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900310 }
311
Mark Salyzyne4369d62014-05-27 10:06:34 -0700312 // log to main
313
314 static const char comm_str[] = " comm=\"";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800315 const char* comm = strstr(str, comm_str);
316 const char* estr = str + strlen(str);
317 const char* commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700318 if (comm) {
319 estr = comm;
320 comm += sizeof(comm_str) - 1;
321 } else if (pid == getpid()) {
322 pid = tid;
323 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700324 } else {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700325 logbuf->wrlock();
Mark Salyzyned777e92015-06-24 16:22:54 -0700326 comm = commfree = logbuf->pidToName(pid);
327 logbuf->unlock();
328 if (!comm) {
329 comm = "unknown";
330 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700331 }
332
Mark Salyzyn501c3732017-03-10 14:31:54 -0800333 const char* ecomm = strchr(comm, '"');
Mark Salyzyne4369d62014-05-27 10:06:34 -0700334 if (ecomm) {
335 ++ecomm;
336 l = ecomm - comm;
337 } else {
338 l = strlen(comm) + 1;
339 ecomm = "";
340 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700341 size_t b = estr - str;
342 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
343 b = LOGGER_ENTRY_MAX_PAYLOAD;
344 }
345 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
346 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700347
Mark Salyzyn501c3732017-03-10 14:31:54 -0800348 if (main) { // begin scope for main buffer
Mark Salyzynddda2122015-10-02 09:22:52 -0700349 char newstr[n];
350
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700351 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700352 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700353 strncpy(newstr + 1 + l, str, b);
354 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700355
Mark Salyzyn202e1532015-02-09 08:21:05 -0800356 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
Mark Salyzyn501c3732017-03-10 14:31:54 -0800357 (n <= USHRT_MAX) ? (unsigned short)n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700358
Mark Salyzyn202e1532015-02-09 08:21:05 -0800359 if (rc >= 0) {
360 notify = true;
361 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700362 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700363 }
364
Mark Salyzyn501c3732017-03-10 14:31:54 -0800365 free(const_cast<char*>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900366 free(str);
367
Mark Salyzyne4369d62014-05-27 10:06:34 -0700368 if (notify) {
369 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800370 if (rc < 0) {
371 rc = n;
372 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700373 }
William Roberts29d238d2013-02-08 09:45:26 +0900374
375 return rc;
376}
377
Mark Salyzyn501c3732017-03-10 14:31:54 -0800378int LogAudit::log(char* buf, size_t len) {
379 char* audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700380 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700381 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900382 }
383
Mark Salyzyneb06de72014-10-13 09:59:37 -0700384 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900385
Mark Salyzyneb06de72014-10-13 09:59:37 -0700386 int rc;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800387 char* type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700388 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700389 rc = logPrint("%s %s", type, audit + 1);
390 } else {
391 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900392 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700393 *audit = ' ';
394 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900395}
396
397int LogAudit::getLogSocket() {
398 int fd = audit_open();
399 if (fd < 0) {
400 return fd;
401 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800402 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900403 audit_close(fd);
404 fd = -1;
405 }
Mark Salyzyn247d6822017-01-03 14:00:19 -0800406 (void)audit_rate_limit(fd, AUDIT_RATE_LIMIT_DEFAULT);
William Roberts29d238d2013-02-08 09:45:26 +0900407 return fd;
408}