2015-03-28 07:20:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-05-07 04:19:24 +02:00
|
|
|
#include "log.h"
|
|
|
|
|
2016-06-30 01:16:41 +02:00
|
|
|
#include <fcntl.h>
|
2015-03-28 07:20:44 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-01-03 17:37:54 +01:00
|
|
|
#include <linux/audit.h>
|
|
|
|
#include <netlink/netlink.h>
|
2015-03-28 07:20:44 +01:00
|
|
|
#include <selinux/selinux.h>
|
|
|
|
|
2016-06-25 00:12:21 +02:00
|
|
|
void InitKernelLogging(char* argv[]) {
|
2016-06-30 01:16:41 +02:00
|
|
|
// Make stdin/stdout/stderr all point to /dev/null.
|
|
|
|
int fd = open("/sys/fs/selinux/null", O_RDWR);
|
|
|
|
if (fd == -1) {
|
|
|
|
int saved_errno = errno;
|
2016-08-05 01:09:39 +02:00
|
|
|
android::base::InitLogging(argv, &android::base::KernelLogger);
|
2016-06-30 01:16:41 +02:00
|
|
|
errno = saved_errno;
|
|
|
|
PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
|
|
|
|
}
|
|
|
|
dup2(fd, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
if (fd > 2) close(fd);
|
2016-06-25 00:12:21 +02:00
|
|
|
|
2016-08-05 01:09:39 +02:00
|
|
|
android::base::InitLogging(argv, &android::base::KernelLogger);
|
2015-03-28 07:20:44 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 17:37:54 +01:00
|
|
|
static void selinux_avc_log(char* buf, size_t buf_len) {
|
|
|
|
size_t str_len = strnlen(buf, buf_len);
|
|
|
|
|
|
|
|
// trim newline at end of string
|
|
|
|
buf[str_len - 1] = '\0';
|
|
|
|
|
|
|
|
struct nl_sock* sk = nl_socket_alloc();
|
|
|
|
if (sk == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nl_connect(sk, NETLINK_AUDIT);
|
|
|
|
int result;
|
|
|
|
do {
|
|
|
|
result = nl_send_simple(sk, AUDIT_USER_AVC, 0, buf, str_len);
|
|
|
|
} while (result == -NLE_INTR);
|
|
|
|
nl_socket_free(sk);
|
|
|
|
}
|
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
int selinux_klog_callback(int type, const char *fmt, ...) {
|
2016-06-25 00:12:21 +02:00
|
|
|
android::base::LogSeverity severity = android::base::ERROR;
|
2015-03-28 07:20:44 +01:00
|
|
|
if (type == SELINUX_WARNING) {
|
2016-06-25 00:12:21 +02:00
|
|
|
severity = android::base::WARNING;
|
2015-03-28 07:20:44 +01:00
|
|
|
} else if (type == SELINUX_INFO) {
|
2016-06-25 00:12:21 +02:00
|
|
|
severity = android::base::INFO;
|
2015-03-28 07:20:44 +01:00
|
|
|
}
|
2016-06-25 00:12:21 +02:00
|
|
|
char buf[1024];
|
2015-03-28 07:20:44 +01:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2017-01-03 17:37:54 +01:00
|
|
|
int res = vsnprintf(buf, sizeof(buf), fmt, ap);
|
2015-03-28 07:20:44 +01:00
|
|
|
va_end(ap);
|
2017-01-03 17:37:54 +01:00
|
|
|
if (res <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (type == SELINUX_AVC) {
|
|
|
|
selinux_avc_log(buf, sizeof(buf));
|
|
|
|
} else {
|
|
|
|
android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
|
|
|
|
}
|
2015-03-28 07:20:44 +01:00
|
|
|
return 0;
|
|
|
|
}
|