blob: c26716d550542c30afcfd97a31a0dd37210b73bc [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),
Mark Salyzynce80da32016-12-29 15:16:06 -080050 main(__android_logger_property_get_bool("ro.logd.auditd.main",
51 BOOL_DEFAULT_TRUE)),
52 events(__android_logger_property_get_bool("ro.logd.auditd.events",
53 BOOL_DEFAULT_TRUE)),
Mark Salyzyn77187782015-05-12 15:21:31 -070054 initialized(false) {
Sami Tolvanena742d102016-06-14 18:04:43 +000055 static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
56 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
57 ' ', 's', 't', 'a', 'r', 't', '\n' };
58 write(fdDmesg, auditd_message, sizeof(auditd_message));
William Roberts29d238d2013-02-08 09:45:26 +090059}
60
61bool LogAudit::onDataAvailable(SocketClient *cli) {
Mark Salyzyneb06de72014-10-13 09:59:37 -070062 if (!initialized) {
63 prctl(PR_SET_NAME, "logd.auditd");
64 initialized = true;
65 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070066
William Roberts29d238d2013-02-08 09:45:26 +090067 struct audit_message rep;
68
Mark Salyzyne0fa2912014-04-28 16:39:04 -070069 rep.nlh.nlmsg_type = 0;
70 rep.nlh.nlmsg_len = 0;
71 rep.data[0] = '\0';
72
William Roberts29d238d2013-02-08 09:45:26 +090073 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
74 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
75 return false;
76 }
77
Mark Salyzyneb06de72014-10-13 09:59:37 -070078 logPrint("type=%d %.*s",
79 rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts29d238d2013-02-08 09:45:26 +090080
81 return true;
82}
83
William Roberts29d238d2013-02-08 09:45:26 +090084int LogAudit::logPrint(const char *fmt, ...) {
85 if (fmt == NULL) {
86 return -EINVAL;
87 }
88
89 va_list args;
90
91 char *str = NULL;
92 va_start(args, fmt);
93 int rc = vasprintf(&str, fmt, args);
94 va_end(args);
95
96 if (rc < 0) {
97 return rc;
98 }
99
Mark Salyzyne4369d62014-05-27 10:06:34 -0700100 char *cp;
101 while ((cp = strstr(str, " "))) {
102 memmove(cp, cp + 1, strlen(cp + 1) + 1);
103 }
104
Sami Tolvanena742d102016-06-14 18:04:43 +0000105 bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
Mark Salyzyneb06de72014-10-13 09:59:37 -0700106 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700107 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700108 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
109 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700110 static const char newline[] = "\n";
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700111
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700112 // Dedupe messages, checking for identical messages starting with avc:
113 static unsigned count;
114 static char *last_str;
115 static bool last_info;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700116
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700117 if (last_str != NULL) {
118 static const char avc[] = "): avc: ";
119 char *avcl = strstr(last_str, avc);
120 bool skip = false;
121
122 if (avcl) {
123 char *avcr = strstr(str, avc);
124
Mark Salyzyna2c02222016-12-13 10:31:29 -0800125 skip = avcr && !fastcmp<strcmp>(avcl + strlen(avc),
126 avcr + strlen(avc));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700127 if (skip) {
128 ++count;
129 free(last_str);
130 last_str = strdup(str);
131 last_info = info;
132 }
133 }
134 if (!skip) {
135 static const char resume[] = " duplicate messages suppressed\n";
136
137 iov[0].iov_base = last_info ?
138 const_cast<char *>(log_info) :
139 const_cast<char *>(log_warning);
140 iov[0].iov_len = last_info ?
141 sizeof(log_info) :
142 sizeof(log_warning);
143 iov[1].iov_base = last_str;
144 iov[1].iov_len = strlen(last_str);
145 if (count > 1) {
146 iov[2].iov_base = const_cast<char *>(resume);
147 iov[2].iov_len = strlen(resume);
148 } else {
149 iov[2].iov_base = const_cast<char *>(newline);
150 iov[2].iov_len = strlen(newline);
151 }
152
Mark Salyzync8d31942016-11-03 10:29:23 -0700153 writev(fdDmesg, iov, arraysize(iov));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700154 free(last_str);
155 last_str = NULL;
156 }
157 }
158 if (last_str == NULL) {
159 count = 0;
160 last_str = strdup(str);
161 last_info = info;
162 }
163 if (count == 0) {
164 iov[0].iov_base = info ?
165 const_cast<char *>(log_info) :
166 const_cast<char *>(log_warning);
167 iov[0].iov_len = info ?
168 sizeof(log_info) :
169 sizeof(log_warning);
170 iov[1].iov_base = str;
171 iov[1].iov_len = strlen(str);
172 iov[2].iov_base = const_cast<char *>(newline);
173 iov[2].iov_len = strlen(newline);
174
Mark Salyzync8d31942016-11-03 10:29:23 -0700175 writev(fdDmesg, iov, arraysize(iov));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700176 }
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700177 }
178
Mark Salyzynce80da32016-12-29 15:16:06 -0800179 if (!main && !events) {
180 free(str);
181 return 0;
182 }
183
William Roberts29d238d2013-02-08 09:45:26 +0900184 pid_t pid = getpid();
185 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700186 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900187 log_time now;
188
189 static const char audit_str[] = " audit(";
190 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900191 if (timeptr
192 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
193 && (*cp == ':')) {
194 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700195 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700196 if (!isMonotonic()) {
197 if (android::isMonotonic(now)) {
198 LogKlog::convertMonotonicToReal(now);
199 }
200 } else {
201 if (!android::isMonotonic(now)) {
202 LogKlog::convertRealToMonotonic(now);
203 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700204 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700205 } else if (isMonotonic()) {
206 now = log_time(CLOCK_MONOTONIC);
William Roberts29d238d2013-02-08 09:45:26 +0900207 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700208 now = log_time(CLOCK_REALTIME);
William Roberts29d238d2013-02-08 09:45:26 +0900209 }
210
211 static const char pid_str[] = " pid=";
212 char *pidptr = strstr(str, pid_str);
213 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
214 cp = pidptr + sizeof(pid_str) - 1;
215 pid = 0;
216 while (isdigit(*cp)) {
217 pid = (pid * 10) + (*cp - '0');
218 ++cp;
219 }
220 tid = pid;
Mark Salyzyned777e92015-06-24 16:22:54 -0700221 logbuf->lock();
William Roberts29d238d2013-02-08 09:45:26 +0900222 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700223 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700224 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900225 }
226
Mark Salyzyne4369d62014-05-27 10:06:34 -0700227 // log to events
228
Mark Salyzynddda2122015-10-02 09:22:52 -0700229 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800230 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700231
232 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900233
Mark Salyzynce80da32016-12-29 15:16:06 -0800234 if (events) { // begin scope for event buffer
Mark Salyzynddda2122015-10-02 09:22:52 -0700235 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
236
237 android_log_event_string_t *event
238 = reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800239 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700240 event->type = EVENT_TYPE_STRING;
241 event->length = htole32(l);
242 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700243
Mark Salyzyn202e1532015-02-09 08:21:05 -0800244 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
245 reinterpret_cast<char *>(event),
246 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800247 if (rc >= 0) {
248 notify = true;
249 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700250 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900251 }
252
Mark Salyzyne4369d62014-05-27 10:06:34 -0700253 // log to main
254
255 static const char comm_str[] = " comm=\"";
256 const char *comm = strstr(str, comm_str);
257 const char *estr = str + strlen(str);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700258 const char *commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700259 if (comm) {
260 estr = comm;
261 comm += sizeof(comm_str) - 1;
262 } else if (pid == getpid()) {
263 pid = tid;
264 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700265 } else {
266 logbuf->lock();
267 comm = commfree = logbuf->pidToName(pid);
268 logbuf->unlock();
269 if (!comm) {
270 comm = "unknown";
271 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700272 }
273
274 const char *ecomm = strchr(comm, '"');
275 if (ecomm) {
276 ++ecomm;
277 l = ecomm - comm;
278 } else {
279 l = strlen(comm) + 1;
280 ecomm = "";
281 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700282 size_t b = estr - str;
283 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
284 b = LOGGER_ENTRY_MAX_PAYLOAD;
285 }
286 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
287 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700288
Mark Salyzynce80da32016-12-29 15:16:06 -0800289 if (main) { // begin scope for main buffer
Mark Salyzynddda2122015-10-02 09:22:52 -0700290 char newstr[n];
291
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700292 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700293 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700294 strncpy(newstr + 1 + l, str, b);
295 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700296
Mark Salyzyn202e1532015-02-09 08:21:05 -0800297 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
298 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700299
Mark Salyzyn202e1532015-02-09 08:21:05 -0800300 if (rc >= 0) {
301 notify = true;
302 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700303 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700304 }
305
Mark Salyzyn758058f2015-08-21 16:44:30 -0700306 free(const_cast<char *>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900307 free(str);
308
Mark Salyzyne4369d62014-05-27 10:06:34 -0700309 if (notify) {
310 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800311 if (rc < 0) {
312 rc = n;
313 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700314 }
William Roberts29d238d2013-02-08 09:45:26 +0900315
316 return rc;
317}
318
Mark Salyzyn151beac2015-09-04 11:37:42 -0700319int LogAudit::log(char *buf, size_t len) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700320 char *audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700321 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700322 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900323 }
324
Mark Salyzyneb06de72014-10-13 09:59:37 -0700325 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900326
Mark Salyzyneb06de72014-10-13 09:59:37 -0700327 int rc;
328 char *type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700329 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700330 rc = logPrint("%s %s", type, audit + 1);
331 } else {
332 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900333 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700334 *audit = ' ';
335 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900336}
337
338int LogAudit::getLogSocket() {
339 int fd = audit_open();
340 if (fd < 0) {
341 return fd;
342 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800343 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900344 audit_close(fd);
345 fd = -1;
346 }
347 return fd;
348}