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