David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019, 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 | #define LOG_TAG "Util" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/stringprintf.h> |
| 27 | |
| 28 | #include <android/security/identity/ICredentialStore.h> |
| 29 | |
| 30 | #include "Util.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace security { |
| 34 | namespace identity { |
| 35 | |
| 36 | using ::android::base::StringPrintf; |
| 37 | |
| 38 | Status halResultToGenericError(const Result& result) { |
| 39 | string message = |
| 40 | StringPrintf("HAL failed with code %d: %s", int(result.code), result.message.c_str()); |
| 41 | return Status::fromServiceSpecificError(ICredentialStore::ERROR_GENERIC, message.c_str()); |
| 42 | } |
| 43 | |
| 44 | optional<vector<uint8_t>> fileGetContents(const string& path) { |
| 45 | int fd = open(path.c_str(), O_RDONLY); |
| 46 | if (fd == -1) { |
| 47 | PLOG(ERROR) << "Error opening " << path; |
| 48 | return {}; |
| 49 | } |
| 50 | |
| 51 | struct stat statbuf; |
| 52 | if (fstat(fd, &statbuf) != 0) { |
| 53 | PLOG(ERROR) << "Error statting " << path; |
| 54 | close(fd); |
| 55 | return {}; |
| 56 | } |
| 57 | vector<uint8_t> data; |
| 58 | data.resize(statbuf.st_size); |
| 59 | |
| 60 | uint8_t* p = data.data(); |
| 61 | size_t remaining = data.size(); |
| 62 | while (remaining > 0) { |
Greg Kaiser | c8966aa | 2020-01-21 07:05:08 -0800 | [diff] [blame^] | 63 | ssize_t numRead = TEMP_FAILURE_RETRY(read(fd, p, remaining)); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 64 | if (numRead <= 0) { |
| 65 | PLOG(ERROR) << "Failed reading from '" << path << "'"; |
| 66 | close(fd); |
| 67 | return {}; |
| 68 | } |
| 69 | p += numRead; |
| 70 | remaining -= numRead; |
| 71 | } |
| 72 | close(fd); |
| 73 | |
| 74 | return data; |
| 75 | } |
| 76 | |
| 77 | bool fileSetContents(const string& path, const vector<uint8_t>& data) { |
| 78 | char tempName[4096]; |
| 79 | int fd; |
| 80 | |
| 81 | string tempNameStr = path + ".XXXXXX"; |
| 82 | if (tempNameStr.size() >= sizeof tempName - 1) { |
| 83 | LOG(ERROR) << "Path name too long"; |
| 84 | return false; |
| 85 | } |
| 86 | strncpy(tempName, tempNameStr.c_str(), sizeof tempName); |
| 87 | |
| 88 | fd = mkstemp(tempName); |
| 89 | if (fd == -1) { |
| 90 | PLOG(ERROR) << "Error creating temp file for '" << path << "'"; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | const uint8_t* p = data.data(); |
| 95 | size_t remaining = data.size(); |
| 96 | while (remaining > 0) { |
Greg Kaiser | c8966aa | 2020-01-21 07:05:08 -0800 | [diff] [blame^] | 97 | ssize_t numWritten = TEMP_FAILURE_RETRY(write(fd, p, remaining)); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 98 | if (numWritten <= 0) { |
| 99 | PLOG(ERROR) << "Failed writing into temp file for '" << path << "'"; |
| 100 | close(fd); |
| 101 | return false; |
| 102 | } |
| 103 | p += numWritten; |
| 104 | remaining -= numWritten; |
| 105 | } |
| 106 | |
| 107 | if (TEMP_FAILURE_RETRY(fsync(fd) == -1)) { |
| 108 | PLOG(ERROR) << "Failed fsyncing temp file for '" << path << "'"; |
| 109 | close(fd); |
| 110 | return false; |
| 111 | } |
| 112 | close(fd); |
| 113 | |
| 114 | if (rename(tempName, path.c_str()) != 0) { |
| 115 | PLOG(ERROR) << "Error renaming temp file for '" << path << "'"; |
| 116 | close(fd); |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | } // namespace identity |
| 124 | } // namespace security |
| 125 | } // namespace android |