blob: fffc9ba961bee6566d244b85d369b125d31edf0a [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
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -080027#include <cutils/properties.h>
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 Salyzynae4d9282014-10-15 08:49:39 -070034#include "LogKlog.h"
William Roberts29d238d2013-02-08 09:45:26 +090035
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -080036#ifndef AUDITD_ENFORCE_INTEGRITY
37#define AUDITD_ENFORCE_INTEGRITY false
38#endif
39
Mark Salyzynccbadc62015-03-12 12:25:35 -070040#define KMSG_PRIORITY(PRI) \
41 '<', \
42 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
43 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, \
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -070044 '>'
45
Mark Salyzyn77187782015-05-12 15:21:31 -070046LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg) :
47 SocketListener(getLogSocket(), false),
48 logbuf(buf),
49 reader(reader),
50 fdDmesg(fdDmesg),
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -080051 policyLoaded(false),
52 rebootToSafeMode(false),
Mark Salyzyn77187782015-05-12 15:21:31 -070053 initialized(false) {
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -080054 logToDmesg("start");
William Roberts29d238d2013-02-08 09:45:26 +090055}
56
57bool LogAudit::onDataAvailable(SocketClient *cli) {
Mark Salyzyneb06de72014-10-13 09:59:37 -070058 if (!initialized) {
59 prctl(PR_SET_NAME, "logd.auditd");
60 initialized = true;
61 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070062
William Roberts29d238d2013-02-08 09:45:26 +090063 struct audit_message rep;
64
Mark Salyzyne0fa2912014-04-28 16:39:04 -070065 rep.nlh.nlmsg_type = 0;
66 rep.nlh.nlmsg_len = 0;
67 rep.data[0] = '\0';
68
William Roberts29d238d2013-02-08 09:45:26 +090069 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
70 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
71 return false;
72 }
73
Mark Salyzyneb06de72014-10-13 09:59:37 -070074 logPrint("type=%d %.*s",
75 rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts29d238d2013-02-08 09:45:26 +090076
77 return true;
78}
79
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -080080void LogAudit::logToDmesg(const std::string& str)
81{
82 static const char prefix[] = { KMSG_PRIORITY(LOG_INFO),
83 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
84 ' ', '\0' };
85 std::string message = prefix + str + "\n";
86 write(fdDmesg, message.c_str(), message.length());
87}
88
89std::string LogAudit::getProperty(const std::string& name)
90{
91 char value[PROP_VALUE_MAX] = {0};
92 property_get(name.c_str(), value, "");
93 return value;
94}
95
96void LogAudit::enforceIntegrity() {
97 if (!AUDITD_ENFORCE_INTEGRITY) {
98 logToDmesg("integrity enforcement suppressed; not rebooting");
99 } else if (rebootToSafeMode) {
100 if (getProperty("persist.sys.safemode") == "1") {
101 logToDmesg("integrity enforcement suppressed; in safe mode");
102 return;
103 }
104
105 logToDmesg("enforcing integrity; rebooting to safe mode");
106 property_set("persist.sys.safemode", "1");
107
108 std::string buildDate = getProperty("ro.build.date.utc");
109 if (!buildDate.empty()) {
110 property_set("persist.sys.audit_safemode", buildDate.c_str());
111 }
112
113 property_set("sys.powerctl", "reboot");
114 } else {
115 logToDmesg("enforcing integrity: rebooting to recovery");
116 property_set("sys.powerctl", "reboot,recovery");
117 }
118}
119
William Roberts29d238d2013-02-08 09:45:26 +0900120int LogAudit::logPrint(const char *fmt, ...) {
121 if (fmt == NULL) {
122 return -EINVAL;
123 }
124
125 va_list args;
126
127 char *str = NULL;
128 va_start(args, fmt);
129 int rc = vasprintf(&str, fmt, args);
130 va_end(args);
131
132 if (rc < 0) {
133 return rc;
134 }
135
Mark Salyzyne4369d62014-05-27 10:06:34 -0700136 char *cp;
137 while ((cp = strstr(str, " "))) {
138 memmove(cp, cp + 1, strlen(cp + 1) + 1);
139 }
140
Sami Tolvanen0bdad0f2016-02-05 14:27:52 -0800141 bool loaded = strstr(str, " policy loaded ");
142
143 if (loaded) {
144 if (policyLoaded) {
145 // SELinux policy changes are not allowed
146 enforceIntegrity();
147 } else {
148 logToDmesg("policy loaded");
149 policyLoaded = true;
150 }
151 }
152
153 bool permissive = strstr(str, " enforcing=0") ||
154 strstr(str, " permissive=1");
155
156 if (permissive) {
157 // SELinux in permissive mode is not allowed
158 enforceIntegrity();
159 }
160
161 bool info = loaded || permissive;
Mark Salyzyneb06de72014-10-13 09:59:37 -0700162 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700163 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700164 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
165 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700166
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700167 iov[0].iov_base = info ? const_cast<char *>(log_info)
168 : const_cast<char *>(log_warning);
169 iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700170 iov[1].iov_base = str;
171 iov[1].iov_len = strlen(str);
172 iov[2].iov_base = const_cast<char *>("\n");
173 iov[2].iov_len = 1;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700174
175 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
176 }
177
William Roberts29d238d2013-02-08 09:45:26 +0900178 pid_t pid = getpid();
179 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700180 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900181 log_time now;
182
183 static const char audit_str[] = " audit(";
184 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900185 if (timeptr
186 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
187 && (*cp == ':')) {
188 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700189 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700190 if (!isMonotonic()) {
191 if (android::isMonotonic(now)) {
192 LogKlog::convertMonotonicToReal(now);
193 }
194 } else {
195 if (!android::isMonotonic(now)) {
196 LogKlog::convertRealToMonotonic(now);
197 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700198 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700199 } else if (isMonotonic()) {
200 now = log_time(CLOCK_MONOTONIC);
William Roberts29d238d2013-02-08 09:45:26 +0900201 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700202 now = log_time(CLOCK_REALTIME);
William Roberts29d238d2013-02-08 09:45:26 +0900203 }
204
205 static const char pid_str[] = " pid=";
206 char *pidptr = strstr(str, pid_str);
207 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
208 cp = pidptr + sizeof(pid_str) - 1;
209 pid = 0;
210 while (isdigit(*cp)) {
211 pid = (pid * 10) + (*cp - '0');
212 ++cp;
213 }
214 tid = pid;
Mark Salyzyned777e92015-06-24 16:22:54 -0700215 logbuf->lock();
William Roberts29d238d2013-02-08 09:45:26 +0900216 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700217 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700218 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900219 }
220
Mark Salyzyne4369d62014-05-27 10:06:34 -0700221 // log to events
222
Mark Salyzynddda2122015-10-02 09:22:52 -0700223 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800224 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700225
226 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900227
Mark Salyzynddda2122015-10-02 09:22:52 -0700228 { // begin scope for event buffer
229 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
230
231 android_log_event_string_t *event
232 = reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800233 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700234 event->type = EVENT_TYPE_STRING;
235 event->length = htole32(l);
236 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700237
Mark Salyzyn202e1532015-02-09 08:21:05 -0800238 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
239 reinterpret_cast<char *>(event),
240 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800241 if (rc >= 0) {
242 notify = true;
243 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700244 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900245 }
246
Mark Salyzyne4369d62014-05-27 10:06:34 -0700247 // log to main
248
249 static const char comm_str[] = " comm=\"";
250 const char *comm = strstr(str, comm_str);
251 const char *estr = str + strlen(str);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700252 const char *commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700253 if (comm) {
254 estr = comm;
255 comm += sizeof(comm_str) - 1;
256 } else if (pid == getpid()) {
257 pid = tid;
258 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700259 } else {
260 logbuf->lock();
261 comm = commfree = logbuf->pidToName(pid);
262 logbuf->unlock();
263 if (!comm) {
264 comm = "unknown";
265 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700266 }
267
268 const char *ecomm = strchr(comm, '"');
269 if (ecomm) {
270 ++ecomm;
271 l = ecomm - comm;
272 } else {
273 l = strlen(comm) + 1;
274 ecomm = "";
275 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700276 size_t b = estr - str;
277 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
278 b = LOGGER_ENTRY_MAX_PAYLOAD;
279 }
280 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
281 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700282
Mark Salyzynddda2122015-10-02 09:22:52 -0700283 { // begin scope for main buffer
284 char newstr[n];
285
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700286 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700287 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700288 strncpy(newstr + 1 + l, str, b);
289 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700290
Mark Salyzyn202e1532015-02-09 08:21:05 -0800291 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
292 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700293
Mark Salyzyn202e1532015-02-09 08:21:05 -0800294 if (rc >= 0) {
295 notify = true;
296 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700297 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700298 }
299
Mark Salyzyn758058f2015-08-21 16:44:30 -0700300 free(const_cast<char *>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900301 free(str);
302
Mark Salyzyne4369d62014-05-27 10:06:34 -0700303 if (notify) {
304 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800305 if (rc < 0) {
306 rc = n;
307 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700308 }
William Roberts29d238d2013-02-08 09:45:26 +0900309
310 return rc;
311}
312
Mark Salyzyn151beac2015-09-04 11:37:42 -0700313int LogAudit::log(char *buf, size_t len) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700314 char *audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700315 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700316 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900317 }
318
Mark Salyzyneb06de72014-10-13 09:59:37 -0700319 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900320
Mark Salyzyneb06de72014-10-13 09:59:37 -0700321 int rc;
322 char *type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700323 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700324 rc = logPrint("%s %s", type, audit + 1);
325 } else {
326 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900327 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700328 *audit = ' ';
329 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900330}
331
332int LogAudit::getLogSocket() {
333 int fd = audit_open();
334 if (fd < 0) {
335 return fd;
336 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800337 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900338 audit_close(fd);
339 fd = -1;
340 }
341 return fd;
342}