Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Stephen Crane | 5e3a3ce | 2020-10-30 15:04:38 -0700 | [diff] [blame^] | 26 | #include <log/log_read.h> |
| 27 | #include <time.h> |
| 28 | #include <iostream> |
Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 29 | |
| 30 | using android::base::ErrnoError; |
| 31 | using android::base::Error; |
| 32 | using android::base::Result; |
| 33 | using android::base::unique_fd; |
| 34 | |
| 35 | #define TIPC_IOC_MAGIC 'r' |
| 36 | #define TIPC_IOC_CONNECT _IOW(TIPC_IOC_MAGIC, 0x80, char*) |
| 37 | |
Stephen Crane | 5e3a3ce | 2020-10-30 15:04:38 -0700 | [diff] [blame^] | 38 | namespace { |
| 39 | |
| 40 | const size_t kTimeoutSeconds = 5; |
| 41 | const std::string kTrustyLogTag = "trusty-log"; |
| 42 | |
| 43 | const time_t kInitialTime = time(nullptr); |
| 44 | |
| 45 | void PrintTrustyLog() { |
| 46 | auto logger_list = android_logger_list_open(LOG_ID_KERNEL, ANDROID_LOG_NONBLOCK, 1000, 0); |
| 47 | if (logger_list == nullptr) { |
| 48 | std::cerr << "Could not open android kernel log\n"; |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | while (true) { |
| 53 | log_msg log_msg; |
| 54 | int rc = android_logger_list_read(logger_list, &log_msg); |
| 55 | if (rc < 0) { |
| 56 | break; |
| 57 | } |
| 58 | if (log_msg.entry.sec < kInitialTime) { |
| 59 | continue; |
| 60 | } |
| 61 | char* msg = log_msg.msg(); |
| 62 | if (msg) { |
| 63 | std::string line(msg, log_msg.entry.len); |
| 64 | if (line.find(kTrustyLogTag) != std::string::npos) { |
| 65 | std::cerr << line.substr(kTrustyLogTag.length() + 2) << std::endl; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | android_logger_list_free(logger_list); |
| 71 | } |
| 72 | |
| 73 | } // namespace |
Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 74 | |
| 75 | namespace android { |
| 76 | namespace trusty { |
| 77 | namespace fuzz { |
| 78 | |
| 79 | TrustyApp::TrustyApp(std::string tipc_dev, std::string ta_port) |
| 80 | : tipc_dev_(tipc_dev), ta_port_(ta_port), ta_fd_(-1) {} |
| 81 | |
| 82 | Result<void> TrustyApp::Connect() { |
| 83 | /* |
| 84 | * TODO: We can't use libtrusty because (yet) |
| 85 | * (1) cc_fuzz can't deal with vendor components (b/170753563) |
| 86 | * (2) We need non-blocking behavior to detect Trusty going down. |
| 87 | * (we could implement the timeout in the fuzzing code though, as |
| 88 | * it needs to be around the call to read()) |
| 89 | */ |
| 90 | alarm(kTimeoutSeconds); |
| 91 | int fd = open(tipc_dev_.c_str(), O_RDWR); |
| 92 | alarm(0); |
| 93 | if (fd < 0) { |
| 94 | return ErrnoError() << "failed to open TIPC device: "; |
| 95 | } |
| 96 | ta_fd_.reset(fd); |
| 97 | |
| 98 | // This ioctl will time out in the kernel if it can't connect. |
| 99 | int rc = TEMP_FAILURE_RETRY(ioctl(ta_fd_, TIPC_IOC_CONNECT, ta_port_.c_str())); |
| 100 | if (rc < 0) { |
| 101 | return ErrnoError() << "failed to connect to TIPC service: "; |
| 102 | } |
| 103 | |
| 104 | return {}; |
| 105 | } |
| 106 | |
| 107 | Result<void> TrustyApp::Read(void* buf, size_t len) { |
| 108 | if (ta_fd_ == -1) { |
| 109 | return Error() << "TA is not connected to yet: "; |
| 110 | } |
| 111 | |
| 112 | alarm(kTimeoutSeconds); |
| 113 | int rc = read(ta_fd_, buf, len); |
| 114 | alarm(0); |
| 115 | if (rc < 0) { |
| 116 | return Error() << "failed to read TIPC message from TA: "; |
| 117 | } |
| 118 | |
| 119 | return {}; |
| 120 | } |
| 121 | |
| 122 | Result<void> TrustyApp::Write(const void* buf, size_t len) { |
| 123 | if (ta_fd_ == -1) { |
| 124 | return Error() << "TA is not connected to yet: "; |
| 125 | } |
| 126 | |
| 127 | alarm(kTimeoutSeconds); |
| 128 | int rc = write(ta_fd_, buf, len); |
| 129 | alarm(0); |
| 130 | if (rc < 0) { |
| 131 | return Error() << "failed to read TIPC message from TA: "; |
| 132 | } |
| 133 | |
| 134 | return {}; |
| 135 | } |
| 136 | |
| 137 | Result<int> TrustyApp::GetRawFd() { |
| 138 | if (ta_fd_ == -1) { |
| 139 | return Error() << "TA is not connected to yet: "; |
| 140 | } |
| 141 | |
| 142 | return ta_fd_; |
| 143 | } |
| 144 | |
Stephen Crane | 5e3a3ce | 2020-10-30 15:04:38 -0700 | [diff] [blame^] | 145 | void Abort() { |
| 146 | PrintTrustyLog(); |
| 147 | exit(-1); |
| 148 | } |
| 149 | |
Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 150 | } // namespace fuzz |
| 151 | } // namespace trusty |
| 152 | } // namespace android |