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