blob: 24c3f52d99ef2db2c985b11073e53bf17a9cc621 [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 Salyzynddda2122015-10-02 09:22:52 -070028#include <log/logger.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"
William Roberts29d238d2013-02-08 09:45:26 +090037
Mark Salyzynccbadc62015-03-12 12:25:35 -070038#define KMSG_PRIORITY(PRI) \
39 '<', \
40 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
41 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, \
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070042 '>'
43
Mark Salyzyn77187782015-05-12 15:21:31 -070044LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg) :
45 SocketListener(getLogSocket(), false),
46 logbuf(buf),
47 reader(reader),
48 fdDmesg(fdDmesg),
49 initialized(false) {
Sami Tolvanena742d102016-06-14 18:04:43 +000050 static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
51 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
52 ' ', 's', 't', 'a', 'r', 't', '\n' };
53 write(fdDmesg, auditd_message, sizeof(auditd_message));
William Roberts29d238d2013-02-08 09:45:26 +090054}
55
56bool LogAudit::onDataAvailable(SocketClient *cli) {
Mark Salyzyneb06de72014-10-13 09:59:37 -070057 if (!initialized) {
58 prctl(PR_SET_NAME, "logd.auditd");
59 initialized = true;
60 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070061
William Roberts29d238d2013-02-08 09:45:26 +090062 struct audit_message rep;
63
Mark Salyzyne0fa2912014-04-28 16:39:04 -070064 rep.nlh.nlmsg_type = 0;
65 rep.nlh.nlmsg_len = 0;
66 rep.data[0] = '\0';
67
William Roberts29d238d2013-02-08 09:45:26 +090068 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
69 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
70 return false;
71 }
72
Mark Salyzyneb06de72014-10-13 09:59:37 -070073 logPrint("type=%d %.*s",
74 rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts29d238d2013-02-08 09:45:26 +090075
76 return true;
77}
78
William Roberts29d238d2013-02-08 09:45:26 +090079int LogAudit::logPrint(const char *fmt, ...) {
80 if (fmt == NULL) {
81 return -EINVAL;
82 }
83
84 va_list args;
85
86 char *str = NULL;
87 va_start(args, fmt);
88 int rc = vasprintf(&str, fmt, args);
89 va_end(args);
90
91 if (rc < 0) {
92 return rc;
93 }
94
Mark Salyzyne4369d62014-05-27 10:06:34 -070095 char *cp;
96 while ((cp = strstr(str, " "))) {
97 memmove(cp, cp + 1, strlen(cp + 1) + 1);
98 }
99
Sami Tolvanena742d102016-06-14 18:04:43 +0000100 bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
Mark Salyzyneb06de72014-10-13 09:59:37 -0700101 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700102 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700103 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
104 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700105
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700106 iov[0].iov_base = info ? const_cast<char *>(log_info)
107 : const_cast<char *>(log_warning);
108 iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700109 iov[1].iov_base = str;
110 iov[1].iov_len = strlen(str);
111 iov[2].iov_base = const_cast<char *>("\n");
112 iov[2].iov_len = 1;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700113
114 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
115 }
116
William Roberts29d238d2013-02-08 09:45:26 +0900117 pid_t pid = getpid();
118 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700119 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900120 log_time now;
121
122 static const char audit_str[] = " audit(";
123 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900124 if (timeptr
125 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
126 && (*cp == ':')) {
127 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700128 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700129 if (!isMonotonic()) {
130 if (android::isMonotonic(now)) {
131 LogKlog::convertMonotonicToReal(now);
132 }
133 } else {
134 if (!android::isMonotonic(now)) {
135 LogKlog::convertRealToMonotonic(now);
136 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700137 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700138 } else if (isMonotonic()) {
139 now = log_time(CLOCK_MONOTONIC);
William Roberts29d238d2013-02-08 09:45:26 +0900140 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700141 now = log_time(CLOCK_REALTIME);
William Roberts29d238d2013-02-08 09:45:26 +0900142 }
143
144 static const char pid_str[] = " pid=";
145 char *pidptr = strstr(str, pid_str);
146 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
147 cp = pidptr + sizeof(pid_str) - 1;
148 pid = 0;
149 while (isdigit(*cp)) {
150 pid = (pid * 10) + (*cp - '0');
151 ++cp;
152 }
153 tid = pid;
Mark Salyzyned777e92015-06-24 16:22:54 -0700154 logbuf->lock();
William Roberts29d238d2013-02-08 09:45:26 +0900155 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700156 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700157 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900158 }
159
Mark Salyzyne4369d62014-05-27 10:06:34 -0700160 // log to events
161
Mark Salyzynddda2122015-10-02 09:22:52 -0700162 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800163 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700164
165 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900166
Mark Salyzynddda2122015-10-02 09:22:52 -0700167 { // begin scope for event buffer
168 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
169
170 android_log_event_string_t *event
171 = reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800172 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700173 event->type = EVENT_TYPE_STRING;
174 event->length = htole32(l);
175 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700176
Mark Salyzyn202e1532015-02-09 08:21:05 -0800177 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
178 reinterpret_cast<char *>(event),
179 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800180 if (rc >= 0) {
181 notify = true;
182 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700183 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900184 }
185
Mark Salyzyne4369d62014-05-27 10:06:34 -0700186 // log to main
187
188 static const char comm_str[] = " comm=\"";
189 const char *comm = strstr(str, comm_str);
190 const char *estr = str + strlen(str);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700191 const char *commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700192 if (comm) {
193 estr = comm;
194 comm += sizeof(comm_str) - 1;
195 } else if (pid == getpid()) {
196 pid = tid;
197 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700198 } else {
199 logbuf->lock();
200 comm = commfree = logbuf->pidToName(pid);
201 logbuf->unlock();
202 if (!comm) {
203 comm = "unknown";
204 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700205 }
206
207 const char *ecomm = strchr(comm, '"');
208 if (ecomm) {
209 ++ecomm;
210 l = ecomm - comm;
211 } else {
212 l = strlen(comm) + 1;
213 ecomm = "";
214 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700215 size_t b = estr - str;
216 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
217 b = LOGGER_ENTRY_MAX_PAYLOAD;
218 }
219 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
220 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700221
Mark Salyzynddda2122015-10-02 09:22:52 -0700222 { // begin scope for main buffer
223 char newstr[n];
224
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700225 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700226 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700227 strncpy(newstr + 1 + l, str, b);
228 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700229
Mark Salyzyn202e1532015-02-09 08:21:05 -0800230 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
231 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700232
Mark Salyzyn202e1532015-02-09 08:21:05 -0800233 if (rc >= 0) {
234 notify = true;
235 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700236 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700237 }
238
Mark Salyzyn758058f2015-08-21 16:44:30 -0700239 free(const_cast<char *>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900240 free(str);
241
Mark Salyzyne4369d62014-05-27 10:06:34 -0700242 if (notify) {
243 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800244 if (rc < 0) {
245 rc = n;
246 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700247 }
William Roberts29d238d2013-02-08 09:45:26 +0900248
249 return rc;
250}
251
Mark Salyzyn151beac2015-09-04 11:37:42 -0700252int LogAudit::log(char *buf, size_t len) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700253 char *audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700254 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700255 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900256 }
257
Mark Salyzyneb06de72014-10-13 09:59:37 -0700258 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900259
Mark Salyzyneb06de72014-10-13 09:59:37 -0700260 int rc;
261 char *type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700262 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700263 rc = logPrint("%s %s", type, audit + 1);
264 } else {
265 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900266 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700267 *audit = ' ';
268 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900269}
270
271int LogAudit::getLogSocket() {
272 int fd = audit_open();
273 if (fd < 0) {
274 return fd;
275 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800276 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900277 audit_close(fd);
278 fd = -1;
279 }
280 return fd;
281}