William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 1 | /* |
| 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 Salyzyn | 29eb570 | 2015-03-03 16:21:27 -0800 | [diff] [blame] | 18 | #include <endian.h> |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 19 | #include <errno.h> |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 20 | #include <limits.h> |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 21 | #include <stdarg.h> |
| 22 | #include <stdlib.h> |
Mark Salyzyn | 317bfb9 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 23 | #include <string.h> |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 24 | #include <sys/prctl.h> |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 25 | #include <sys/uio.h> |
Mark Salyzyn | 7ee2aef | 2014-09-28 14:41:50 -0700 | [diff] [blame] | 26 | #include <syslog.h> |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 27 | |
Mark Salyzyn | c8d3194 | 2016-11-03 10:29:23 -0700 | [diff] [blame] | 28 | #include <android-base/macros.h> |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 29 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 29eb570 | 2015-03-03 16:21:27 -0800 | [diff] [blame] | 30 | #include <private/android_logger.h> |
| 31 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 32 | #include "LogAudit.h" |
Mark Salyzyn | 317bfb9 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 33 | #include "LogBuffer.h" |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 34 | #include "LogKlog.h" |
Mark Salyzyn | 317bfb9 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 35 | #include "LogReader.h" |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 36 | #include "LogUtils.h" |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 37 | #include "libaudit.h" |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 38 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 39 | #define KMSG_PRIORITY(PRI) \ |
| 40 | '<', '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \ |
| 41 | '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, '>' |
Mark Salyzyn | 7ee2aef | 2014-09-28 14:41:50 -0700 | [diff] [blame] | 42 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 43 | LogAudit::LogAudit(LogBuffer* buf, LogReader* reader, int fdDmesg) |
| 44 | : SocketListener(mSock = getLogSocket(), false), |
| 45 | logbuf(buf), |
| 46 | reader(reader), |
| 47 | fdDmesg(fdDmesg), |
| 48 | main(__android_logger_property_get_bool("ro.logd.auditd.main", |
| 49 | BOOL_DEFAULT_TRUE)), |
| 50 | events(__android_logger_property_get_bool("ro.logd.auditd.events", |
Mark Salyzyn | ce80da3 | 2016-12-29 15:16:06 -0800 | [diff] [blame] | 51 | BOOL_DEFAULT_TRUE)), |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 52 | initialized(false), |
| 53 | tooFast(false) { |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 54 | static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO), |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 55 | 'l', |
| 56 | 'o', |
| 57 | 'g', |
| 58 | 'd', |
| 59 | '.', |
| 60 | 'a', |
| 61 | 'u', |
| 62 | 'd', |
| 63 | 'i', |
| 64 | 't', |
| 65 | 'd', |
| 66 | ':', |
| 67 | ' ', |
| 68 | 's', |
| 69 | 't', |
| 70 | 'a', |
| 71 | 'r', |
| 72 | 't', |
| 73 | '\n' }; |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 74 | write(fdDmesg, auditd_message, sizeof(auditd_message)); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 75 | } |
| 76 | |
Mark Salyzyn | 247d682 | 2017-01-03 14:00:19 -0800 | [diff] [blame] | 77 | void LogAudit::checkRateLimit() { |
Mark Salyzyn | 247d682 | 2017-01-03 14:00:19 -0800 | [diff] [blame] | 78 | // trim list for AUDIT_RATE_LIMIT_BURST_DURATION of history |
| 79 | log_time oldest(AUDIT_RATE_LIMIT_BURST_DURATION, 0); |
| 80 | bucket.emplace(android_log_clockid()); |
| 81 | oldest = bucket.back() - oldest; |
| 82 | while (bucket.front() < oldest) bucket.pop(); |
| 83 | |
| 84 | static const size_t upperThreshold = |
| 85 | ((AUDIT_RATE_LIMIT_BURST_DURATION * |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 86 | (AUDIT_RATE_LIMIT_DEFAULT + AUDIT_RATE_LIMIT_MAX)) + |
| 87 | 1) / |
| 88 | 2; |
Mark Salyzyn | 247d682 | 2017-01-03 14:00:19 -0800 | [diff] [blame] | 89 | if (bucket.size() >= upperThreshold) { |
| 90 | // Hit peak, slow down source |
| 91 | if (!tooFast) { |
| 92 | tooFast = true; |
| 93 | audit_rate_limit(mSock, AUDIT_RATE_LIMIT_MAX); |
| 94 | } |
| 95 | |
| 96 | // We do not need to hold on to the full set of timing data history, |
| 97 | // let's ensure it does not grow without bounds. This also ensures |
| 98 | // that std::dequeue underneath behaves almost like a ring buffer. |
| 99 | do { |
| 100 | bucket.pop(); |
| 101 | } while (bucket.size() >= upperThreshold); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if (!tooFast) return; |
| 106 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 107 | static const size_t lowerThreshold = |
| 108 | AUDIT_RATE_LIMIT_BURST_DURATION * AUDIT_RATE_LIMIT_MAX; |
Mark Salyzyn | 247d682 | 2017-01-03 14:00:19 -0800 | [diff] [blame] | 109 | |
| 110 | if (bucket.size() >= lowerThreshold) return; |
| 111 | |
| 112 | tooFast = false; |
| 113 | // Went below max sustained rate, allow source to speed up |
| 114 | audit_rate_limit(mSock, AUDIT_RATE_LIMIT_DEFAULT); |
| 115 | } |
| 116 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 117 | bool LogAudit::onDataAvailable(SocketClient* cli) { |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 118 | if (!initialized) { |
| 119 | prctl(PR_SET_NAME, "logd.auditd"); |
| 120 | initialized = true; |
| 121 | } |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 122 | |
Mark Salyzyn | 247d682 | 2017-01-03 14:00:19 -0800 | [diff] [blame] | 123 | checkRateLimit(); |
| 124 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 125 | struct audit_message rep; |
| 126 | |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 127 | rep.nlh.nlmsg_type = 0; |
| 128 | rep.nlh.nlmsg_len = 0; |
| 129 | rep.data[0] = '\0'; |
| 130 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 131 | if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) { |
| 132 | SLOGE("Failed on audit_get_reply with error: %s", strerror(errno)); |
| 133 | return false; |
| 134 | } |
| 135 | |
Mark Salyzyn | 247d682 | 2017-01-03 14:00:19 -0800 | [diff] [blame] | 136 | logPrint("type=%d %.*s", rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 137 | |
| 138 | return true; |
| 139 | } |
| 140 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 141 | int LogAudit::logPrint(const char* fmt, ...) { |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 142 | if (fmt == NULL) { |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | |
| 146 | va_list args; |
| 147 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 148 | char* str = NULL; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 149 | va_start(args, fmt); |
| 150 | int rc = vasprintf(&str, fmt, args); |
| 151 | va_end(args); |
| 152 | |
| 153 | if (rc < 0) { |
| 154 | return rc; |
| 155 | } |
| 156 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 157 | char* cp; |
Nick Kralevich | 2e58867 | 2017-01-03 10:35:34 -0800 | [diff] [blame] | 158 | // Work around kernels missing |
| 159 | // https://github.com/torvalds/linux/commit/b8f89caafeb55fba75b74bea25adc4e4cd91be67 |
| 160 | // Such kernels improperly add newlines inside audit messages. |
| 161 | while ((cp = strchr(str, '\n'))) { |
| 162 | *cp = ' '; |
| 163 | } |
| 164 | |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 165 | while ((cp = strstr(str, " "))) { |
| 166 | memmove(cp, cp + 1, strlen(cp + 1) + 1); |
| 167 | } |
| 168 | |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 169 | bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded "); |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 170 | if ((fdDmesg >= 0) && initialized) { |
Mark Salyzyn | 6bdeee0 | 2014-09-19 11:59:42 -0700 | [diff] [blame] | 171 | struct iovec iov[3]; |
Mark Salyzyn | 7ee2aef | 2014-09-28 14:41:50 -0700 | [diff] [blame] | 172 | static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) }; |
| 173 | static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) }; |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 174 | static const char newline[] = "\n"; |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 175 | |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 176 | // Dedupe messages, checking for identical messages starting with avc: |
| 177 | static unsigned count; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 178 | static char* last_str; |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 179 | static bool last_info; |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 180 | |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 181 | if (last_str != NULL) { |
| 182 | static const char avc[] = "): avc: "; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 183 | char* avcl = strstr(last_str, avc); |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 184 | bool skip = false; |
| 185 | |
| 186 | if (avcl) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 187 | char* avcr = strstr(str, avc); |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 188 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 189 | skip = avcr && |
| 190 | !fastcmp<strcmp>(avcl + strlen(avc), avcr + strlen(avc)); |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 191 | if (skip) { |
| 192 | ++count; |
| 193 | free(last_str); |
| 194 | last_str = strdup(str); |
| 195 | last_info = info; |
| 196 | } |
| 197 | } |
| 198 | if (!skip) { |
| 199 | static const char resume[] = " duplicate messages suppressed\n"; |
| 200 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 201 | iov[0].iov_base = last_info ? const_cast<char*>(log_info) |
| 202 | : const_cast<char*>(log_warning); |
| 203 | iov[0].iov_len = |
| 204 | last_info ? sizeof(log_info) : sizeof(log_warning); |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 205 | iov[1].iov_base = last_str; |
| 206 | iov[1].iov_len = strlen(last_str); |
| 207 | if (count > 1) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 208 | iov[2].iov_base = const_cast<char*>(resume); |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 209 | iov[2].iov_len = strlen(resume); |
| 210 | } else { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 211 | iov[2].iov_base = const_cast<char*>(newline); |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 212 | iov[2].iov_len = strlen(newline); |
| 213 | } |
| 214 | |
Mark Salyzyn | c8d3194 | 2016-11-03 10:29:23 -0700 | [diff] [blame] | 215 | writev(fdDmesg, iov, arraysize(iov)); |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 216 | free(last_str); |
| 217 | last_str = NULL; |
| 218 | } |
| 219 | } |
| 220 | if (last_str == NULL) { |
| 221 | count = 0; |
| 222 | last_str = strdup(str); |
| 223 | last_info = info; |
| 224 | } |
| 225 | if (count == 0) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 226 | iov[0].iov_base = info ? const_cast<char*>(log_info) |
| 227 | : const_cast<char*>(log_warning); |
| 228 | iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning); |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 229 | iov[1].iov_base = str; |
| 230 | iov[1].iov_len = strlen(str); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 231 | iov[2].iov_base = const_cast<char*>(newline); |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 232 | iov[2].iov_len = strlen(newline); |
| 233 | |
Mark Salyzyn | c8d3194 | 2016-11-03 10:29:23 -0700 | [diff] [blame] | 234 | writev(fdDmesg, iov, arraysize(iov)); |
Mark Salyzyn | 4d205f8 | 2016-07-15 15:45:58 -0700 | [diff] [blame] | 235 | } |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Mark Salyzyn | ce80da3 | 2016-12-29 15:16:06 -0800 | [diff] [blame] | 238 | if (!main && !events) { |
| 239 | free(str); |
| 240 | return 0; |
| 241 | } |
| 242 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 243 | pid_t pid = getpid(); |
| 244 | pid_t tid = gettid(); |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 245 | uid_t uid = AID_LOGD; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 246 | log_time now; |
| 247 | |
| 248 | static const char audit_str[] = " audit("; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 249 | char* timeptr = strstr(str, audit_str); |
| 250 | if (timeptr && |
| 251 | ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q"))) && |
| 252 | (*cp == ':')) { |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 253 | memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 254 | memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1); |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 255 | if (!isMonotonic()) { |
| 256 | if (android::isMonotonic(now)) { |
| 257 | LogKlog::convertMonotonicToReal(now); |
| 258 | } |
| 259 | } else { |
| 260 | if (!android::isMonotonic(now)) { |
| 261 | LogKlog::convertRealToMonotonic(now); |
| 262 | } |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 263 | } |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 264 | } else if (isMonotonic()) { |
| 265 | now = log_time(CLOCK_MONOTONIC); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 266 | } else { |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 267 | now = log_time(CLOCK_REALTIME); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static const char pid_str[] = " pid="; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 271 | char* pidptr = strstr(str, pid_str); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 272 | if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) { |
| 273 | cp = pidptr + sizeof(pid_str) - 1; |
| 274 | pid = 0; |
| 275 | while (isdigit(*cp)) { |
| 276 | pid = (pid * 10) + (*cp - '0'); |
| 277 | ++cp; |
| 278 | } |
| 279 | tid = pid; |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 280 | logbuf->lock(); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 281 | uid = logbuf->pidToUid(pid); |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 282 | logbuf->unlock(); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 283 | memmove(pidptr, cp, strlen(cp) + 1); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 284 | } |
| 285 | |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 286 | // log to events |
| 287 | |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 288 | size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD); |
Mark Salyzyn | 29eb570 | 2015-03-03 16:21:27 -0800 | [diff] [blame] | 289 | size_t n = l + sizeof(android_log_event_string_t); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 290 | |
| 291 | bool notify = false; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 292 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 293 | if (events) { // begin scope for event buffer |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 294 | uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; |
| 295 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 296 | android_log_event_string_t* event = |
| 297 | reinterpret_cast<android_log_event_string_t*>(buffer); |
Mark Salyzyn | 29eb570 | 2015-03-03 16:21:27 -0800 | [diff] [blame] | 298 | event->header.tag = htole32(AUDITD_LOG_TAG); |
Nick Kralevich | 58ba58a | 2015-04-07 01:25:43 -0700 | [diff] [blame] | 299 | event->type = EVENT_TYPE_STRING; |
| 300 | event->length = htole32(l); |
| 301 | memcpy(event->data, str, l); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 302 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 303 | rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid, |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 304 | reinterpret_cast<char*>(event), |
| 305 | (n <= USHRT_MAX) ? (unsigned short)n : USHRT_MAX); |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 306 | if (rc >= 0) { |
| 307 | notify = true; |
| 308 | } |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 309 | // end scope for event buffer |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 310 | } |
| 311 | |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 312 | // log to main |
| 313 | |
| 314 | static const char comm_str[] = " comm=\""; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 315 | const char* comm = strstr(str, comm_str); |
| 316 | const char* estr = str + strlen(str); |
| 317 | const char* commfree = NULL; |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 318 | if (comm) { |
| 319 | estr = comm; |
| 320 | comm += sizeof(comm_str) - 1; |
| 321 | } else if (pid == getpid()) { |
| 322 | pid = tid; |
| 323 | comm = "auditd"; |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 324 | } else { |
| 325 | logbuf->lock(); |
| 326 | comm = commfree = logbuf->pidToName(pid); |
| 327 | logbuf->unlock(); |
| 328 | if (!comm) { |
| 329 | comm = "unknown"; |
| 330 | } |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 333 | const char* ecomm = strchr(comm, '"'); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 334 | if (ecomm) { |
| 335 | ++ecomm; |
| 336 | l = ecomm - comm; |
| 337 | } else { |
| 338 | l = strlen(comm) + 1; |
| 339 | ecomm = ""; |
| 340 | } |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 341 | size_t b = estr - str; |
| 342 | if (b > LOGGER_ENTRY_MAX_PAYLOAD) { |
| 343 | b = LOGGER_ENTRY_MAX_PAYLOAD; |
| 344 | } |
| 345 | size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b); |
| 346 | n = b + e + l + 2; |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 347 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 348 | if (main) { // begin scope for main buffer |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 349 | char newstr[n]; |
| 350 | |
Mark Salyzyn | 6bdeee0 | 2014-09-19 11:59:42 -0700 | [diff] [blame] | 351 | *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN; |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 352 | strlcpy(newstr + 1, comm, l); |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 353 | strncpy(newstr + 1 + l, str, b); |
| 354 | strncpy(newstr + 1 + l + b, ecomm, e); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 355 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 356 | rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr, |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 357 | (n <= USHRT_MAX) ? (unsigned short)n : USHRT_MAX); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 358 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 359 | if (rc >= 0) { |
| 360 | notify = true; |
| 361 | } |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 362 | // end scope for main buffer |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 365 | free(const_cast<char*>(commfree)); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 366 | free(str); |
| 367 | |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 368 | if (notify) { |
| 369 | reader->notifyNewLog(); |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 370 | if (rc < 0) { |
| 371 | rc = n; |
| 372 | } |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 373 | } |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 374 | |
| 375 | return rc; |
| 376 | } |
| 377 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 378 | int LogAudit::log(char* buf, size_t len) { |
| 379 | char* audit = strstr(buf, " audit("); |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 380 | if (!audit || (audit >= &buf[len])) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 381 | return 0; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 382 | } |
| 383 | |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 384 | *audit = '\0'; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 385 | |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 386 | int rc; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 387 | char* type = strstr(buf, "type="); |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 388 | if (type && (type < &buf[len])) { |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 389 | rc = logPrint("%s %s", type, audit + 1); |
| 390 | } else { |
| 391 | rc = logPrint("%s", audit + 1); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 392 | } |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 393 | *audit = ' '; |
| 394 | return rc; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | int LogAudit::getLogSocket() { |
| 398 | int fd = audit_open(); |
| 399 | if (fd < 0) { |
| 400 | return fd; |
| 401 | } |
Nick Kralevich | c234a1b | 2014-11-19 13:33:22 -0800 | [diff] [blame] | 402 | if (audit_setup(fd, getpid()) < 0) { |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 403 | audit_close(fd); |
| 404 | fd = -1; |
| 405 | } |
Mark Salyzyn | 247d682 | 2017-01-03 14:00:19 -0800 | [diff] [blame] | 406 | (void)audit_rate_limit(fd, AUDIT_RATE_LIMIT_DEFAULT); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 407 | return fd; |
| 408 | } |