blob: 01c78c2a87157846bf56bf87c5a3fce956ce7915 [file] [log] [blame]
William Roberts210c5842013-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
Tom Cherry479b5032020-05-07 14:44:43 -070017#include "LogAudit.h"
18
William Roberts210c5842013-02-08 09:45:26 +090019#include <ctype.h>
Mark Salyzyn64679af2015-03-03 16:21:27 -080020#include <endian.h>
William Roberts210c5842013-02-08 09:45:26 +090021#include <errno.h>
Mark Salyzyn59d09102014-04-28 16:39:04 -070022#include <limits.h>
William Roberts210c5842013-02-08 09:45:26 +090023#include <stdarg.h>
Chih-Hung Hsieh9ff81552018-08-13 14:22:56 -070024#include <stdint.h>
William Roberts210c5842013-02-08 09:45:26 +090025#include <stdlib.h>
Mark Salyzyna92051d2016-02-23 08:55:43 -080026#include <string.h>
Mark Salyzyn4e5efdc2014-04-28 14:07:23 -070027#include <sys/prctl.h>
Mark Salyzynae0a7bf2014-04-03 09:55:26 -070028#include <sys/uio.h>
Mark Salyzynaa773a82014-09-28 14:41:50 -070029#include <syslog.h>
William Roberts210c5842013-02-08 09:45:26 +090030
Yi-Yo Chiang7e7eee42021-11-08 19:31:33 +080031#include <android-base/file.h>
32#include <android-base/logging.h>
Mark Salyzyn071f4692016-11-03 10:29:23 -070033#include <android-base/macros.h>
Tom Cherry41401172020-07-28 09:51:54 -070034#include <android-base/properties.h>
Yi-Yo Chiang7e7eee42021-11-08 19:31:33 +080035#include <android-base/strings.h>
Mark Salyzynb4853952015-03-17 07:56:32 -070036#include <private/android_filesystem_config.h>
Mark Salyzyn64679af2015-03-03 16:21:27 -080037#include <private/android_logger.h>
38
Mark Salyzyne73a18b2014-10-15 08:49:39 -070039#include "LogKlog.h"
Mark Salyzyn24a57dc2016-12-13 10:31:29 -080040#include "LogUtils.h"
Mark Salyzyn65059532017-03-10 14:31:54 -080041#include "libaudit.h"
William Roberts210c5842013-02-08 09:45:26 +090042
Yo Chiang2997fa62021-01-27 19:50:43 +080043using namespace std::string_literals;
44
Tom Cherry41401172020-07-28 09:51:54 -070045using android::base::GetBoolProperty;
46
Mark Salyzyn65059532017-03-10 14:31:54 -080047#define KMSG_PRIORITY(PRI) \
48 '<', '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
49 '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, '>'
Mark Salyzynaa773a82014-09-28 14:41:50 -070050
Tom Cherry5ecfbf02020-05-11 16:29:29 -070051LogAudit::LogAudit(LogBuffer* buf, int fdDmesg, LogStatistics* stats)
Jeff Vander Stoepb66ed0f2018-01-03 11:04:26 -080052 : SocketListener(getLogSocket(), false),
Mark Salyzyn65059532017-03-10 14:31:54 -080053 logbuf(buf),
Mark Salyzyn65059532017-03-10 14:31:54 -080054 fdDmesg(fdDmesg),
Tom Cherry41401172020-07-28 09:51:54 -070055 main(GetBoolProperty("ro.logd.auditd.main", true)),
56 events(GetBoolProperty("ro.logd.auditd.events", true)),
Tom Cherry479b5032020-05-07 14:44:43 -070057 initialized(false),
58 stats_(stats) {
Sami Tolvanenba239532016-06-14 18:04:43 +000059 static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
Mark Salyzyn65059532017-03-10 14:31:54 -080060 'l',
61 'o',
62 'g',
63 'd',
64 '.',
65 'a',
66 'u',
67 'd',
68 'i',
69 't',
70 'd',
71 ':',
72 ' ',
73 's',
74 't',
75 'a',
76 'r',
77 't',
78 '\n' };
Sami Tolvanenba239532016-06-14 18:04:43 +000079 write(fdDmesg, auditd_message, sizeof(auditd_message));
William Roberts210c5842013-02-08 09:45:26 +090080}
81
Mark Salyzyn65059532017-03-10 14:31:54 -080082bool LogAudit::onDataAvailable(SocketClient* cli) {
Mark Salyzyn78956ab2014-10-13 09:59:37 -070083 if (!initialized) {
84 prctl(PR_SET_NAME, "logd.auditd");
85 initialized = true;
86 }
Mark Salyzyn4e5efdc2014-04-28 14:07:23 -070087
William Roberts210c5842013-02-08 09:45:26 +090088 struct audit_message rep;
89
Mark Salyzyn59d09102014-04-28 16:39:04 -070090 rep.nlh.nlmsg_type = 0;
91 rep.nlh.nlmsg_len = 0;
92 rep.data[0] = '\0';
93
William Roberts210c5842013-02-08 09:45:26 +090094 if (audit_get_reply(cli->getSocket(), &rep, GET_REPLY_BLOCKING, 0) < 0) {
95 SLOGE("Failed on audit_get_reply with error: %s", strerror(errno));
96 return false;
97 }
98
Mark Salyzynec9b9ad2017-01-03 14:00:19 -080099 logPrint("type=%d %.*s", rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
William Roberts210c5842013-02-08 09:45:26 +0900100
101 return true;
102}
103
Max Bires9cf521b2017-08-15 11:07:49 -0700104static inline bool hasMetadata(char* str, int str_len) {
105 // need to check and see if str already contains bug metadata from
106 // possibility of stuttering if log audit crashes and then reloads kernel
107 // messages. Kernel denials that contain metadata will either end in
108 // "b/[0-9]+$" or "b/[0-9]+ duplicate messages suppressed$" which will put
109 // a '/' character at either 9 or 39 indices away from the end of the str.
110 return str_len >= 39 &&
111 (str[str_len - 9] == '/' || str[str_len - 39] == '/');
112}
113
Yi-Yo Chiang6d026fd2021-11-09 14:54:56 +0800114static auto populateDenialMap() {
115 std::map<std::tuple<std::string, std::string, std::string>, std::string> denial_to_bug;
Yi-Yo Chiang7e7eee42021-11-08 19:31:33 +0800116 // Order matters. Only the first occurrence of a
117 // (scontext, tcontext, tclass) combination is recorded.
118 for (const auto& bug_map_file :
119 {"/system_ext/etc/selinux/bug_map"s, "/vendor/etc/selinux/selinux_denial_metadata"s,
120 "/system/etc/selinux/bug_map"s}) {
121 std::string file_contents;
122 if (!android::base::ReadFileToString(bug_map_file, &file_contents)) {
123 continue;
124 }
125 int errors = 0;
126 for (const auto& line : android::base::Split(file_contents, "\n")) {
127 const auto fields = android::base::Tokenize(line, " ");
128 if (fields.empty() || android::base::StartsWith(fields.front(), '#')) {
129 continue;
130 }
131 if (fields.size() == 4) {
132 const std::string& scontext = fields[0];
133 const std::string& tcontext = fields[1];
134 const std::string& tclass = fields[2];
135 const std::string& bug_num = fields[3];
136 const auto [it, success] =
137 denial_to_bug.try_emplace({scontext, tcontext, tclass}, bug_num);
138 if (!success) {
139 const auto& [key, value] = *it;
140 LOG(WARNING) << "Ignored bug_map definition in " << bug_map_file << ": '"
141 << line
142 << "', (scontext, tcontext, tclass) denial combination is already "
143 "tagged with bug metadata '"
144 << value << "'";
145 }
146 } else {
147 LOG(ERROR) << "Ignored ill-formed bug_map definition in " << bug_map_file << ": '"
148 << line << "'";
149 ++errors;
150 }
151 }
152 if (errors) {
153 LOG(ERROR) << "Loaded bug_map file with " << errors << " errors: " << bug_map_file;
154 } else {
155 LOG(INFO) << "Loaded bug_map file: " << bug_map_file;
Max Bires9cf521b2017-08-15 11:07:49 -0700156 }
157 }
158 return denial_to_bug;
159}
160
161std::string LogAudit::denialParse(const std::string& denial, char terminator,
162 const std::string& search_term) {
163 size_t start_index = denial.find(search_term);
164 if (start_index != std::string::npos) {
165 start_index += search_term.length();
166 return denial.substr(
167 start_index, denial.find(terminator, start_index) - start_index);
168 }
169 return "";
170}
171
Yo Chiang2997fa62021-01-27 19:50:43 +0800172std::string LogAudit::auditParse(const std::string& string, uid_t uid) {
Yi-Yo Chiang6d026fd2021-11-09 14:54:56 +0800173 // Allocate a static map object to memoize the loaded bug_map files.
174 static auto denial_to_bug = populateDenialMap();
175
Yo Chiang2997fa62021-01-27 19:50:43 +0800176 std::string result;
Max Bires9cf521b2017-08-15 11:07:49 -0700177 std::string scontext = denialParse(string, ':', "scontext=u:object_r:");
178 std::string tcontext = denialParse(string, ':', "tcontext=u:object_r:");
179 std::string tclass = denialParse(string, ' ', "tclass=");
180 if (scontext.empty()) {
181 scontext = denialParse(string, ':', "scontext=u:r:");
182 }
183 if (tcontext.empty()) {
184 tcontext = denialParse(string, ':', "tcontext=u:r:");
185 }
Yi-Yo Chiang6d026fd2021-11-09 14:54:56 +0800186 auto search = denial_to_bug.find({scontext, tcontext, tclass});
Max Bires9cf521b2017-08-15 11:07:49 -0700187 if (search != denial_to_bug.end()) {
Yo Chiang2997fa62021-01-27 19:50:43 +0800188 result = " bug=" + search->second;
Max Bires9cf521b2017-08-15 11:07:49 -0700189 }
Jeff Vander Stoep78beb5e2018-05-03 14:57:39 -0700190
Dan Austin491b8242018-11-27 07:30:11 -0800191 // Ensure the uid name is not null before passing it to the bug string.
Jeff Vander Stoep78beb5e2018-05-03 14:57:39 -0700192 if (uid >= AID_APP_START && uid <= AID_APP_END) {
Dan Austin491b8242018-11-27 07:30:11 -0800193 char* uidname = android::uidToName(uid);
194 if (uidname) {
Yo Chiang2997fa62021-01-27 19:50:43 +0800195 result.append(" app="s + uidname);
Dan Austin491b8242018-11-27 07:30:11 -0800196 free(uidname);
197 }
Jeff Vander Stoep78beb5e2018-05-03 14:57:39 -0700198 }
Yo Chiang2997fa62021-01-27 19:50:43 +0800199 return result;
Max Bires9cf521b2017-08-15 11:07:49 -0700200}
201
Mark Salyzyn65059532017-03-10 14:31:54 -0800202int LogAudit::logPrint(const char* fmt, ...) {
Yi Kong36feb152018-07-13 17:39:22 -0700203 if (fmt == nullptr) {
William Roberts210c5842013-02-08 09:45:26 +0900204 return -EINVAL;
205 }
206
207 va_list args;
208
Yi Kong36feb152018-07-13 17:39:22 -0700209 char* str = nullptr;
William Roberts210c5842013-02-08 09:45:26 +0900210 va_start(args, fmt);
211 int rc = vasprintf(&str, fmt, args);
212 va_end(args);
213
214 if (rc < 0) {
215 return rc;
216 }
Mark Salyzyn65059532017-03-10 14:31:54 -0800217 char* cp;
Nick Kralevich908486f2017-01-03 10:35:34 -0800218 // Work around kernels missing
219 // https://github.com/torvalds/linux/commit/b8f89caafeb55fba75b74bea25adc4e4cd91be67
220 // Such kernels improperly add newlines inside audit messages.
221 while ((cp = strchr(str, '\n'))) {
222 *cp = ' ';
223 }
224
Jeff Vander Stoep78beb5e2018-05-03 14:57:39 -0700225 pid_t pid = getpid();
226 pid_t tid = gettid();
227 uid_t uid = AID_LOGD;
228 static const char pid_str[] = " pid=";
229 char* pidptr = strstr(str, pid_str);
230 if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
231 cp = pidptr + sizeof(pid_str) - 1;
232 pid = 0;
233 while (isdigit(*cp)) {
234 pid = (pid * 10) + (*cp - '0');
235 ++cp;
236 }
237 tid = pid;
Tom Cherry479b5032020-05-07 14:44:43 -0700238 uid = stats_->PidToUid(pid);
Jeff Vander Stoep78beb5e2018-05-03 14:57:39 -0700239 memmove(pidptr, cp, strlen(cp) + 1);
240 }
241
Sami Tolvanenba239532016-06-14 18:04:43 +0000242 bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
Jeff Vander Stoep78beb5e2018-05-03 14:57:39 -0700243 static std::string denial_metadata;
Mark Salyzyn78956ab2014-10-13 09:59:37 -0700244 if ((fdDmesg >= 0) && initialized) {
Max Bires9cf521b2017-08-15 11:07:49 -0700245 struct iovec iov[4];
Mark Salyzynaa773a82014-09-28 14:41:50 -0700246 static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
247 static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
Mark Salyzynf815ca72016-07-15 15:45:58 -0700248 static const char newline[] = "\n";
Mark Salyzynae0a7bf2014-04-03 09:55:26 -0700249
Yo Chiang2997fa62021-01-27 19:50:43 +0800250 denial_metadata = auditParse(str, uid);
Nick Kralevichba406b22019-04-24 13:29:35 -0700251 iov[0].iov_base = info ? const_cast<char*>(log_info) : const_cast<char*>(log_warning);
252 iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
253 iov[1].iov_base = str;
254 iov[1].iov_len = strlen(str);
255 iov[2].iov_base = const_cast<char*>(denial_metadata.c_str());
256 iov[2].iov_len = denial_metadata.length();
257 iov[3].iov_base = const_cast<char*>(newline);
258 iov[3].iov_len = strlen(newline);
Mark Salyzynae0a7bf2014-04-03 09:55:26 -0700259
Nick Kralevichba406b22019-04-24 13:29:35 -0700260 writev(fdDmesg, iov, arraysize(iov));
Mark Salyzynae0a7bf2014-04-03 09:55:26 -0700261 }
262
Mark Salyzyncaf43862016-12-29 15:16:06 -0800263 if (!main && !events) {
264 free(str);
265 return 0;
266 }
267
Bernie Innocenti440e0352019-01-16 15:25:18 +0900268 log_time now(log_time::EPOCH);
William Roberts210c5842013-02-08 09:45:26 +0900269
270 static const char audit_str[] = " audit(";
Mark Salyzyn65059532017-03-10 14:31:54 -0800271 char* timeptr = strstr(str, audit_str);
Tom Cherrya5ff7ae2020-04-08 14:36:05 -0700272 if (timeptr && ((cp = now.strptime(timeptr + sizeof(audit_str) - 1, "%s.%q"))) &&
Mark Salyzyn65059532017-03-10 14:31:54 -0800273 (*cp == ':')) {
William Roberts210c5842013-02-08 09:45:26 +0900274 memcpy(timeptr + sizeof(audit_str) - 1, "0.0", 3);
Mark Salyzyn380423a2014-05-27 10:06:34 -0700275 memmove(timeptr + sizeof(audit_str) - 1 + 3, cp, strlen(cp) + 1);
William Roberts210c5842013-02-08 09:45:26 +0900276 } else {
Mark Salyzyn0e2fe422015-09-08 08:56:32 -0700277 now = log_time(CLOCK_REALTIME);
William Roberts210c5842013-02-08 09:45:26 +0900278 }
279
Mark Salyzyn380423a2014-05-27 10:06:34 -0700280 // log to events
281
Max Bires9cf521b2017-08-15 11:07:49 -0700282 size_t str_len = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
283 if (((fdDmesg < 0) || !initialized) && !hasMetadata(str, str_len))
Yo Chiang2997fa62021-01-27 19:50:43 +0800284 denial_metadata = auditParse(str, uid);
Jeff Vander Stoep78beb5e2018-05-03 14:57:39 -0700285 str_len = (str_len + denial_metadata.length() <= LOGGER_ENTRY_MAX_PAYLOAD)
286 ? str_len + denial_metadata.length()
Max Bires9cf521b2017-08-15 11:07:49 -0700287 : LOGGER_ENTRY_MAX_PAYLOAD;
288 size_t message_len = str_len + sizeof(android_log_event_string_t);
Mark Salyzyn380423a2014-05-27 10:06:34 -0700289
Tom Cherry7e3bc962020-05-04 17:10:16 -0700290 unsigned int notify = 0;
William Roberts210c5842013-02-08 09:45:26 +0900291
Mark Salyzyn65059532017-03-10 14:31:54 -0800292 if (events) { // begin scope for event buffer
Max Bires9cf521b2017-08-15 11:07:49 -0700293 uint32_t buffer[(message_len + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
Mark Salyzynebaf2d62015-10-02 09:22:52 -0700294
Mark Salyzyn65059532017-03-10 14:31:54 -0800295 android_log_event_string_t* event =
296 reinterpret_cast<android_log_event_string_t*>(buffer);
Mark Salyzyn64679af2015-03-03 16:21:27 -0800297 event->header.tag = htole32(AUDITD_LOG_TAG);
Nick Kralevich253374f2015-04-07 01:25:43 -0700298 event->type = EVENT_TYPE_STRING;
Max Bires42ec64b2017-09-14 13:01:28 -0700299 event->length = htole32(str_len);
Jeff Vander Stoep78beb5e2018-05-03 14:57:39 -0700300 memcpy(event->data, str, str_len - denial_metadata.length());
301 memcpy(event->data + str_len - denial_metadata.length(),
302 denial_metadata.c_str(), denial_metadata.length());
Mark Salyzyn380423a2014-05-27 10:06:34 -0700303
Tom Cherry0281b202020-05-12 13:16:41 -0700304 rc = logbuf->Log(LOG_ID_EVENTS, now, uid, pid, tid, reinterpret_cast<char*>(event),
305 (message_len <= UINT16_MAX) ? (uint16_t)message_len : UINT16_MAX);
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800306 if (rc >= 0) {
Hao Wang10d19f62017-12-04 14:10:40 +0800307 notify |= 1 << LOG_ID_EVENTS;
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800308 }
Mark Salyzynebaf2d62015-10-02 09:22:52 -0700309 // end scope for event buffer
William Roberts210c5842013-02-08 09:45:26 +0900310 }
311
Mark Salyzyn380423a2014-05-27 10:06:34 -0700312 // log to main
313
314 static const char comm_str[] = " comm=\"";
Mark Salyzyn65059532017-03-10 14:31:54 -0800315 const char* comm = strstr(str, comm_str);
316 const char* estr = str + strlen(str);
Yi Kong36feb152018-07-13 17:39:22 -0700317 const char* commfree = nullptr;
Mark Salyzyn380423a2014-05-27 10:06:34 -0700318 if (comm) {
319 estr = comm;
320 comm += sizeof(comm_str) - 1;
321 } else if (pid == getpid()) {
322 pid = tid;
323 comm = "auditd";
Mark Salyzyn059b7582015-06-24 16:22:54 -0700324 } else {
Tom Cherry479b5032020-05-07 14:44:43 -0700325 comm = commfree = stats_->PidToName(pid);
Mark Salyzyn059b7582015-06-24 16:22:54 -0700326 if (!comm) {
327 comm = "unknown";
328 }
Mark Salyzyn380423a2014-05-27 10:06:34 -0700329 }
330
Mark Salyzyn65059532017-03-10 14:31:54 -0800331 const char* ecomm = strchr(comm, '"');
Mark Salyzyn380423a2014-05-27 10:06:34 -0700332 if (ecomm) {
333 ++ecomm;
Max Bires9cf521b2017-08-15 11:07:49 -0700334 str_len = ecomm - comm;
Mark Salyzyn380423a2014-05-27 10:06:34 -0700335 } else {
Max Bires9cf521b2017-08-15 11:07:49 -0700336 str_len = strlen(comm) + 1;
Mark Salyzyn380423a2014-05-27 10:06:34 -0700337 ecomm = "";
338 }
Max Bires9cf521b2017-08-15 11:07:49 -0700339 size_t prefix_len = estr - str;
340 if (prefix_len > LOGGER_ENTRY_MAX_PAYLOAD) {
341 prefix_len = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzynebaf2d62015-10-02 09:22:52 -0700342 }
Max Bires9cf521b2017-08-15 11:07:49 -0700343 size_t suffix_len = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - prefix_len);
Jeff Vander Stoep78beb5e2018-05-03 14:57:39 -0700344 message_len =
345 str_len + prefix_len + suffix_len + denial_metadata.length() + 2;
Mark Salyzyn380423a2014-05-27 10:06:34 -0700346
Mark Salyzyn65059532017-03-10 14:31:54 -0800347 if (main) { // begin scope for main buffer
Max Bires9cf521b2017-08-15 11:07:49 -0700348 char newstr[message_len];
Mark Salyzynebaf2d62015-10-02 09:22:52 -0700349
Mark Salyzyn2904c7f2014-09-19 11:59:42 -0700350 *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
Max Bires9cf521b2017-08-15 11:07:49 -0700351 strlcpy(newstr + 1, comm, str_len);
352 strncpy(newstr + 1 + str_len, str, prefix_len);
353 strncpy(newstr + 1 + str_len + prefix_len, ecomm, suffix_len);
354 strncpy(newstr + 1 + str_len + prefix_len + suffix_len,
Jeff Vander Stoep78beb5e2018-05-03 14:57:39 -0700355 denial_metadata.c_str(), denial_metadata.length());
Mark Salyzyn380423a2014-05-27 10:06:34 -0700356
Tom Cherry0281b202020-05-12 13:16:41 -0700357 rc = logbuf->Log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
358 (message_len <= UINT16_MAX) ? (uint16_t)message_len : UINT16_MAX);
Mark Salyzyn380423a2014-05-27 10:06:34 -0700359
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800360 if (rc >= 0) {
Hao Wang10d19f62017-12-04 14:10:40 +0800361 notify |= 1 << LOG_ID_MAIN;
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800362 }
Mark Salyzynebaf2d62015-10-02 09:22:52 -0700363 // end scope for main buffer
Mark Salyzyn380423a2014-05-27 10:06:34 -0700364 }
365
Mark Salyzyn65059532017-03-10 14:31:54 -0800366 free(const_cast<char*>(commfree));
William Roberts210c5842013-02-08 09:45:26 +0900367 free(str);
368
Mark Salyzyn380423a2014-05-27 10:06:34 -0700369 if (notify) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800370 if (rc < 0) {
Max Bires9cf521b2017-08-15 11:07:49 -0700371 rc = message_len;
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800372 }
Mark Salyzyn380423a2014-05-27 10:06:34 -0700373 }
William Roberts210c5842013-02-08 09:45:26 +0900374
375 return rc;
376}
377
Mark Salyzyn65059532017-03-10 14:31:54 -0800378int LogAudit::log(char* buf, size_t len) {
379 char* audit = strstr(buf, " audit(");
Mark Salyzynfd2c1cc2015-09-04 11:37:42 -0700380 if (!audit || (audit >= &buf[len])) {
Mark Salyzyne73a18b2014-10-15 08:49:39 -0700381 return 0;
William Roberts210c5842013-02-08 09:45:26 +0900382 }
383
Mark Salyzyn78956ab2014-10-13 09:59:37 -0700384 *audit = '\0';
William Roberts210c5842013-02-08 09:45:26 +0900385
Mark Salyzyn78956ab2014-10-13 09:59:37 -0700386 int rc;
Mark Salyzyn65059532017-03-10 14:31:54 -0800387 char* type = strstr(buf, "type=");
Mark Salyzynfd2c1cc2015-09-04 11:37:42 -0700388 if (type && (type < &buf[len])) {
Mark Salyzyn78956ab2014-10-13 09:59:37 -0700389 rc = logPrint("%s %s", type, audit + 1);
390 } else {
391 rc = logPrint("%s", audit + 1);
William Roberts210c5842013-02-08 09:45:26 +0900392 }
Mark Salyzyn78956ab2014-10-13 09:59:37 -0700393 *audit = ' ';
394 return rc;
William Roberts210c5842013-02-08 09:45:26 +0900395}
396
397int LogAudit::getLogSocket() {
398 int fd = audit_open();
399 if (fd < 0) {
400 return fd;
401 }
Nick Kralevichd8774052014-11-19 13:33:22 -0800402 if (audit_setup(fd, getpid()) < 0) {
William Roberts210c5842013-02-08 09:45:26 +0900403 audit_close(fd);
404 fd = -1;
405 }
William Roberts210c5842013-02-08 09:45:26 +0900406 return fd;
407}