blob: 453279c0a53ad44e693095cc1898b26562d84258 [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));
57 return -savedErrno;
58 }
59
60 if (pfd.revents & POLLNVAL) {
61 return BAD_VALUE;
62 }
63 if (pfd.revents & POLLERR) {
64 return DEAD_OBJECT;
65 }
66 if (pfd.revents & POLLHUP) {
67 return DEAD_OBJECT;
68 }
69 if (pfd.revents & POLLIN) {
70 return OK;
71 }
72
73 return WOULD_BLOCK;
74 }
75
76 status_t interruptableWriteFully(
77 FdTrigger* fdTrigger, iovec* iovs, int niovs,
78 const std::optional<android::base::function_ref<status_t()>>& altPoll,
79 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 };
Pawan49d74cb2022-08-03 21:19:11 +000088 return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, writeFn, "tipc_send",
Andrei Homescua8fa78c2022-04-05 05:56:52 +000089 POLLOUT, altPoll);
90 }
91
92 status_t interruptableReadFully(
93 FdTrigger* fdTrigger, iovec* iovs, int niovs,
94 const std::optional<android::base::function_ref<status_t()>>& altPoll,
95 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* /*ancillaryFds*/)
96 override {
97 auto readFn = [&](iovec* iovs, size_t niovs) -> ssize_t {
98 // Fill the read buffer at most once per readFn call, then try to
99 // return as much of it as possible. If the input iovecs are spread
100 // across multiple messages that require multiple fillReadBuffer
101 // calls, we expect the caller to advance the iovecs past the first
102 // read and call readFn as many times as needed to get all the data
103 status_t ret = fillReadBuffer();
104 if (ret != OK) {
Andrei Homescu2fb43012022-08-17 04:15:00 +0000105 // We need to emulate a Linux read call, which sets errno on
106 // error and returns -1
107 errno = -ret;
108 return -1;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000109 }
110
111 ssize_t processSize = 0;
112 for (size_t i = 0; i < niovs && mReadBufferPos < mReadBufferSize; i++) {
113 auto& iov = iovs[i];
114 size_t numBytes = std::min(iov.iov_len, mReadBufferSize - mReadBufferPos);
115 memcpy(iov.iov_base, mReadBuffer.get() + mReadBufferPos, numBytes);
116 mReadBufferPos += numBytes;
117 processSize += numBytes;
118 }
119
120 return processSize;
121 };
Pawan49d74cb2022-08-03 21:19:11 +0000122 return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, readFn, "read", POLLIN,
123 altPoll);
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000124 }
125
Pawan49d74cb2022-08-03 21:19:11 +0000126 bool isWaiting() override { return mSocket.isInPollingState(); }
127
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000128private:
129 status_t fillReadBuffer() {
130 if (mReadBufferPos < mReadBufferSize) {
131 return OK;
132 }
133
134 if (!mReadBuffer) {
135 // Guarantee at least kDefaultBufferSize bytes
136 mReadBufferCapacity = std::max(mReadBufferCapacity, kDefaultBufferSize);
137 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
138 if (!mReadBuffer) {
139 return NO_MEMORY;
140 }
141 }
142
143 // Reset the size and position in case we have to exit with an error.
144 // After we read a message into the buffer, we update the size
145 // with the actual value.
146 mReadBufferPos = 0;
147 mReadBufferSize = 0;
148
149 while (true) {
Pawan49d74cb2022-08-03 21:19:11 +0000150 ssize_t processSize = TEMP_FAILURE_RETRY(
151 read(mSocket.fd.get(), mReadBuffer.get(), mReadBufferCapacity));
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000152 if (processSize == 0) {
153 return DEAD_OBJECT;
154 } else if (processSize < 0) {
155 int savedErrno = errno;
156 if (savedErrno == EMSGSIZE) {
157 // Buffer was too small, double it and retry
158 if (__builtin_mul_overflow(mReadBufferCapacity, 2, &mReadBufferCapacity)) {
159 return NO_MEMORY;
160 }
161 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
162 if (!mReadBuffer) {
163 return NO_MEMORY;
164 }
165 continue;
166 } else {
167 LOG_RPC_DETAIL("RpcTransport fillBuffer(): %s", strerror(savedErrno));
168 return -savedErrno;
169 }
170 } else {
171 mReadBufferSize = static_cast<size_t>(processSize);
172 return OK;
173 }
174 }
175 }
176
Pawan3e0061c2022-08-26 21:08:34 +0000177 RpcTransportFd mSocket;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000178
179 // For now, we copy all the input data into a temporary buffer because
180 // we might get multiple interruptableReadFully calls per message, but
181 // the tipc device only allows one read call. We read every message into
182 // this temporary buffer, then return pieces of it from our method.
183 //
184 // The special transaction GET_MAX_THREADS takes 40 bytes, so the default
185 // size should start pretty high.
186 static constexpr size_t kDefaultBufferSize = 64;
187 std::unique_ptr<uint8_t[]> mReadBuffer;
188 size_t mReadBufferPos = 0;
189 size_t mReadBufferSize = 0;
190 size_t mReadBufferCapacity = 0;
191};
192
193// RpcTransportCtx for Trusty.
194class RpcTransportCtxTipcAndroid : public RpcTransportCtx {
195public:
Pawan3e0061c2022-08-26 21:08:34 +0000196 std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd fd,
197 FdTrigger*) const override {
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000198 return std::make_unique<RpcTransportTipcAndroid>(std::move(fd));
199 }
200 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
201};
202
203} // namespace
204
205std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newServerCtx() const {
206 return std::make_unique<RpcTransportCtxTipcAndroid>();
207}
208
209std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newClientCtx() const {
210 return std::make_unique<RpcTransportCtxTipcAndroid>();
211}
212
213const char* RpcTransportCtxFactoryTipcAndroid::toCString() const {
214 return "trusty";
215}
216
217std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryTipcAndroid::make() {
218 return std::unique_ptr<RpcTransportCtxFactoryTipcAndroid>(
219 new RpcTransportCtxFactoryTipcAndroid());
220}
221
222} // namespace android