blob: d760d1ff9d8bc5c93b6c94a34756dcb08814efb6 [file] [log] [blame]
Kelvin Zhangab3ce602021-02-24 14:46:40 -05001//
2// Copyright (C) 2021 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// limi
15
16#include "update_engine/payload_consumer/verified_source_fd.h"
17
18#include <fcntl.h>
19#include <sys/stat.h>
20
21#include <memory>
22#include <utility>
23#include <vector>
24
25#include <base/strings/string_number_conversions.h>
26#include <base/strings/string_util.h>
27#include <base/strings/stringprintf.h>
28
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -070029#include "update_engine/common/error_code.h"
30#include "update_engine/common/hash_calculator.h"
Kelvin Zhangab3ce602021-02-24 14:46:40 -050031#include "update_engine/common/utils.h"
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -070032#include "update_engine/payload_consumer/extent_writer.h"
33#include "update_engine/payload_consumer/file_descriptor.h"
Kelvin Zhangab3ce602021-02-24 14:46:40 -050034#include "update_engine/payload_consumer/file_descriptor_utils.h"
Kelvin Zhangab3ce602021-02-24 14:46:40 -050035#include "update_engine/payload_consumer/partition_writer.h"
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -070036#include "update_engine/update_metadata.pb.h"
37#if USE_FEC
38#include "update_engine/payload_consumer/fec_file_descriptor.h"
39#endif
Kelvin Zhangab3ce602021-02-24 14:46:40 -050040
41namespace chromeos_update_engine {
42using std::string;
43
44bool VerifiedSourceFd::OpenCurrentECCPartition() {
45 // No support for ECC for full payloads.
46 // Full payload should not have any opeartion that requires ECC partitions.
47 if (source_ecc_fd_)
48 return true;
49
50 if (source_ecc_open_failure_)
51 return false;
52
53#if USE_FEC
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -070054 auto fd = std::make_shared<FecFileDescriptor>();
Kelvin Zhangab3ce602021-02-24 14:46:40 -050055 if (!fd->Open(source_path_.c_str(), O_RDONLY, 0)) {
56 PLOG(ERROR) << "Unable to open ECC source partition " << source_path_;
57 source_ecc_open_failure_ = true;
58 return false;
59 }
60 source_ecc_fd_ = fd;
61#else
62 // No support for ECC compiled.
63 source_ecc_open_failure_ = true;
64#endif // USE_FEC
65
66 return !source_ecc_open_failure_;
67}
68
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -070069bool VerifiedSourceFd::WriteBackCorrectedSourceBlocks(
70 const std::vector<unsigned char>& source_data,
71 const google::protobuf::RepeatedPtrField<Extent>& extents) {
Kelvin Zhangcf299152023-06-16 10:58:40 -070072 utils::SetBlockDeviceReadOnly(source_path_, false);
73 DEFER {
74 utils::SetBlockDeviceReadOnly(source_path_, true);
75 };
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -070076 auto fd = std::make_shared<EintrSafeFileDescriptor>();
77 TEST_AND_RETURN_FALSE_ERRNO(fd->Open(source_path_.c_str(), O_RDWR));
78 DirectExtentWriter writer(fd);
79 TEST_AND_RETURN_FALSE(writer.Init(extents, block_size_));
Kelvin Zhangcf299152023-06-16 10:58:40 -070080 TEST_AND_RETURN_FALSE(writer.Write(source_data.data(), source_data.size()));
81 return true;
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -070082}
83
Kelvin Zhangab3ce602021-02-24 14:46:40 -050084FileDescriptorPtr VerifiedSourceFd::ChooseSourceFD(
85 const InstallOperation& operation, ErrorCode* error) {
86 if (source_fd_ == nullptr) {
87 LOG(ERROR) << "ChooseSourceFD fail: source_fd_ == nullptr";
88 return nullptr;
89 }
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -070090 if (error) {
91 *error = ErrorCode::kSuccess;
92 }
Kelvin Zhangab3ce602021-02-24 14:46:40 -050093 if (!operation.has_src_sha256_hash()) {
94 // When the operation doesn't include a source hash, we attempt the error
95 // corrected device first since we can't verify the block in the raw device
96 // at this point, but we first need to make sure all extents are readable
97 // since the error corrected device can be shorter or not available.
98 if (OpenCurrentECCPartition() &&
99 fd_utils::ReadAndHashExtents(
100 source_ecc_fd_, operation.src_extents(), block_size_, nullptr)) {
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -0700101 if (error) {
102 *error = ErrorCode::kDownloadOperationHashMissingError;
103 }
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500104 return source_ecc_fd_;
105 }
106 return source_fd_;
107 }
108
109 brillo::Blob source_hash;
110 brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(),
111 operation.src_sha256_hash().end());
Kelvin Zhang8b07db52024-07-24 09:13:25 -0700112 if (!fd_utils::ReadAndHashExtents(
113 source_fd_, operation.src_extents(), block_size_, &source_hash)) {
114 LOG(ERROR) << "Failed to compute hash for operation " << operation.type()
115 << " data offset: " << operation.data_offset();
116 if (error) {
117 *error = ErrorCode::kDownloadOperationHashVerificationError;
118 }
119 return nullptr;
120 }
121 if (source_hash == expected_source_hash) {
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500122 return source_fd_;
123 }
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -0700124 if (error) {
125 *error = ErrorCode::kDownloadOperationHashMismatch;
126 }
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500127 // We fall back to use the error corrected device if the hash of the raw
128 // device doesn't match or there was an error reading the source partition.
129 if (!OpenCurrentECCPartition()) {
130 // The following function call will return false since the source hash
131 // mismatches, but we still want to call it so it prints the appropriate
132 // log message.
133 PartitionWriter::ValidateSourceHash(
134 source_hash, operation, source_fd_, error);
135 return nullptr;
136 }
137 LOG(WARNING) << "Source hash from RAW device mismatched: found "
138 << base::HexEncode(source_hash.data(), source_hash.size())
139 << ", expected "
140 << base::HexEncode(expected_source_hash.data(),
141 expected_source_hash.size());
142
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -0700143 std::vector<unsigned char> source_data;
144 if (!utils::ReadExtents(
145 source_ecc_fd_, operation.src_extents(), &source_data, block_size_)) {
146 return nullptr;
147 }
148 if (!HashCalculator::RawHashOfData(source_data, &source_hash)) {
149 return nullptr;
150 }
151 if (PartitionWriter::ValidateSourceHash(
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500152 source_hash, operation, source_ecc_fd_, error)) {
153 source_ecc_recovered_failures_++;
Kelvin Zhang7dd5a5e2023-05-11 15:30:38 -0700154 if (WriteBackCorrectedSourceBlocks(source_data, operation.src_extents())) {
155 if (error) {
156 *error = ErrorCode::kSuccess;
157 }
158 return source_fd_;
159 }
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500160 return source_ecc_fd_;
161 }
162 return nullptr;
163}
164
165bool VerifiedSourceFd::Open() {
166 source_fd_ = std::make_shared<EintrSafeFileDescriptor>();
167 if (source_fd_ == nullptr)
168 return false;
Kelvin Zhang8fffea92023-07-25 20:34:26 -0700169 if (!source_fd_->Open(source_path_.c_str(), O_RDONLY)) {
170 PLOG(ERROR) << "Failed to open " << source_path_;
171 }
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500172 return true;
173}
174
175} // namespace chromeos_update_engine