Mark Salyzyn | 0175b07 | 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 | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 17 | #include <limits.h> |
Mark Salyzyn | 4690640 | 2016-01-12 10:08:03 -0800 | [diff] [blame] | 18 | #include <sys/cdefs.h> |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 19 | #include <sys/prctl.h> |
Mark Salyzyn | 0175b07 | 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 | |
| 25 | #include <cutils/sockets.h> |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | b5f6e45 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 27 | #include <private/android_logger.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | |
Mark Salyzyn | 2ad0bd0 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 29 | #include "LogBuffer.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | #include "LogListener.h" |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 31 | #include "LogUtils.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 33 | LogListener::LogListener(LogBuffer *buf, LogReader *reader) : |
| 34 | SocketListener(getLogSocket(), false), |
| 35 | logbuf(buf), |
| 36 | reader(reader) { |
| 37 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 38 | |
| 39 | bool LogListener::onDataAvailable(SocketClient *cli) { |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 40 | static bool name_set; |
| 41 | if (!name_set) { |
| 42 | prctl(PR_SET_NAME, "logd.writer"); |
| 43 | name_set = true; |
| 44 | } |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 45 | |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 46 | char buffer[sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time) |
Mark Salyzyn | b059cf5 | 2014-03-18 15:30:17 -0700 | [diff] [blame] | 47 | + LOGGER_ENTRY_MAX_PAYLOAD]; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 48 | struct iovec iov = { buffer, sizeof(buffer) }; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | 5f4a946 | 2016-06-06 19:56:24 -0700 | [diff] [blame] | 50 | alignas(4) char control[CMSG_SPACE(sizeof(struct ucred))]; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 51 | struct msghdr hdr = { |
| 52 | NULL, |
| 53 | 0, |
| 54 | &iov, |
| 55 | 1, |
| 56 | control, |
| 57 | sizeof(control), |
| 58 | 0, |
| 59 | }; |
| 60 | |
| 61 | int socket = cli->getSocket(); |
| 62 | |
Mark Salyzyn | 1d9c7e7 | 2015-12-15 12:30:46 -0800 | [diff] [blame] | 63 | // To clear the entire buffer is secure/safe, but this contributes to 1.68% |
| 64 | // overhead under logging load. We are safe because we check counts. |
| 65 | // memset(buffer, 0, sizeof(buffer)); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 66 | ssize_t n = recvmsg(socket, &hdr, 0); |
Mark Salyzyn | b5f6e45 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 67 | if (n <= (ssize_t)(sizeof(android_log_header_t))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
| 71 | struct ucred *cred = NULL; |
| 72 | |
| 73 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr); |
| 74 | while (cmsg != NULL) { |
| 75 | if (cmsg->cmsg_level == SOL_SOCKET |
| 76 | && cmsg->cmsg_type == SCM_CREDENTIALS) { |
| 77 | cred = (struct ucred *)CMSG_DATA(cmsg); |
| 78 | break; |
| 79 | } |
| 80 | cmsg = CMSG_NXTHDR(&hdr, cmsg); |
| 81 | } |
| 82 | |
| 83 | if (cred == NULL) { |
| 84 | return false; |
| 85 | } |
| 86 | |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 87 | if (cred->uid == AID_LOGD) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 88 | // ignore log messages we send to ourself. |
| 89 | // Such log messages are often generated by libraries we depend on |
| 90 | // which use standard Android logging. |
| 91 | return false; |
| 92 | } |
| 93 | |
Mark Salyzyn | b5f6e45 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 94 | android_log_header_t *header = reinterpret_cast<android_log_header_t *>(buffer); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 95 | if (/* header->id < LOG_ID_MIN || */ header->id >= LOG_ID_MAX || header->id == LOG_ID_KERNEL) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 96 | return false; |
| 97 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 98 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 99 | if ((header->id == LOG_ID_SECURITY) && |
| 100 | (!__android_log_security() || |
Mark Salyzyn | 674ce6e | 2016-01-26 21:47:35 +0000 | [diff] [blame] | 101 | !clientHasLogCredentials(cred->uid, cred->gid, cred->pid))) { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | |
Mark Salyzyn | b5f6e45 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 105 | char *msg = ((char *)buffer) + sizeof(android_log_header_t); |
| 106 | n -= sizeof(android_log_header_t); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 107 | |
Mark Salyzyn | 22e287d | 2014-03-21 13:12:16 -0700 | [diff] [blame] | 108 | // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a |
| 109 | // truncated message to the logs. |
Mark Salyzyn | e0fa291 | 2014-04-28 16:39:04 -0700 | [diff] [blame] | 110 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 111 | if (logbuf->log((log_id_t)header->id, header->realtime, |
| 112 | cred->uid, cred->pid, header->tid, msg, |
| 113 | ((size_t) n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX) >= 0) { |
| 114 | reader->notifyNewLog(); |
| 115 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 116 | |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | int LogListener::getLogSocket() { |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 121 | static const char socketName[] = "logdw"; |
| 122 | int sock = android_get_control_socket(socketName); |
| 123 | |
| 124 | if (sock < 0) { |
| 125 | sock = socket_local_server(socketName, |
| 126 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 127 | SOCK_DGRAM); |
| 128 | } |
| 129 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 130 | int on = 1; |
| 131 | if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) { |
| 132 | return -1; |
| 133 | } |
| 134 | return sock; |
| 135 | } |