blob: 8859d55da04781cc8469e16214df36b6af396f01 [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 Salyzyn2ad0bd02016-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 Salyzyn2ad0bd02016-02-23 08:55:43 -080028#include <string>
29
Sami Tolvanend122ee62016-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 Salyzyn2ad0bd02016-02-23 08:55:43 -080037#include "LogBuffer.h"
Mark Salyzynae4d9282014-10-15 08:49:39 -070038#include "LogKlog.h"
Mark Salyzyn2ad0bd02016-02-23 08:55:43 -080039#include "LogReader.h"
William Roberts29d238d2013-02-08 09:45:26 +090040
Sami Tolvanend122ee62016-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 Tolvanend122ee62016-02-05 14:27:52 -080056 policyLoaded(false),
57 rebootToSafeMode(false),
Mark Salyzyn77187782015-05-12 15:21:31 -070058 initialized(false) {
Sami Tolvanend122ee62016-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 Tolvanend122ee62016-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() {
Sami Tolvanenabda9342016-02-29 09:07:46 -0800102 static bool loggedOnce;
103 bool once = loggedOnce;
104
105 loggedOnce = true;
106
Sami Tolvanend122ee62016-02-05 14:27:52 -0800107 if (!AUDITD_ENFORCE_INTEGRITY) {
Sami Tolvanenabda9342016-02-29 09:07:46 -0800108 if (!once) {
109 logToDmesg("integrity enforcement suppressed; not rebooting");
110 }
Sami Tolvanend122ee62016-02-05 14:27:52 -0800111 } else if (rebootToSafeMode) {
112 if (getProperty("persist.sys.safemode") == "1") {
Sami Tolvanenabda9342016-02-29 09:07:46 -0800113 if (!once) {
114 logToDmesg("integrity enforcement suppressed; in safe mode");
115 }
Sami Tolvanend122ee62016-02-05 14:27:52 -0800116 return;
117 }
118
119 logToDmesg("enforcing integrity; rebooting to safe mode");
120 property_set("persist.sys.safemode", "1");
121
122 std::string buildDate = getProperty("ro.build.date.utc");
123 if (!buildDate.empty()) {
124 property_set("persist.sys.audit_safemode", buildDate.c_str());
125 }
126
127 property_set("sys.powerctl", "reboot");
128 } else {
129 logToDmesg("enforcing integrity: rebooting to recovery");
130 property_set("sys.powerctl", "reboot,recovery");
131 }
132}
133
William Roberts29d238d2013-02-08 09:45:26 +0900134int LogAudit::logPrint(const char *fmt, ...) {
135 if (fmt == NULL) {
136 return -EINVAL;
137 }
138
139 va_list args;
140
141 char *str = NULL;
142 va_start(args, fmt);
143 int rc = vasprintf(&str, fmt, args);
144 va_end(args);
145
146 if (rc < 0) {
147 return rc;
148 }
149
Mark Salyzyne4369d62014-05-27 10:06:34 -0700150 char *cp;
151 while ((cp = strstr(str, " "))) {
152 memmove(cp, cp + 1, strlen(cp + 1) + 1);
153 }
154
Sami Tolvanend122ee62016-02-05 14:27:52 -0800155 bool loaded = strstr(str, " policy loaded ");
156
157 if (loaded) {
158 if (policyLoaded) {
159 // SELinux policy changes are not allowed
160 enforceIntegrity();
161 } else {
162 logToDmesg("policy loaded");
163 policyLoaded = true;
164 }
165 }
166
Sami Tolvanen2060a832016-02-29 14:10:59 -0800167 // Note: The audit log can include untrusted strings, but those containing
168 // "a control character, unprintable character, double quote mark, or a
169 // space" are hex encoded. The space character before the search term is
170 // therefore needed to prevent denial of service. Do not remove the space.
Sami Tolvanend122ee62016-02-05 14:27:52 -0800171 bool permissive = strstr(str, " enforcing=0") ||
172 strstr(str, " permissive=1");
173
174 if (permissive) {
175 // SELinux in permissive mode is not allowed
176 enforceIntegrity();
177 }
178
179 bool info = loaded || permissive;
Mark Salyzyneb06de72014-10-13 09:59:37 -0700180 if ((fdDmesg >= 0) && initialized) {
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700181 struct iovec iov[3];
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700182 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
183 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700184 static const char newline[] = "\n";
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700185
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700186 // Dedupe messages, checking for identical messages starting with avc:
187 static unsigned count;
188 static char *last_str;
189 static bool last_info;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700190
Mark Salyzyn4d205f82016-07-15 15:45:58 -0700191 if (last_str != NULL) {
192 static const char avc[] = "): avc: ";
193 char *avcl = strstr(last_str, avc);
194 bool skip = false;
195
196 if (avcl) {
197 char *avcr = strstr(str, avc);
198
199 skip = avcr && !strcmp(avcl + strlen(avc), avcr + strlen(avc));
200 if (skip) {
201 ++count;
202 free(last_str);
203 last_str = strdup(str);
204 last_info = info;
205 }
206 }
207 if (!skip) {
208 static const char resume[] = " duplicate messages suppressed\n";
209
210 iov[0].iov_base = last_info ?
211 const_cast<char *>(log_info) :
212 const_cast<char *>(log_warning);
213 iov[0].iov_len = last_info ?
214 sizeof(log_info) :
215 sizeof(log_warning);
216 iov[1].iov_base = last_str;
217 iov[1].iov_len = strlen(last_str);
218 if (count > 1) {
219 iov[2].iov_base = const_cast<char *>(resume);
220 iov[2].iov_len = strlen(resume);
221 } else {
222 iov[2].iov_base = const_cast<char *>(newline);
223 iov[2].iov_len = strlen(newline);
224 }
225
226 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
227 free(last_str);
228 last_str = NULL;
229 }
230 }
231 if (last_str == NULL) {
232 count = 0;
233 last_str = strdup(str);
234 last_info = info;
235 }
236 if (count == 0) {
237 iov[0].iov_base = info ?
238 const_cast<char *>(log_info) :
239 const_cast<char *>(log_warning);
240 iov[0].iov_len = info ?
241 sizeof(log_info) :
242 sizeof(log_warning);
243 iov[1].iov_base = str;
244 iov[1].iov_len = strlen(str);
245 iov[2].iov_base = const_cast<char *>(newline);
246 iov[2].iov_len = strlen(newline);
247
248 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
249 }
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700250 }
251
William Roberts29d238d2013-02-08 09:45:26 +0900252 pid_t pid = getpid();
253 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700254 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900255 log_time now;
256
257 static const char audit_str[] = " audit(";
258 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900259 if (timeptr
260 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
261 && (*cp == ':')) {
262 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700263 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700264 if (!isMonotonic()) {
265 if (android::isMonotonic(now)) {
266 LogKlog::convertMonotonicToReal(now);
267 }
268 } else {
269 if (!android::isMonotonic(now)) {
270 LogKlog::convertRealToMonotonic(now);
271 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700272 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700273 } else if (isMonotonic()) {
274 now = log_time(CLOCK_MONOTONIC);
William Roberts29d238d2013-02-08 09:45:26 +0900275 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700276 now = log_time(CLOCK_REALTIME);
William Roberts29d238d2013-02-08 09:45:26 +0900277 }
278
279 static const char pid_str[] = " pid=";
280 char *pidptr = strstr(str, pid_str);
281 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
282 cp = pidptr + sizeof(pid_str) - 1;
283 pid = 0;
284 while (isdigit(*cp)) {
285 pid = (pid * 10) + (*cp - '0');
286 ++cp;
287 }
288 tid = pid;
Mark Salyzyned777e92015-06-24 16:22:54 -0700289 logbuf->lock();
William Roberts29d238d2013-02-08 09:45:26 +0900290 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700291 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700292 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900293 }
294
Mark Salyzyne4369d62014-05-27 10:06:34 -0700295 // log to events
296
Mark Salyzynddda2122015-10-02 09:22:52 -0700297 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800298 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700299
300 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900301
Mark Salyzynddda2122015-10-02 09:22:52 -0700302 { // begin scope for event buffer
303 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
304
305 android_log_event_string_t *event
306 = reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800307 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700308 event->type = EVENT_TYPE_STRING;
309 event->length = htole32(l);
310 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700311
Mark Salyzyn202e1532015-02-09 08:21:05 -0800312 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
313 reinterpret_cast<char *>(event),
314 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800315 if (rc >= 0) {
316 notify = true;
317 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700318 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900319 }
320
Mark Salyzyne4369d62014-05-27 10:06:34 -0700321 // log to main
322
323 static const char comm_str[] = " comm=\"";
324 const char *comm = strstr(str, comm_str);
325 const char *estr = str + strlen(str);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700326 const char *commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700327 if (comm) {
328 estr = comm;
329 comm += sizeof(comm_str) - 1;
330 } else if (pid == getpid()) {
331 pid = tid;
332 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700333 } else {
334 logbuf->lock();
335 comm = commfree = logbuf->pidToName(pid);
336 logbuf->unlock();
337 if (!comm) {
338 comm = "unknown";
339 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700340 }
341
342 const char *ecomm = strchr(comm, '"');
343 if (ecomm) {
344 ++ecomm;
345 l = ecomm - comm;
346 } else {
347 l = strlen(comm) + 1;
348 ecomm = "";
349 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700350 size_t b = estr - str;
351 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
352 b = LOGGER_ENTRY_MAX_PAYLOAD;
353 }
354 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
355 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700356
Mark Salyzynddda2122015-10-02 09:22:52 -0700357 { // begin scope for main buffer
358 char newstr[n];
359
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700360 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700361 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700362 strncpy(newstr + 1 + l, str, b);
363 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700364
Mark Salyzyn202e1532015-02-09 08:21:05 -0800365 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
366 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700367
Mark Salyzyn202e1532015-02-09 08:21:05 -0800368 if (rc >= 0) {
369 notify = true;
370 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700371 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700372 }
373
Mark Salyzyn758058f2015-08-21 16:44:30 -0700374 free(const_cast<char *>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900375 free(str);
376
Mark Salyzyne4369d62014-05-27 10:06:34 -0700377 if (notify) {
378 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800379 if (rc < 0) {
380 rc = n;
381 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700382 }
William Roberts29d238d2013-02-08 09:45:26 +0900383
384 return rc;
385}
386
Mark Salyzyn151beac2015-09-04 11:37:42 -0700387int LogAudit::log(char *buf, size_t len) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700388 char *audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700389 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700390 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900391 }
392
Mark Salyzyneb06de72014-10-13 09:59:37 -0700393 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900394
Mark Salyzyneb06de72014-10-13 09:59:37 -0700395 int rc;
396 char *type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700397 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700398 rc = logPrint("%s %s", type, audit + 1);
399 } else {
400 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900401 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700402 *audit = ' ';
403 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900404}
405
406int LogAudit::getLogSocket() {
407 int fd = audit_open();
408 if (fd < 0) {
409 return fd;
410 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800411 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900412 audit_close(fd);
413 fd = -1;
414 }
415 return fd;
416}