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