Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 1 | /* |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 2 | * Copyright 2021, The Android Open Source Project |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 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 | #include "TrustyApp.h" |
| 18 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 19 | #include <BufferAllocator/BufferAllocator.h> |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 21 | #include <sys/mman.h> |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 22 | #include <sys/uio.h> |
| 23 | #include <trusty/tipc.h> |
| 24 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 25 | #define countof(arr) (sizeof(arr) / sizeof(arr[0])) |
| 26 | |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace trusty { |
Tri Vo | abd86f8 | 2021-02-19 22:09:22 -0800 | [diff] [blame] | 29 | namespace confirmationui { |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 30 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 31 | using ::android::base::unique_fd; |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 32 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 33 | ssize_t TrustyApp::TrustyRpc(const uint8_t* obegin, const uint8_t* oend, uint8_t* ibegin, |
| 34 | uint8_t* iend) { |
| 35 | uint32_t olen = oend - obegin; |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 36 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 37 | if (olen > shm_len_) { |
| 38 | LOG(ERROR) << AT << "request message too long to fit in shared memory"; |
| 39 | return -1; |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 42 | memcpy(shm_base_, obegin, olen); |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 43 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 44 | confirmationui_hdr hdr = { |
| 45 | .cmd = CONFIRMATIONUI_CMD_MSG, |
| 46 | }; |
| 47 | confirmationui_msg_args args = { |
| 48 | .msg_len = olen, |
| 49 | }; |
| 50 | iovec iov[] = { |
| 51 | { |
| 52 | .iov_base = &hdr, |
| 53 | .iov_len = sizeof(hdr), |
| 54 | }, |
| 55 | { |
| 56 | .iov_base = &args, |
| 57 | .iov_len = sizeof(args), |
| 58 | }, |
| 59 | }; |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 60 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 61 | int rc = tipc_send(handle_, iov, countof(iov), NULL, 0); |
| 62 | if (rc != static_cast<int>(sizeof(hdr) + sizeof(args))) { |
| 63 | LOG(ERROR) << AT << "failed to send MSG request"; |
| 64 | return -1; |
| 65 | } |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 66 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 67 | rc = readv(handle_, iov, countof(iov)); |
| 68 | if (rc != static_cast<int>(sizeof(hdr) + sizeof(args))) { |
| 69 | LOG(ERROR) << AT << "failed to receive MSG response"; |
| 70 | return -1; |
| 71 | } |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 72 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 73 | if (hdr.cmd != (CONFIRMATIONUI_CMD_MSG | CONFIRMATIONUI_RESP_BIT)) { |
| 74 | LOG(ERROR) << AT << "unknown response command: " << hdr.cmd; |
| 75 | return -1; |
| 76 | } |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 77 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 78 | uint32_t ilen = iend - ibegin; |
| 79 | if (args.msg_len > ilen) { |
| 80 | LOG(ERROR) << AT << "response message too long to fit in return buffer"; |
| 81 | return -1; |
| 82 | } |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 83 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 84 | memcpy(ibegin, shm_base_, args.msg_len); |
| 85 | |
| 86 | return args.msg_len; |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TrustyApp::TrustyApp(const std::string& path, const std::string& appname) |
| 90 | : handle_(kInvalidHandle) { |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 91 | unique_fd tipc_handle(tipc_connect(path.c_str(), appname.c_str())); |
| 92 | if (tipc_handle < 0) { |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 93 | LOG(ERROR) << AT << "failed to connect to Trusty TA \"" << appname << "\" using dev:" |
| 94 | << "\"" << path << "\""; |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 95 | return; |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 96 | } |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 97 | |
Kalesh Singh | 623d140 | 2023-09-06 11:32:53 -0700 | [diff] [blame] | 98 | uint32_t shm_len = CONFIRMATIONUI_MAX_MSG_SIZE; |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 99 | BufferAllocator allocator; |
| 100 | unique_fd dma_buf(allocator.Alloc("system", shm_len)); |
| 101 | if (dma_buf < 0) { |
| 102 | LOG(ERROR) << AT << "failed to allocate shared memory buffer"; |
| 103 | return; |
| 104 | } |
| 105 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 106 | confirmationui_hdr hdr = { |
| 107 | .cmd = CONFIRMATIONUI_CMD_INIT, |
| 108 | }; |
| 109 | confirmationui_init_req args = { |
| 110 | .shm_len = shm_len, |
| 111 | }; |
| 112 | iovec iov[] = { |
| 113 | { |
| 114 | .iov_base = &hdr, |
| 115 | .iov_len = sizeof(hdr), |
| 116 | }, |
| 117 | { |
| 118 | .iov_base = &args, |
| 119 | .iov_len = sizeof(args), |
| 120 | }, |
| 121 | }; |
| 122 | trusty_shm shm = { |
| 123 | .fd = dma_buf, |
| 124 | .transfer = TRUSTY_SHARE, |
| 125 | }; |
| 126 | |
| 127 | int rc = tipc_send(tipc_handle, iov, 2, &shm, 1); |
| 128 | if (rc != static_cast<int>(sizeof(hdr) + sizeof(args))) { |
| 129 | LOG(ERROR) << AT << "failed to send INIT request"; |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | rc = read(tipc_handle, &hdr, sizeof(hdr)); |
| 134 | if (rc != static_cast<int>(sizeof(hdr))) { |
| 135 | LOG(ERROR) << AT << "failed to receive INIT response"; |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | if (hdr.cmd != (CONFIRMATIONUI_CMD_INIT | CONFIRMATIONUI_RESP_BIT)) { |
| 140 | LOG(ERROR) << AT << "unknown response command: " << hdr.cmd; |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | void* shm_base = mmap(0, shm_len, PROT_READ | PROT_WRITE, MAP_SHARED, dma_buf, 0); |
| 145 | if (shm_base == MAP_FAILED) { |
| 146 | LOG(ERROR) << AT << "failed to mmap() shared memory buffer"; |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | handle_ = std::move(tipc_handle); |
| 151 | shm_base_ = shm_base; |
| 152 | shm_len_ = shm_len; |
| 153 | |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 154 | LOG(INFO) << AT << "succeeded to connect to Trusty TA \"" << appname << "\""; |
| 155 | } |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 156 | |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 157 | TrustyApp::~TrustyApp() { |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 158 | LOG(INFO) << "Done shutting down TrustyApp"; |
| 159 | } |
| 160 | |
Tri Vo | abd86f8 | 2021-02-19 22:09:22 -0800 | [diff] [blame] | 161 | } // namespace confirmationui |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 162 | } // namespace trusty |
| 163 | } // namespace android |