blob: 6f29ead542c4feced21f405e0bfad214ae6a30f2 [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 "libaudit.h"
33#include "LogAudit.h"
Mark Salyzyn317bfb92016-02-23 08:55:43 -080034#include "LogBuffer.h"
Mark Salyzynae4d9282014-10-15 08:49:39 -070035#include "LogKlog.h"
Mark Salyzyn317bfb92016-02-23 08:55:43 -080036#include "LogReader.h"
Mark Salyzyna2c02222016-12-13 10:31:29 -080037#include "LogUtils.h"
William Roberts29d238d2013-02-08 09:45:26 +090038
Mark Salyzynccbadc62015-03-12 12:25:35 -070039#define KMSG_PRIORITY(PRI) \
40 '<', \
41 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
42 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, \
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070043 '>'
44
Mark Salyzyn77187782015-05-12 15:21:31 -070045LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg) :
46 SocketListener(getLogSocket(), false),
47 logbuf(buf),
48 reader(reader),
49 fdDmesg(fdDmesg),
50 initialized(false) {
Sami Tolvanena742d102016-06-14 18:04:43 +000051 static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
52 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
53 ' ', 's', 't', 'a', 'r', 't', '\n' };
54 write(fdDmesg, auditd_message, sizeof(auditd_message));
William Roberts29d238d2013-02-08 09:45:26 +090055}
56
57bool LogAudit::onDataAvailable(SocketClient *cli) {
Mark Salyzyneb06de72014-10-13 09:59:37 -070058 if (!initialized) {
59 prctl(PR_SET_NAME, "logd.auditd");
60 initialized = true;
61 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070062
William Roberts29d238d2013-02-08 09:45:26 +090063 struct audit_message rep;
64
Mark Salyzyne0fa2912014-04-28 16:39:04 -070065 rep.nlh.nlmsg_type = 0;
66 rep.nlh.nlmsg_len = 0;
67 rep.data[0] = '\0';
68
William Roberts29d238d2013-02-08 09:45:26 +090069 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
70 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
71 return false;
72 }
73
Mark Salyzyneb06de72014-10-13 09:59:37 -070074 logPrint("type=%d %.*s",
75 rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts29d238d2013-02-08 09:45:26 +090076
77 return true;
78}
79
William Roberts29d238d2013-02-08 09:45:26 +090080int LogAudit::logPrint(const char *fmt, ...) {
81 if (fmt == NULL) {
82 return -EINVAL;
83 }
84
85 va_list args;
86
87 char *str = NULL;
88 va_start(args, fmt);
89 int rc = vasprintf(&str, fmt, args);
90 va_end(args);
91
92 if (rc < 0) {
93 return rc;
94 }
95
Mark Salyzyne4369d62014-05-27 10:06:34 -070096 char *cp;
Nick Kralevich2e588672017-01-03 10:35:34 -080097 // Work around kernels missing
98 // https://github.com/torvalds/linux/commit/b8f89caafeb55fba75b74bea25adc4e4cd91be67
99 // Such kernels improperly add newlines inside audit messages.
100 while ((cp = strchr(str, '\n'))) {
101 *cp = ' ';
102 }
103
Mark Salyzyne4369d62014-05-27 10:06:34 -0700104 while ((cp = strstr(str, " "))) {
105 memmove(cp, cp + 1, strlen(cp + 1) + 1);
106 }
107
Sami Tolvanena742d102016-06-14 18:04:43 +0000108 bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
Mark Salyzyneb06de72014-10-13 09:59:37 -0700109 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700110 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700111 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
112 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700113 static const char newline[] = "\n";
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700114
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700115 // Dedupe messages, checking for identical messages starting with avc:
116 static unsigned count;
117 static char *last_str;
118 static bool last_info;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700119
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700120 if (last_str != NULL) {
121 static const char avc[] = "): avc: ";
122 char *avcl = strstr(last_str, avc);
123 bool skip = false;
124
125 if (avcl) {
126 char *avcr = strstr(str, avc);
127
Mark Salyzyna2c02222016-12-13 10:31:29 -0800128 skip = avcr && !fastcmp<strcmp>(avcl + strlen(avc),
129 avcr + strlen(avc));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700130 if (skip) {
131 ++count;
132 free(last_str);
133 last_str = strdup(str);
134 last_info = info;
135 }
136 }
137 if (!skip) {
138 static const char resume[] = " duplicate messages suppressed\n";
139
140 iov[0].iov_base = last_info ?
141 const_cast<char *>(log_info) :
142 const_cast<char *>(log_warning);
143 iov[0].iov_len = last_info ?
144 sizeof(log_info) :
145 sizeof(log_warning);
146 iov[1].iov_base = last_str;
147 iov[1].iov_len = strlen(last_str);
148 if (count > 1) {
149 iov[2].iov_base = const_cast<char *>(resume);
150 iov[2].iov_len = strlen(resume);
151 } else {
152 iov[2].iov_base = const_cast<char *>(newline);
153 iov[2].iov_len = strlen(newline);
154 }
155
Mark Salyzync8d31942016-11-03 10:29:23 -0700156 writev(fdDmesg, iov, arraysize(iov));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700157 free(last_str);
158 last_str = NULL;
159 }
160 }
161 if (last_str == NULL) {
162 count = 0;
163 last_str = strdup(str);
164 last_info = info;
165 }
166 if (count == 0) {
167 iov[0].iov_base = info ?
168 const_cast<char *>(log_info) :
169 const_cast<char *>(log_warning);
170 iov[0].iov_len = info ?
171 sizeof(log_info) :
172 sizeof(log_warning);
173 iov[1].iov_base = str;
174 iov[1].iov_len = strlen(str);
175 iov[2].iov_base = const_cast<char *>(newline);
176 iov[2].iov_len = strlen(newline);
177
Mark Salyzync8d31942016-11-03 10:29:23 -0700178 writev(fdDmesg, iov, arraysize(iov));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700179 }
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700180 }
181
William Roberts29d238d2013-02-08 09:45:26 +0900182 pid_t pid = getpid();
183 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700184 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900185 log_time now;
186
187 static const char audit_str[] = " audit(";
188 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900189 if (timeptr
190 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
191 && (*cp == ':')) {
192 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700193 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700194 if (!isMonotonic()) {
195 if (android::isMonotonic(now)) {
196 LogKlog::convertMonotonicToReal(now);
197 }
198 } else {
199 if (!android::isMonotonic(now)) {
200 LogKlog::convertRealToMonotonic(now);
201 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700202 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700203 } else if (isMonotonic()) {
204 now = log_time(CLOCK_MONOTONIC);
William Roberts29d238d2013-02-08 09:45:26 +0900205 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700206 now = log_time(CLOCK_REALTIME);
William Roberts29d238d2013-02-08 09:45:26 +0900207 }
208
209 static const char pid_str[] = " pid=";
210 char *pidptr = strstr(str, pid_str);
211 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
212 cp = pidptr + sizeof(pid_str) - 1;
213 pid = 0;
214 while (isdigit(*cp)) {
215 pid = (pid * 10) + (*cp - '0');
216 ++cp;
217 }
218 tid = pid;
Mark Salyzyned777e92015-06-24 16:22:54 -0700219 logbuf->lock();
William Roberts29d238d2013-02-08 09:45:26 +0900220 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700221 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700222 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900223 }
224
Mark Salyzyne4369d62014-05-27 10:06:34 -0700225 // log to events
226
Mark Salyzynddda2122015-10-02 09:22:52 -0700227 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800228 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700229
230 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900231
Mark Salyzynddda2122015-10-02 09:22:52 -0700232 { // begin scope for event buffer
233 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
234
235 android_log_event_string_t *event
236 = reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800237 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700238 event->type = EVENT_TYPE_STRING;
239 event->length = htole32(l);
240 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700241
Mark Salyzyn202e1532015-02-09 08:21:05 -0800242 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
243 reinterpret_cast<char *>(event),
244 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800245 if (rc >= 0) {
246 notify = true;
247 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700248 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900249 }
250
Mark Salyzyne4369d62014-05-27 10:06:34 -0700251 // log to main
252
253 static const char comm_str[] = " comm=\"";
254 const char *comm = strstr(str, comm_str);
255 const char *estr = str + strlen(str);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700256 const char *commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700257 if (comm) {
258 estr = comm;
259 comm += sizeof(comm_str) - 1;
260 } else if (pid == getpid()) {
261 pid = tid;
262 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700263 } else {
264 logbuf->lock();
265 comm = commfree = logbuf->pidToName(pid);
266 logbuf->unlock();
267 if (!comm) {
268 comm = "unknown";
269 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700270 }
271
272 const char *ecomm = strchr(comm, '"');
273 if (ecomm) {
274 ++ecomm;
275 l = ecomm - comm;
276 } else {
277 l = strlen(comm) + 1;
278 ecomm = "";
279 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700280 size_t b = estr - str;
281 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
282 b = LOGGER_ENTRY_MAX_PAYLOAD;
283 }
284 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
285 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700286
Mark Salyzynddda2122015-10-02 09:22:52 -0700287 { // begin scope for main buffer
288 char newstr[n];
289
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700290 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700291 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700292 strncpy(newstr + 1 + l, str, b);
293 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700294
Mark Salyzyn202e1532015-02-09 08:21:05 -0800295 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
296 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700297
Mark Salyzyn202e1532015-02-09 08:21:05 -0800298 if (rc >= 0) {
299 notify = true;
300 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700301 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700302 }
303
Mark Salyzyn758058f2015-08-21 16:44:30 -0700304 free(const_cast<char *>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900305 free(str);
306
Mark Salyzyne4369d62014-05-27 10:06:34 -0700307 if (notify) {
308 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800309 if (rc < 0) {
310 rc = n;
311 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700312 }
William Roberts29d238d2013-02-08 09:45:26 +0900313
314 return rc;
315}
316
Mark Salyzyn151beac2015-09-04 11:37:42 -0700317int LogAudit::log(char *buf, size_t len) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700318 char *audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700319 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700320 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900321 }
322
Mark Salyzyneb06de72014-10-13 09:59:37 -0700323 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900324
Mark Salyzyneb06de72014-10-13 09:59:37 -0700325 int rc;
326 char *type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700327 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700328 rc = logPrint("%s %s", type, audit + 1);
329 } else {
330 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900331 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700332 *audit = ' ';
333 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900334}
335
336int LogAudit::getLogSocket() {
337 int fd = audit_open();
338 if (fd < 0) {
339 return fd;
340 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800341 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900342 audit_close(fd);
343 fd = -1;
344 }
345 return fd;
346}