blob: c82201b28b08608b2fac1dbc25e613a4253cd2f0 [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:
39 explicit RpcTransportTipcAndroid(android::base::unique_fd socket)
40 : mSocket(std::move(socket)) {}
41
42 status_t pollRead() override {
43 if (mReadBufferPos < mReadBufferSize) {
44 // We have more data in the read buffer
45 return OK;
46 }
47
48 // Trusty IPC device is not a socket, so MSG_PEEK is not available
49 pollfd pfd{.fd = mSocket.get(), .events = static_cast<int16_t>(POLLIN), .revents = 0};
50 ssize_t ret = TEMP_FAILURE_RETRY(::poll(&pfd, 1, 0));
51 if (ret < 0) {
52 int savedErrno = errno;
53 if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
54 return WOULD_BLOCK;
55 }
56
57 LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno));
58 return -savedErrno;
59 }
60
61 if (pfd.revents & POLLNVAL) {
62 return BAD_VALUE;
63 }
64 if (pfd.revents & POLLERR) {
65 return DEAD_OBJECT;
66 }
67 if (pfd.revents & POLLHUP) {
68 return DEAD_OBJECT;
69 }
70 if (pfd.revents & POLLIN) {
71 return OK;
72 }
73
74 return WOULD_BLOCK;
75 }
76
77 status_t interruptableWriteFully(
78 FdTrigger* fdTrigger, iovec* iovs, int niovs,
79 const std::optional<android::base::function_ref<status_t()>>& altPoll,
80 const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds)
81 override {
82 auto writeFn = [&](iovec* iovs, size_t niovs) -> ssize_t {
83 // TODO: send ancillaryFds. For now, we just abort if anyone tries
84 // to send any.
85 LOG_ALWAYS_FATAL_IF(ancillaryFds != nullptr && !ancillaryFds->empty(),
86 "File descriptors are not supported on Trusty yet");
87 return TEMP_FAILURE_RETRY(tipc_send(mSocket.get(), iovs, niovs, nullptr, 0));
88 };
89 return interruptableReadOrWrite(mSocket.get(), fdTrigger, iovs, niovs, writeFn, "tipc_send",
90 POLLOUT, altPoll);
91 }
92
93 status_t interruptableReadFully(
94 FdTrigger* fdTrigger, iovec* iovs, int niovs,
95 const std::optional<android::base::function_ref<status_t()>>& altPoll,
96 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* /*ancillaryFds*/)
97 override {
98 auto readFn = [&](iovec* iovs, size_t niovs) -> ssize_t {
99 // Fill the read buffer at most once per readFn call, then try to
100 // return as much of it as possible. If the input iovecs are spread
101 // across multiple messages that require multiple fillReadBuffer
102 // calls, we expect the caller to advance the iovecs past the first
103 // read and call readFn as many times as needed to get all the data
104 status_t ret = fillReadBuffer();
105 if (ret != OK) {
Andrei Homescu2fb43012022-08-17 04:15:00 +0000106 // We need to emulate a Linux read call, which sets errno on
107 // error and returns -1
108 errno = -ret;
109 return -1;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000110 }
111
112 ssize_t processSize = 0;
113 for (size_t i = 0; i < niovs && mReadBufferPos < mReadBufferSize; i++) {
114 auto& iov = iovs[i];
115 size_t numBytes = std::min(iov.iov_len, mReadBufferSize - mReadBufferPos);
116 memcpy(iov.iov_base, mReadBuffer.get() + mReadBufferPos, numBytes);
117 mReadBufferPos += numBytes;
118 processSize += numBytes;
119 }
120
121 return processSize;
122 };
123 return interruptableReadOrWrite(mSocket.get(), fdTrigger, iovs, niovs, readFn, "read",
124 POLLIN, altPoll);
125 }
126
127private:
128 status_t fillReadBuffer() {
129 if (mReadBufferPos < mReadBufferSize) {
130 return OK;
131 }
132
133 if (!mReadBuffer) {
134 // Guarantee at least kDefaultBufferSize bytes
135 mReadBufferCapacity = std::max(mReadBufferCapacity, kDefaultBufferSize);
136 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
137 if (!mReadBuffer) {
138 return NO_MEMORY;
139 }
140 }
141
142 // Reset the size and position in case we have to exit with an error.
143 // After we read a message into the buffer, we update the size
144 // with the actual value.
145 mReadBufferPos = 0;
146 mReadBufferSize = 0;
147
148 while (true) {
149 ssize_t processSize =
150 TEMP_FAILURE_RETRY(read(mSocket.get(), mReadBuffer.get(), mReadBufferCapacity));
151 if (processSize == 0) {
152 return DEAD_OBJECT;
153 } else if (processSize < 0) {
154 int savedErrno = errno;
155 if (savedErrno == EMSGSIZE) {
156 // Buffer was too small, double it and retry
157 if (__builtin_mul_overflow(mReadBufferCapacity, 2, &mReadBufferCapacity)) {
158 return NO_MEMORY;
159 }
160 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
161 if (!mReadBuffer) {
162 return NO_MEMORY;
163 }
164 continue;
165 } else {
166 LOG_RPC_DETAIL("RpcTransport fillBuffer(): %s", strerror(savedErrno));
167 return -savedErrno;
168 }
169 } else {
170 mReadBufferSize = static_cast<size_t>(processSize);
171 return OK;
172 }
173 }
174 }
175
176 base::unique_fd mSocket;
177
178 // For now, we copy all the input data into a temporary buffer because
179 // we might get multiple interruptableReadFully calls per message, but
180 // the tipc device only allows one read call. We read every message into
181 // this temporary buffer, then return pieces of it from our method.
182 //
183 // The special transaction GET_MAX_THREADS takes 40 bytes, so the default
184 // size should start pretty high.
185 static constexpr size_t kDefaultBufferSize = 64;
186 std::unique_ptr<uint8_t[]> mReadBuffer;
187 size_t mReadBufferPos = 0;
188 size_t mReadBufferSize = 0;
189 size_t mReadBufferCapacity = 0;
190};
191
192// RpcTransportCtx for Trusty.
193class RpcTransportCtxTipcAndroid : public RpcTransportCtx {
194public:
195 std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd,
196 FdTrigger*) const override {
197 return std::make_unique<RpcTransportTipcAndroid>(std::move(fd));
198 }
199 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
200};
201
202} // namespace
203
204std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newServerCtx() const {
205 return std::make_unique<RpcTransportCtxTipcAndroid>();
206}
207
208std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newClientCtx() const {
209 return std::make_unique<RpcTransportCtxTipcAndroid>();
210}
211
212const char* RpcTransportCtxFactoryTipcAndroid::toCString() const {
213 return "trusty";
214}
215
216std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryTipcAndroid::make() {
217 return std::unique_ptr<RpcTransportCtxFactoryTipcAndroid>(
218 new RpcTransportCtxFactoryTipcAndroid());
219}
220
221} // namespace android