blob: aa05932b0134c3bb37eda7d0523caa5c836415ed [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;
97 while ((cp = strstr(str, " "))) {
98 memmove(cp, cp + 1, strlen(cp + 1) + 1);
99 }
100
Sami Tolvanena742d102016-06-14 18:04:43 +0000101 bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
Mark Salyzyneb06de72014-10-13 09:59:37 -0700102 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700103 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700104 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
105 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700106 static const char newline[] = "\n";
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700107
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700108 // Dedupe messages, checking for identical messages starting with avc:
109 static unsigned count;
110 static char *last_str;
111 static bool last_info;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700112
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700113 if (last_str != NULL) {
114 static const char avc[] = "): avc: ";
115 char *avcl = strstr(last_str, avc);
116 bool skip = false;
117
118 if (avcl) {
119 char *avcr = strstr(str, avc);
120
Mark Salyzyna2c02222016-12-13 10:31:29 -0800121 skip = avcr && !fastcmp<strcmp>(avcl + strlen(avc),
122 avcr + strlen(avc));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700123 if (skip) {
124 ++count;
125 free(last_str);
126 last_str = strdup(str);
127 last_info = info;
128 }
129 }
130 if (!skip) {
131 static const char resume[] = " duplicate messages suppressed\n";
132
133 iov[0].iov_base = last_info ?
134 const_cast<char *>(log_info) :
135 const_cast<char *>(log_warning);
136 iov[0].iov_len = last_info ?
137 sizeof(log_info) :
138 sizeof(log_warning);
139 iov[1].iov_base = last_str;
140 iov[1].iov_len = strlen(last_str);
141 if (count > 1) {
142 iov[2].iov_base = const_cast<char *>(resume);
143 iov[2].iov_len = strlen(resume);
144 } else {
145 iov[2].iov_base = const_cast<char *>(newline);
146 iov[2].iov_len = strlen(newline);
147 }
148
Mark Salyzync8d31942016-11-03 10:29:23 -0700149 writev(fdDmesg, iov, arraysize(iov));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700150 free(last_str);
151 last_str = NULL;
152 }
153 }
154 if (last_str == NULL) {
155 count = 0;
156 last_str = strdup(str);
157 last_info = info;
158 }
159 if (count == 0) {
160 iov[0].iov_base = info ?
161 const_cast<char *>(log_info) :
162 const_cast<char *>(log_warning);
163 iov[0].iov_len = info ?
164 sizeof(log_info) :
165 sizeof(log_warning);
166 iov[1].iov_base = str;
167 iov[1].iov_len = strlen(str);
168 iov[2].iov_base = const_cast<char *>(newline);
169 iov[2].iov_len = strlen(newline);
170
Mark Salyzync8d31942016-11-03 10:29:23 -0700171 writev(fdDmesg, iov, arraysize(iov));
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700172 }
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700173 }
174
William Roberts29d238d2013-02-08 09:45:26 +0900175 pid_t pid = getpid();
176 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700177 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900178 log_time now;
179
180 static const char audit_str[] = " audit(";
181 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900182 if (timeptr
183 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
184 && (*cp == ':')) {
185 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700186 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700187 if (!isMonotonic()) {
188 if (android::isMonotonic(now)) {
189 LogKlog::convertMonotonicToReal(now);
190 }
191 } else {
192 if (!android::isMonotonic(now)) {
193 LogKlog::convertRealToMonotonic(now);
194 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700195 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700196 } else if (isMonotonic()) {
197 now = log_time(CLOCK_MONOTONIC);
William Roberts29d238d2013-02-08 09:45:26 +0900198 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700199 now = log_time(CLOCK_REALTIME);
William Roberts29d238d2013-02-08 09:45:26 +0900200 }
201
202 static const char pid_str[] = " pid=";
203 char *pidptr = strstr(str, pid_str);
204 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
205 cp = pidptr + sizeof(pid_str) - 1;
206 pid = 0;
207 while (isdigit(*cp)) {
208 pid = (pid * 10) + (*cp - '0');
209 ++cp;
210 }
211 tid = pid;
Mark Salyzyned777e92015-06-24 16:22:54 -0700212 logbuf->lock();
William Roberts29d238d2013-02-08 09:45:26 +0900213 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700214 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700215 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900216 }
217
Mark Salyzyne4369d62014-05-27 10:06:34 -0700218 // log to events
219
Mark Salyzynddda2122015-10-02 09:22:52 -0700220 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800221 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700222
223 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900224
Mark Salyzynddda2122015-10-02 09:22:52 -0700225 { // begin scope for event buffer
226 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
227
228 android_log_event_string_t *event
229 = reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800230 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700231 event->type = EVENT_TYPE_STRING;
232 event->length = htole32(l);
233 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700234
Mark Salyzyn202e1532015-02-09 08:21:05 -0800235 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
236 reinterpret_cast<char *>(event),
237 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800238 if (rc >= 0) {
239 notify = true;
240 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700241 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900242 }
243
Mark Salyzyne4369d62014-05-27 10:06:34 -0700244 // log to main
245
246 static const char comm_str[] = " comm=\"";
247 const char *comm = strstr(str, comm_str);
248 const char *estr = str + strlen(str);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700249 const char *commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700250 if (comm) {
251 estr = comm;
252 comm += sizeof(comm_str) - 1;
253 } else if (pid == getpid()) {
254 pid = tid;
255 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700256 } else {
257 logbuf->lock();
258 comm = commfree = logbuf->pidToName(pid);
259 logbuf->unlock();
260 if (!comm) {
261 comm = "unknown";
262 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700263 }
264
265 const char *ecomm = strchr(comm, '"');
266 if (ecomm) {
267 ++ecomm;
268 l = ecomm - comm;
269 } else {
270 l = strlen(comm) + 1;
271 ecomm = "";
272 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700273 size_t b = estr - str;
274 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
275 b = LOGGER_ENTRY_MAX_PAYLOAD;
276 }
277 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
278 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700279
Mark Salyzynddda2122015-10-02 09:22:52 -0700280 { // begin scope for main buffer
281 char newstr[n];
282
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700283 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700284 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700285 strncpy(newstr + 1 + l, str, b);
286 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700287
Mark Salyzyn202e1532015-02-09 08:21:05 -0800288 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
289 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700290
Mark Salyzyn202e1532015-02-09 08:21:05 -0800291 if (rc >= 0) {
292 notify = true;
293 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700294 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700295 }
296
Mark Salyzyn758058f2015-08-21 16:44:30 -0700297 free(const_cast<char *>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900298 free(str);
299
Mark Salyzyne4369d62014-05-27 10:06:34 -0700300 if (notify) {
301 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800302 if (rc < 0) {
303 rc = n;
304 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700305 }
William Roberts29d238d2013-02-08 09:45:26 +0900306
307 return rc;
308}
309
Mark Salyzyn151beac2015-09-04 11:37:42 -0700310int LogAudit::log(char *buf, size_t len) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700311 char *audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700312 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700313 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900314 }
315
Mark Salyzyneb06de72014-10-13 09:59:37 -0700316 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900317
Mark Salyzyneb06de72014-10-13 09:59:37 -0700318 int rc;
319 char *type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700320 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700321 rc = logPrint("%s %s", type, audit + 1);
322 } else {
323 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900324 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700325 *audit = ' ';
326 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900327}
328
329int LogAudit::getLogSocket() {
330 int fd = audit_open();
331 if (fd < 0) {
332 return fd;
333 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800334 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900335 audit_close(fd);
336 fd = -1;
337 }
338 return fd;
339}