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 
17 #include <limits.h>
18 #include <sys/cdefs.h>
19 #include <sys/prctl.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <sys/un.h>
23 #include <unistd.h>
24 
25 #include <thread>
26 
27 #include <cutils/sockets.h>
28 #include <private/android_filesystem_config.h>
29 #include <private/android_logger.h>
30 
31 #include "LogBuffer.h"
32 #include "LogListener.h"
33 #include "LogPermissions.h"
34 
LogListener(LogBuffer * buf)35 LogListener::LogListener(LogBuffer* buf) : socket_(GetLogSocket()), logbuf_(buf) {}
36 
StartListener()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 
ThreadFunction()46 void LogListener::ThreadFunction() {
47     prctl(PR_SET_NAME, "logd.writer");
48 
49     while (true) {
50         HandleData();
51     }
52 }
53 
HandleData()54 void LogListener::HandleData() {
55     // + 1 to ensure null terminator if MAX_PAYLOAD buffer is received
56     __attribute__((uninitialized)) char
57             buffer[sizeof(android_log_header_t) + LOGGER_ENTRY_MAX_PAYLOAD + 1];
58     struct iovec iov = {buffer, sizeof(buffer) - 1};
59 
60     alignas(4) char control[CMSG_SPACE(sizeof(struct ucred))];
61     struct msghdr hdr = {
62         nullptr, 0, &iov, 1, control, sizeof(control), 0,
63     };
64 
65     ssize_t n = recvmsg(socket_, &hdr, 0);
66     if (n <= (ssize_t)(sizeof(android_log_header_t))) {
67         return;
68     }
69 
70     // To clear the entire buffer would be safe, but this contributes to 1.68%
71     // overhead under logging load. We are safe because we check counts, but
72     // still need to clear null terminator
73     buffer[n] = 0;
74 
75     struct ucred* cred = nullptr;
76 
77     struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr);
78     while (cmsg != nullptr) {
79         if (cmsg->cmsg_level == SOL_SOCKET &&
80             cmsg->cmsg_type == SCM_CREDENTIALS) {
81             cred = (struct ucred*)CMSG_DATA(cmsg);
82             break;
83         }
84         cmsg = CMSG_NXTHDR(&hdr, cmsg);
85     }
86 
87     if (cred == nullptr) {
88         return;
89     }
90 
91     if (cred->uid == AID_LOGD) {
92         // ignore log messages we send to ourself.
93         // Such log messages are often generated by libraries we depend on
94         // which use standard Android logging.
95         return;
96     }
97 
98     android_log_header_t* header =
99         reinterpret_cast<android_log_header_t*>(buffer);
100     log_id_t logId = static_cast<log_id_t>(header->id);
101     if (/* logId < LOG_ID_MIN || */ logId >= LOG_ID_MAX ||
102         logId == LOG_ID_KERNEL) {
103         return;
104     }
105 
106     if (logId == LOG_ID_SECURITY) {
107         if (!__android_log_security()) {
108             return;
109         }
110         if (!clientCanWriteSecurityLog(cred->uid, cred->gid, cred->pid)) {
111             return;
112         }
113     }
114 
115     char* msg = ((char*)buffer) + sizeof(android_log_header_t);
116     n -= sizeof(android_log_header_t);
117 
118     // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a
119     // truncated message to the logs.
120 
121     logbuf_->Log(logId, header->realtime, cred->uid, cred->pid, header->tid, msg,
122                  ((size_t)n <= UINT16_MAX) ? (uint16_t)n : UINT16_MAX);
123 }
124 
GetLogSocket()125 int LogListener::GetLogSocket() {
126     static const char socketName[] = "logdw";
127     int sock = android_get_control_socket(socketName);
128 
129     if (sock < 0) {  // logd started up in init.sh
130         sock = socket_local_server(
131             socketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_DGRAM);
132 
133         int on = 1;
134         if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on))) {
135             return -1;
136         }
137     }
138     return sock;
139 }
140