blob: ee6489b9a2b77b459ea1b6d96c50659bf8be93be [file] [log] [blame]
Elliott Hughesda40c002015-03-27 23:20:44 -07001/*
2 * Copyright (C) 2015 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
Elliott Hughese5ce30f2015-05-06 19:19:24 -070017#include "log.h"
18
Elliott Hughes171a8292016-06-29 16:16:41 -070019#include <fcntl.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070020#include <linux/audit.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070021#include <string.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070022
Tom Cherry3f5eaae52017-04-06 16:30:22 -070023#include <android-base/logging.h>
Nick Kralevich8adb4d92017-01-03 08:37:54 -080024#include <netlink/netlink.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070025#include <selinux/selinux.h>
26
Elliott Hughesf86b5a62016-06-24 15:12:21 -070027void InitKernelLogging(char* argv[]) {
Elliott Hughes171a8292016-06-29 16:16:41 -070028 // Make stdin/stdout/stderr all point to /dev/null.
29 int fd = open("/sys/fs/selinux/null", O_RDWR);
30 if (fd == -1) {
31 int saved_errno = errno;
Elliott Hughes7bc87a52016-08-04 16:09:39 -070032 android::base::InitLogging(argv, &android::base::KernelLogger);
Elliott Hughes171a8292016-06-29 16:16:41 -070033 errno = saved_errno;
34 PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
35 }
36 dup2(fd, 0);
37 dup2(fd, 1);
38 dup2(fd, 2);
39 if (fd > 2) close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -070040
Elliott Hughes7bc87a52016-08-04 16:09:39 -070041 android::base::InitLogging(argv, &android::base::KernelLogger);
Elliott Hughesda40c002015-03-27 23:20:44 -070042}
43
Nick Kralevich8adb4d92017-01-03 08:37:54 -080044static void selinux_avc_log(char* buf, size_t buf_len) {
45 size_t str_len = strnlen(buf, buf_len);
46
47 // trim newline at end of string
48 buf[str_len - 1] = '\0';
49
50 struct nl_sock* sk = nl_socket_alloc();
51 if (sk == NULL) {
52 return;
53 }
54 nl_connect(sk, NETLINK_AUDIT);
55 int result;
56 do {
57 result = nl_send_simple(sk, AUDIT_USER_AVC, 0, buf, str_len);
58 } while (result == -NLE_INTR);
59 nl_socket_free(sk);
60}
61
Elliott Hughesda40c002015-03-27 23:20:44 -070062int selinux_klog_callback(int type, const char *fmt, ...) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070063 android::base::LogSeverity severity = android::base::ERROR;
Elliott Hughesda40c002015-03-27 23:20:44 -070064 if (type == SELINUX_WARNING) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070065 severity = android::base::WARNING;
Elliott Hughesda40c002015-03-27 23:20:44 -070066 } else if (type == SELINUX_INFO) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070067 severity = android::base::INFO;
Elliott Hughesda40c002015-03-27 23:20:44 -070068 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -070069 char buf[1024];
Elliott Hughesda40c002015-03-27 23:20:44 -070070 va_list ap;
71 va_start(ap, fmt);
Nick Kralevich8adb4d92017-01-03 08:37:54 -080072 int res = vsnprintf(buf, sizeof(buf), fmt, ap);
Elliott Hughesda40c002015-03-27 23:20:44 -070073 va_end(ap);
Nick Kralevich8adb4d92017-01-03 08:37:54 -080074 if (res <= 0) {
75 return 0;
76 }
77 if (type == SELINUX_AVC) {
78 selinux_avc_log(buf, sizeof(buf));
79 } else {
80 android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
81 }
Elliott Hughesda40c002015-03-27 23:20:44 -070082 return 0;
83}