blob: 230dd11257086413ebea1683d8b99ccf2834cc7b [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 Salyzyn317bfb92016-02-23 08:55:43 -080028#include <string>
29
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -080030#include <cutils/properties.h>
Mark Salyzynddda2122015-10-02 09:22:52 -070031#include <log/logger.h>
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070032#include <private/android_filesystem_config.h>
Mark Salyzyn29eb5702015-03-03 16:21:27 -080033#include <private/android_logger.h>
34
William Roberts29d238d2013-02-08 09:45:26 +090035#include "libaudit.h"
36#include "LogAudit.h"
Mark Salyzyn317bfb92016-02-23 08:55:43 -080037#include "LogBuffer.h"
Mark Salyzynae4d9282014-10-15 08:49:39 -070038#include "LogKlog.h"
Mark Salyzyn317bfb92016-02-23 08:55:43 -080039#include "LogReader.h"
William Roberts29d238d2013-02-08 09:45:26 +090040
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -080041#ifndef AUDITD_ENFORCE_INTEGRITY
42#define AUDITD_ENFORCE_INTEGRITY false
43#endif
44
Mark Salyzynccbadc62015-03-12 12:25:35 -070045#define KMSG_PRIORITY(PRI) \
46 '<', \
47 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
48 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, \
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070049 '>'
50
Mark Salyzyn77187782015-05-12 15:21:31 -070051LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg) :
52 SocketListener(getLogSocket(), false),
53 logbuf(buf),
54 reader(reader),
55 fdDmesg(fdDmesg),
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -080056 policyLoaded(false),
57 rebootToSafeMode(false),
Mark Salyzyn77187782015-05-12 15:21:31 -070058 initialized(false) {
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -080059 logToDmesg("start");
William Roberts29d238d2013-02-08 09:45:26 +090060}
61
62bool LogAudit::onDataAvailable(SocketClient *cli) {
Mark Salyzyneb06de72014-10-13 09:59:37 -070063 if (!initialized) {
64 prctl(PR_SET_NAME, "logd.auditd");
65 initialized = true;
66 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070067
William Roberts29d238d2013-02-08 09:45:26 +090068 struct audit_message rep;
69
Mark Salyzyne0fa2912014-04-28 16:39:04 -070070 rep.nlh.nlmsg_type = 0;
71 rep.nlh.nlmsg_len = 0;
72 rep.data[0] = '\0';
73
William Roberts29d238d2013-02-08 09:45:26 +090074 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
75 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
76 return false;
77 }
78
Mark Salyzyneb06de72014-10-13 09:59:37 -070079 logPrint("type=%d %.*s",
80 rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts29d238d2013-02-08 09:45:26 +090081
82 return true;
83}
84
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -080085void LogAudit::logToDmesg(const std::string& str)
86{
87 static const char prefix[] = { KMSG_PRIORITY(LOG_INFO),
88 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
89 ' ', '\0' };
90 std::string message = prefix + str + "\n";
91 write(fdDmesg, message.c_str(), message.length());
92}
93
94std::string LogAudit::getProperty(const std::string& name)
95{
96 char value[PROP_VALUE_MAX] = {0};
97 property_get(name.c_str(), value, "");
98 return value;
99}
100
101void LogAudit::enforceIntegrity() {
102 if (!AUDITD_ENFORCE_INTEGRITY) {
103 logToDmesg("integrity enforcement suppressed; not rebooting");
104 } else if (rebootToSafeMode) {
105 if (getProperty("persist.sys.safemode") == "1") {
106 logToDmesg("integrity enforcement suppressed; in safe mode");
107 return;
108 }
109
110 logToDmesg("enforcing integrity; rebooting to safe mode");
111 property_set("persist.sys.safemode", "1");
112
113 std::string buildDate = getProperty("ro.build.date.utc");
114 if (!buildDate.empty()) {
115 property_set("persist.sys.audit_safemode", buildDate.c_str());
116 }
117
118 property_set("sys.powerctl", "reboot");
119 } else {
120 logToDmesg("enforcing integrity: rebooting to recovery");
121 property_set("sys.powerctl", "reboot,recovery");
122 }
123}
124
William Roberts29d238d2013-02-08 09:45:26 +0900125int LogAudit::logPrint(const char *fmt, ...) {
126 if (fmt == NULL) {
127 return -EINVAL;
128 }
129
130 va_list args;
131
132 char *str = NULL;
133 va_start(args, fmt);
134 int rc = vasprintf(&str, fmt, args);
135 va_end(args);
136
137 if (rc < 0) {
138 return rc;
139 }
140
Mark Salyzyne4369d62014-05-27 10:06:34 -0700141 char *cp;
142 while ((cp = strstr(str, " "))) {
143 memmove(cp, cp + 1, strlen(cp + 1) + 1);
144 }
145
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -0800146 bool loaded = strstr(str, " policy loaded ");
147
148 if (loaded) {
149 if (policyLoaded) {
150 // SELinux policy changes are not allowed
151 enforceIntegrity();
152 } else {
153 logToDmesg("policy loaded");
154 policyLoaded = true;
155 }
156 }
157
158 bool permissive = strstr(str, " enforcing=0") ||
159 strstr(str, " permissive=1");
160
161 if (permissive) {
162 // SELinux in permissive mode is not allowed
163 enforceIntegrity();
164 }
165
166 bool info = loaded || permissive;
Mark Salyzyneb06de72014-10-13 09:59:37 -0700167 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700168 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700169 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
170 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700171
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700172 iov[0].iov_base = info ? const_cast<char *>(log_info)
173 : const_cast<char *>(log_warning);
174 iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700175 iov[1].iov_base = str;
176 iov[1].iov_len = strlen(str);
177 iov[2].iov_base = const_cast<char *>("\n");
178 iov[2].iov_len = 1;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700179
180 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
181 }
182
William Roberts29d238d2013-02-08 09:45:26 +0900183 pid_t pid = getpid();
184 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700185 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900186 log_time now;
187
188 static const char audit_str[] = " audit(";
189 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900190 if (timeptr
191 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
192 && (*cp == ':')) {
193 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700194 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700195 if (!isMonotonic()) {
196 if (android::isMonotonic(now)) {
197 LogKlog::convertMonotonicToReal(now);
198 }
199 } else {
200 if (!android::isMonotonic(now)) {
201 LogKlog::convertRealToMonotonic(now);
202 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700203 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700204 } else if (isMonotonic()) {
205 now = log_time(CLOCK_MONOTONIC);
William Roberts29d238d2013-02-08 09:45:26 +0900206 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700207 now = log_time(CLOCK_REALTIME);
William Roberts29d238d2013-02-08 09:45:26 +0900208 }
209
210 static const char pid_str[] = " pid=";
211 char *pidptr = strstr(str, pid_str);
212 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
213 cp = pidptr + sizeof(pid_str) - 1;
214 pid = 0;
215 while (isdigit(*cp)) {
216 pid = (pid * 10) + (*cp - '0');
217 ++cp;
218 }
219 tid = pid;
Mark Salyzyned777e92015-06-24 16:22:54 -0700220 logbuf->lock();
William Roberts29d238d2013-02-08 09:45:26 +0900221 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700222 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700223 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900224 }
225
Mark Salyzyne4369d62014-05-27 10:06:34 -0700226 // log to events
227
Mark Salyzynddda2122015-10-02 09:22:52 -0700228 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800229 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700230
231 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900232
Mark Salyzynddda2122015-10-02 09:22:52 -0700233 { // begin scope for event buffer
234 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
235
236 android_log_event_string_t *event
237 = reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800238 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700239 event->type = EVENT_TYPE_STRING;
240 event->length = htole32(l);
241 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700242
Mark Salyzyn202e1532015-02-09 08:21:05 -0800243 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
244 reinterpret_cast<char *>(event),
245 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800246 if (rc >= 0) {
247 notify = true;
248 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700249 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900250 }
251
Mark Salyzyne4369d62014-05-27 10:06:34 -0700252 // log to main
253
254 static const char comm_str[] = " comm=\"";
255 const char *comm = strstr(str, comm_str);
256 const char *estr = str + strlen(str);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700257 const char *commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700258 if (comm) {
259 estr = comm;
260 comm += sizeof(comm_str) - 1;
261 } else if (pid == getpid()) {
262 pid = tid;
263 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700264 } else {
265 logbuf->lock();
266 comm = commfree = logbuf->pidToName(pid);
267 logbuf->unlock();
268 if (!comm) {
269 comm = "unknown";
270 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700271 }
272
273 const char *ecomm = strchr(comm, '"');
274 if (ecomm) {
275 ++ecomm;
276 l = ecomm - comm;
277 } else {
278 l = strlen(comm) + 1;
279 ecomm = "";
280 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700281 size_t b = estr - str;
282 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
283 b = LOGGER_ENTRY_MAX_PAYLOAD;
284 }
285 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
286 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700287
Mark Salyzynddda2122015-10-02 09:22:52 -0700288 { // begin scope for main buffer
289 char newstr[n];
290
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700291 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700292 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700293 strncpy(newstr + 1 + l, str, b);
294 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700295
Mark Salyzyn202e1532015-02-09 08:21:05 -0800296 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
297 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700298
Mark Salyzyn202e1532015-02-09 08:21:05 -0800299 if (rc >= 0) {
300 notify = true;
301 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700302 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700303 }
304
Mark Salyzyn758058f2015-08-21 16:44:30 -0700305 free(const_cast<char *>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900306 free(str);
307
Mark Salyzyne4369d62014-05-27 10:06:34 -0700308 if (notify) {
309 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800310 if (rc < 0) {
311 rc = n;
312 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700313 }
William Roberts29d238d2013-02-08 09:45:26 +0900314
315 return rc;
316}
317
Mark Salyzyn151beac2015-09-04 11:37:42 -0700318int LogAudit::log(char *buf, size_t len) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700319 char *audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700320 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700321 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900322 }
323
Mark Salyzyneb06de72014-10-13 09:59:37 -0700324 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900325
Mark Salyzyneb06de72014-10-13 09:59:37 -0700326 int rc;
327 char *type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700328 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700329 rc = logPrint("%s %s", type, audit + 1);
330 } else {
331 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900332 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700333 *audit = ' ';
334 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900335}
336
337int LogAudit::getLogSocket() {
338 int fd = audit_open();
339 if (fd < 0) {
340 return fd;
341 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800342 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900343 audit_close(fd);
344 fd = -1;
345 }
346 return fd;
347}