| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2012 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 | // | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_ | 
|  | 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_ | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 19 |  | 
|  | 20 | #include <sys/stat.h> | 
|  | 21 | #include <sys/types.h> | 
|  | 22 |  | 
| Amin Hassani | abe4a77 | 2018-07-26 11:19:10 -0700 | [diff] [blame] | 23 | #include <memory> | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 24 | #include <string> | 
| Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 25 | #include <utility> | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 26 | #include <vector> | 
|  | 27 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 28 | #include <brillo/message_loops/message_loop.h> | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 29 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/common/action.h" | 
|  | 31 | #include "update_engine/common/hash_calculator.h" | 
| Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 32 | #include "update_engine/common/scoped_task_id.h" | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 33 | #include "update_engine/payload_consumer/file_descriptor.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 34 | #include "update_engine/payload_consumer/install_plan.h" | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 35 | #include "update_engine/payload_consumer/verity_writer_interface.h" | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 36 |  | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 37 | // This action will hash all the partitions of the target slot involved in the | 
|  | 38 | // update. The hashes are then verified against the ones in the InstallPlan. | 
|  | 39 | // If the target hash does not match, the action will fail. In case of failure, | 
|  | 40 | // the error code will depend on whether the source slot hashes are provided and | 
|  | 41 | // match. | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | namespace chromeos_update_engine { | 
|  | 44 |  | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 45 | // The step FilesystemVerifier is on. On kVerifyTargetHash it computes the hash | 
|  | 46 | // on the target partitions based on the already populated size and verifies it | 
|  | 47 | // matches the one in the target_hash in the InstallPlan. | 
|  | 48 | // If the hash matches, then we skip the kVerifySourceHash step, otherwise we | 
|  | 49 | // need to check if the source is the root cause of the mismatch. | 
|  | 50 | enum class VerifierStep { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 51 | kVerifyTargetHash, | 
| Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 52 | kVerifySourceHash, | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 53 | }; | 
|  | 54 |  | 
| Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 55 | class FilesystemVerifyDelegate { | 
|  | 56 | public: | 
|  | 57 | virtual ~FilesystemVerifyDelegate() = default; | 
|  | 58 | virtual void OnVerifyProgressUpdate(double progress) = 0; | 
|  | 59 | }; | 
|  | 60 |  | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 61 | class FilesystemVerifierAction : public InstallPlanAction { | 
|  | 62 | public: | 
| Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 63 | explicit FilesystemVerifierAction( | 
|  | 64 | DynamicPartitionControlInterface* dynamic_control) | 
|  | 65 | : verity_writer_(verity_writer::CreateVerityWriter()), | 
|  | 66 | dynamic_control_(dynamic_control) { | 
|  | 67 | CHECK(dynamic_control_); | 
|  | 68 | } | 
|  | 69 |  | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 70 | ~FilesystemVerifierAction() override = default; | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | void PerformAction() override; | 
|  | 73 | void TerminateProcessing() override; | 
|  | 74 |  | 
| Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 75 | // Used for listening to progress updates | 
|  | 76 | void set_delegate(FilesystemVerifyDelegate* delegate) { | 
|  | 77 | this->delegate_ = delegate; | 
|  | 78 | } | 
|  | 79 | [[nodiscard]] FilesystemVerifyDelegate* get_delegate() const { | 
|  | 80 | return this->delegate_; | 
|  | 81 | } | 
|  | 82 |  | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 83 | // Debugging/logging | 
|  | 84 | static std::string StaticType() { return "FilesystemVerifierAction"; } | 
|  | 85 | std::string Type() const override { return StaticType(); } | 
|  | 86 |  | 
|  | 87 | private: | 
| Amin Hassani | abe4a77 | 2018-07-26 11:19:10 -0700 | [diff] [blame] | 88 | friend class FilesystemVerifierActionTestDelegate; | 
| Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 89 | void WriteVerityAndHashPartition(FileDescriptorPtr fd, | 
|  | 90 | const off64_t start_offset, | 
|  | 91 | const off64_t end_offset, | 
|  | 92 | void* buffer, | 
|  | 93 | const size_t buffer_size); | 
|  | 94 | void HashPartition(FileDescriptorPtr fd, | 
|  | 95 | const off64_t start_offset, | 
|  | 96 | const off64_t end_offset, | 
|  | 97 | void* buffer, | 
|  | 98 | const size_t buffer_size); | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 99 |  | 
|  | 100 | // Return true if we need to write verity bytes. | 
|  | 101 | bool ShouldWriteVerity(); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 102 | // Starts the hashing of the current partition. If there aren't any partitions | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 103 | // remaining to be hashed, it finishes the action. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 104 | void StartPartitionHashing(); | 
|  | 105 |  | 
| Kelvin Zhang | e012f65 | 2021-05-10 16:00:31 -0400 | [diff] [blame] | 106 | const std::string& GetPartitionPath() const; | 
|  | 107 |  | 
|  | 108 | bool IsVABC(const InstallPlan::Partition& partition) const; | 
|  | 109 |  | 
|  | 110 | size_t GetPartitionSize() const; | 
|  | 111 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 112 | // When the read is done, finalize the hash checking of the current partition | 
|  | 113 | // and continue checking the next one. | 
|  | 114 | void FinishPartitionHashing(); | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | // Cleans up all the variables we use for async operations and tells the | 
|  | 117 | // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be | 
|  | 118 | // true if TerminateProcessing() was called. | 
|  | 119 | void Cleanup(ErrorCode code); | 
|  | 120 |  | 
| Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 121 | // Invoke delegate callback to report progress, if delegate is not null | 
|  | 122 | void UpdateProgress(double progress); | 
|  | 123 |  | 
| Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 124 | // Updates progress of current partition. |progress| should be in range [0, | 
|  | 125 | // 1], and it will be scaled appropriately with # of partitions. | 
|  | 126 | void UpdatePartitionProgress(double progress); | 
|  | 127 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 128 | // Initialize read_fd_ and write_fd_ | 
|  | 129 | bool InitializeFd(const std::string& part_path); | 
| Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 130 | bool InitializeFdVABC(bool should_write_verity); | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 131 |  | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 132 | // The type of the partition that we are verifying. | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 133 | VerifierStep verifier_step_ = VerifierStep::kVerifyTargetHash; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 134 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 135 | // The index in the install_plan_.partitions vector of the partition currently | 
|  | 136 | // being hashed. | 
|  | 137 | size_t partition_index_{0}; | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 138 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 139 | // If not null, the FileDescriptor used to read from the device. | 
| Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 140 | // verity writer might attempt to write to this fd, if verity is enabled. | 
|  | 141 | FileDescriptorPtr partition_fd_; | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 142 |  | 
|  | 143 | // Buffer for storing data we read. | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 144 | brillo::Blob buffer_; | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 145 |  | 
| Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 146 | bool cancelled_{false};  // true if the action has been cancelled. | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 147 |  | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 148 | // Calculates the hash of the data. | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 149 | std::unique_ptr<HashCalculator> hasher_; | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 150 |  | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 151 | // Write verity data of the current partition. | 
|  | 152 | std::unique_ptr<VerityWriterInterface> verity_writer_; | 
|  | 153 |  | 
| Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 154 | // Verifies the untouched dynamic partitions for partial updates. | 
|  | 155 | DynamicPartitionControlInterface* dynamic_control_{nullptr}; | 
|  | 156 |  | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 157 | // Reads and hashes this many bytes from the head of the input stream. When | 
|  | 158 | // the partition starts to be hashed, this field is initialized from the | 
|  | 159 | // corresponding InstallPlan::Partition size which is the total size | 
|  | 160 | // update_engine is expected to write, and may be smaller than the size of the | 
|  | 161 | // partition in gpt. | 
|  | 162 | uint64_t partition_size_{0}; | 
|  | 163 |  | 
|  | 164 | // The byte offset that we are reading in the current partition. | 
|  | 165 | uint64_t offset_{0}; | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 166 |  | 
| Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 167 | // The end offset of filesystem data, first byte position of hashtree. | 
|  | 168 | uint64_t filesystem_data_end_{0}; | 
|  | 169 |  | 
| Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 170 | // An observer that observes progress updates of this action. | 
|  | 171 | FilesystemVerifyDelegate* delegate_{}; | 
|  | 172 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 173 | // Callback that should be cancelled on |TerminateProcessing|. Usually this | 
|  | 174 | // points to pending read callbacks from async stream. | 
| Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 175 | ScopedTaskId pending_task_id_; | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 176 |  | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 177 | DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction); | 
|  | 178 | }; | 
|  | 179 |  | 
|  | 180 | }  // namespace chromeos_update_engine | 
|  | 181 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 182 | #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_ |