1 /*
2  * Copyright 2021, 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 #include "TrustyApp.h"
18 
19 #include <BufferAllocator/BufferAllocator.h>
20 #include <android-base/logging.h>
21 #include <sys/mman.h>
22 #include <sys/uio.h>
23 #include <trusty/tipc.h>
24 
25 #define countof(arr) (sizeof(arr) / sizeof(arr[0]))
26 
27 namespace android {
28 namespace trusty {
29 namespace confirmationui {
30 
31 using ::android::base::unique_fd;
32 
TrustyRpc(const uint8_t * obegin,const uint8_t * oend,uint8_t * ibegin,uint8_t * iend)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;
36 
37     if (olen > shm_len_) {
38         LOG(ERROR) << AT << "request message too long to fit in shared memory";
39         return -1;
40     }
41 
42     memcpy(shm_base_, obegin, olen);
43 
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     };
60 
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     }
66 
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     }
72 
73     if (hdr.cmd != (CONFIRMATIONUI_CMD_MSG | CONFIRMATIONUI_RESP_BIT)) {
74         LOG(ERROR) << AT << "unknown response command: " << hdr.cmd;
75         return -1;
76     }
77 
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     }
83 
84     memcpy(ibegin, shm_base_, args.msg_len);
85 
86     return args.msg_len;
87 }
88 
TrustyApp(const std::string & path,const std::string & appname)89 TrustyApp::TrustyApp(const std::string& path, const std::string& appname)
90     : handle_(kInvalidHandle) {
91     unique_fd tipc_handle(tipc_connect(path.c_str(), appname.c_str()));
92     if (tipc_handle < 0) {
93         LOG(ERROR) << AT << "failed to connect to Trusty TA \"" << appname << "\" using dev:"
94                    << "\"" << path << "\"";
95         return;
96     }
97 
98     uint32_t shm_len = CONFIRMATIONUI_MAX_MSG_SIZE;
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 
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 
154     LOG(INFO) << AT << "succeeded to connect to Trusty TA \"" << appname << "\"";
155 }
156 
~TrustyApp()157 TrustyApp::~TrustyApp() {
158     LOG(INFO) << "Done shutting down TrustyApp";
159 }
160 
161 }  // namespace confirmationui
162 }  // namespace trusty
163 }  // namespace android
164