Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 1 | /* |
| 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 Wasilczyk | 71ab6fd | 2023-10-11 18:47:46 +0000 | [diff] [blame] | 29 | using namespace android::binder::impl; |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 30 | using android::base::Error; |
| 31 | using android::base::Result; |
| 32 | |
| 33 | namespace android { |
| 34 | |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 35 | // RpcTransport for writing Trusty IPC clients in Android. |
| 36 | class RpcTransportTipcAndroid : public RpcTransport { |
| 37 | public: |
Pawan | 3e0061c | 2022-08-26 21:08:34 +0000 | [diff] [blame] | 38 | explicit RpcTransportTipcAndroid(android::RpcTransportFd socket) : mSocket(std::move(socket)) {} |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 39 | |
| 40 | status_t pollRead() override { |
| 41 | if (mReadBufferPos < mReadBufferSize) { |
| 42 | // We have more data in the read buffer |
| 43 | return OK; |
| 44 | } |
| 45 | |
| 46 | // Trusty IPC device is not a socket, so MSG_PEEK is not available |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 47 | pollfd pfd{.fd = mSocket.fd.get(), .events = static_cast<int16_t>(POLLIN), .revents = 0}; |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 48 | ssize_t ret = TEMP_FAILURE_RETRY(::poll(&pfd, 1, 0)); |
| 49 | if (ret < 0) { |
| 50 | int savedErrno = errno; |
| 51 | if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) { |
| 52 | return WOULD_BLOCK; |
| 53 | } |
| 54 | |
| 55 | LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno)); |
Andrei Homescu | 4407bc1 | 2023-03-21 23:25:25 +0000 | [diff] [blame] | 56 | return adjustStatus(-savedErrno); |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | if (pfd.revents & POLLNVAL) { |
| 60 | return BAD_VALUE; |
| 61 | } |
| 62 | if (pfd.revents & POLLERR) { |
| 63 | return DEAD_OBJECT; |
| 64 | } |
Andrei Homescu | 805ca7a | 2022-12-09 07:16:23 +0000 | [diff] [blame] | 65 | if (pfd.revents & POLLIN) { |
| 66 | // Copied from FdTrigger.cpp: Even though POLLHUP may also be set, |
| 67 | // treat it as a success condition to ensure data is drained. |
| 68 | return OK; |
| 69 | } |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 70 | if (pfd.revents & POLLHUP) { |
| 71 | return DEAD_OBJECT; |
| 72 | } |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 73 | |
| 74 | return WOULD_BLOCK; |
| 75 | } |
| 76 | |
| 77 | status_t interruptableWriteFully( |
| 78 | FdTrigger* fdTrigger, iovec* iovs, int niovs, |
Tomasz Wasilczyk | 71ab6fd | 2023-10-11 18:47:46 +0000 | [diff] [blame] | 79 | const std::optional<SmallFunction<status_t()>>& altPoll, |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 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"); |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 87 | return TEMP_FAILURE_RETRY(tipc_send(mSocket.fd.get(), iovs, niovs, nullptr, 0)); |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 88 | }; |
Andrei Homescu | 4407bc1 | 2023-03-21 23:25:25 +0000 | [diff] [blame] | 89 | |
| 90 | status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, writeFn, |
| 91 | "tipc_send", POLLOUT, altPoll); |
| 92 | return adjustStatus(status); |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | status_t interruptableReadFully( |
| 96 | FdTrigger* fdTrigger, iovec* iovs, int niovs, |
Tomasz Wasilczyk | 71ab6fd | 2023-10-11 18:47:46 +0000 | [diff] [blame] | 97 | const std::optional<SmallFunction<status_t()>>& altPoll, |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 98 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* /*ancillaryFds*/) |
| 99 | override { |
| 100 | auto readFn = [&](iovec* iovs, size_t niovs) -> ssize_t { |
| 101 | // Fill the read buffer at most once per readFn call, then try to |
| 102 | // return as much of it as possible. If the input iovecs are spread |
| 103 | // across multiple messages that require multiple fillReadBuffer |
| 104 | // calls, we expect the caller to advance the iovecs past the first |
| 105 | // read and call readFn as many times as needed to get all the data |
| 106 | status_t ret = fillReadBuffer(); |
| 107 | if (ret != OK) { |
Andrei Homescu | 2fb4301 | 2022-08-17 04:15:00 +0000 | [diff] [blame] | 108 | // We need to emulate a Linux read call, which sets errno on |
| 109 | // error and returns -1 |
| 110 | errno = -ret; |
| 111 | return -1; |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | ssize_t processSize = 0; |
| 115 | for (size_t i = 0; i < niovs && mReadBufferPos < mReadBufferSize; i++) { |
| 116 | auto& iov = iovs[i]; |
| 117 | size_t numBytes = std::min(iov.iov_len, mReadBufferSize - mReadBufferPos); |
| 118 | memcpy(iov.iov_base, mReadBuffer.get() + mReadBufferPos, numBytes); |
| 119 | mReadBufferPos += numBytes; |
| 120 | processSize += numBytes; |
| 121 | } |
| 122 | |
| 123 | return processSize; |
| 124 | }; |
Andrei Homescu | 4407bc1 | 2023-03-21 23:25:25 +0000 | [diff] [blame] | 125 | |
| 126 | status_t status = interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, readFn, "read", |
| 127 | POLLIN, altPoll); |
| 128 | return adjustStatus(status); |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 131 | bool isWaiting() override { return mSocket.isInPollingState(); } |
| 132 | |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 133 | private: |
Andrei Homescu | 4407bc1 | 2023-03-21 23:25:25 +0000 | [diff] [blame] | 134 | status_t adjustStatus(status_t status) { |
| 135 | if (status == -ENOTCONN) { |
| 136 | // TIPC returns ENOTCONN on disconnect, but that's basically |
| 137 | // the same as DEAD_OBJECT and the latter is the common libbinder |
| 138 | // error code for dead connections |
| 139 | return DEAD_OBJECT; |
| 140 | } |
| 141 | |
| 142 | return status; |
| 143 | } |
| 144 | |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 145 | status_t fillReadBuffer() { |
| 146 | if (mReadBufferPos < mReadBufferSize) { |
| 147 | return OK; |
| 148 | } |
| 149 | |
| 150 | if (!mReadBuffer) { |
| 151 | // Guarantee at least kDefaultBufferSize bytes |
| 152 | mReadBufferCapacity = std::max(mReadBufferCapacity, kDefaultBufferSize); |
| 153 | mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]); |
| 154 | if (!mReadBuffer) { |
| 155 | return NO_MEMORY; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Reset the size and position in case we have to exit with an error. |
| 160 | // After we read a message into the buffer, we update the size |
| 161 | // with the actual value. |
| 162 | mReadBufferPos = 0; |
| 163 | mReadBufferSize = 0; |
| 164 | |
| 165 | while (true) { |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 166 | ssize_t processSize = TEMP_FAILURE_RETRY( |
| 167 | read(mSocket.fd.get(), mReadBuffer.get(), mReadBufferCapacity)); |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 168 | if (processSize == 0) { |
| 169 | return DEAD_OBJECT; |
| 170 | } else if (processSize < 0) { |
| 171 | int savedErrno = errno; |
| 172 | if (savedErrno == EMSGSIZE) { |
| 173 | // Buffer was too small, double it and retry |
| 174 | if (__builtin_mul_overflow(mReadBufferCapacity, 2, &mReadBufferCapacity)) { |
| 175 | return NO_MEMORY; |
| 176 | } |
| 177 | mReadBuffer.reset(new (std::nothrow) uint8_t[mReadBufferCapacity]); |
| 178 | if (!mReadBuffer) { |
| 179 | return NO_MEMORY; |
| 180 | } |
| 181 | continue; |
| 182 | } else { |
| 183 | LOG_RPC_DETAIL("RpcTransport fillBuffer(): %s", strerror(savedErrno)); |
Andrei Homescu | 4407bc1 | 2023-03-21 23:25:25 +0000 | [diff] [blame] | 184 | return adjustStatus(-savedErrno); |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 185 | } |
| 186 | } else { |
| 187 | mReadBufferSize = static_cast<size_t>(processSize); |
| 188 | return OK; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Pawan | 3e0061c | 2022-08-26 21:08:34 +0000 | [diff] [blame] | 193 | RpcTransportFd mSocket; |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 194 | |
| 195 | // For now, we copy all the input data into a temporary buffer because |
| 196 | // we might get multiple interruptableReadFully calls per message, but |
| 197 | // the tipc device only allows one read call. We read every message into |
| 198 | // this temporary buffer, then return pieces of it from our method. |
| 199 | // |
| 200 | // The special transaction GET_MAX_THREADS takes 40 bytes, so the default |
| 201 | // size should start pretty high. |
| 202 | static constexpr size_t kDefaultBufferSize = 64; |
| 203 | std::unique_ptr<uint8_t[]> mReadBuffer; |
| 204 | size_t mReadBufferPos = 0; |
| 205 | size_t mReadBufferSize = 0; |
| 206 | size_t mReadBufferCapacity = 0; |
| 207 | }; |
| 208 | |
| 209 | // RpcTransportCtx for Trusty. |
| 210 | class RpcTransportCtxTipcAndroid : public RpcTransportCtx { |
| 211 | public: |
Pawan | 3e0061c | 2022-08-26 21:08:34 +0000 | [diff] [blame] | 212 | std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd fd, |
| 213 | FdTrigger*) const override { |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 214 | return std::make_unique<RpcTransportTipcAndroid>(std::move(fd)); |
| 215 | } |
| 216 | std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; } |
| 217 | }; |
| 218 | |
Andrei Homescu | a8fa78c | 2022-04-05 05:56:52 +0000 | [diff] [blame] | 219 | std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newServerCtx() const { |
| 220 | return std::make_unique<RpcTransportCtxTipcAndroid>(); |
| 221 | } |
| 222 | |
| 223 | std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryTipcAndroid::newClientCtx() const { |
| 224 | return std::make_unique<RpcTransportCtxTipcAndroid>(); |
| 225 | } |
| 226 | |
| 227 | const char* RpcTransportCtxFactoryTipcAndroid::toCString() const { |
| 228 | return "trusty"; |
| 229 | } |
| 230 | |
| 231 | std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryTipcAndroid::make() { |
| 232 | return std::unique_ptr<RpcTransportCtxFactoryTipcAndroid>( |
| 233 | new RpcTransportCtxFactoryTipcAndroid()); |
| 234 | } |
| 235 | |
| 236 | } // namespace android |