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> |
Tri Vo | 90c0e83 | 2020-11-18 16:17:09 -0800 | [diff] [blame] | 28 | #include <trusty/tipc.h> |
Stephen Crane | 5e3a3ce | 2020-10-30 15:04:38 -0700 | [diff] [blame] | 29 | #include <iostream> |
Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 30 | |
| 31 | using android::base::ErrnoError; |
| 32 | using android::base::Error; |
| 33 | using android::base::Result; |
| 34 | using android::base::unique_fd; |
| 35 | |
Stephen Crane | 5e3a3ce | 2020-10-30 15:04:38 -0700 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | const size_t kTimeoutSeconds = 5; |
| 39 | const std::string kTrustyLogTag = "trusty-log"; |
| 40 | |
| 41 | const time_t kInitialTime = time(nullptr); |
| 42 | |
| 43 | void PrintTrustyLog() { |
| 44 | auto logger_list = android_logger_list_open(LOG_ID_KERNEL, ANDROID_LOG_NONBLOCK, 1000, 0); |
| 45 | if (logger_list == nullptr) { |
| 46 | std::cerr << "Could not open android kernel log\n"; |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | while (true) { |
| 51 | log_msg log_msg; |
| 52 | int rc = android_logger_list_read(logger_list, &log_msg); |
| 53 | if (rc < 0) { |
| 54 | break; |
| 55 | } |
| 56 | if (log_msg.entry.sec < kInitialTime) { |
| 57 | continue; |
| 58 | } |
| 59 | char* msg = log_msg.msg(); |
| 60 | if (msg) { |
| 61 | std::string line(msg, log_msg.entry.len); |
| 62 | if (line.find(kTrustyLogTag) != std::string::npos) { |
| 63 | std::cerr << line.substr(kTrustyLogTag.length() + 2) << std::endl; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | android_logger_list_free(logger_list); |
| 69 | } |
| 70 | |
| 71 | } // namespace |
Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 72 | |
| 73 | namespace android { |
| 74 | namespace trusty { |
| 75 | namespace fuzz { |
| 76 | |
| 77 | TrustyApp::TrustyApp(std::string tipc_dev, std::string ta_port) |
| 78 | : tipc_dev_(tipc_dev), ta_port_(ta_port), ta_fd_(-1) {} |
| 79 | |
| 80 | Result<void> TrustyApp::Connect() { |
Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 81 | alarm(kTimeoutSeconds); |
Tri Vo | 90c0e83 | 2020-11-18 16:17:09 -0800 | [diff] [blame] | 82 | int fd = tipc_connect(tipc_dev_.c_str(), ta_port_.c_str()); |
Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 83 | alarm(0); |
| 84 | if (fd < 0) { |
| 85 | return ErrnoError() << "failed to open TIPC device: "; |
| 86 | } |
| 87 | ta_fd_.reset(fd); |
| 88 | |
Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 89 | return {}; |
| 90 | } |
| 91 | |
| 92 | Result<void> TrustyApp::Read(void* buf, size_t len) { |
| 93 | if (ta_fd_ == -1) { |
| 94 | return Error() << "TA is not connected to yet: "; |
| 95 | } |
| 96 | |
| 97 | alarm(kTimeoutSeconds); |
| 98 | int rc = read(ta_fd_, buf, len); |
| 99 | alarm(0); |
| 100 | if (rc < 0) { |
| 101 | return Error() << "failed to read TIPC message from TA: "; |
| 102 | } |
| 103 | |
| 104 | return {}; |
| 105 | } |
| 106 | |
| 107 | Result<void> TrustyApp::Write(const 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 = write(ta_fd_, buf, len); |
| 114 | alarm(0); |
| 115 | if (rc < 0) { |
Tri Vo | ce812a2 | 2021-02-10 13:31:22 -0800 | [diff] [blame] | 116 | return Error() << "failed to write TIPC message to TA: "; |
Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | return {}; |
| 120 | } |
| 121 | |
| 122 | Result<int> TrustyApp::GetRawFd() { |
| 123 | if (ta_fd_ == -1) { |
| 124 | return Error() << "TA is not connected to yet: "; |
| 125 | } |
| 126 | |
| 127 | return ta_fd_; |
| 128 | } |
| 129 | |
Stephen Crane | e54e8d4 | 2021-04-14 20:57:50 +0000 | [diff] [blame] | 130 | void TrustyApp::Disconnect() { |
| 131 | ta_fd_.reset(); |
| 132 | } |
| 133 | |
Stephen Crane | 5e3a3ce | 2020-10-30 15:04:38 -0700 | [diff] [blame] | 134 | void Abort() { |
| 135 | PrintTrustyLog(); |
| 136 | exit(-1); |
| 137 | } |
| 138 | |
Tri Vo | e8823ff | 2020-10-13 21:59:07 -0700 | [diff] [blame] | 139 | } // namespace fuzz |
| 140 | } // namespace trusty |
| 141 | } // namespace android |