blob: 2f7cbfb1a7287a5d6eb823b464d80a1cf9f1e6d9 [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 Salyzynae4d9282014-10-15 08:49:39 -0700126 //
127 // We are either in 1970ish (MONOTONIC) or 2015+ish (REALTIME) so to
128 // differentiate without prejudice, we use 1980 to delineate, earlier
129 // is monotonic, later is real.
130 //
131# define EPOCH_PLUS_10_YEARS (10 * 1461 / 4 * 24 * 60 * 60)
132 if (now.tv_sec < EPOCH_PLUS_10_YEARS) {
133 LogKlog::convertMonotonicToReal(now);
134 }
William Roberts29d238d2013-02-08 09:45:26 +0900135 } else {
136 now.strptime("", ""); // side effect of setting CLOCK_REALTIME
137 }
138
139 static const char pid_str[] = " pid=";
140 char *pidptr = strstr(str, pid_str);
141 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
142 cp = pidptr + sizeof(pid_str) - 1;
143 pid = 0;
144 while (isdigit(*cp)) {
145 pid = (pid * 10) + (*cp - '0');
146 ++cp;
147 }
148 tid = pid;
Mark Salyzyned777e92015-06-24 16:22:54 -0700149 logbuf->lock();
William Roberts29d238d2013-02-08 09:45:26 +0900150 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700151 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700152 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900153 }
154
Mark Salyzyne4369d62014-05-27 10:06:34 -0700155 // log to events
156
Mark Salyzynddda2122015-10-02 09:22:52 -0700157 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800158 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700159
160 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900161
Mark Salyzynddda2122015-10-02 09:22:52 -0700162 { // begin scope for event buffer
163 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
164
165 android_log_event_string_t *event
166 = reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800167 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700168 event->type = EVENT_TYPE_STRING;
169 event->length = htole32(l);
170 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700171
Mark Salyzyn202e1532015-02-09 08:21:05 -0800172 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
173 reinterpret_cast<char *>(event),
174 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800175 if (rc >= 0) {
176 notify = true;
177 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700178 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900179 }
180
Mark Salyzyne4369d62014-05-27 10:06:34 -0700181 // log to main
182
183 static const char comm_str[] = " comm=\"";
184 const char *comm = strstr(str, comm_str);
185 const char *estr = str + strlen(str);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700186 const char *commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700187 if (comm) {
188 estr = comm;
189 comm += sizeof(comm_str) - 1;
190 } else if (pid == getpid()) {
191 pid = tid;
192 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700193 } else {
194 logbuf->lock();
195 comm = commfree = logbuf->pidToName(pid);
196 logbuf->unlock();
197 if (!comm) {
198 comm = "unknown";
199 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700200 }
201
202 const char *ecomm = strchr(comm, '"');
203 if (ecomm) {
204 ++ecomm;
205 l = ecomm - comm;
206 } else {
207 l = strlen(comm) + 1;
208 ecomm = "";
209 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700210 size_t b = estr - str;
211 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
212 b = LOGGER_ENTRY_MAX_PAYLOAD;
213 }
214 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
215 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700216
Mark Salyzynddda2122015-10-02 09:22:52 -0700217 { // begin scope for main buffer
218 char newstr[n];
219
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700220 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700221 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700222 strncpy(newstr + 1 + l, str, b);
223 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700224
Mark Salyzyn202e1532015-02-09 08:21:05 -0800225 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
226 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700227
Mark Salyzyn202e1532015-02-09 08:21:05 -0800228 if (rc >= 0) {
229 notify = true;
230 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700231 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700232 }
233
Mark Salyzyn758058f2015-08-21 16:44:30 -0700234 free(const_cast<char *>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900235 free(str);
236
Mark Salyzyne4369d62014-05-27 10:06:34 -0700237 if (notify) {
238 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800239 if (rc < 0) {
240 rc = n;
241 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700242 }
William Roberts29d238d2013-02-08 09:45:26 +0900243
244 return rc;
245}
246
Mark Salyzyn151beac2015-09-04 11:37:42 -0700247int LogAudit::log(char *buf, size_t len) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700248 char *audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700249 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700250 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900251 }
252
Mark Salyzyneb06de72014-10-13 09:59:37 -0700253 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900254
Mark Salyzyneb06de72014-10-13 09:59:37 -0700255 int rc;
256 char *type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700257 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700258 rc = logPrint("%s %s", type, audit + 1);
259 } else {
260 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900261 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700262 *audit = ' ';
263 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900264}
265
266int LogAudit::getLogSocket() {
267 int fd = audit_open();
268 if (fd < 0) {
269 return fd;
270 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800271 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900272 audit_close(fd);
273 fd = -1;
274 }
275 return fd;
276}