blob: ab00422a90345d2ec46b5b4b764270b4b0d02509 [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"
32
Mark Salyzynccbadc62015-03-12 12:25:35 -070033#define KMSG_PRIORITY(PRI) \
34 '<', \
35 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
36 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, \
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070037 '>'
38
Mark Salyzyneb06de72014-10-13 09:59:37 -070039LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg)
William Roberts29d238d2013-02-08 09:45:26 +090040 : SocketListener(getLogSocket(), false)
41 , logbuf(buf)
Mark Salyzyne9bebd02014-04-03 09:55:26 -070042 , reader(reader)
Mark Salyzyneb06de72014-10-13 09:59:37 -070043 , fdDmesg(fdDmesg)
44 , initialized(false) {
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070045 static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
46 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
47 ' ', 's', 't', 'a', 'r', 't', '\n' };
Mark Salyzyneb06de72014-10-13 09:59:37 -070048 write(fdDmesg, auditd_message, sizeof(auditd_message));
William Roberts29d238d2013-02-08 09:45:26 +090049}
50
51bool LogAudit::onDataAvailable(SocketClient *cli) {
Mark Salyzyneb06de72014-10-13 09:59:37 -070052 if (!initialized) {
53 prctl(PR_SET_NAME, "logd.auditd");
54 initialized = true;
55 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070056
William Roberts29d238d2013-02-08 09:45:26 +090057 struct audit_message rep;
58
Mark Salyzyne0fa2912014-04-28 16:39:04 -070059 rep.nlh.nlmsg_type = 0;
60 rep.nlh.nlmsg_len = 0;
61 rep.data[0] = '\0';
62
William Roberts29d238d2013-02-08 09:45:26 +090063 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
64 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
65 return false;
66 }
67
Mark Salyzyneb06de72014-10-13 09:59:37 -070068 logPrint("type=%d %.*s",
69 rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts29d238d2013-02-08 09:45:26 +090070
71 return true;
72}
73
William Roberts29d238d2013-02-08 09:45:26 +090074int LogAudit::logPrint(const char *fmt, ...) {
75 if (fmt == NULL) {
76 return -EINVAL;
77 }
78
79 va_list args;
80
81 char *str = NULL;
82 va_start(args, fmt);
83 int rc = vasprintf(&str, fmt, args);
84 va_end(args);
85
86 if (rc < 0) {
87 return rc;
88 }
89
Mark Salyzyne4369d62014-05-27 10:06:34 -070090 char *cp;
91 while ((cp = strstr(str, " "))) {
92 memmove(cp, cp + 1, strlen(cp + 1) + 1);
93 }
94
Mark Salyzyn6bdeee02014-09-19 11:59:42 -070095 bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
Mark Salyzyneb06de72014-10-13 09:59:37 -070096 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -070097 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070098 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
99 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700100
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700101 iov[0].iov_base = info ? const_cast<char *>(log_info)
102 : const_cast<char *>(log_warning);
103 iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700104 iov[1].iov_base = str;
105 iov[1].iov_len = strlen(str);
106 iov[2].iov_base = const_cast<char *>("\n");
107 iov[2].iov_len = 1;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700108
109 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
110 }
111
William Roberts29d238d2013-02-08 09:45:26 +0900112 pid_t pid = getpid();
113 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700114 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900115 log_time now;
116
117 static const char audit_str[] = " audit(";
118 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900119 if (timeptr
120 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
121 && (*cp == ':')) {
122 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700123 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900124 } else {
125 now.strptime("", ""); // side effect of setting CLOCK_REALTIME
126 }
127
128 static const char pid_str[] = " pid=";
129 char *pidptr = strstr(str, pid_str);
130 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
131 cp = pidptr + sizeof(pid_str) - 1;
132 pid = 0;
133 while (isdigit(*cp)) {
134 pid = (pid * 10) + (*cp - '0');
135 ++cp;
136 }
137 tid = pid;
138 uid = logbuf->pidToUid(pid);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700139 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900140 }
141
Mark Salyzyne4369d62014-05-27 10:06:34 -0700142 // log to events
143
144 size_t l = strlen(str);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800145 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700146
147 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900148
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800149 android_log_event_string_t *event = static_cast<android_log_event_string_t *>(malloc(n));
150 if (!event) {
Mark Salyzyne4369d62014-05-27 10:06:34 -0700151 rc = -ENOMEM;
152 } else {
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800153 event->header.tag = htole32(AUDITD_LOG_TAG);
154 event->payload.type = EVENT_TYPE_STRING;
155 event->payload.length = htole32(l);
156 memcpy(event->payload.data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700157
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800158 logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
159 reinterpret_cast<char *>(event),
Mark Salyzyne4369d62014-05-27 10:06:34 -0700160 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800161 free(event);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700162
163 notify = true;
William Roberts29d238d2013-02-08 09:45:26 +0900164 }
165
Mark Salyzyne4369d62014-05-27 10:06:34 -0700166 // log to main
167
168 static const char comm_str[] = " comm=\"";
169 const char *comm = strstr(str, comm_str);
170 const char *estr = str + strlen(str);
171 if (comm) {
172 estr = comm;
173 comm += sizeof(comm_str) - 1;
174 } else if (pid == getpid()) {
175 pid = tid;
176 comm = "auditd";
177 } else if (!(comm = logbuf->pidToName(pid))) {
178 comm = "unknown";
179 }
180
181 const char *ecomm = strchr(comm, '"');
182 if (ecomm) {
183 ++ecomm;
184 l = ecomm - comm;
185 } else {
186 l = strlen(comm) + 1;
187 ecomm = "";
188 }
189 n = (estr - str) + strlen(ecomm) + l + 2;
190
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800191 char *newstr = static_cast<char *>(malloc(n));
Mark Salyzyne4369d62014-05-27 10:06:34 -0700192 if (!newstr) {
193 rc = -ENOMEM;
194 } else {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700195 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700196 strlcpy(newstr + 1, comm, l);
197 strncpy(newstr + 1 + l, str, estr - str);
198 strcpy(newstr + 1 + l + (estr - str), ecomm);
199
200 logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
201 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
202 free(newstr);
203
204 notify = true;
205 }
206
William Roberts29d238d2013-02-08 09:45:26 +0900207 free(str);
208
Mark Salyzyne4369d62014-05-27 10:06:34 -0700209 if (notify) {
210 reader->notifyNewLog();
211 }
William Roberts29d238d2013-02-08 09:45:26 +0900212
213 return rc;
214}
215
Mark Salyzyneb06de72014-10-13 09:59:37 -0700216int LogAudit::log(char *buf) {
217 char *audit = strstr(buf, " audit(");
218 if (!audit) {
219 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900220 }
221
Mark Salyzyneb06de72014-10-13 09:59:37 -0700222 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900223
Mark Salyzyneb06de72014-10-13 09:59:37 -0700224 int rc;
225 char *type = strstr(buf, "type=");
226 if (type) {
227 rc = logPrint("%s %s", type, audit + 1);
228 } else {
229 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900230 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700231 *audit = ' ';
232 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900233}
234
235int LogAudit::getLogSocket() {
236 int fd = audit_open();
237 if (fd < 0) {
238 return fd;
239 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800240 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900241 audit_close(fd);
242 fd = -1;
243 }
244 return fd;
245}