blob: 143fb0429971d05ff2528736f2fe762e8f888bba [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 Salyzyn8daa9af2014-04-28 14:07:23 -070023#include <sys/prctl.h>
Mark Salyzyne9bebd02014-04-03 09:55:26 -070024#include <sys/uio.h>
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070025#include <syslog.h>
William Roberts29d238d2013-02-08 09:45:26 +090026
Mark Salyzynddda2122015-10-02 09:22:52 -070027#include <log/logger.h>
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070028#include <private/android_filesystem_config.h>
Mark Salyzyn29eb5702015-03-03 16:21:27 -080029#include <private/android_logger.h>
30
William Roberts29d238d2013-02-08 09:45:26 +090031#include "libaudit.h"
32#include "LogAudit.h"
Mark Salyzynae4d9282014-10-15 08:49:39 -070033#include "LogKlog.h"
William Roberts29d238d2013-02-08 09:45:26 +090034
Mark Salyzynccbadc62015-03-12 12:25:35 -070035#define KMSG_PRIORITY(PRI) \
36 '<', \
37 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
38 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, \
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070039 '>'
40
Mark Salyzyn77187782015-05-12 15:21:31 -070041LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg) :
42 SocketListener(getLogSocket(), false),
43 logbuf(buf),
44 reader(reader),
45 fdDmesg(fdDmesg),
46 initialized(false) {
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070047 static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
48 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
49 ' ', 's', 't', 'a', 'r', 't', '\n' };
Mark Salyzyneb06de72014-10-13 09:59:37 -070050 write(fdDmesg, auditd_message, sizeof(auditd_message));
William Roberts29d238d2013-02-08 09:45:26 +090051}
52
53bool LogAudit::onDataAvailable(SocketClient *cli) {
Mark Salyzyneb06de72014-10-13 09:59:37 -070054 if (!initialized) {
55 prctl(PR_SET_NAME, "logd.auditd");
56 initialized = true;
57 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070058
William Roberts29d238d2013-02-08 09:45:26 +090059 struct audit_message rep;
60
Mark Salyzyne0fa2912014-04-28 16:39:04 -070061 rep.nlh.nlmsg_type = 0;
62 rep.nlh.nlmsg_len = 0;
63 rep.data[0] = '\0';
64
William Roberts29d238d2013-02-08 09:45:26 +090065 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
66 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
67 return false;
68 }
69
Mark Salyzyneb06de72014-10-13 09:59:37 -070070 logPrint("type=%d %.*s",
71 rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts29d238d2013-02-08 09:45:26 +090072
73 return true;
74}
75
William Roberts29d238d2013-02-08 09:45:26 +090076int LogAudit::logPrint(const char *fmt, ...) {
77 if (fmt == NULL) {
78 return -EINVAL;
79 }
80
81 va_list args;
82
83 char *str = NULL;
84 va_start(args, fmt);
85 int rc = vasprintf(&str, fmt, args);
86 va_end(args);
87
88 if (rc < 0) {
89 return rc;
90 }
91
Mark Salyzyne4369d62014-05-27 10:06:34 -070092 char *cp;
93 while ((cp = strstr(str, " "))) {
94 memmove(cp, cp + 1, strlen(cp + 1) + 1);
95 }
96
Mark Salyzyn6bdeee02014-09-19 11:59:42 -070097 bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
Mark Salyzyneb06de72014-10-13 09:59:37 -070098 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -070099 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700100 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
101 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700102
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700103 iov[0].iov_base = info ? const_cast<char *>(log_info)
104 : const_cast<char *>(log_warning);
105 iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700106 iov[1].iov_base = str;
107 iov[1].iov_len = strlen(str);
108 iov[2].iov_base = const_cast<char *>("\n");
109 iov[2].iov_len = 1;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700110
111 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
112 }
113
William Roberts29d238d2013-02-08 09:45:26 +0900114 pid_t pid = getpid();
115 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700116 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900117 log_time now;
118
119 static const char audit_str[] = " audit(";
120 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900121 if (timeptr
122 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
123 && (*cp == ':')) {
124 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700125 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700126 if (!isMonotonic()) {
127 if (android::isMonotonic(now)) {
128 LogKlog::convertMonotonicToReal(now);
129 }
130 } else {
131 if (!android::isMonotonic(now)) {
132 LogKlog::convertRealToMonotonic(now);
133 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700134 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700135 } else if (isMonotonic()) {
136 now = log_time(CLOCK_MONOTONIC);
William Roberts29d238d2013-02-08 09:45:26 +0900137 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700138 now = log_time(CLOCK_REALTIME);
William Roberts29d238d2013-02-08 09:45:26 +0900139 }
140
141 static const char pid_str[] = " pid=";
142 char *pidptr = strstr(str, pid_str);
143 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
144 cp = pidptr + sizeof(pid_str) - 1;
145 pid = 0;
146 while (isdigit(*cp)) {
147 pid = (pid * 10) + (*cp - '0');
148 ++cp;
149 }
150 tid = pid;
Mark Salyzyned777e92015-06-24 16:22:54 -0700151 logbuf->lock();
William Roberts29d238d2013-02-08 09:45:26 +0900152 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700153 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700154 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900155 }
156
Mark Salyzyne4369d62014-05-27 10:06:34 -0700157 // log to events
158
Mark Salyzynddda2122015-10-02 09:22:52 -0700159 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800160 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700161
162 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900163
Mark Salyzynddda2122015-10-02 09:22:52 -0700164 { // begin scope for event buffer
165 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
166
167 android_log_event_string_t *event
168 = reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800169 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700170 event->type = EVENT_TYPE_STRING;
171 event->length = htole32(l);
172 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700173
Mark Salyzyn202e1532015-02-09 08:21:05 -0800174 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
175 reinterpret_cast<char *>(event),
176 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800177 if (rc >= 0) {
178 notify = true;
179 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700180 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900181 }
182
Mark Salyzyne4369d62014-05-27 10:06:34 -0700183 // log to main
184
185 static const char comm_str[] = " comm=\"";
186 const char *comm = strstr(str, comm_str);
187 const char *estr = str + strlen(str);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700188 const char *commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700189 if (comm) {
190 estr = comm;
191 comm += sizeof(comm_str) - 1;
192 } else if (pid == getpid()) {
193 pid = tid;
194 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700195 } else {
196 logbuf->lock();
197 comm = commfree = logbuf->pidToName(pid);
198 logbuf->unlock();
199 if (!comm) {
200 comm = "unknown";
201 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700202 }
203
204 const char *ecomm = strchr(comm, '"');
205 if (ecomm) {
206 ++ecomm;
207 l = ecomm - comm;
208 } else {
209 l = strlen(comm) + 1;
210 ecomm = "";
211 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700212 size_t b = estr - str;
213 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
214 b = LOGGER_ENTRY_MAX_PAYLOAD;
215 }
216 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
217 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700218
Mark Salyzynddda2122015-10-02 09:22:52 -0700219 { // begin scope for main buffer
220 char newstr[n];
221
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700222 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700223 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700224 strncpy(newstr + 1 + l, str, b);
225 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700226
Mark Salyzyn202e1532015-02-09 08:21:05 -0800227 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
228 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700229
Mark Salyzyn202e1532015-02-09 08:21:05 -0800230 if (rc >= 0) {
231 notify = true;
232 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700233 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700234 }
235
Mark Salyzyn758058f2015-08-21 16:44:30 -0700236 free(const_cast<char *>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900237 free(str);
238
Mark Salyzyne4369d62014-05-27 10:06:34 -0700239 if (notify) {
240 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800241 if (rc < 0) {
242 rc = n;
243 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700244 }
William Roberts29d238d2013-02-08 09:45:26 +0900245
246 return rc;
247}
248
Mark Salyzyn151beac2015-09-04 11:37:42 -0700249int LogAudit::log(char *buf, size_t len) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700250 char *audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700251 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700252 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900253 }
254
Mark Salyzyneb06de72014-10-13 09:59:37 -0700255 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900256
Mark Salyzyneb06de72014-10-13 09:59:37 -0700257 int rc;
258 char *type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700259 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700260 rc = logPrint("%s %s", type, audit + 1);
261 } else {
262 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900263 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700264 *audit = ' ';
265 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900266}
267
268int LogAudit::getLogSocket() {
269 int fd = audit_open();
270 if (fd < 0) {
271 return fd;
272 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800273 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900274 audit_close(fd);
275 fd = -1;
276 }
277 return fd;
278}