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 | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 23 | #include <sys/prctl.h> |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 24 | #include <sys/uio.h> |
Mark Salyzyn | 7ee2aef | 2014-09-28 14:41:50 -0700 | [diff] [blame] | 25 | #include <syslog.h> |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 26 | |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 27 | #include <log/logger.h> |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 29eb570 | 2015-03-03 16:21:27 -0800 | [diff] [blame] | 29 | #include <private/android_logger.h> |
| 30 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 31 | #include "libaudit.h" |
| 32 | #include "LogAudit.h" |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 33 | #include "LogKlog.h" |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 34 | |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 35 | #define KMSG_PRIORITY(PRI) \ |
| 36 | '<', \ |
| 37 | '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \ |
| 38 | '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, \ |
Mark Salyzyn | 7ee2aef | 2014-09-28 14:41:50 -0700 | [diff] [blame] | 39 | '>' |
| 40 | |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 41 | LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg) : |
| 42 | SocketListener(getLogSocket(), false), |
| 43 | logbuf(buf), |
| 44 | reader(reader), |
| 45 | fdDmesg(fdDmesg), |
| 46 | initialized(false) { |
Mark Salyzyn | 7ee2aef | 2014-09-28 14:41:50 -0700 | [diff] [blame] | 47 | static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO), |
| 48 | 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':', |
| 49 | ' ', 's', 't', 'a', 'r', 't', '\n' }; |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 50 | write(fdDmesg, auditd_message, sizeof(auditd_message)); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool LogAudit::onDataAvailable(SocketClient *cli) { |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 54 | if (!initialized) { |
| 55 | prctl(PR_SET_NAME, "logd.auditd"); |
| 56 | initialized = true; |
| 57 | } |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 58 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 59 | struct audit_message rep; |
| 60 | |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 61 | rep.nlh.nlmsg_type = 0; |
| 62 | rep.nlh.nlmsg_len = 0; |
| 63 | rep.data[0] = '\0'; |
| 64 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 65 | if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) { |
| 66 | SLOGE("Failed on audit_get_reply with error: %s", strerror(errno)); |
| 67 | return false; |
| 68 | } |
| 69 | |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 70 | logPrint("type=%d %.*s", |
| 71 | rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 76 | int LogAudit::logPrint(const char *fmt, ...) { |
| 77 | if (fmt == NULL) { |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | va_list args; |
| 82 | |
| 83 | char *str = NULL; |
| 84 | va_start(args, fmt); |
| 85 | int rc = vasprintf(&str, fmt, args); |
| 86 | va_end(args); |
| 87 | |
| 88 | if (rc < 0) { |
| 89 | return rc; |
| 90 | } |
| 91 | |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 92 | char *cp; |
| 93 | while ((cp = strstr(str, " "))) { |
| 94 | memmove(cp, cp + 1, strlen(cp + 1) + 1); |
| 95 | } |
| 96 | |
Mark Salyzyn | 6bdeee0 | 2014-09-19 11:59:42 -0700 | [diff] [blame] | 97 | bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded "); |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 98 | if ((fdDmesg >= 0) && initialized) { |
Mark Salyzyn | 6bdeee0 | 2014-09-19 11:59:42 -0700 | [diff] [blame] | 99 | struct iovec iov[3]; |
Mark Salyzyn | 7ee2aef | 2014-09-28 14:41:50 -0700 | [diff] [blame] | 100 | static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) }; |
| 101 | static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) }; |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 102 | |
Mark Salyzyn | 7ee2aef | 2014-09-28 14:41:50 -0700 | [diff] [blame] | 103 | iov[0].iov_base = info ? const_cast<char *>(log_info) |
| 104 | : const_cast<char *>(log_warning); |
| 105 | iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning); |
Mark Salyzyn | 6bdeee0 | 2014-09-19 11:59:42 -0700 | [diff] [blame] | 106 | iov[1].iov_base = str; |
| 107 | iov[1].iov_len = strlen(str); |
| 108 | iov[2].iov_base = const_cast<char *>("\n"); |
| 109 | iov[2].iov_len = 1; |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 110 | |
| 111 | writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0])); |
| 112 | } |
| 113 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 114 | pid_t pid = getpid(); |
| 115 | pid_t tid = gettid(); |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 116 | uid_t uid = AID_LOGD; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 117 | log_time now; |
| 118 | |
| 119 | static const char audit_str[] = " audit("; |
| 120 | char *timeptr = strstr(str, audit_str); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 121 | if (timeptr |
| 122 | && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q"))) |
| 123 | && (*cp == ':')) { |
| 124 | memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 125 | memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1); |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 126 | if (!isMonotonic()) { |
| 127 | if (android::isMonotonic(now)) { |
| 128 | LogKlog::convertMonotonicToReal(now); |
| 129 | } |
| 130 | } else { |
| 131 | if (!android::isMonotonic(now)) { |
| 132 | LogKlog::convertRealToMonotonic(now); |
| 133 | } |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 134 | } |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 135 | } else if (isMonotonic()) { |
| 136 | now = log_time(CLOCK_MONOTONIC); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 137 | } else { |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 138 | now = log_time(CLOCK_REALTIME); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static const char pid_str[] = " pid="; |
| 142 | char *pidptr = strstr(str, pid_str); |
| 143 | if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) { |
| 144 | cp = pidptr + sizeof(pid_str) - 1; |
| 145 | pid = 0; |
| 146 | while (isdigit(*cp)) { |
| 147 | pid = (pid * 10) + (*cp - '0'); |
| 148 | ++cp; |
| 149 | } |
| 150 | tid = pid; |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 151 | logbuf->lock(); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 152 | uid = logbuf->pidToUid(pid); |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 153 | logbuf->unlock(); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 154 | memmove(pidptr, cp, strlen(cp) + 1); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 155 | } |
| 156 | |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 157 | // log to events |
| 158 | |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 159 | size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD); |
Mark Salyzyn | 29eb570 | 2015-03-03 16:21:27 -0800 | [diff] [blame] | 160 | size_t n = l + sizeof(android_log_event_string_t); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 161 | |
| 162 | bool notify = false; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 163 | |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 164 | { // begin scope for event buffer |
| 165 | uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; |
| 166 | |
| 167 | android_log_event_string_t *event |
| 168 | = reinterpret_cast<android_log_event_string_t *>(buffer); |
Mark Salyzyn | 29eb570 | 2015-03-03 16:21:27 -0800 | [diff] [blame] | 169 | event->header.tag = htole32(AUDITD_LOG_TAG); |
Nick Kralevich | 58ba58a | 2015-04-07 01:25:43 -0700 | [diff] [blame] | 170 | event->type = EVENT_TYPE_STRING; |
| 171 | event->length = htole32(l); |
| 172 | memcpy(event->data, str, l); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 173 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 174 | rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid, |
| 175 | reinterpret_cast<char *>(event), |
| 176 | (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX); |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 177 | if (rc >= 0) { |
| 178 | notify = true; |
| 179 | } |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 180 | // end scope for event buffer |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 181 | } |
| 182 | |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 183 | // log to main |
| 184 | |
| 185 | static const char comm_str[] = " comm=\""; |
| 186 | const char *comm = strstr(str, comm_str); |
| 187 | const char *estr = str + strlen(str); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 188 | const char *commfree = NULL; |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 189 | if (comm) { |
| 190 | estr = comm; |
| 191 | comm += sizeof(comm_str) - 1; |
| 192 | } else if (pid == getpid()) { |
| 193 | pid = tid; |
| 194 | comm = "auditd"; |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 195 | } else { |
| 196 | logbuf->lock(); |
| 197 | comm = commfree = logbuf->pidToName(pid); |
| 198 | logbuf->unlock(); |
| 199 | if (!comm) { |
| 200 | comm = "unknown"; |
| 201 | } |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | const char *ecomm = strchr(comm, '"'); |
| 205 | if (ecomm) { |
| 206 | ++ecomm; |
| 207 | l = ecomm - comm; |
| 208 | } else { |
| 209 | l = strlen(comm) + 1; |
| 210 | ecomm = ""; |
| 211 | } |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 212 | size_t b = estr - str; |
| 213 | if (b > LOGGER_ENTRY_MAX_PAYLOAD) { |
| 214 | b = LOGGER_ENTRY_MAX_PAYLOAD; |
| 215 | } |
| 216 | size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b); |
| 217 | n = b + e + l + 2; |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 218 | |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 219 | { // begin scope for main buffer |
| 220 | char newstr[n]; |
| 221 | |
Mark Salyzyn | 6bdeee0 | 2014-09-19 11:59:42 -0700 | [diff] [blame] | 222 | *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN; |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 223 | strlcpy(newstr + 1, comm, l); |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 224 | strncpy(newstr + 1 + l, str, b); |
| 225 | strncpy(newstr + 1 + l + b, ecomm, e); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 226 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 227 | rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr, |
| 228 | (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX); |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 229 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 230 | if (rc >= 0) { |
| 231 | notify = true; |
| 232 | } |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 233 | // end scope for main buffer |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 236 | free(const_cast<char *>(commfree)); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 237 | free(str); |
| 238 | |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 239 | if (notify) { |
| 240 | reader->notifyNewLog(); |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 241 | if (rc < 0) { |
| 242 | rc = n; |
| 243 | } |
Mark Salyzyn | e4369d6 | 2014-05-27 10:06:34 -0700 | [diff] [blame] | 244 | } |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 245 | |
| 246 | return rc; |
| 247 | } |
| 248 | |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 249 | int LogAudit::log(char *buf, size_t len) { |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 250 | char *audit = strstr(buf, " audit("); |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 251 | if (!audit || (audit >= &buf[len])) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 252 | return 0; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 253 | } |
| 254 | |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 255 | *audit = '\0'; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 256 | |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 257 | int rc; |
| 258 | char *type = strstr(buf, "type="); |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 259 | if (type && (type < &buf[len])) { |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 260 | rc = logPrint("%s %s", type, audit + 1); |
| 261 | } else { |
| 262 | rc = logPrint("%s", audit + 1); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 263 | } |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 264 | *audit = ' '; |
| 265 | return rc; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int LogAudit::getLogSocket() { |
| 269 | int fd = audit_open(); |
| 270 | if (fd < 0) { |
| 271 | return fd; |
| 272 | } |
Nick Kralevich | c234a1b | 2014-11-19 13:33:22 -0800 | [diff] [blame] | 273 | if (audit_setup(fd, getpid()) < 0) { |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 274 | audit_close(fd); |
| 275 | fd = -1; |
| 276 | } |
| 277 | return fd; |
| 278 | } |