blob: 3d26a3d119b08c6cfbb95920eba1f3810b044f91 [file] [log] [blame]
Andrei Homescua8fa78c2022-04-05 05:56:52 +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#define LOG_TAG "RpcTransportTipcAndroid"
18
19#include <binder/RpcSession.h>
20#include <binder/RpcTransportTipcAndroid.h>
21#include <log/log.h>
22#include <poll.h>
23#include <trusty/tipc.h>
24
25#include "FdTrigger.h"
26#include "RpcState.h"
27#include "RpcTransportUtils.h"
28
Andrei Homescua8fa78c2022-04-05 05:56:52 +000029namespace android {
30
Andrei Homescua8fa78c2022-04-05 05:56:52 +000031// RpcTransport for writing Trusty IPC clients in Android.
32class RpcTransportTipcAndroid : public RpcTransport {
33public:
Pawan3e0061c2022-08-26 21:08:34 +000034 explicit RpcTransportTipcAndroid(android::RpcTransportFd socket) : mSocket(std::move(socket)) {}
Andrei Homescua8fa78c2022-04-05 05:56:52 +000035
36 status_t pollRead() override {
37 if (mReadBufferPos < mReadBufferSize) {
38 // We have more data in the read buffer
39 return OK;
40 }
41
42 // Trusty IPC device is not a socket, so MSG_PEEK is not available
Pawan49d74cb2022-08-03 21:19:11 +000043 pollfd pfd{.fd = mSocket.fd.get(), .events = static_cast<int16_t>(POLLIN), .revents = 0};
Andrei Homescua8fa78c2022-04-05 05:56:52 +000044 ssize_t ret = TEMP_FAILURE_RETRY(::poll(&pfd, 1, 0));
45 if (ret < 0) {
46 int savedErrno = errno;
47 if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
48 return WOULD_BLOCK;
49 }
50
51 LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno));
Andrei Homescu4407bc12023-03-21 23:25:25 +000052 return adjustStatus(-savedErrno);
Andrei Homescua8fa78c2022-04-05 05:56:52 +000053 }
54
55 if (pfd.revents & POLLNVAL) {
56 return BAD_VALUE;
57 }
58 if (pfd.revents & POLLERR) {
59 return DEAD_OBJECT;
60 }
Andrei Homescu805ca7a2022-12-09 07:16:23 +000061 if (pfd.revents & POLLIN) {
62 // Copied from FdTrigger.cpp: Even though POLLHUP may also be set,
63 // treat it as a success condition to ensure data is drained.
64 return OK;
65 }
Andrei Homescua8fa78c2022-04-05 05:56:52 +000066 if (pfd.revents & POLLHUP) {
67 return DEAD_OBJECT;
68 }
Andrei Homescua8fa78c2022-04-05 05:56:52 +000069
70 return WOULD_BLOCK;
71 }
72
73 status_t interruptableWriteFully(
74 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Sebastian Pickl84b7cff2023-10-30 08:02:24 +000075 const std::optional<android::base::function_ref<status_t()>>& altPoll,
Andrei Homescua8fa78c2022-04-05 05:56:52 +000076 const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds)
77 override {
78 auto writeFn = [&](iovec* iovs, size_t niovs) -> ssize_t {
79 // TODO: send ancillaryFds. For now, we just abort if anyone tries
80 // to send any.
81 LOG_ALWAYS_FATAL_IF(ancillaryFds != nullptr && !ancillaryFds->empty(),
82 "File descriptors are not supported on Trusty yet");
Pawan49d74cb2022-08-03 21:19:11 +000083 return TEMP_FAILURE_RETRY(tipc_send(mSocket.fd.get(), iovs, niovs, nullptr, 0));
Andrei Homescua8fa78c2022-04-05 05:56:52 +000084 };
Andrei Homescu4407bc12023-03-21 23:25:25 +000085
86 status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, writeFn,
87 "tipc_send", POLLOUT, altPoll);
88 return adjustStatus(status);
Andrei Homescua8fa78c2022-04-05 05:56:52 +000089 }
90
91 status_t interruptableReadFully(
92 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Sebastian Pickl84b7cff2023-10-30 08:02:24 +000093 const std::optional<android::base::function_ref<status_t()>>& altPoll,
Andrei Homescua8fa78c2022-04-05 05:56:52 +000094 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* /*ancillaryFds*/)
95 override {
96 auto readFn = [&](iovec* iovs, size_t niovs) -> ssize_t {
97 // Fill the read buffer at most once per readFn call, then try to
98 // return as much of it as possible. If the input iovecs are spread
99 // across multiple messages that require multiple fillReadBuffer
100 // calls, we expect the caller to advance the iovecs past the first
101 // read and call readFn as many times as needed to get all the data
102 status_t ret = fillReadBuffer();
103 if (ret != OK) {
Andrei Homescu2fb43012022-08-17 04:15:00 +0000104 // We need to emulate a Linux read call, which sets errno on
105 // error and returns -1
106 errno = -ret;
107 return -1;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000108 }
109
110 ssize_t processSize = 0;
111 for (size_t i = 0; i < niovs && mReadBufferPos < mReadBufferSize; i++) {
112 auto& iov = iovs[i];
113 size_t numBytes = std::min(iov.iov_len, mReadBufferSize - mReadBufferPos);
114 memcpy(iov.iov_base, mReadBuffer.get() + mReadBufferPos, numBytes);
115 mReadBufferPos += numBytes;
116 processSize += numBytes;
117 }
118
119 return processSize;
120 };
Andrei Homescu4407bc12023-03-21 23:25:25 +0000121
122 status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, readFn, "read",
123 POLLIN, altPoll);
124 return adjustStatus(status);
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000125 }
126
Pawan49d74cb2022-08-03 21:19:11 +0000127 bool isWaiting() override { return mSocket.isInPollingState(); }
128
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000129private:
Andrei Homescu4407bc12023-03-21 23:25:25 +0000130 status_t adjustStatus(status_t status) {
131 if (status == -ENOTCONN) {
132 // TIPC returns ENOTCONN on disconnect, but that's basically
133 // the same as DEAD_OBJECT and the latter is the common libbinder
134 // error code for dead connections
135 return DEAD_OBJECT;
136 }
137
138 return status;
139 }
140
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000141 status_t fillReadBuffer() {
142 if (mReadBufferPos < mReadBufferSize) {
143 return OK;
144 }
145
146 if (!mReadBuffer) {
147 // Guarantee at least kDefaultBufferSize bytes
148 mReadBufferCapacity = std::max(mReadBufferCapacity, kDefaultBufferSize);
149 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
150 if (!mReadBuffer) {
151 return NO_MEMORY;
152 }
153 }
154
155 // Reset the size and position in case we have to exit with an error.
156 // After we read a message into the buffer, we update the size
157 // with the actual value.
158 mReadBufferPos = 0;
159 mReadBufferSize = 0;
160
161 while (true) {
Pawan49d74cb2022-08-03 21:19:11 +0000162 ssize_t processSize = TEMP_FAILURE_RETRY(
163 read(mSocket.fd.get(), mReadBuffer.get(), mReadBufferCapacity));
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000164 if (processSize == 0) {
165 return DEAD_OBJECT;
166 } else if (processSize < 0) {
167 int savedErrno = errno;
168 if (savedErrno == EMSGSIZE) {
169 // Buffer was too small, double it and retry
170 if (__builtin_mul_overflow(mReadBufferCapacity, 2, &mReadBufferCapacity)) {
171 return NO_MEMORY;
172 }
173 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
174 if (!mReadBuffer) {
175 return NO_MEMORY;
176 }
177 continue;
178 } else {
179 LOG_RPC_DETAIL("RpcTransport fillBuffer(): %s", strerror(savedErrno));
Andrei Homescu4407bc12023-03-21 23:25:25 +0000180 return adjustStatus(-savedErrno);
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000181 }
182 } else {
183 mReadBufferSize = static_cast<size_t>(processSize);
184 return OK;
185 }
186 }
187 }
188
Pawan3e0061c2022-08-26 21:08:34 +0000189 RpcTransportFd mSocket;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000190
191 // For now, we copy all the input data into a temporary buffer because
192 // we might get multiple interruptableReadFully calls per message, but
193 // the tipc device only allows one read call. We read every message into
194 // this temporary buffer, then return pieces of it from our method.
195 //
196 // The special transaction GET_MAX_THREADS takes 40 bytes, so the default
197 // size should start pretty high.
198 static constexpr size_t kDefaultBufferSize = 64;
199 std::unique_ptr<uint8_t[]> mReadBuffer;
200 size_t mReadBufferPos = 0;
201 size_t mReadBufferSize = 0;
202 size_t mReadBufferCapacity = 0;
203};
204
205// RpcTransportCtx for Trusty.
206class RpcTransportCtxTipcAndroid : public RpcTransportCtx {
207public:
Pawan3e0061c2022-08-26 21:08:34 +0000208 std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd fd,
209 FdTrigger*) const override {
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000210 return std::make_unique<RpcTransportTipcAndroid>(std::move(fd));
211 }
212 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
213};
214
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000215std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newServerCtx() const {
216 return std::make_unique<RpcTransportCtxTipcAndroid>();
217}
218
219std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newClientCtx() const {
220 return std::make_unique<RpcTransportCtxTipcAndroid>();
221}
222
223const char* RpcTransportCtxFactoryTipcAndroid::toCString() const {
224 return "trusty";
225}
226
227std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryTipcAndroid::make() {
228 return std::unique_ptr<RpcTransportCtxFactoryTipcAndroid>(
229 new RpcTransportCtxFactoryTipcAndroid());
230}
231
232} // namespace android