Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012-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 | |
Mark Salyzyn | 59d0910 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 17 | #include <limits.h> |
Mark Salyzyn | d89faa8 | 2016-01-12 10:08:03 -0800 | [diff] [blame] | 18 | #include <sys/cdefs.h> |
Mark Salyzyn | 4e5efdc | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 19 | #include <sys/prctl.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 20 | #include <sys/socket.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/un.h> |
| 23 | #include <unistd.h> |
| 24 | |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 25 | #include <thread> |
| 26 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 27 | #include <cutils/sockets.h> |
Mark Salyzyn | b485395 | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 6904e18 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 29 | #include <private/android_logger.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | |
Mark Salyzyn | 1d21d54 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 31 | #include "LogBuffer.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | #include "LogListener.h" |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 33 | #include "LogUtils.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 35 | LogListener::LogListener(LogBuffer* buf) : socket_(GetLogSocket()), logbuf_(buf) {} |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 36 | |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 37 | bool LogListener::StartListener() { |
| 38 | if (socket_ <= 0) { |
| 39 | return false; |
| 40 | } |
| 41 | auto thread = std::thread(&LogListener::ThreadFunction, this); |
| 42 | thread.detach(); |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | void LogListener::ThreadFunction() { |
Mark Salyzyn | b485395 | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 47 | static bool name_set; |
| 48 | if (!name_set) { |
| 49 | prctl(PR_SET_NAME, "logd.writer"); |
| 50 | name_set = true; |
| 51 | } |
Mark Salyzyn | 4e5efdc | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 52 | |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 53 | while (true) { |
| 54 | HandleData(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void LogListener::HandleData() { |
Paul Elliott | 043810b | 2017-11-07 11:11:41 +0000 | [diff] [blame] | 59 | // + 1 to ensure null terminator if MAX_PAYLOAD buffer is received |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 60 | __attribute__((uninitialized)) char |
| 61 | buffer[sizeof(android_log_header_t) + LOGGER_ENTRY_MAX_PAYLOAD + 1]; |
| 62 | struct iovec iov = {buffer, sizeof(buffer) - 1}; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 63 | |
Elliott Hughes | 273936a | 2016-06-06 19:56:24 -0700 | [diff] [blame] | 64 | alignas(4) char control[CMSG_SPACE(sizeof(struct ucred))]; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 65 | struct msghdr hdr = { |
Yi Kong | 36feb15 | 2018-07-13 17:39:22 -0700 | [diff] [blame] | 66 | nullptr, 0, &iov, 1, control, sizeof(control), 0, |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Mark Salyzyn | f9d20d1 | 2015-12-15 12:30:46 -0800 | [diff] [blame] | 69 | // To clear the entire buffer is secure/safe, but this contributes to 1.68% |
Paul Elliott | 043810b | 2017-11-07 11:11:41 +0000 | [diff] [blame] | 70 | // overhead under logging load. We are safe because we check counts, but |
| 71 | // still need to clear null terminator |
Mark Salyzyn | f9d20d1 | 2015-12-15 12:30:46 -0800 | [diff] [blame] | 72 | // memset(buffer, 0, sizeof(buffer)); |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 73 | ssize_t n = recvmsg(socket_, &hdr, 0); |
Mark Salyzyn | 6904e18 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 74 | if (n <= (ssize_t)(sizeof(android_log_header_t))) { |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 75 | return; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Paul Elliott | 043810b | 2017-11-07 11:11:41 +0000 | [diff] [blame] | 78 | buffer[n] = 0; |
| 79 | |
Yi Kong | 36feb15 | 2018-07-13 17:39:22 -0700 | [diff] [blame] | 80 | struct ucred* cred = nullptr; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 81 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 82 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr); |
Yi Kong | 36feb15 | 2018-07-13 17:39:22 -0700 | [diff] [blame] | 83 | while (cmsg != nullptr) { |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 84 | if (cmsg->cmsg_level == SOL_SOCKET && |
| 85 | cmsg->cmsg_type == SCM_CREDENTIALS) { |
| 86 | cred = (struct ucred*)CMSG_DATA(cmsg); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | cmsg = CMSG_NXTHDR(&hdr, cmsg); |
| 90 | } |
| 91 | |
Yi Kong | 36feb15 | 2018-07-13 17:39:22 -0700 | [diff] [blame] | 92 | if (cred == nullptr) { |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 93 | return; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Mark Salyzyn | b485395 | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 96 | if (cred->uid == AID_LOGD) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 97 | // ignore log messages we send to ourself. |
| 98 | // Such log messages are often generated by libraries we depend on |
| 99 | // which use standard Android logging. |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 100 | return; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 103 | android_log_header_t* header = |
| 104 | reinterpret_cast<android_log_header_t*>(buffer); |
Hao Wang | 10d19f6 | 2017-12-04 14:10:40 +0800 | [diff] [blame] | 105 | log_id_t logId = static_cast<log_id_t>(header->id); |
| 106 | if (/* logId < LOG_ID_MIN || */ logId >= LOG_ID_MAX || |
| 107 | logId == LOG_ID_KERNEL) { |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 108 | return; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 109 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 110 | |
Hao Wang | 10d19f6 | 2017-12-04 14:10:40 +0800 | [diff] [blame] | 111 | if ((logId == LOG_ID_SECURITY) && |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 112 | (!__android_log_security() || |
| 113 | !clientHasLogCredentials(cred->uid, cred->gid, cred->pid))) { |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 114 | return; |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 117 | char* msg = ((char*)buffer) + sizeof(android_log_header_t); |
Mark Salyzyn | 6904e18 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 118 | n -= sizeof(android_log_header_t); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 119 | |
Mark Salyzyn | 63a4ad5 | 2014-03-21 13:12:16 -0700 | [diff] [blame] | 120 | // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a |
| 121 | // truncated message to the logs. |
Mark Salyzyn | 59d0910 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 122 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 123 | logbuf_->log(logId, header->realtime, cred->uid, cred->pid, header->tid, msg, |
| 124 | ((size_t)n <= UINT16_MAX) ? (uint16_t)n : UINT16_MAX); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Tom Cherry | 7955fd4 | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 127 | int LogListener::GetLogSocket() { |
Mark Salyzyn | 3f6151e | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 128 | static const char socketName[] = "logdw"; |
| 129 | int sock = android_get_control_socket(socketName); |
| 130 | |
Mark Salyzyn | c461901 | 2017-05-05 15:32:10 -0700 | [diff] [blame] | 131 | if (sock < 0) { // logd started up in init.sh |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 132 | sock = socket_local_server( |
| 133 | socketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_DGRAM); |
Mark Salyzyn | 3f6151e | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 134 | |
Mark Salyzyn | c461901 | 2017-05-05 15:32:10 -0700 | [diff] [blame] | 135 | int on = 1; |
| 136 | if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on))) { |
| 137 | return -1; |
| 138 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 139 | } |
| 140 | return sock; |
| 141 | } |