blob: d5a6da2e3d894643d0e0180458777bcda659c788 [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
29using android::base::Error;
30using android::base::Result;
31
32namespace android {
33
34namespace {
35
36// RpcTransport for writing Trusty IPC clients in Android.
37class RpcTransportTipcAndroid : public RpcTransport {
38public:
Pawan3e0061c2022-08-26 21:08:34 +000039 explicit RpcTransportTipcAndroid(android::RpcTransportFd socket) : mSocket(std::move(socket)) {}
Andrei Homescua8fa78c2022-04-05 05:56:52 +000040
41 status_t pollRead() override {
42 if (mReadBufferPos < mReadBufferSize) {
43 // We have more data in the read buffer
44 return OK;
45 }
46
47 // Trusty IPC device is not a socket, so MSG_PEEK is not available
Pawan49d74cb2022-08-03 21:19:11 +000048 pollfd pfd{.fd = mSocket.fd.get(), .events = static_cast<int16_t>(POLLIN), .revents = 0};
Andrei Homescua8fa78c2022-04-05 05:56:52 +000049 ssize_t ret = TEMP_FAILURE_RETRY(::poll(&pfd, 1, 0));
50 if (ret < 0) {
51 int savedErrno = errno;
52 if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
53 return WOULD_BLOCK;
54 }
55
56 LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno));
Andrei Homescu4407bc12023-03-21 23:25:25 +000057 return adjustStatus(-savedErrno);
Andrei Homescua8fa78c2022-04-05 05:56:52 +000058 }
59
60 if (pfd.revents & POLLNVAL) {
61 return BAD_VALUE;
62 }
63 if (pfd.revents & POLLERR) {
64 return DEAD_OBJECT;
65 }
Andrei Homescu805ca7a2022-12-09 07:16:23 +000066 if (pfd.revents & POLLIN) {
67 // Copied from FdTrigger.cpp: Even though POLLHUP may also be set,
68 // treat it as a success condition to ensure data is drained.
69 return OK;
70 }
Andrei Homescua8fa78c2022-04-05 05:56:52 +000071 if (pfd.revents & POLLHUP) {
72 return DEAD_OBJECT;
73 }
Andrei Homescua8fa78c2022-04-05 05:56:52 +000074
75 return WOULD_BLOCK;
76 }
77
78 status_t interruptableWriteFully(
79 FdTrigger* fdTrigger, iovec* iovs, int niovs,
80 const std::optional<android::base::function_ref<status_t()>>& altPoll,
81 const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds)
82 override {
83 auto writeFn = [&](iovec* iovs, size_t niovs) -> ssize_t {
84 // TODO: send ancillaryFds. For now, we just abort if anyone tries
85 // to send any.
86 LOG_ALWAYS_FATAL_IF(ancillaryFds != nullptr && !ancillaryFds->empty(),
87 "File descriptors are not supported on Trusty yet");
Pawan49d74cb2022-08-03 21:19:11 +000088 return TEMP_FAILURE_RETRY(tipc_send(mSocket.fd.get(), iovs, niovs, nullptr, 0));
Andrei Homescua8fa78c2022-04-05 05:56:52 +000089 };
Andrei Homescu4407bc12023-03-21 23:25:25 +000090
91 status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, writeFn,
92 "tipc_send", POLLOUT, altPoll);
93 return adjustStatus(status);
Andrei Homescua8fa78c2022-04-05 05:56:52 +000094 }
95
96 status_t interruptableReadFully(
97 FdTrigger* fdTrigger, iovec* iovs, int niovs,
98 const std::optional<android::base::function_ref<status_t()>>& altPoll,
99 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* /*ancillaryFds*/)
100 override {
101 auto readFn = [&](iovec* iovs, size_t niovs) -> ssize_t {
102 // Fill the read buffer at most once per readFn call, then try to
103 // return as much of it as possible. If the input iovecs are spread
104 // across multiple messages that require multiple fillReadBuffer
105 // calls, we expect the caller to advance the iovecs past the first
106 // read and call readFn as many times as needed to get all the data
107 status_t ret = fillReadBuffer();
108 if (ret != OK) {
Andrei Homescu2fb43012022-08-17 04:15:00 +0000109 // We need to emulate a Linux read call, which sets errno on
110 // error and returns -1
111 errno = -ret;
112 return -1;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000113 }
114
115 ssize_t processSize = 0;
116 for (size_t i = 0; i < niovs && mReadBufferPos < mReadBufferSize; i++) {
117 auto& iov = iovs[i];
118 size_t numBytes = std::min(iov.iov_len, mReadBufferSize - mReadBufferPos);
119 memcpy(iov.iov_base, mReadBuffer.get() + mReadBufferPos, numBytes);
120 mReadBufferPos += numBytes;
121 processSize += numBytes;
122 }
123
124 return processSize;
125 };
Andrei Homescu4407bc12023-03-21 23:25:25 +0000126
127 status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, readFn, "read",
128 POLLIN, altPoll);
129 return adjustStatus(status);
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000130 }
131
Pawan49d74cb2022-08-03 21:19:11 +0000132 bool isWaiting() override { return mSocket.isInPollingState(); }
133
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000134private:
Andrei Homescu4407bc12023-03-21 23:25:25 +0000135 status_t adjustStatus(status_t status) {
136 if (status == -ENOTCONN) {
137 // TIPC returns ENOTCONN on disconnect, but that's basically
138 // the same as DEAD_OBJECT and the latter is the common libbinder
139 // error code for dead connections
140 return DEAD_OBJECT;
141 }
142
143 return status;
144 }
145
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000146 status_t fillReadBuffer() {
147 if (mReadBufferPos < mReadBufferSize) {
148 return OK;
149 }
150
151 if (!mReadBuffer) {
152 // Guarantee at least kDefaultBufferSize bytes
153 mReadBufferCapacity = std::max(mReadBufferCapacity, kDefaultBufferSize);
154 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
155 if (!mReadBuffer) {
156 return NO_MEMORY;
157 }
158 }
159
160 // Reset the size and position in case we have to exit with an error.
161 // After we read a message into the buffer, we update the size
162 // with the actual value.
163 mReadBufferPos = 0;
164 mReadBufferSize = 0;
165
166 while (true) {
Pawan49d74cb2022-08-03 21:19:11 +0000167 ssize_t processSize = TEMP_FAILURE_RETRY(
168 read(mSocket.fd.get(), mReadBuffer.get(), mReadBufferCapacity));
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000169 if (processSize == 0) {
170 return DEAD_OBJECT;
171 } else if (processSize < 0) {
172 int savedErrno = errno;
173 if (savedErrno == EMSGSIZE) {
174 // Buffer was too small, double it and retry
175 if (__builtin_mul_overflow(mReadBufferCapacity, 2, &mReadBufferCapacity)) {
176 return NO_MEMORY;
177 }
178 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
179 if (!mReadBuffer) {
180 return NO_MEMORY;
181 }
182 continue;
183 } else {
184 LOG_RPC_DETAIL("RpcTransport fillBuffer(): %s", strerror(savedErrno));
Andrei Homescu4407bc12023-03-21 23:25:25 +0000185 return adjustStatus(-savedErrno);
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000186 }
187 } else {
188 mReadBufferSize = static_cast<size_t>(processSize);
189 return OK;
190 }
191 }
192 }
193
Pawan3e0061c2022-08-26 21:08:34 +0000194 RpcTransportFd mSocket;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000195
196 // For now, we copy all the input data into a temporary buffer because
197 // we might get multiple interruptableReadFully calls per message, but
198 // the tipc device only allows one read call. We read every message into
199 // this temporary buffer, then return pieces of it from our method.
200 //
201 // The special transaction GET_MAX_THREADS takes 40 bytes, so the default
202 // size should start pretty high.
203 static constexpr size_t kDefaultBufferSize = 64;
204 std::unique_ptr<uint8_t[]> mReadBuffer;
205 size_t mReadBufferPos = 0;
206 size_t mReadBufferSize = 0;
207 size_t mReadBufferCapacity = 0;
208};
209
210// RpcTransportCtx for Trusty.
211class RpcTransportCtxTipcAndroid : public RpcTransportCtx {
212public:
Pawan3e0061c2022-08-26 21:08:34 +0000213 std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd fd,
214 FdTrigger*) const override {
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000215 return std::make_unique<RpcTransportTipcAndroid>(std::move(fd));
216 }
217 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
218};
219
220} // namespace
221
222std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newServerCtx() const {
223 return std::make_unique<RpcTransportCtxTipcAndroid>();
224}
225
226std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newClientCtx() const {
227 return std::make_unique<RpcTransportCtxTipcAndroid>();
228}
229
230const char* RpcTransportCtxFactoryTipcAndroid::toCString() const {
231 return "trusty";
232}
233
234std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryTipcAndroid::make() {
235 return std::unique_ptr<RpcTransportCtxFactoryTipcAndroid>(
236 new RpcTransportCtxFactoryTipcAndroid());
237}
238
239} // namespace android