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> |
| 18 | #include <errno.h> |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame^] | 19 | #include <limits.h> |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 20 | #include <stdarg.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/klog.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> |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 25 | |
| 26 | #include "libaudit.h" |
| 27 | #include "LogAudit.h" |
| 28 | |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 29 | LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmsg) |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 30 | : SocketListener(getLogSocket(), false) |
| 31 | , logbuf(buf) |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 32 | , reader(reader) |
| 33 | , fdDmesg(-1) { |
| 34 | logDmesg(); |
| 35 | fdDmesg = fdDmsg; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | bool LogAudit::onDataAvailable(SocketClient *cli) { |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 39 | prctl(PR_SET_NAME, "logd.auditd"); |
| 40 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 41 | struct audit_message rep; |
| 42 | |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame^] | 43 | rep.nlh.nlmsg_type = 0; |
| 44 | rep.nlh.nlmsg_len = 0; |
| 45 | rep.data[0] = '\0'; |
| 46 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 47 | if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) { |
| 48 | SLOGE("Failed on audit_get_reply with error: %s", strerror(errno)); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | logPrint("type=%d %.*s", rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data); |
| 53 | |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | #define AUDIT_LOG_ID LOG_ID_MAIN |
| 58 | #define AUDIT_LOG_PRIO ANDROID_LOG_WARN |
| 59 | |
| 60 | int LogAudit::logPrint(const char *fmt, ...) { |
| 61 | if (fmt == NULL) { |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | |
| 65 | va_list args; |
| 66 | |
| 67 | char *str = NULL; |
| 68 | va_start(args, fmt); |
| 69 | int rc = vasprintf(&str, fmt, args); |
| 70 | va_end(args); |
| 71 | |
| 72 | if (rc < 0) { |
| 73 | return rc; |
| 74 | } |
| 75 | |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 76 | if (fdDmesg >= 0) { |
| 77 | struct iovec iov[2]; |
| 78 | |
| 79 | iov[0].iov_base = str; |
| 80 | iov[0].iov_len = strlen(str); |
| 81 | iov[1].iov_base = const_cast<char *>("\n"); |
| 82 | iov[1].iov_len = 1; |
| 83 | |
| 84 | writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0])); |
| 85 | } |
| 86 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 87 | pid_t pid = getpid(); |
| 88 | pid_t tid = gettid(); |
| 89 | uid_t uid = getuid(); |
| 90 | log_time now; |
| 91 | |
| 92 | static const char audit_str[] = " audit("; |
| 93 | char *timeptr = strstr(str, audit_str); |
| 94 | char *cp; |
| 95 | if (timeptr |
| 96 | && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q"))) |
| 97 | && (*cp == ':')) { |
| 98 | memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3); |
| 99 | strcpy(timeptr + sizeof(audit_str) - 1 + 3, cp); |
| 100 | } else { |
| 101 | now.strptime("", ""); // side effect of setting CLOCK_REALTIME |
| 102 | } |
| 103 | |
| 104 | static const char pid_str[] = " pid="; |
| 105 | char *pidptr = strstr(str, pid_str); |
| 106 | if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) { |
| 107 | cp = pidptr + sizeof(pid_str) - 1; |
| 108 | pid = 0; |
| 109 | while (isdigit(*cp)) { |
| 110 | pid = (pid * 10) + (*cp - '0'); |
| 111 | ++cp; |
| 112 | } |
| 113 | tid = pid; |
| 114 | uid = logbuf->pidToUid(pid); |
| 115 | strcpy(pidptr, cp); |
| 116 | } |
| 117 | |
| 118 | static const char comm_str[] = " comm=\""; |
| 119 | char *comm = strstr(str, comm_str); |
| 120 | if (comm) { |
| 121 | cp = comm; |
| 122 | comm += sizeof(comm_str) - 1; |
| 123 | char *ecomm = strchr(comm, '"'); |
| 124 | if (ecomm) { |
| 125 | *ecomm = '\0'; |
| 126 | } |
| 127 | comm = strdup(comm); |
| 128 | if (ecomm) { |
| 129 | strcpy(cp, ecomm + 1); |
| 130 | } |
| 131 | } else if (pid == getpid()) { |
| 132 | pid = tid; |
| 133 | comm = strdup("auditd"); |
| 134 | } else if (!(comm = logbuf->pidToName(pid))) { |
| 135 | comm = strdup("unknown"); |
| 136 | } |
| 137 | |
| 138 | size_t l = strlen(comm) + 1; |
| 139 | size_t n = l + strlen(str) + 2; |
| 140 | |
| 141 | char *newstr = reinterpret_cast<char *>(malloc(n)); |
| 142 | if (!newstr) { |
| 143 | free(comm); |
| 144 | free(str); |
| 145 | return -ENOMEM; |
| 146 | } |
| 147 | |
| 148 | *newstr = AUDIT_LOG_PRIO; |
| 149 | strcpy(newstr + 1, comm); |
| 150 | free(comm); |
| 151 | strcpy(newstr + 1 + l, str); |
| 152 | free(str); |
| 153 | |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame^] | 154 | logbuf->log(AUDIT_LOG_ID, now, uid, pid, tid, newstr, |
| 155 | (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX); |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 156 | reader->notifyNewLog(); |
| 157 | |
| 158 | free(newstr); |
| 159 | |
| 160 | return rc; |
| 161 | } |
| 162 | |
Mark Salyzyn | e9bebd0 | 2014-04-03 09:55:26 -0700 | [diff] [blame] | 163 | void LogAudit::logDmesg() { |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 164 | int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
| 165 | if (len <= 0) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | len++; |
| 170 | char buf[len]; |
| 171 | |
| 172 | int rc = klogctl(KLOG_READ_ALL, buf, len); |
| 173 | |
| 174 | buf[len - 1] = '\0'; |
| 175 | |
| 176 | for(char *tok = buf; (rc >= 0) && ((tok = strtok(tok, "\r\n"))); tok = NULL) { |
| 177 | char *audit = strstr(tok, " audit("); |
| 178 | if (!audit) { |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | *audit++ = '\0'; |
| 183 | |
| 184 | char *type = strstr(tok, "type="); |
| 185 | if (type) { |
| 186 | rc = logPrint("%s %s", type, audit); |
| 187 | } else { |
| 188 | rc = logPrint("%s", audit); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | int LogAudit::getLogSocket() { |
| 194 | int fd = audit_open(); |
| 195 | if (fd < 0) { |
| 196 | return fd; |
| 197 | } |
| 198 | if (audit_set_pid(fd, getpid(), WAIT_YES) < 0) { |
| 199 | audit_close(fd); |
| 200 | fd = -1; |
| 201 | } |
| 202 | return fd; |
| 203 | } |