Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | |
Ayrton Munoz | 0413737 | 2022-08-03 17:05:09 -0400 | [diff] [blame] | 17 | #if defined(TRUSTY_USERSPACE) |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 18 | #include <openssl/rand.h> |
Andrei Homescu | 1c18a80 | 2022-08-17 04:59:01 +0000 | [diff] [blame] | 19 | #include <trusty_ipc.h> |
Ayrton Munoz | 0413737 | 2022-08-03 17:05:09 -0400 | [diff] [blame] | 20 | #else |
| 21 | #include <lib/rand/rand.h> |
| 22 | #endif |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 23 | |
Andrei Homescu | 024727b | 2022-08-24 23:54:59 +0000 | [diff] [blame] | 24 | #include <binder/RpcTransportTipcTrusty.h> |
| 25 | |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 26 | #include "../OS.h" |
Andrei Homescu | 1c18a80 | 2022-08-17 04:59:01 +0000 | [diff] [blame] | 27 | #include "TrustyStatus.h" |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 28 | |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 29 | using android::binder::borrowed_fd; |
| 30 | using android::binder::unique_fd; |
| 31 | |
Tomasz Wasilczyk | 0d9dec2 | 2023-10-06 20:28:49 +0000 | [diff] [blame] | 32 | namespace android::binder::os { |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 33 | |
Tomasz Wasilczyk | daf727a | 2023-11-21 13:50:09 -0800 | [diff] [blame^] | 34 | void trace_begin(uint64_t, const char*) {} |
| 35 | |
| 36 | void trace_end(uint64_t) {} |
| 37 | |
| 38 | uint64_t GetThreadId() { |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | bool report_sysprop_change() { |
| 43 | return false; |
| 44 | } |
| 45 | |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 46 | status_t setNonBlocking(borrowed_fd /*fd*/) { |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 47 | // Trusty IPC syscalls are all non-blocking by default. |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 48 | return OK; |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | status_t getRandomBytes(uint8_t* data, size_t size) { |
Ayrton Munoz | 0413737 | 2022-08-03 17:05:09 -0400 | [diff] [blame] | 52 | #if defined(TRUSTY_USERSPACE) |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 53 | int res = RAND_bytes(data, size); |
| 54 | return res == 1 ? OK : UNKNOWN_ERROR; |
Ayrton Munoz | 0413737 | 2022-08-03 17:05:09 -0400 | [diff] [blame] | 55 | #else |
| 56 | int res = rand_get_bytes(data, size); |
| 57 | return res == 0 ? OK : UNKNOWN_ERROR; |
| 58 | #endif // TRUSTY_USERSPACE |
Andrei Homescu | 74a5445 | 2021-12-10 05:30:21 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Andrei Homescu | 1c18a80 | 2022-08-17 04:59:01 +0000 | [diff] [blame] | 61 | status_t dupFileDescriptor(int oldFd, int* newFd) { |
| 62 | int res = dup(oldFd); |
| 63 | if (res < 0) { |
| 64 | return statusFromTrusty(res); |
| 65 | } |
| 66 | |
| 67 | *newFd = res; |
| 68 | return OK; |
Andrei Homescu | 24ad36e | 2022-08-04 01:33:33 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Andrei Homescu | 024727b | 2022-08-24 23:54:59 +0000 | [diff] [blame] | 71 | std::unique_ptr<RpcTransportCtxFactory> makeDefaultRpcTransportCtxFactory() { |
| 72 | return RpcTransportCtxFactoryTipcTrusty::make(); |
| 73 | } |
| 74 | |
David Brazdil | c3964f6 | 2022-10-24 23:06:14 +0100 | [diff] [blame] | 75 | ssize_t sendMessageOnSocket( |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 76 | const RpcTransportFd& /* socket */, iovec* /* iovs */, int /* niovs */, |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 77 | const std::vector<std::variant<unique_fd, borrowed_fd>>* /* ancillaryFds */) { |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 78 | errno = ENOTSUP; |
| 79 | return -1; |
| 80 | } |
| 81 | |
David Brazdil | c3964f6 | 2022-10-24 23:06:14 +0100 | [diff] [blame] | 82 | ssize_t receiveMessageFromSocket( |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 83 | const RpcTransportFd& /* socket */, iovec* /* iovs */, int /* niovs */, |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 84 | std::vector<std::variant<unique_fd, borrowed_fd>>* /* ancillaryFds */) { |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 85 | errno = ENOTSUP; |
| 86 | return -1; |
| 87 | } |
| 88 | |
Tomasz Wasilczyk | 0d9dec2 | 2023-10-06 20:28:49 +0000 | [diff] [blame] | 89 | } // namespace android::binder::os |