| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 17 | #include "log.h" | 
|  | 18 |  | 
| Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 19 | #include <fcntl.h> | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 20 | #include <string.h> | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 21 |  | 
| Nick Kralevich | 8adb4d9 | 2017-01-03 08:37:54 -0800 | [diff] [blame] | 22 | #include <linux/audit.h> | 
|  | 23 | #include <netlink/netlink.h> | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 24 | #include <selinux/selinux.h> | 
|  | 25 |  | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 26 | void InitKernelLogging(char* argv[]) { | 
| Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 27 | // Make stdin/stdout/stderr all point to /dev/null. | 
|  | 28 | int fd = open("/sys/fs/selinux/null", O_RDWR); | 
|  | 29 | if (fd == -1) { | 
|  | 30 | int saved_errno = errno; | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 31 | android::base::InitLogging(argv, &android::base::KernelLogger); | 
| Elliott Hughes | 171a829 | 2016-06-29 16:16:41 -0700 | [diff] [blame] | 32 | errno = saved_errno; | 
|  | 33 | PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null"; | 
|  | 34 | } | 
|  | 35 | dup2(fd, 0); | 
|  | 36 | dup2(fd, 1); | 
|  | 37 | dup2(fd, 2); | 
|  | 38 | if (fd > 2) close(fd); | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 39 |  | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 40 | android::base::InitLogging(argv, &android::base::KernelLogger); | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
| Nick Kralevich | 8adb4d9 | 2017-01-03 08:37:54 -0800 | [diff] [blame] | 43 | static void selinux_avc_log(char* buf, size_t buf_len) { | 
|  | 44 | size_t str_len = strnlen(buf, buf_len); | 
|  | 45 |  | 
|  | 46 | // trim newline at end of string | 
|  | 47 | buf[str_len - 1] = '\0'; | 
|  | 48 |  | 
|  | 49 | struct nl_sock* sk = nl_socket_alloc(); | 
|  | 50 | if (sk == NULL) { | 
|  | 51 | return; | 
|  | 52 | } | 
|  | 53 | nl_connect(sk, NETLINK_AUDIT); | 
|  | 54 | int result; | 
|  | 55 | do { | 
|  | 56 | result = nl_send_simple(sk, AUDIT_USER_AVC, 0, buf, str_len); | 
|  | 57 | } while (result == -NLE_INTR); | 
|  | 58 | nl_socket_free(sk); | 
|  | 59 | } | 
|  | 60 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 61 | int selinux_klog_callback(int type, const char *fmt, ...) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 62 | android::base::LogSeverity severity = android::base::ERROR; | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 63 | if (type == SELINUX_WARNING) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 64 | severity = android::base::WARNING; | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 65 | } else if (type == SELINUX_INFO) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 66 | severity = android::base::INFO; | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 67 | } | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 68 | char buf[1024]; | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 69 | va_list ap; | 
|  | 70 | va_start(ap, fmt); | 
| Nick Kralevich | 8adb4d9 | 2017-01-03 08:37:54 -0800 | [diff] [blame] | 71 | int res = vsnprintf(buf, sizeof(buf), fmt, ap); | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 72 | va_end(ap); | 
| Nick Kralevich | 8adb4d9 | 2017-01-03 08:37:54 -0800 | [diff] [blame] | 73 | if (res <= 0) { | 
|  | 74 | return 0; | 
|  | 75 | } | 
|  | 76 | if (type == SELINUX_AVC) { | 
|  | 77 | selinux_avc_log(buf, sizeof(buf)); | 
|  | 78 | } else { | 
|  | 79 | android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf); | 
|  | 80 | } | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 81 | return 0; | 
|  | 82 | } |