blob: 0c81d830328d94f51bb5fc816451b8fc01afbbab [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
Andrei Homescua8fa78c2022-04-05 05:56:52 +000034// RpcTransport for writing Trusty IPC clients in Android.
35class RpcTransportTipcAndroid : public RpcTransport {
36public:
Pawan3e0061c2022-08-26 21:08:34 +000037 explicit RpcTransportTipcAndroid(android::RpcTransportFd socket) : mSocket(std::move(socket)) {}
Andrei Homescua8fa78c2022-04-05 05:56:52 +000038
39 status_t pollRead() override {
40 if (mReadBufferPos < mReadBufferSize) {
41 // We have more data in the read buffer
42 return OK;
43 }
44
45 // Trusty IPC device is not a socket, so MSG_PEEK is not available
Pawan49d74cb2022-08-03 21:19:11 +000046 pollfd pfd{.fd = mSocket.fd.get(), .events = static_cast<int16_t>(POLLIN), .revents = 0};
Andrei Homescua8fa78c2022-04-05 05:56:52 +000047 ssize_t ret = TEMP_FAILURE_RETRY(::poll(&pfd, 1, 0));
48 if (ret < 0) {
49 int savedErrno = errno;
50 if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
51 return WOULD_BLOCK;
52 }
53
54 LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno));
Andrei Homescu4407bc12023-03-21 23:25:25 +000055 return adjustStatus(-savedErrno);
Andrei Homescua8fa78c2022-04-05 05:56:52 +000056 }
57
58 if (pfd.revents & POLLNVAL) {
59 return BAD_VALUE;
60 }
61 if (pfd.revents & POLLERR) {
62 return DEAD_OBJECT;
63 }
Andrei Homescu805ca7a2022-12-09 07:16:23 +000064 if (pfd.revents & POLLIN) {
65 // Copied from FdTrigger.cpp: Even though POLLHUP may also be set,
66 // treat it as a success condition to ensure data is drained.
67 return OK;
68 }
Andrei Homescua8fa78c2022-04-05 05:56:52 +000069 if (pfd.revents & POLLHUP) {
70 return DEAD_OBJECT;
71 }
Andrei Homescua8fa78c2022-04-05 05:56:52 +000072
73 return WOULD_BLOCK;
74 }
75
76 status_t interruptableWriteFully(
77 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Sebastian Pickl84b7cff2023-10-30 08:02:24 +000078 const std::optional<android::base::function_ref<status_t()>>& altPoll,
Andrei Homescua8fa78c2022-04-05 05:56:52 +000079 const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds)
80 override {
81 auto writeFn = [&](iovec* iovs, size_t niovs) -> ssize_t {
82 // TODO: send ancillaryFds. For now, we just abort if anyone tries
83 // to send any.
84 LOG_ALWAYS_FATAL_IF(ancillaryFds != nullptr && !ancillaryFds->empty(),
85 "File descriptors are not supported on Trusty yet");
Pawan49d74cb2022-08-03 21:19:11 +000086 return TEMP_FAILURE_RETRY(tipc_send(mSocket.fd.get(), iovs, niovs, nullptr, 0));
Andrei Homescua8fa78c2022-04-05 05:56:52 +000087 };
Andrei Homescu4407bc12023-03-21 23:25:25 +000088
89 status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, writeFn,
90 "tipc_send", POLLOUT, altPoll);
91 return adjustStatus(status);
Andrei Homescua8fa78c2022-04-05 05:56:52 +000092 }
93
94 status_t interruptableReadFully(
95 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Sebastian Pickl84b7cff2023-10-30 08:02:24 +000096 const std::optional<android::base::function_ref<status_t()>>& altPoll,
Andrei Homescua8fa78c2022-04-05 05:56:52 +000097 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* /*ancillaryFds*/)
98 override {
99 auto readFn = [&](iovec* iovs, size_t niovs) -> ssize_t {
100 // Fill the read buffer at most once per readFn call, then try to
101 // return as much of it as possible. If the input iovecs are spread
102 // across multiple messages that require multiple fillReadBuffer
103 // calls, we expect the caller to advance the iovecs past the first
104 // read and call readFn as many times as needed to get all the data
105 status_t ret = fillReadBuffer();
106 if (ret != OK) {
Andrei Homescu2fb43012022-08-17 04:15:00 +0000107 // We need to emulate a Linux read call, which sets errno on
108 // error and returns -1
109 errno = -ret;
110 return -1;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000111 }
112
113 ssize_t processSize = 0;
114 for (size_t i = 0; i < niovs && mReadBufferPos < mReadBufferSize; i++) {
115 auto& iov = iovs[i];
116 size_t numBytes = std::min(iov.iov_len, mReadBufferSize - mReadBufferPos);
117 memcpy(iov.iov_base, mReadBuffer.get() + mReadBufferPos, numBytes);
118 mReadBufferPos += numBytes;
119 processSize += numBytes;
120 }
121
122 return processSize;
123 };
Andrei Homescu4407bc12023-03-21 23:25:25 +0000124
125 status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, readFn, "read",
126 POLLIN, altPoll);
127 return adjustStatus(status);
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000128 }
129
Pawan49d74cb2022-08-03 21:19:11 +0000130 bool isWaiting() override { return mSocket.isInPollingState(); }
131
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000132private:
Andrei Homescu4407bc12023-03-21 23:25:25 +0000133 status_t adjustStatus(status_t status) {
134 if (status == -ENOTCONN) {
135 // TIPC returns ENOTCONN on disconnect, but that's basically
136 // the same as DEAD_OBJECT and the latter is the common libbinder
137 // error code for dead connections
138 return DEAD_OBJECT;
139 }
140
141 return status;
142 }
143
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000144 status_t fillReadBuffer() {
145 if (mReadBufferPos < mReadBufferSize) {
146 return OK;
147 }
148
149 if (!mReadBuffer) {
150 // Guarantee at least kDefaultBufferSize bytes
151 mReadBufferCapacity = std::max(mReadBufferCapacity, kDefaultBufferSize);
152 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
153 if (!mReadBuffer) {
154 return NO_MEMORY;
155 }
156 }
157
158 // Reset the size and position in case we have to exit with an error.
159 // After we read a message into the buffer, we update the size
160 // with the actual value.
161 mReadBufferPos = 0;
162 mReadBufferSize = 0;
163
164 while (true) {
Pawan49d74cb2022-08-03 21:19:11 +0000165 ssize_t processSize = TEMP_FAILURE_RETRY(
166 read(mSocket.fd.get(), mReadBuffer.get(), mReadBufferCapacity));
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000167 if (processSize == 0) {
168 return DEAD_OBJECT;
169 } else if (processSize < 0) {
170 int savedErrno = errno;
171 if (savedErrno == EMSGSIZE) {
172 // Buffer was too small, double it and retry
173 if (__builtin_mul_overflow(mReadBufferCapacity, 2, &mReadBufferCapacity)) {
174 return NO_MEMORY;
175 }
176 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
177 if (!mReadBuffer) {
178 return NO_MEMORY;
179 }
180 continue;
181 } else {
182 LOG_RPC_DETAIL("RpcTransport fillBuffer(): %s", strerror(savedErrno));
Andrei Homescu4407bc12023-03-21 23:25:25 +0000183 return adjustStatus(-savedErrno);
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000184 }
185 } else {
186 mReadBufferSize = static_cast<size_t>(processSize);
187 return OK;
188 }
189 }
190 }
191
Pawan3e0061c2022-08-26 21:08:34 +0000192 RpcTransportFd mSocket;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000193
194 // For now, we copy all the input data into a temporary buffer because
195 // we might get multiple interruptableReadFully calls per message, but
196 // the tipc device only allows one read call. We read every message into
197 // this temporary buffer, then return pieces of it from our method.
198 //
199 // The special transaction GET_MAX_THREADS takes 40 bytes, so the default
200 // size should start pretty high.
201 static constexpr size_t kDefaultBufferSize = 64;
202 std::unique_ptr<uint8_t[]> mReadBuffer;
203 size_t mReadBufferPos = 0;
204 size_t mReadBufferSize = 0;
205 size_t mReadBufferCapacity = 0;
206};
207
208// RpcTransportCtx for Trusty.
209class RpcTransportCtxTipcAndroid : public RpcTransportCtx {
210public:
Pawan3e0061c2022-08-26 21:08:34 +0000211 std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd fd,
212 FdTrigger*) const override {
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000213 return std::make_unique<RpcTransportTipcAndroid>(std::move(fd));
214 }
215 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
216};
217
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000218std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newServerCtx() const {
219 return std::make_unique<RpcTransportCtxTipcAndroid>();
220}
221
222std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newClientCtx() const {
223 return std::make_unique<RpcTransportCtxTipcAndroid>();
224}
225
226const char* RpcTransportCtxFactoryTipcAndroid::toCString() const {
227 return "trusty";
228}
229
230std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryTipcAndroid::make() {
231 return std::unique_ptr<RpcTransportCtxFactoryTipcAndroid>(
232 new RpcTransportCtxFactoryTipcAndroid());
233}
234
235} // namespace android