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