Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [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 "fsverity_init" |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <filesystem> |
| 22 | #include <memory> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <android-base/file.h> |
| 27 | #include <android-base/logging.h> |
| 28 | #include <android-base/properties.h> |
| 29 | #include <android-base/strings.h> |
| 30 | #include <keystore/keystore_client.h> |
| 31 | #include <keystore/keystore_client_impl.h> |
| 32 | #include <keystore/keystore_get.h> |
| 33 | #include <log/log.h> |
| 34 | #include <mini_keyctl_utils.h> |
| 35 | #include <private/android_filesystem_config.h> |
| 36 | |
| 37 | bool LoadKeyToKeyring(key_serial_t keyring_id, const char* desc, const char* data, size_t size) { |
| 38 | key_serial_t key = add_key("asymmetric", desc, data, size, keyring_id); |
| 39 | if (key < 0) { |
| 40 | PLOG(ERROR) << "Failed to add key"; |
| 41 | return false; |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | void LoadKeyFromVerifiedPartitions(key_serial_t keyring_id) { |
| 47 | const char* dir = "/product/etc/security/fsverity"; |
| 48 | if (!std::filesystem::exists(dir)) { |
| 49 | LOG(ERROR) << "no such dir: " << dir; |
| 50 | return; |
| 51 | } |
| 52 | for (const auto& entry : std::filesystem::directory_iterator(dir)) { |
| 53 | if (!android::base::EndsWithIgnoreCase(entry.path().c_str(), ".der")) continue; |
| 54 | std::string content; |
| 55 | if (!android::base::ReadFileToString(entry.path(), &content)) { |
| 56 | continue; |
| 57 | } |
| 58 | if (!LoadKeyToKeyring(keyring_id, "fsv_system", content.c_str(), content.size())) { |
| 59 | LOG(ERROR) << "Failed to load key from " << entry.path(); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | std::unique_ptr<keystore::KeystoreClient> CreateKeystoreInstance() { |
| 65 | return std::unique_ptr<keystore::KeystoreClient>( |
| 66 | static_cast<keystore::KeystoreClient*>(new keystore::KeystoreClientImpl)); |
| 67 | } |
| 68 | |
| 69 | void LoadKeysFromKeystore(key_serial_t keyring_id) { |
| 70 | auto client = CreateKeystoreInstance(); |
| 71 | |
| 72 | std::vector<std::string> aliases; |
| 73 | if (client == nullptr || !client->listKeysOfUid("FSV_", AID_FSVERITY_CERT, &aliases)) { |
| 74 | LOG(ERROR) << "Failed to list key"; |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | // Always try to load all keys even if some fails to load. The rest may still |
| 79 | // be important to have. |
| 80 | for (auto& alias : aliases) { |
| 81 | auto blob = client->getKey(alias, AID_FSVERITY_CERT); |
| 82 | if (!LoadKeyToKeyring(keyring_id, "fsv_user", reinterpret_cast<char*>(blob->data()), |
| 83 | blob->size())) { |
| 84 | LOG(ERROR) << "Failed to load key " << alias << " from keyring"; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | int main(int /*argc*/, const char** /*argv*/) { |
| 90 | key_serial_t keyring_id = android::GetKeyringId(".fs-verity"); |
| 91 | if (keyring_id < 0) { |
| 92 | LOG(ERROR) << "Failed to find .fs-verity keyring id"; |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | // Requires files backed by fs-verity to be verified with a key in .fs-verity |
| 97 | // keyring. |
| 98 | if (!android::base::WriteStringToFile("1", "/proc/sys/fs/verity/require_signatures")) { |
| 99 | PLOG(ERROR) << "Failed to enforce fs-verity signature"; |
| 100 | } |
| 101 | |
| 102 | LoadKeyFromVerifiedPartitions(keyring_id); |
| 103 | LoadKeysFromKeystore(keyring_id); |
| 104 | |
| 105 | if (!android::base::GetBoolProperty("ro.debuggable", false)) { |
| 106 | if (keyctl_restrict_keyring(keyring_id, nullptr, nullptr) < 0) { |
| 107 | PLOG(ERROR) << "Cannot restrict .fs-verity keyring"; |
| 108 | } |
| 109 | } |
| 110 | return 0; |
| 111 | } |