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> |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 22 | #include <string> |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 23 | |
| 24 | #include <android-base/file.h> |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/properties.h> |
| 27 | #include <android-base/strings.h> |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
| 29 | #include <mini_keyctl_utils.h> |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 30 | |
| 31 | bool LoadKeyToKeyring(key_serial_t keyring_id, const char* desc, const char* data, size_t size) { |
| 32 | key_serial_t key = add_key("asymmetric", desc, data, size, keyring_id); |
| 33 | if (key < 0) { |
| 34 | PLOG(ERROR) << "Failed to add key"; |
| 35 | return false; |
| 36 | } |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | void LoadKeyFromVerifiedPartitions(key_serial_t keyring_id) { |
| 41 | const char* dir = "/product/etc/security/fsverity"; |
| 42 | if (!std::filesystem::exists(dir)) { |
| 43 | LOG(ERROR) << "no such dir: " << dir; |
| 44 | return; |
| 45 | } |
| 46 | for (const auto& entry : std::filesystem::directory_iterator(dir)) { |
| 47 | if (!android::base::EndsWithIgnoreCase(entry.path().c_str(), ".der")) continue; |
| 48 | std::string content; |
| 49 | if (!android::base::ReadFileToString(entry.path(), &content)) { |
| 50 | continue; |
| 51 | } |
| 52 | if (!LoadKeyToKeyring(keyring_id, "fsv_system", content.c_str(), content.size())) { |
| 53 | LOG(ERROR) << "Failed to load key from " << entry.path(); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 58 | int main(int /*argc*/, const char** /*argv*/) { |
| 59 | key_serial_t keyring_id = android::GetKeyringId(".fs-verity"); |
| 60 | if (keyring_id < 0) { |
| 61 | LOG(ERROR) << "Failed to find .fs-verity keyring id"; |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | // Requires files backed by fs-verity to be verified with a key in .fs-verity |
| 66 | // keyring. |
| 67 | if (!android::base::WriteStringToFile("1", "/proc/sys/fs/verity/require_signatures")) { |
| 68 | PLOG(ERROR) << "Failed to enforce fs-verity signature"; |
| 69 | } |
| 70 | |
| 71 | LoadKeyFromVerifiedPartitions(keyring_id); |
Victor Hsieh | d0a4b20 | 2019-08-26 11:26:27 -0700 | [diff] [blame] | 72 | |
| 73 | if (!android::base::GetBoolProperty("ro.debuggable", false)) { |
| 74 | if (keyctl_restrict_keyring(keyring_id, nullptr, nullptr) < 0) { |
| 75 | PLOG(ERROR) << "Cannot restrict .fs-verity keyring"; |
| 76 | } |
| 77 | } |
| 78 | return 0; |
| 79 | } |