1 /*
2  * Copyright (C) 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 <BufferAllocator/BufferAllocator.h>
18 #include <android-base/unique_fd.h>
19 #include <apploader_ipc.h>
20 #include <stdlib.h>
21 #include <sys/mman.h>
22 #include <trusty/coverage/coverage.h>
23 #include <trusty/fuzz/counters.h>
24 #include <trusty/fuzz/utils.h>
25 #include <trusty/tipc.h>
26 #include <unistd.h>
27 #include <iostream>
28 
29 using android::base::unique_fd;
30 using android::trusty::coverage::CoverageRecord;
31 using android::trusty::fuzz::ExtraCounters;
32 using android::trusty::fuzz::TrustyApp;
33 
34 #define TIPC_DEV "/dev/trusty-ipc-dev0"
35 #define APPLOADER_PORT "com.android.trusty.apploader"
36 #define APPLOADER_MODULE_NAME "apploader.syms.elf"
37 
38 /* Apploader TA's UUID is 081ba88f-f1ee-452e-b5e8-a7e9ef173a97 */
39 static struct uuid apploader_uuid = {
40         0x081ba88f,
41         0xf1ee,
42         0x452e,
43         {0xb5, 0xe8, 0xa7, 0xe9, 0xef, 0x17, 0x3a, 0x97},
44 };
45 
SendLoadMsg(int chan,int dma_buf,size_t dma_buf_size)46 static bool SendLoadMsg(int chan, int dma_buf, size_t dma_buf_size) {
47     apploader_header hdr = {
48             .cmd = APPLOADER_CMD_LOAD_APPLICATION,
49     };
50     apploader_load_app_req req = {
51             .package_size = static_cast<uint64_t>(dma_buf_size),
52     };
53     iovec iov[] = {
54             {
55                     .iov_base = &hdr,
56                     .iov_len = sizeof(hdr),
57             },
58             {
59                     .iov_base = &req,
60                     .iov_len = sizeof(req),
61             },
62     };
63     trusty_shm shm = {
64             .fd = dma_buf,
65             .transfer = TRUSTY_SHARE,
66     };
67 
68     int rc = tipc_send(chan, iov, 2, &shm, 1);
69     if (rc != static_cast<int>(sizeof(hdr) + sizeof(req))) {
70         std::cerr << "Failed to send request" << std::endl;
71         return false;
72     }
73 
74     apploader_resp resp;
75     rc = read(chan, &resp, sizeof(resp));
76     if (rc != static_cast<int>(sizeof(resp))) {
77         std::cerr << "Failed to receive response" << std::endl;
78         return false;
79     }
80 
81     return true;
82 }
83 
84 static CoverageRecord record(TIPC_DEV, &apploader_uuid, APPLOADER_MODULE_NAME);
85 
LLVMFuzzerInitialize(int *,char ***)86 extern "C" int LLVMFuzzerInitialize(int* /* argc */, char*** /* argv */) {
87     auto ret = record.Open();
88     if (!ret.ok()) {
89         std::cerr << ret.error() << std::endl;
90         exit(-1);
91     }
92     return 0;
93 }
94 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
96     ExtraCounters counters(&record);
97     counters.Reset();
98 
99     android::trusty::fuzz::TrustyApp ta(TIPC_DEV, APPLOADER_PORT);
100     auto ret = ta.Connect();
101     if (!ret.ok()) {
102         std::cerr << ret.error() << std::endl;
103         android::trusty::fuzz::Abort();
104     }
105 
106     uint64_t shm_len = size ? size : 4096;
107     BufferAllocator alloc;
108     unique_fd dma_buf(alloc.Alloc(kDmabufSystemHeapName, shm_len));
109     if (dma_buf < 0) {
110         std::cerr << "Failed to create dmabuf of size: " << shm_len << std::endl;
111         android::trusty::fuzz::Abort();
112     }
113 
114     void* shm_base = mmap(0, shm_len, PROT_READ | PROT_WRITE, MAP_SHARED, dma_buf, 0);
115     if (shm_base == MAP_FAILED) {
116         std::cerr << "Failed to mmap() dmabuf" << std::endl;
117         android::trusty::fuzz::Abort();
118     }
119 
120     memcpy(shm_base, data, size);
121 
122     bool success = SendLoadMsg(*ta.GetRawFd(), dma_buf, shm_len);
123     if (!success) {
124         std::cerr << "Failed to send load message" << std::endl;
125         android::trusty::fuzz::Abort();
126     }
127 
128     munmap(shm_base, shm_len);
129     return 0;
130 }
131