blob: 28a0d5e930502f7d1c220bceedbca24d6ea41a17 [file] [log] [blame]
Andrei Homescu7c0b79f2022-06-30 02:00:46 +00001/*
2 * Copyright (C) 2022 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 "OS.h"
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070018#include "Utils.h"
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000019
20#include <android-base/file.h>
Andrei Homescu024727b2022-08-24 23:54:59 +000021#include <binder/RpcTransportRaw.h>
David Brazdil21c887c2022-09-23 12:25:18 +010022#include <log/log.h>
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000023#include <string.h>
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070024#include <sys/socket.h>
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000025
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +000026namespace android::binder::os {
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000027
David Brazdil21c887c2022-09-23 12:25:18 +010028// Linux kernel supports up to 253 (from SCM_MAX_FD) for unix sockets.
29constexpr size_t kMaxFdsPerMsg = 253;
30
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070031status_t setNonBlocking(borrowed_fd fd) {
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000032 int flags = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_GETFL));
33 if (flags == -1) {
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070034 PLOGE("Failed setNonBlocking: Could not get flags for fd");
35 return -errno;
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000036 }
37 if (int ret = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_SETFL, flags | O_NONBLOCK)); ret == -1) {
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070038 PLOGE("Failed setNonBlocking: Could not set non-blocking flag for fd");
39 return -errno;
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000040 }
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070041 return OK;
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000042}
43
44status_t getRandomBytes(uint8_t* data, size_t size) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070045 unique_fd fd(TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)));
46 if (!fd.ok()) {
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000047 return -errno;
48 }
49
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070050 if (!ReadFully(fd, data, size)) {
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000051 return -errno;
52 }
53 return OK;
54}
55
Andrei Homescu24ad36e2022-08-04 01:33:33 +000056status_t dupFileDescriptor(int oldFd, int* newFd) {
57 int ret = fcntl(oldFd, F_DUPFD_CLOEXEC, 0);
58 if (ret < 0) {
59 return -errno;
60 }
61
62 *newFd = ret;
63 return OK;
64}
65
Andrei Homescu024727b2022-08-24 23:54:59 +000066std::unique_ptr<RpcTransportCtxFactory> makeDefaultRpcTransportCtxFactory() {
67 return RpcTransportCtxFactoryRaw::make();
68}
69
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070070ssize_t sendMessageOnSocket(const RpcTransportFd& socket, iovec* iovs, int niovs,
71 const std::vector<std::variant<unique_fd, borrowed_fd>>* ancillaryFds) {
David Brazdil21c887c2022-09-23 12:25:18 +010072 if (ancillaryFds != nullptr && !ancillaryFds->empty()) {
73 if (ancillaryFds->size() > kMaxFdsPerMsg) {
74 errno = EINVAL;
75 return -1;
76 }
77
78 // CMSG_DATA is not necessarily aligned, so we copy the FDs into a buffer and then
79 // use memcpy.
80 int fds[kMaxFdsPerMsg];
81 for (size_t i = 0; i < ancillaryFds->size(); i++) {
82 fds[i] = std::visit([](const auto& fd) { return fd.get(); }, ancillaryFds->at(i));
83 }
84 const size_t fdsByteSize = sizeof(int) * ancillaryFds->size();
85
86 alignas(struct cmsghdr) char msgControlBuf[CMSG_SPACE(sizeof(int) * kMaxFdsPerMsg)];
87
88 msghdr msg{
89 .msg_iov = iovs,
90 .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs),
91 .msg_control = msgControlBuf,
92 .msg_controllen = sizeof(msgControlBuf),
93 };
94
95 cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
96 cmsg->cmsg_level = SOL_SOCKET;
97 cmsg->cmsg_type = SCM_RIGHTS;
98 cmsg->cmsg_len = CMSG_LEN(fdsByteSize);
99 memcpy(CMSG_DATA(cmsg), fds, fdsByteSize);
100
101 msg.msg_controllen = CMSG_SPACE(fdsByteSize);
102 return TEMP_FAILURE_RETRY(sendmsg(socket.fd.get(), &msg, MSG_NOSIGNAL | MSG_CMSG_CLOEXEC));
103 }
104
105 msghdr msg{
106 .msg_iov = iovs,
107 // posix uses int, glibc uses size_t. niovs is a
108 // non-negative int and can be cast to either.
109 .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs),
110 };
111 return TEMP_FAILURE_RETRY(sendmsg(socket.fd.get(), &msg, MSG_NOSIGNAL));
112}
113
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700114ssize_t receiveMessageFromSocket(const RpcTransportFd& socket, iovec* iovs, int niovs,
115 std::vector<std::variant<unique_fd, borrowed_fd>>* ancillaryFds) {
David Brazdil21c887c2022-09-23 12:25:18 +0100116 if (ancillaryFds != nullptr) {
117 int fdBuffer[kMaxFdsPerMsg];
118 alignas(struct cmsghdr) char msgControlBuf[CMSG_SPACE(sizeof(fdBuffer))];
119
120 msghdr msg{
121 .msg_iov = iovs,
122 .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs),
123 .msg_control = msgControlBuf,
124 .msg_controllen = sizeof(msgControlBuf),
125 };
126 ssize_t processSize = TEMP_FAILURE_RETRY(recvmsg(socket.fd.get(), &msg, MSG_NOSIGNAL));
127 if (processSize < 0) {
128 return -1;
129 }
130
131 for (cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
132 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
133 // NOTE: It is tempting to reinterpret_cast, but cmsg(3) explicitly asks
134 // application devs to memcpy the data to ensure memory alignment.
135 size_t dataLen = cmsg->cmsg_len - CMSG_LEN(0);
136 LOG_ALWAYS_FATAL_IF(dataLen > sizeof(fdBuffer)); // validity check
137 memcpy(fdBuffer, CMSG_DATA(cmsg), dataLen);
138 size_t fdCount = dataLen / sizeof(int);
139 ancillaryFds->reserve(ancillaryFds->size() + fdCount);
140 for (size_t i = 0; i < fdCount; i++) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700141 ancillaryFds->emplace_back(unique_fd(fdBuffer[i]));
David Brazdil21c887c2022-09-23 12:25:18 +0100142 }
143 break;
144 }
145 }
146
147 if (msg.msg_flags & MSG_CTRUNC) {
148 errno = EPIPE;
149 return -1;
150 }
151 return processSize;
152 }
153 msghdr msg{
154 .msg_iov = iovs,
155 // posix uses int, glibc uses size_t. niovs is a
156 // non-negative int and can be cast to either.
157 .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs),
158 };
159
160 return TEMP_FAILURE_RETRY(recvmsg(socket.fd.get(), &msg, MSG_NOSIGNAL));
161}
162
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000163} // namespace android::binder::os