blob: 4ec2e59f29e2a666addcfb72d0da42c4f316fcb0 [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 Salyzyne3aeeee2015-03-17 07:56:32 -070027#include <private/android_filesystem_config.h>
Mark Salyzyn29eb5702015-03-03 16:21:27 -080028#include <private/android_logger.h>
29
William Roberts29d238d2013-02-08 09:45:26 +090030#include "libaudit.h"
31#include "LogAudit.h"
Mark Salyzynae4d9282014-10-15 08:49:39 -070032#include "LogKlog.h"
William Roberts29d238d2013-02-08 09:45:26 +090033
Mark Salyzynccbadc62015-03-12 12:25:35 -070034#define KMSG_PRIORITY(PRI) \
35 '<', \
36 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
37 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, \
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070038 '>'
39
Mark Salyzyn77187782015-05-12 15:21:31 -070040LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg) :
41 SocketListener(getLogSocket(), false),
42 logbuf(buf),
43 reader(reader),
44 fdDmesg(fdDmesg),
45 initialized(false) {
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070046 static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
47 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
48 ' ', 's', 't', 'a', 'r', 't', '\n' };
Mark Salyzyneb06de72014-10-13 09:59:37 -070049 write(fdDmesg, auditd_message, sizeof(auditd_message));
William Roberts29d238d2013-02-08 09:45:26 +090050}
51
52bool LogAudit::onDataAvailable(SocketClient *cli) {
Mark Salyzyneb06de72014-10-13 09:59:37 -070053 if (!initialized) {
54 prctl(PR_SET_NAME, "logd.auditd");
55 initialized = true;
56 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070057
William Roberts29d238d2013-02-08 09:45:26 +090058 struct audit_message rep;
59
Mark Salyzyne0fa2912014-04-28 16:39:04 -070060 rep.nlh.nlmsg_type = 0;
61 rep.nlh.nlmsg_len = 0;
62 rep.data[0] = '\0';
63
William Roberts29d238d2013-02-08 09:45:26 +090064 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
65 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
66 return false;
67 }
68
Mark Salyzyneb06de72014-10-13 09:59:37 -070069 logPrint("type=%d %.*s",
70 rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts29d238d2013-02-08 09:45:26 +090071
72 return true;
73}
74
William Roberts29d238d2013-02-08 09:45:26 +090075int LogAudit::logPrint(const char *fmt, ...) {
76 if (fmt == NULL) {
77 return -EINVAL;
78 }
79
80 va_list args;
81
82 char *str = NULL;
83 va_start(args, fmt);
84 int rc = vasprintf(&str, fmt, args);
85 va_end(args);
86
87 if (rc < 0) {
88 return rc;
89 }
90
Mark Salyzyne4369d62014-05-27 10:06:34 -070091 char *cp;
92 while ((cp = strstr(str, " "))) {
93 memmove(cp, cp + 1, strlen(cp + 1) + 1);
94 }
95
Mark Salyzyn6bdeee02014-09-19 11:59:42 -070096 bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
Mark Salyzyneb06de72014-10-13 09:59:37 -070097 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -070098 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070099 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
100 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700101
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700102 iov[0].iov_base = info ? const_cast<char *>(log_info)
103 : const_cast<char *>(log_warning);
104 iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700105 iov[1].iov_base = str;
106 iov[1].iov_len = strlen(str);
107 iov[2].iov_base = const_cast<char *>("\n");
108 iov[2].iov_len = 1;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700109
110 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
111 }
112
William Roberts29d238d2013-02-08 09:45:26 +0900113 pid_t pid = getpid();
114 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700115 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900116 log_time now;
117
118 static const char audit_str[] = " audit(";
119 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900120 if (timeptr
121 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
122 && (*cp == ':')) {
123 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700124 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700125 //
126 // We are either in 1970ish (MONOTONIC) or 2015+ish (REALTIME) so to
127 // differentiate without prejudice, we use 1980 to delineate, earlier
128 // is monotonic, later is real.
129 //
130# define EPOCH_PLUS_10_YEARS (10 * 1461 / 4 * 24 * 60 * 60)
131 if (now.tv_sec < EPOCH_PLUS_10_YEARS) {
132 LogKlog::convertMonotonicToReal(now);
133 }
William Roberts29d238d2013-02-08 09:45:26 +0900134 } else {
135 now.strptime("", ""); // side effect of setting CLOCK_REALTIME
136 }
137
138 static const char pid_str[] = " pid=";
139 char *pidptr = strstr(str, pid_str);
140 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
141 cp = pidptr + sizeof(pid_str) - 1;
142 pid = 0;
143 while (isdigit(*cp)) {
144 pid = (pid * 10) + (*cp - '0');
145 ++cp;
146 }
147 tid = pid;
148 uid = logbuf->pidToUid(pid);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700149 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900150 }
151
Mark Salyzyne4369d62014-05-27 10:06:34 -0700152 // log to events
153
154 size_t l = strlen(str);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800155 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700156
157 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900158
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800159 android_log_event_string_t *event = static_cast<android_log_event_string_t *>(malloc(n));
160 if (!event) {
Mark Salyzyne4369d62014-05-27 10:06:34 -0700161 rc = -ENOMEM;
162 } else {
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800163 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700164 event->type = EVENT_TYPE_STRING;
165 event->length = htole32(l);
166 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700167
Mark Salyzyn202e1532015-02-09 08:21:05 -0800168 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
169 reinterpret_cast<char *>(event),
170 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800171 free(event);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700172
Mark Salyzyn202e1532015-02-09 08:21:05 -0800173 if (rc >= 0) {
174 notify = true;
175 }
William Roberts29d238d2013-02-08 09:45:26 +0900176 }
177
Mark Salyzyne4369d62014-05-27 10:06:34 -0700178 // log to main
179
180 static const char comm_str[] = " comm=\"";
181 const char *comm = strstr(str, comm_str);
182 const char *estr = str + strlen(str);
183 if (comm) {
184 estr = comm;
185 comm += sizeof(comm_str) - 1;
186 } else if (pid == getpid()) {
187 pid = tid;
188 comm = "auditd";
189 } else if (!(comm = logbuf->pidToName(pid))) {
190 comm = "unknown";
191 }
192
193 const char *ecomm = strchr(comm, '"');
194 if (ecomm) {
195 ++ecomm;
196 l = ecomm - comm;
197 } else {
198 l = strlen(comm) + 1;
199 ecomm = "";
200 }
201 n = (estr - str) + strlen(ecomm) + l + 2;
202
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800203 char *newstr = static_cast<char *>(malloc(n));
Mark Salyzyne4369d62014-05-27 10:06:34 -0700204 if (!newstr) {
205 rc = -ENOMEM;
206 } else {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700207 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700208 strlcpy(newstr + 1, comm, l);
209 strncpy(newstr + 1 + l, str, estr - str);
210 strcpy(newstr + 1 + l + (estr - str), ecomm);
211
Mark Salyzyn202e1532015-02-09 08:21:05 -0800212 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
213 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700214 free(newstr);
215
Mark Salyzyn202e1532015-02-09 08:21:05 -0800216 if (rc >= 0) {
217 notify = true;
218 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700219 }
220
William Roberts29d238d2013-02-08 09:45:26 +0900221 free(str);
222
Mark Salyzyne4369d62014-05-27 10:06:34 -0700223 if (notify) {
224 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800225 if (rc < 0) {
226 rc = n;
227 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700228 }
William Roberts29d238d2013-02-08 09:45:26 +0900229
230 return rc;
231}
232
Mark Salyzyneb06de72014-10-13 09:59:37 -0700233int LogAudit::log(char *buf) {
234 char *audit = strstr(buf, " audit(");
235 if (!audit) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700236 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900237 }
238
Mark Salyzyneb06de72014-10-13 09:59:37 -0700239 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900240
Mark Salyzyneb06de72014-10-13 09:59:37 -0700241 int rc;
242 char *type = strstr(buf, "type=");
243 if (type) {
244 rc = logPrint("%s %s", type, audit + 1);
245 } else {
246 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900247 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700248 *audit = ' ';
249 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900250}
251
252int LogAudit::getLogSocket() {
253 int fd = audit_open();
254 if (fd < 0) {
255 return fd;
256 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800257 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900258 audit_close(fd);
259 fd = -1;
260 }
261 return fd;
262}