blob: af0b77595226430998ee1a75549d41cf55f4e84a [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 Salyzyn29eb5702015-03-03 16:21:27 -080027#include <private/android_logger.h>
28
William Roberts29d238d2013-02-08 09:45:26 +090029#include "libaudit.h"
30#include "LogAudit.h"
31
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070032#define KMSG_PRIORITY(PRI) \
33 '<', \
34 '0' + (LOG_AUTH | (PRI)) / 10, \
35 '0' + (LOG_AUTH | (PRI)) % 10, \
36 '>'
37
Mark Salyzyneb06de72014-10-13 09:59:37 -070038LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg)
William Roberts29d238d2013-02-08 09:45:26 +090039 : SocketListener(getLogSocket(), false)
40 , logbuf(buf)
Mark Salyzyne9bebd02014-04-03 09:55:26 -070041 , reader(reader)
Mark Salyzyneb06de72014-10-13 09:59:37 -070042 , fdDmesg(fdDmesg)
43 , initialized(false) {
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070044 static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
45 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
46 ' ', 's', 't', 'a', 'r', 't', '\n' };
Mark Salyzyneb06de72014-10-13 09:59:37 -070047 write(fdDmesg, auditd_message, sizeof(auditd_message));
William Roberts29d238d2013-02-08 09:45:26 +090048}
49
50bool LogAudit::onDataAvailable(SocketClient *cli) {
Mark Salyzyneb06de72014-10-13 09:59:37 -070051 if (!initialized) {
52 prctl(PR_SET_NAME, "logd.auditd");
53 initialized = true;
54 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070055
William Roberts29d238d2013-02-08 09:45:26 +090056 struct audit_message rep;
57
Mark Salyzyne0fa2912014-04-28 16:39:04 -070058 rep.nlh.nlmsg_type = 0;
59 rep.nlh.nlmsg_len = 0;
60 rep.data[0] = '\0';
61
William Roberts29d238d2013-02-08 09:45:26 +090062 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
63 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
64 return false;
65 }
66
Mark Salyzyneb06de72014-10-13 09:59:37 -070067 logPrint("type=%d %.*s",
68 rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts29d238d2013-02-08 09:45:26 +090069
70 return true;
71}
72
William Roberts29d238d2013-02-08 09:45:26 +090073int LogAudit::logPrint(const char *fmt, ...) {
74 if (fmt == NULL) {
75 return -EINVAL;
76 }
77
78 va_list args;
79
80 char *str = NULL;
81 va_start(args, fmt);
82 int rc = vasprintf(&str, fmt, args);
83 va_end(args);
84
85 if (rc < 0) {
86 return rc;
87 }
88
Mark Salyzyne4369d62014-05-27 10:06:34 -070089 char *cp;
90 while ((cp = strstr(str, " "))) {
91 memmove(cp, cp + 1, strlen(cp + 1) + 1);
92 }
93
Mark Salyzyn6bdeee02014-09-19 11:59:42 -070094 bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
Mark Salyzyneb06de72014-10-13 09:59:37 -070095 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -070096 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070097 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
98 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyne9bebd02014-04-03 09:55:26 -070099
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700100 iov[0].iov_base = info ? const_cast<char *>(log_info)
101 : const_cast<char *>(log_warning);
102 iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700103 iov[1].iov_base = str;
104 iov[1].iov_len = strlen(str);
105 iov[2].iov_base = const_cast<char *>("\n");
106 iov[2].iov_len = 1;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700107
108 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
109 }
110
William Roberts29d238d2013-02-08 09:45:26 +0900111 pid_t pid = getpid();
112 pid_t tid = gettid();
113 uid_t uid = getuid();
114 log_time now;
115
116 static const char audit_str[] = " audit(";
117 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900118 if (timeptr
119 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
120 && (*cp == ':')) {
121 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700122 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900123 } else {
124 now.strptime("", ""); // side effect of setting CLOCK_REALTIME
125 }
126
127 static const char pid_str[] = " pid=";
128 char *pidptr = strstr(str, pid_str);
129 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
130 cp = pidptr + sizeof(pid_str) - 1;
131 pid = 0;
132 while (isdigit(*cp)) {
133 pid = (pid * 10) + (*cp - '0');
134 ++cp;
135 }
136 tid = pid;
137 uid = logbuf->pidToUid(pid);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700138 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900139 }
140
Mark Salyzyne4369d62014-05-27 10:06:34 -0700141 // log to events
142
143 size_t l = strlen(str);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800144 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700145
146 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900147
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800148 android_log_event_string_t *event = static_cast<android_log_event_string_t *>(malloc(n));
149 if (!event) {
Mark Salyzyne4369d62014-05-27 10:06:34 -0700150 rc = -ENOMEM;
151 } else {
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800152 event->header.tag = htole32(AUDITD_LOG_TAG);
153 event->payload.type = EVENT_TYPE_STRING;
154 event->payload.length = htole32(l);
155 memcpy(event->payload.data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700156
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800157 logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
158 reinterpret_cast<char *>(event),
Mark Salyzyne4369d62014-05-27 10:06:34 -0700159 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800160 free(event);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700161
162 notify = true;
William Roberts29d238d2013-02-08 09:45:26 +0900163 }
164
Mark Salyzyne4369d62014-05-27 10:06:34 -0700165 // log to main
166
167 static const char comm_str[] = " comm=\"";
168 const char *comm = strstr(str, comm_str);
169 const char *estr = str + strlen(str);
170 if (comm) {
171 estr = comm;
172 comm += sizeof(comm_str) - 1;
173 } else if (pid == getpid()) {
174 pid = tid;
175 comm = "auditd";
176 } else if (!(comm = logbuf->pidToName(pid))) {
177 comm = "unknown";
178 }
179
180 const char *ecomm = strchr(comm, '"');
181 if (ecomm) {
182 ++ecomm;
183 l = ecomm - comm;
184 } else {
185 l = strlen(comm) + 1;
186 ecomm = "";
187 }
188 n = (estr - str) + strlen(ecomm) + l + 2;
189
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800190 char *newstr = static_cast<char *>(malloc(n));
Mark Salyzyne4369d62014-05-27 10:06:34 -0700191 if (!newstr) {
192 rc = -ENOMEM;
193 } else {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700194 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700195 strlcpy(newstr + 1, comm, l);
196 strncpy(newstr + 1 + l, str, estr - str);
197 strcpy(newstr + 1 + l + (estr - str), ecomm);
198
199 logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
200 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
201 free(newstr);
202
203 notify = true;
204 }
205
William Roberts29d238d2013-02-08 09:45:26 +0900206 free(str);
207
Mark Salyzyne4369d62014-05-27 10:06:34 -0700208 if (notify) {
209 reader->notifyNewLog();
210 }
William Roberts29d238d2013-02-08 09:45:26 +0900211
212 return rc;
213}
214
Mark Salyzyneb06de72014-10-13 09:59:37 -0700215int LogAudit::log(char *buf) {
216 char *audit = strstr(buf, " audit(");
217 if (!audit) {
218 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900219 }
220
Mark Salyzyneb06de72014-10-13 09:59:37 -0700221 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900222
Mark Salyzyneb06de72014-10-13 09:59:37 -0700223 int rc;
224 char *type = strstr(buf, "type=");
225 if (type) {
226 rc = logPrint("%s %s", type, audit + 1);
227 } else {
228 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900229 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700230 *audit = ' ';
231 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900232}
233
234int LogAudit::getLogSocket() {
235 int fd = audit_open();
236 if (fd < 0) {
237 return fd;
238 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800239 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900240 audit_close(fd);
241 fd = -1;
242 }
243 return fd;
244}