blob: 4eb5e83105c4a890fbf356834fb492409816121d [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 Salyzyne9bebd02014-04-03 09:55:26 -0700184
Mark Salyzyn7ee2aef2014-09-28 14:41:50 -0700185 iov[0].iov_base = info ? const_cast<char *>(log_info)
186 : const_cast<char *>(log_warning);
187 iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700188 iov[1].iov_base = str;
189 iov[1].iov_len = strlen(str);
190 iov[2].iov_base = const_cast<char *>("\n");
191 iov[2].iov_len = 1;
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700192
193 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
194 }
195
William Roberts29d238d2013-02-08 09:45:26 +0900196 pid_t pid = getpid();
197 pid_t tid = gettid();
Mark Salyzyne3aeeee2015-03-17 07:56:32 -0700198 uid_t uid = AID_LOGD;
William Roberts29d238d2013-02-08 09:45:26 +0900199 log_time now;
200
201 static const char audit_str[] = " audit(";
202 char *timeptr = strstr(str, audit_str);
William Roberts29d238d2013-02-08 09:45:26 +0900203 if (timeptr
204 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
205 && (*cp == ':')) {
206 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700207 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
Mark Salyzynb6bee332015-09-08 08:56:32 -0700208 if (!isMonotonic()) {
209 if (android::isMonotonic(now)) {
210 LogKlog::convertMonotonicToReal(now);
211 }
212 } else {
213 if (!android::isMonotonic(now)) {
214 LogKlog::convertRealToMonotonic(now);
215 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700216 }
Mark Salyzynb6bee332015-09-08 08:56:32 -0700217 } else if (isMonotonic()) {
218 now = log_time(CLOCK_MONOTONIC);
William Roberts29d238d2013-02-08 09:45:26 +0900219 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700220 now = log_time(CLOCK_REALTIME);
William Roberts29d238d2013-02-08 09:45:26 +0900221 }
222
223 static const char pid_str[] = " pid=";
224 char *pidptr = strstr(str, pid_str);
225 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
226 cp = pidptr + sizeof(pid_str) - 1;
227 pid = 0;
228 while (isdigit(*cp)) {
229 pid = (pid * 10) + (*cp - '0');
230 ++cp;
231 }
232 tid = pid;
Mark Salyzyned777e92015-06-24 16:22:54 -0700233 logbuf->lock();
William Roberts29d238d2013-02-08 09:45:26 +0900234 uid = logbuf->pidToUid(pid);
Mark Salyzyned777e92015-06-24 16:22:54 -0700235 logbuf->unlock();
Mark Salyzyne4369d62014-05-27 10:06:34 -0700236 memmove(pidptr, cp, strlen(cp) + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900237 }
238
Mark Salyzyne4369d62014-05-27 10:06:34 -0700239 // log to events
240
Mark Salyzynddda2122015-10-02 09:22:52 -0700241 size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800242 size_t n = l + sizeof(android_log_event_string_t);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700243
244 bool notify = false;
William Roberts29d238d2013-02-08 09:45:26 +0900245
Mark Salyzynddda2122015-10-02 09:22:52 -0700246 { // begin scope for event buffer
247 uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
248
249 android_log_event_string_t *event
250 = reinterpret_cast<android_log_event_string_t *>(buffer);
Mark Salyzyn29eb5702015-03-03 16:21:27 -0800251 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich58ba58a2015-04-07 01:25:43 -0700252 event->type = EVENT_TYPE_STRING;
253 event->length = htole32(l);
254 memcpy(event->data, str, l);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700255
Mark Salyzyn202e1532015-02-09 08:21:05 -0800256 rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
257 reinterpret_cast<char *>(event),
258 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800259 if (rc >= 0) {
260 notify = true;
261 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700262 // end scope for event buffer
William Roberts29d238d2013-02-08 09:45:26 +0900263 }
264
Mark Salyzyne4369d62014-05-27 10:06:34 -0700265 // log to main
266
267 static const char comm_str[] = " comm=\"";
268 const char *comm = strstr(str, comm_str);
269 const char *estr = str + strlen(str);
Mark Salyzyn758058f2015-08-21 16:44:30 -0700270 const char *commfree = NULL;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700271 if (comm) {
272 estr = comm;
273 comm += sizeof(comm_str) - 1;
274 } else if (pid == getpid()) {
275 pid = tid;
276 comm = "auditd";
Mark Salyzyned777e92015-06-24 16:22:54 -0700277 } else {
278 logbuf->lock();
279 comm = commfree = logbuf->pidToName(pid);
280 logbuf->unlock();
281 if (!comm) {
282 comm = "unknown";
283 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700284 }
285
286 const char *ecomm = strchr(comm, '"');
287 if (ecomm) {
288 ++ecomm;
289 l = ecomm - comm;
290 } else {
291 l = strlen(comm) + 1;
292 ecomm = "";
293 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700294 size_t b = estr - str;
295 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
296 b = LOGGER_ENTRY_MAX_PAYLOAD;
297 }
298 size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
299 n = b + e + l + 2;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700300
Mark Salyzynddda2122015-10-02 09:22:52 -0700301 { // begin scope for main buffer
302 char newstr[n];
303
Mark Salyzyn6bdeee02014-09-19 11:59:42 -0700304 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Mark Salyzyne4369d62014-05-27 10:06:34 -0700305 strlcpy(newstr + 1, comm, l);
Mark Salyzynddda2122015-10-02 09:22:52 -0700306 strncpy(newstr + 1 + l, str, b);
307 strncpy(newstr + 1 + l + b, ecomm, e);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700308
Mark Salyzyn202e1532015-02-09 08:21:05 -0800309 rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
310 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne4369d62014-05-27 10:06:34 -0700311
Mark Salyzyn202e1532015-02-09 08:21:05 -0800312 if (rc >= 0) {
313 notify = true;
314 }
Mark Salyzynddda2122015-10-02 09:22:52 -0700315 // end scope for main buffer
Mark Salyzyne4369d62014-05-27 10:06:34 -0700316 }
317
Mark Salyzyn758058f2015-08-21 16:44:30 -0700318 free(const_cast<char *>(commfree));
William Roberts29d238d2013-02-08 09:45:26 +0900319 free(str);
320
Mark Salyzyne4369d62014-05-27 10:06:34 -0700321 if (notify) {
322 reader->notifyNewLog();
Mark Salyzyn202e1532015-02-09 08:21:05 -0800323 if (rc < 0) {
324 rc = n;
325 }
Mark Salyzyne4369d62014-05-27 10:06:34 -0700326 }
William Roberts29d238d2013-02-08 09:45:26 +0900327
328 return rc;
329}
330
Mark Salyzyn151beac2015-09-04 11:37:42 -0700331int LogAudit::log(char *buf, size_t len) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700332 char *audit = strstr(buf, " audit(");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700333 if (!audit || (audit >= &buf[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700334 return 0;
William Roberts29d238d2013-02-08 09:45:26 +0900335 }
336
Mark Salyzyneb06de72014-10-13 09:59:37 -0700337 *audit = '\0';
William Roberts29d238d2013-02-08 09:45:26 +0900338
Mark Salyzyneb06de72014-10-13 09:59:37 -0700339 int rc;
340 char *type = strstr(buf, "type=");
Mark Salyzyn151beac2015-09-04 11:37:42 -0700341 if (type && (type < &buf[len])) {
Mark Salyzyneb06de72014-10-13 09:59:37 -0700342 rc = logPrint("%s %s", type, audit + 1);
343 } else {
344 rc = logPrint("%s", audit + 1);
William Roberts29d238d2013-02-08 09:45:26 +0900345 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700346 *audit = ' ';
347 return rc;
William Roberts29d238d2013-02-08 09:45:26 +0900348}
349
350int LogAudit::getLogSocket() {
351 int fd = audit_open();
352 if (fd < 0) {
353 return fd;
354 }
Nick Kralevichc234a1b2014-11-19 13:33:22 -0800355 if (audit_setup(fd, getpid()) < 0) {
William Roberts29d238d2013-02-08 09:45:26 +0900356 audit_close(fd);
357 fd = -1;
358 }
359 return fd;
360}