blob: 188ba3b0cb5ea5af0fad96a31d65eba198f12ec9 [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
Tomasz Wasilczyk35804862023-10-30 14:19:19 +000029using namespace android::binder::impl;
30
Andrei Homescua8fa78c2022-04-05 05:56:52 +000031namespace android {
32
Andrei Homescua8fa78c2022-04-05 05:56:52 +000033// RpcTransport for writing Trusty IPC clients in Android.
34class RpcTransportTipcAndroid : public RpcTransport {
35public:
Pawan3e0061c2022-08-26 21:08:34 +000036 explicit RpcTransportTipcAndroid(android::RpcTransportFd socket) : mSocket(std::move(socket)) {}
Andrei Homescua8fa78c2022-04-05 05:56:52 +000037
38 status_t pollRead() override {
39 if (mReadBufferPos < mReadBufferSize) {
40 // We have more data in the read buffer
41 return OK;
42 }
43
44 // Trusty IPC device is not a socket, so MSG_PEEK is not available
Pawan49d74cb2022-08-03 21:19:11 +000045 pollfd pfd{.fd = mSocket.fd.get(), .events = static_cast<int16_t>(POLLIN), .revents = 0};
Andrei Homescua8fa78c2022-04-05 05:56:52 +000046 ssize_t ret = TEMP_FAILURE_RETRY(::poll(&pfd, 1, 0));
47 if (ret < 0) {
48 int savedErrno = errno;
49 if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
50 return WOULD_BLOCK;
51 }
52
53 LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno));
Andrei Homescu4407bc12023-03-21 23:25:25 +000054 return adjustStatus(-savedErrno);
Andrei Homescua8fa78c2022-04-05 05:56:52 +000055 }
56
57 if (pfd.revents & POLLNVAL) {
58 return BAD_VALUE;
59 }
60 if (pfd.revents & POLLERR) {
61 return DEAD_OBJECT;
62 }
Andrei Homescu805ca7a2022-12-09 07:16:23 +000063 if (pfd.revents & POLLIN) {
64 // Copied from FdTrigger.cpp: Even though POLLHUP may also be set,
65 // treat it as a success condition to ensure data is drained.
66 return OK;
67 }
Andrei Homescua8fa78c2022-04-05 05:56:52 +000068 if (pfd.revents & POLLHUP) {
69 return DEAD_OBJECT;
70 }
Andrei Homescua8fa78c2022-04-05 05:56:52 +000071
72 return WOULD_BLOCK;
73 }
74
75 status_t interruptableWriteFully(
76 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Tomasz Wasilczyk35804862023-10-30 14:19:19 +000077 const std::optional<SmallFunction<status_t()>>& altPoll,
Andrei Homescua8fa78c2022-04-05 05:56:52 +000078 const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds)
79 override {
80 auto writeFn = [&](iovec* iovs, size_t niovs) -> ssize_t {
81 // TODO: send ancillaryFds. For now, we just abort if anyone tries
82 // to send any.
83 LOG_ALWAYS_FATAL_IF(ancillaryFds != nullptr && !ancillaryFds->empty(),
84 "File descriptors are not supported on Trusty yet");
Pawan49d74cb2022-08-03 21:19:11 +000085 return TEMP_FAILURE_RETRY(tipc_send(mSocket.fd.get(), iovs, niovs, nullptr, 0));
Andrei Homescua8fa78c2022-04-05 05:56:52 +000086 };
Andrei Homescu4407bc12023-03-21 23:25:25 +000087
88 status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, writeFn,
89 "tipc_send", POLLOUT, altPoll);
90 return adjustStatus(status);
Andrei Homescua8fa78c2022-04-05 05:56:52 +000091 }
92
93 status_t interruptableReadFully(
94 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Tomasz Wasilczyk35804862023-10-30 14:19:19 +000095 const std::optional<SmallFunction<status_t()>>& altPoll,
Andrei Homescua8fa78c2022-04-05 05:56:52 +000096 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 };
Andrei Homescu4407bc12023-03-21 23:25:25 +0000123
124 status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, readFn, "read",
125 POLLIN, altPoll);
126 return adjustStatus(status);
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000127 }
128
Pawan49d74cb2022-08-03 21:19:11 +0000129 bool isWaiting() override { return mSocket.isInPollingState(); }
130
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000131private:
Andrei Homescu4407bc12023-03-21 23:25:25 +0000132 status_t adjustStatus(status_t status) {
133 if (status == -ENOTCONN) {
134 // TIPC returns ENOTCONN on disconnect, but that's basically
135 // the same as DEAD_OBJECT and the latter is the common libbinder
136 // error code for dead connections
137 return DEAD_OBJECT;
138 }
139
140 return status;
141 }
142
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000143 status_t fillReadBuffer() {
144 if (mReadBufferPos < mReadBufferSize) {
145 return OK;
146 }
147
148 if (!mReadBuffer) {
149 // Guarantee at least kDefaultBufferSize bytes
150 mReadBufferCapacity = std::max(mReadBufferCapacity, kDefaultBufferSize);
151 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
152 if (!mReadBuffer) {
153 return NO_MEMORY;
154 }
155 }
156
157 // Reset the size and position in case we have to exit with an error.
158 // After we read a message into the buffer, we update the size
159 // with the actual value.
160 mReadBufferPos = 0;
161 mReadBufferSize = 0;
162
163 while (true) {
Pawan49d74cb2022-08-03 21:19:11 +0000164 ssize_t processSize = TEMP_FAILURE_RETRY(
165 read(mSocket.fd.get(), mReadBuffer.get(), mReadBufferCapacity));
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000166 if (processSize == 0) {
167 return DEAD_OBJECT;
168 } else if (processSize < 0) {
169 int savedErrno = errno;
170 if (savedErrno == EMSGSIZE) {
171 // Buffer was too small, double it and retry
172 if (__builtin_mul_overflow(mReadBufferCapacity, 2, &mReadBufferCapacity)) {
173 return NO_MEMORY;
174 }
175 mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]);
176 if (!mReadBuffer) {
177 return NO_MEMORY;
178 }
179 continue;
180 } else {
181 LOG_RPC_DETAIL("RpcTransport fillBuffer(): %s", strerror(savedErrno));
Andrei Homescu4407bc12023-03-21 23:25:25 +0000182 return adjustStatus(-savedErrno);
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000183 }
184 } else {
185 mReadBufferSize = static_cast<size_t>(processSize);
186 return OK;
187 }
188 }
189 }
190
Pawan3e0061c2022-08-26 21:08:34 +0000191 RpcTransportFd mSocket;
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000192
193 // For now, we copy all the input data into a temporary buffer because
194 // we might get multiple interruptableReadFully calls per message, but
195 // the tipc device only allows one read call. We read every message into
196 // this temporary buffer, then return pieces of it from our method.
197 //
198 // The special transaction GET_MAX_THREADS takes 40 bytes, so the default
199 // size should start pretty high.
200 static constexpr size_t kDefaultBufferSize = 64;
201 std::unique_ptr<uint8_t[]> mReadBuffer;
202 size_t mReadBufferPos = 0;
203 size_t mReadBufferSize = 0;
204 size_t mReadBufferCapacity = 0;
205};
206
207// RpcTransportCtx for Trusty.
208class RpcTransportCtxTipcAndroid : public RpcTransportCtx {
209public:
Pawan3e0061c2022-08-26 21:08:34 +0000210 std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd fd,
211 FdTrigger*) const override {
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000212 return std::make_unique<RpcTransportTipcAndroid>(std::move(fd));
213 }
214 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
215};
216
Andrei Homescua8fa78c2022-04-05 05:56:52 +0000217std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newServerCtx() const {
218 return std::make_unique<RpcTransportCtxTipcAndroid>();
219}
220
221std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newClientCtx() const {
222 return std::make_unique<RpcTransportCtxTipcAndroid>();
223}
224
225const char* RpcTransportCtxFactoryTipcAndroid::toCString() const {
226 return "trusty";
227}
228
229std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryTipcAndroid::make() {
230 return std::unique_ptr<RpcTransportCtxFactoryTipcAndroid>(
231 new RpcTransportCtxFactoryTipcAndroid());
232}
233
234} // namespace android