blob: 0651a927697684ece4b8928889c7fa29e267a5d3 [file] [log] [blame]
William Roberts29d238d2013-02-08 09:45:26 +09001/*
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 Salyzyne0fa2912014-04-28 16:39:04 -070019#include <limits.h>
William Roberts29d238d2013-02-08 09:45:26 +090020#include <stdarg.h>
21#include <stdlib.h>
22#include <sys/klog.h>
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070023#include <sys/prctl.h>
Mark Salyzyne9bebd02014-04-03 09:55:26 -070024#include <sys/uio.h>
William Roberts29d238d2013-02-08 09:45:26 +090025
26#include "libaudit.h"
27#include "LogAudit.h"
28
Mark Salyzyne9bebd02014-04-03 09:55:26 -070029LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmsg)
William Roberts29d238d2013-02-08 09:45:26 +090030 : SocketListener(getLogSocket(), false)
31 , logbuf(buf)
Mark Salyzyne9bebd02014-04-03 09:55:26 -070032 , reader(reader)
33 , fdDmesg(-1) {
34 logDmesg();
35 fdDmesg = fdDmsg;
William Roberts29d238d2013-02-08 09:45:26 +090036}
37
38bool LogAudit::onDataAvailable(SocketClient *cli) {
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070039 prctl(PR_SET_NAME, "logd.auditd");
40
William Roberts29d238d2013-02-08 09:45:26 +090041 struct audit_message rep;
42
Mark Salyzyne0fa2912014-04-28 16:39:04 -070043 rep.nlh.nlmsg_type = 0;
44 rep.nlh.nlmsg_len = 0;
45 rep.data[0] = '\0';
46
William Roberts29d238d2013-02-08 09:45:26 +090047 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
William Roberts29d238d2013-02-08 09:45:26 +090057int LogAudit::logPrint(const char *fmt, ...) {
58 if (fmt == NULL) {
59 return -EINVAL;
60 }
61
62 va_list args;
63
64 char *str = NULL;
65 va_start(args, fmt);
66 int rc = vasprintf(&str, fmt, args);
67 va_end(args);
68
69 if (rc < 0) {
70 return rc;
71 }
72
Mark Salyzyne9bebd02014-04-03 09:55:26 -070073 if (fdDmesg >= 0) {
74 struct iovec iov[2];
75
76 iov[0].iov_base = str;
77 iov[0].iov_len = strlen(str);
78 iov[1].iov_base = const_cast<char *>("\n");
79 iov[1].iov_len = 1;
80
81 writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0]));
82 }
83
William Roberts29d238d2013-02-08 09:45:26 +090084 pid_t pid = getpid();
85 pid_t tid = gettid();
86 uid_t uid = getuid();
87 log_time now;
88
89 static const char audit_str[] = " audit(";
90 char *timeptr = strstr(str, audit_str);
91 char *cp;
92 if (timeptr
93 && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q")))
94 && (*cp == ':')) {
95 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
96 strcpy(timeptr + sizeof(audit_str) - 1 + 3, cp);
97 } else {
98 now.strptime("", ""); // side effect of setting CLOCK_REALTIME
99 }
100
101 static const char pid_str[] = " pid=";
102 char *pidptr = strstr(str, pid_str);
103 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
104 cp = pidptr + sizeof(pid_str) - 1;
105 pid = 0;
106 while (isdigit(*cp)) {
107 pid = (pid * 10) + (*cp - '0');
108 ++cp;
109 }
110 tid = pid;
111 uid = logbuf->pidToUid(pid);
112 strcpy(pidptr, cp);
113 }
114
Mark Salyzyn989980c2014-05-14 12:37:22 -0700115 size_t n = strlen(str);
116 n += sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint32_t);
William Roberts29d238d2013-02-08 09:45:26 +0900117
118 char *newstr = reinterpret_cast<char *>(malloc(n));
119 if (!newstr) {
William Roberts29d238d2013-02-08 09:45:26 +0900120 free(str);
121 return -ENOMEM;
122 }
123
Mark Salyzyn989980c2014-05-14 12:37:22 -0700124 char *msg = newstr;
125 *msg++ = AUDITD_LOG_TAG & 0xFF;
126 *msg++ = (AUDITD_LOG_TAG >> 8) & 0xFF;
127 *msg++ = (AUDITD_LOG_TAG >> 16) & 0xFF;
128 *msg++ = (AUDITD_LOG_TAG >> 24) & 0xFF;
129 *msg++ = EVENT_TYPE_STRING;
130 size_t l = n - sizeof(uint32_t) - sizeof(uint8_t) - sizeof(uint32_t);
131 *msg++ = l & 0xFF;
132 *msg++ = (l >> 8) & 0xFF;
133 *msg++ = (l >> 16) & 0xFF;
134 *msg++ = (l >> 24) & 0xFF;
135 memcpy(msg, str, l);
William Roberts29d238d2013-02-08 09:45:26 +0900136 free(str);
137
Mark Salyzyn989980c2014-05-14 12:37:22 -0700138 logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid, newstr,
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700139 (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
William Roberts29d238d2013-02-08 09:45:26 +0900140 reader->notifyNewLog();
141
142 free(newstr);
143
144 return rc;
145}
146
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700147void LogAudit::logDmesg() {
William Roberts29d238d2013-02-08 09:45:26 +0900148 int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
149 if (len <= 0) {
150 return;
151 }
152
153 len++;
154 char buf[len];
155
156 int rc = klogctl(KLOG_READ_ALL, buf, len);
157
158 buf[len - 1] = '\0';
159
160 for(char *tok = buf; (rc >= 0) && ((tok = strtok(tok, "\r\n"))); tok = NULL) {
161 char *audit = strstr(tok, " audit(");
162 if (!audit) {
163 continue;
164 }
165
166 *audit++ = '\0';
167
168 char *type = strstr(tok, "type=");
169 if (type) {
170 rc = logPrint("%s %s", type, audit);
171 } else {
172 rc = logPrint("%s", audit);
173 }
174 }
175}
176
177int LogAudit::getLogSocket() {
178 int fd = audit_open();
179 if (fd < 0) {
180 return fd;
181 }
182 if (audit_set_pid(fd, getpid(), WAIT_YES) < 0) {
183 audit_close(fd);
184 fd = -1;
185 }
186 return fd;
187}