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