blob: 8b3ddfbd6fb242c6e5a2090057631224ab7b29b9 [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 }
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 };
Pawan49d74cb2022-08-03 21:19:11 +000090 return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, writeFn, "tipc_send",
Andrei Homescua8fa78c2022-04-05 05:56:52 +000091 POLLOUT, altPoll);
92 }
93
94 status_t interruptableReadFully(
95 FdTrigger* fdTrigger, iovec* iovs, int niovs,
96 const std::optional<android::base::function_ref<status_t()>>& altPoll,
97 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 };
Pawan49d74cb2022-08-03 21:19:11 +0000124 return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, readFn, "read", POLLIN,
125 altPoll);
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000126 }
127
Pawan49d74cb2022-08-03 21:19:11 +0000128 bool isWaiting() override { return mSocket.isInPollingState(); }
129
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000130private:
131 status_t fillReadBuffer() {
132 if (mReadBufferPos < mReadBufferSize) {
133 return OK;
134 }
135
136 if (!mReadBuffer) {
137 // Guarantee at least kDefaultBufferSize bytes
138 mReadBufferCapacity = std::max(mReadBufferCapacity, kDefaultBufferSize);
139 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
140 if (!mReadBuffer) {
141 return NO_MEMORY;
142 }
143 }
144
145 // Reset the size and position in case we have to exit with an error.
146 // After we read a message into the buffer, we update the size
147 // with the actual value.
148 mReadBufferPos = 0;
149 mReadBufferSize = 0;
150
151 while (true) {
Pawan49d74cb2022-08-03 21:19:11 +0000152 ssize_t processSize = TEMP_FAILURE_RETRY(
153 read(mSocket.fd.get(), mReadBuffer.get(), mReadBufferCapacity));
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000154 if (processSize == 0) {
155 return DEAD_OBJECT;
156 } else if (processSize < 0) {
157 int savedErrno = errno;
158 if (savedErrno == EMSGSIZE) {
159 // Buffer was too small, double it and retry
160 if (__builtin_mul_overflow(mReadBufferCapacity, 2, &mReadBufferCapacity)) {
161 return NO_MEMORY;
162 }
163 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
164 if (!mReadBuffer) {
165 return NO_MEMORY;
166 }
167 continue;
168 } else {
169 LOG_RPC_DETAIL("RpcTransport fillBuffer(): %s", strerror(savedErrno));
170 return -savedErrno;
171 }
172 } else {
173 mReadBufferSize = static_cast<size_t>(processSize);
174 return OK;
175 }
176 }
177 }
178
Pawan3e0061c2022-08-26 21:08:34 +0000179 RpcTransportFd mSocket;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000180
181 // For now, we copy all the input data into a temporary buffer because
182 // we might get multiple interruptableReadFully calls per message, but
183 // the tipc device only allows one read call. We read every message into
184 // this temporary buffer, then return pieces of it from our method.
185 //
186 // The special transaction GET_MAX_THREADS takes 40 bytes, so the default
187 // size should start pretty high.
188 static constexpr size_t kDefaultBufferSize = 64;
189 std::unique_ptr<uint8_t[]> mReadBuffer;
190 size_t mReadBufferPos = 0;
191 size_t mReadBufferSize = 0;
192 size_t mReadBufferCapacity = 0;
193};
194
195// RpcTransportCtx for Trusty.
196class RpcTransportCtxTipcAndroid : public RpcTransportCtx {
197public:
Pawan3e0061c2022-08-26 21:08:34 +0000198 std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd fd,
199 FdTrigger*) const override {
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000200 return std::make_unique<RpcTransportTipcAndroid>(std::move(fd));
201 }
202 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
203};
204
205} // namespace
206
207std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newServerCtx() const {
208 return std::make_unique<RpcTransportCtxTipcAndroid>();
209}
210
211std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newClientCtx() const {
212 return std::make_unique<RpcTransportCtxTipcAndroid>();
213}
214
215const char* RpcTransportCtxFactoryTipcAndroid::toCString() const {
216 return "trusty";
217}
218
219std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryTipcAndroid::make() {
220 return std::unique_ptr<RpcTransportCtxFactoryTipcAndroid>(
221 new RpcTransportCtxFactoryTipcAndroid());
222}
223
224} // namespace android