blob: ca998d46d3bfcc0d9ad6fed6732e0936fadb250d [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"
Tomasz Wasilczyk26db5e42023-11-02 11:45:11 -070019#include "file.h"
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000020
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 Wasilczyk26db5e42023-11-02 11:45:11 -070026using android::binder::ReadFully;
27
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +000028namespace android::binder::os {
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000029
David Brazdil21c887c2022-09-23 12:25:18 +010030// Linux kernel supports up to 253 (from SCM_MAX_FD) for unix sockets.
31constexpr size_t kMaxFdsPerMsg = 253;
32
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070033status_t setNonBlocking(borrowed_fd fd) {
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000034 int flags = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_GETFL));
35 if (flags == -1) {
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070036 PLOGE("Failed setNonBlocking: Could not get flags for fd");
37 return -errno;
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000038 }
39 if (int ret = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_SETFL, flags | O_NONBLOCK)); ret == -1) {
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070040 PLOGE("Failed setNonBlocking: Could not set non-blocking flag for fd");
41 return -errno;
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000042 }
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070043 return OK;
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000044}
45
46status_t getRandomBytes(uint8_t* data, size_t size) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070047 unique_fd fd(TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)));
48 if (!fd.ok()) {
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000049 return -errno;
50 }
51
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070052 if (!ReadFully(fd, data, size)) {
Andrei Homescu7c0b79f2022-06-30 02:00:46 +000053 return -errno;
54 }
55 return OK;
56}
57
Andrei Homescu24ad36e2022-08-04 01:33:33 +000058status_t dupFileDescriptor(int oldFd, int* newFd) {
59 int ret = fcntl(oldFd, F_DUPFD_CLOEXEC, 0);
60 if (ret < 0) {
61 return -errno;
62 }
63
64 *newFd = ret;
65 return OK;
66}
67
Andrei Homescu024727b2022-08-24 23:54:59 +000068std::unique_ptr<RpcTransportCtxFactory> makeDefaultRpcTransportCtxFactory() {
69 return RpcTransportCtxFactoryRaw::make();
70}
71
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070072ssize_t sendMessageOnSocket(const RpcTransportFd& socket, iovec* iovs, int niovs,
73 const std::vector<std::variant<unique_fd, borrowed_fd>>* ancillaryFds) {
David Brazdil21c887c2022-09-23 12:25:18 +010074 if (ancillaryFds != nullptr && !ancillaryFds->empty()) {
75 if (ancillaryFds->size() > kMaxFdsPerMsg) {
76 errno = EINVAL;
77 return -1;
78 }
79
80 // CMSG_DATA is not necessarily aligned, so we copy the FDs into a buffer and then
81 // use memcpy.
82 int fds[kMaxFdsPerMsg];
83 for (size_t i = 0; i < ancillaryFds->size(); i++) {
84 fds[i] = std::visit([](const auto& fd) { return fd.get(); }, ancillaryFds->at(i));
85 }
86 const size_t fdsByteSize = sizeof(int) * ancillaryFds->size();
87
88 alignas(struct cmsghdr) char msgControlBuf[CMSG_SPACE(sizeof(int) * kMaxFdsPerMsg)];
89
90 msghdr msg{
91 .msg_iov = iovs,
92 .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs),
93 .msg_control = msgControlBuf,
94 .msg_controllen = sizeof(msgControlBuf),
95 };
96
97 cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
98 cmsg->cmsg_level = SOL_SOCKET;
99 cmsg->cmsg_type = SCM_RIGHTS;
100 cmsg->cmsg_len = CMSG_LEN(fdsByteSize);
101 memcpy(CMSG_DATA(cmsg), fds, fdsByteSize);
102
103 msg.msg_controllen = CMSG_SPACE(fdsByteSize);
104 return TEMP_FAILURE_RETRY(sendmsg(socket.fd.get(), &msg, MSG_NOSIGNAL | MSG_CMSG_CLOEXEC));
105 }
106
107 msghdr msg{
108 .msg_iov = iovs,
109 // posix uses int, glibc uses size_t. niovs is a
110 // non-negative int and can be cast to either.
111 .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs),
112 };
113 return TEMP_FAILURE_RETRY(sendmsg(socket.fd.get(), &msg, MSG_NOSIGNAL));
114}
115
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700116ssize_t receiveMessageFromSocket(const RpcTransportFd& socket, iovec* iovs, int niovs,
117 std::vector<std::variant<unique_fd, borrowed_fd>>* ancillaryFds) {
David Brazdil21c887c2022-09-23 12:25:18 +0100118 if (ancillaryFds != nullptr) {
119 int fdBuffer[kMaxFdsPerMsg];
120 alignas(struct cmsghdr) char msgControlBuf[CMSG_SPACE(sizeof(fdBuffer))];
121
122 msghdr msg{
123 .msg_iov = iovs,
124 .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs),
125 .msg_control = msgControlBuf,
126 .msg_controllen = sizeof(msgControlBuf),
127 };
128 ssize_t processSize = TEMP_FAILURE_RETRY(recvmsg(socket.fd.get(), &msg, MSG_NOSIGNAL));
129 if (processSize < 0) {
130 return -1;
131 }
132
133 for (cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
134 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
135 // NOTE: It is tempting to reinterpret_cast, but cmsg(3) explicitly asks
136 // application devs to memcpy the data to ensure memory alignment.
137 size_t dataLen = cmsg->cmsg_len - CMSG_LEN(0);
138 LOG_ALWAYS_FATAL_IF(dataLen > sizeof(fdBuffer)); // validity check
139 memcpy(fdBuffer, CMSG_DATA(cmsg), dataLen);
140 size_t fdCount = dataLen / sizeof(int);
141 ancillaryFds->reserve(ancillaryFds->size() + fdCount);
142 for (size_t i = 0; i < fdCount; i++) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700143 ancillaryFds->emplace_back(unique_fd(fdBuffer[i]));
David Brazdil21c887c2022-09-23 12:25:18 +0100144 }
145 break;
146 }
147 }
148
149 if (msg.msg_flags & MSG_CTRUNC) {
150 errno = EPIPE;
151 return -1;
152 }
153 return processSize;
154 }
155 msghdr msg{
156 .msg_iov = iovs,
157 // posix uses int, glibc uses size_t. niovs is a
158 // non-negative int and can be cast to either.
159 .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs),
160 };
161
162 return TEMP_FAILURE_RETRY(recvmsg(socket.fd.get(), &msg, MSG_NOSIGNAL));
163}
164
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000165} // namespace android::binder::os