blob: a389e0b61132eeff4326f6d2b11450ad2959683b [file] [log] [blame]
Tri Voe8823ff2020-10-13 21:59:07 -07001/*
2 * Copyright (C) 2020 The Android Open Sourete 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 "trusty-fuzz-utils"
18
19#include <trusty/fuzz/utils.h>
20
21#include <android-base/logging.h>
22#include <android-base/unique_fd.h>
23#include <linux/ioctl.h>
24#include <linux/types.h>
25#include <linux/uio.h>
26
27using android::base::ErrnoError;
28using android::base::Error;
29using android::base::Result;
30using android::base::unique_fd;
31
32#define TIPC_IOC_MAGIC 'r'
33#define TIPC_IOC_CONNECT _IOW(TIPC_IOC_MAGIC, 0x80, char*)
34
35static const size_t kTimeoutSeconds = 5;
36
37namespace android {
38namespace trusty {
39namespace fuzz {
40
41TrustyApp::TrustyApp(std::string tipc_dev, std::string ta_port)
42 : tipc_dev_(tipc_dev), ta_port_(ta_port), ta_fd_(-1) {}
43
44Result<void> TrustyApp::Connect() {
45 /*
46 * TODO: We can't use libtrusty because (yet)
47 * (1) cc_fuzz can't deal with vendor components (b/170753563)
48 * (2) We need non-blocking behavior to detect Trusty going down.
49 * (we could implement the timeout in the fuzzing code though, as
50 * it needs to be around the call to read())
51 */
52 alarm(kTimeoutSeconds);
53 int fd = open(tipc_dev_.c_str(), O_RDWR);
54 alarm(0);
55 if (fd < 0) {
56 return ErrnoError() << "failed to open TIPC device: ";
57 }
58 ta_fd_.reset(fd);
59
60 // This ioctl will time out in the kernel if it can't connect.
61 int rc = TEMP_FAILURE_RETRY(ioctl(ta_fd_, TIPC_IOC_CONNECT, ta_port_.c_str()));
62 if (rc < 0) {
63 return ErrnoError() << "failed to connect to TIPC service: ";
64 }
65
66 return {};
67}
68
69Result<void> TrustyApp::Read(void* buf, size_t len) {
70 if (ta_fd_ == -1) {
71 return Error() << "TA is not connected to yet: ";
72 }
73
74 alarm(kTimeoutSeconds);
75 int rc = read(ta_fd_, buf, len);
76 alarm(0);
77 if (rc < 0) {
78 return Error() << "failed to read TIPC message from TA: ";
79 }
80
81 return {};
82}
83
84Result<void> TrustyApp::Write(const void* buf, size_t len) {
85 if (ta_fd_ == -1) {
86 return Error() << "TA is not connected to yet: ";
87 }
88
89 alarm(kTimeoutSeconds);
90 int rc = write(ta_fd_, buf, len);
91 alarm(0);
92 if (rc < 0) {
93 return Error() << "failed to read TIPC message from TA: ";
94 }
95
96 return {};
97}
98
99Result<int> TrustyApp::GetRawFd() {
100 if (ta_fd_ == -1) {
101 return Error() << "TA is not connected to yet: ";
102 }
103
104 return ta_fd_;
105}
106
107} // namespace fuzz
108} // namespace trusty
109} // namespace android