1 //
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 // limitations under the License.
15 //
16 
17 #include "update_engine/payload_consumer/install_operation_executor.h"
18 
19 #include <fcntl.h>
20 #include <glob.h>
21 #include <linux/fs.h>
22 
23 #include <memory>
24 #include <utility>
25 #include <vector>
26 
27 #include <base/files/memory_mapped_file.h>
28 #include <base/files/file_util.h>
29 #include <bsdiff/bspatch.h>
30 #include <puffin/brotli_util.h>
31 #include <puffin/puffpatch.h>
32 #include <zucchini/patch_reader.h>
33 #include <zucchini/zucchini.h>
34 
35 #include "update_engine/common/utils.h"
36 #include "update_engine/lz4diff/lz4patch.h"
37 #include "update_engine/lz4diff/lz4diff_compress.h"
38 #include "update_engine/payload_consumer/bzip_extent_writer.h"
39 #include "update_engine/payload_consumer/cached_file_descriptor.h"
40 #include "update_engine/payload_consumer/extent_reader.h"
41 #include "update_engine/payload_consumer/extent_writer.h"
42 #include "update_engine/payload_consumer/file_descriptor.h"
43 #include "update_engine/payload_consumer/file_descriptor_utils.h"
44 #include "update_engine/payload_consumer/xz_extent_writer.h"
45 #include "update_engine/update_metadata.pb.h"
46 
47 namespace chromeos_update_engine {
48 
49 class BsdiffExtentFile : public bsdiff::FileInterface {
50  public:
BsdiffExtentFile(std::unique_ptr<ExtentReader> reader,size_t size)51   BsdiffExtentFile(std::unique_ptr<ExtentReader> reader, size_t size)
52       : BsdiffExtentFile(std::move(reader), nullptr, size) {}
BsdiffExtentFile(std::unique_ptr<ExtentWriter> writer,size_t size)53   BsdiffExtentFile(std::unique_ptr<ExtentWriter> writer, size_t size)
54       : BsdiffExtentFile(nullptr, std::move(writer), size) {}
55 
56   ~BsdiffExtentFile() override = default;
57 
Read(void * buf,size_t count,size_t * bytes_read)58   bool Read(void* buf, size_t count, size_t* bytes_read) override {
59     TEST_AND_RETURN_FALSE(reader_->Read(buf, count));
60     *bytes_read = count;
61     offset_ += count;
62     return true;
63   }
64 
Write(const void * buf,size_t count,size_t * bytes_written)65   bool Write(const void* buf, size_t count, size_t* bytes_written) override {
66     TEST_AND_RETURN_FALSE(writer_->Write(buf, count));
67     *bytes_written = count;
68     offset_ += count;
69     return true;
70   }
71 
Seek(off_t pos)72   bool Seek(off_t pos) override {
73     if (reader_ != nullptr) {
74       TEST_AND_RETURN_FALSE(reader_->Seek(pos));
75       offset_ = pos;
76     } else {
77       // For writes technically there should be no change of position, or it
78       // should be equivalent of current offset.
79       TEST_AND_RETURN_FALSE(offset_ == static_cast<uint64_t>(pos));
80     }
81     return true;
82   }
83 
Close()84   bool Close() override { return true; }
85 
GetSize(uint64_t * size)86   bool GetSize(uint64_t* size) override {
87     *size = size_;
88     return true;
89   }
90 
91  private:
BsdiffExtentFile(std::unique_ptr<ExtentReader> reader,std::unique_ptr<ExtentWriter> writer,size_t size)92   BsdiffExtentFile(std::unique_ptr<ExtentReader> reader,
93                    std::unique_ptr<ExtentWriter> writer,
94                    size_t size)
95       : reader_(std::move(reader)),
96         writer_(std::move(writer)),
97         size_(size),
98         offset_(0) {}
99 
100   std::unique_ptr<ExtentReader> reader_;
101   std::unique_ptr<ExtentWriter> writer_;
102   uint64_t size_;
103   uint64_t offset_;
104 
105   DISALLOW_COPY_AND_ASSIGN(BsdiffExtentFile);
106 };
107 // A class to be passed to |puffpatch| for reading from |source_fd_| and writing
108 // into |target_fd_|.
109 class PuffinExtentStream : public puffin::StreamInterface {
110  public:
111   // Constructor for creating a stream for reading from an |ExtentReader|.
PuffinExtentStream(std::unique_ptr<ExtentReader> reader,uint64_t size)112   PuffinExtentStream(std::unique_ptr<ExtentReader> reader, uint64_t size)
113       : PuffinExtentStream(std::move(reader), nullptr, size) {}
114 
115   // Constructor for creating a stream for writing to an |ExtentWriter|.
PuffinExtentStream(std::unique_ptr<ExtentWriter> writer,uint64_t size)116   PuffinExtentStream(std::unique_ptr<ExtentWriter> writer, uint64_t size)
117       : PuffinExtentStream(nullptr, std::move(writer), size) {}
118 
119   ~PuffinExtentStream() override = default;
120 
GetSize(uint64_t * size) const121   bool GetSize(uint64_t* size) const override {
122     *size = size_;
123     return true;
124   }
125 
GetOffset(uint64_t * offset) const126   bool GetOffset(uint64_t* offset) const override {
127     *offset = offset_;
128     return true;
129   }
130 
Seek(uint64_t offset)131   bool Seek(uint64_t offset) override {
132     if (is_read_) {
133       TEST_AND_RETURN_FALSE(reader_->Seek(offset));
134       offset_ = offset;
135     } else {
136       // For writes technically there should be no change of position, or it
137       // should equivalent of current offset.
138       TEST_AND_RETURN_FALSE(offset_ == offset);
139     }
140     return true;
141   }
142 
Read(void * buffer,size_t count)143   bool Read(void* buffer, size_t count) override {
144     TEST_AND_RETURN_FALSE(is_read_);
145     TEST_AND_RETURN_FALSE(reader_->Read(buffer, count));
146     offset_ += count;
147     return true;
148   }
149 
Write(const void * buffer,size_t count)150   bool Write(const void* buffer, size_t count) override {
151     TEST_AND_RETURN_FALSE(!is_read_);
152     TEST_AND_RETURN_FALSE(writer_->Write(buffer, count));
153     offset_ += count;
154     return true;
155   }
156 
Close()157   bool Close() override { return true; }
158 
159  private:
PuffinExtentStream(std::unique_ptr<ExtentReader> reader,std::unique_ptr<ExtentWriter> writer,uint64_t size)160   PuffinExtentStream(std::unique_ptr<ExtentReader> reader,
161                      std::unique_ptr<ExtentWriter> writer,
162                      uint64_t size)
163       : reader_(std::move(reader)),
164         writer_(std::move(writer)),
165         size_(size),
166         offset_(0),
167         is_read_(reader_ ? true : false) {}
168 
169   std::unique_ptr<ExtentReader> reader_;
170   std::unique_ptr<ExtentWriter> writer_;
171   uint64_t size_;
172   uint64_t offset_;
173   bool is_read_;
174 
175   DISALLOW_COPY_AND_ASSIGN(PuffinExtentStream);
176 };
177 
ExecuteReplaceOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,const void * data)178 bool InstallOperationExecutor::ExecuteReplaceOperation(
179     const InstallOperation& operation,
180     std::unique_ptr<ExtentWriter> writer,
181     const void* data) {
182   TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::REPLACE ||
183                         operation.type() == InstallOperation::REPLACE_BZ ||
184                         operation.type() == InstallOperation::REPLACE_XZ);
185   // Setup the ExtentWriter stack based on the operation type.
186   if (operation.type() == InstallOperation::REPLACE_BZ) {
187     writer.reset(new BzipExtentWriter(std::move(writer)));
188   } else if (operation.type() == InstallOperation::REPLACE_XZ) {
189     writer.reset(new XzExtentWriter(std::move(writer)));
190   }
191   TEST_AND_RETURN_FALSE(writer->Init(operation.dst_extents(), block_size_));
192   TEST_AND_RETURN_FALSE(writer->Write(data, operation.data_length()));
193 
194   return true;
195 }
196 
ExecuteZeroOrDiscardOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer)197 bool InstallOperationExecutor::ExecuteZeroOrDiscardOperation(
198     const InstallOperation& operation, std::unique_ptr<ExtentWriter> writer) {
199   TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::ZERO ||
200                         operation.type() == InstallOperation::DISCARD);
201   using base::MemoryMappedFile;
202   using Access = base::MemoryMappedFile::Access;
203   using Region = base::MemoryMappedFile::Region;
204   writer->Init(operation.dst_extents(), block_size_);
205   // Mmap a region of /dev/zero, as we don't need any actual memory to store
206   // these 0s, so mmap a region of "free memory".
207   base::File dev_zero(base::FilePath("/dev/zero"),
208                       base::File::FLAG_OPEN | base::File::FLAG_READ);
209   MemoryMappedFile buffer;
210   TEST_AND_RETURN_FALSE_ERRNO(buffer.Initialize(
211       std::move(dev_zero),
212       Region{
213           0,
214           static_cast<size_t>(utils::BlocksInExtents(operation.dst_extents()) *
215                               block_size_)},
216       Access::READ_ONLY));
217   writer->Write(buffer.data(), buffer.length());
218   return true;
219 }
220 
ExecuteSourceCopyOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd)221 bool InstallOperationExecutor::ExecuteSourceCopyOperation(
222     const InstallOperation& operation,
223     std::unique_ptr<ExtentWriter> writer,
224     FileDescriptorPtr source_fd) {
225   TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::SOURCE_COPY);
226   TEST_AND_RETURN_FALSE(writer->Init(operation.dst_extents(), block_size_));
227   return fd_utils::CommonHashExtents(
228       source_fd, operation.src_extents(), writer.get(), block_size_, nullptr);
229 }
230 
ExecuteDiffOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd,const void * data,size_t count)231 bool InstallOperationExecutor::ExecuteDiffOperation(
232     const InstallOperation& operation,
233     std::unique_ptr<ExtentWriter> writer,
234     FileDescriptorPtr source_fd,
235     const void* data,
236     size_t count) {
237   TEST_AND_RETURN_FALSE(source_fd != nullptr);
238   TEST_AND_RETURN_FALSE(writer->Init(operation.dst_extents(), block_size_));
239   switch (operation.type()) {
240     case InstallOperation::SOURCE_BSDIFF:
241     case InstallOperation::BSDIFF:
242     case InstallOperation::BROTLI_BSDIFF:
243       return ExecuteSourceBsdiffOperation(
244           operation, std::move(writer), source_fd, data, count);
245     case InstallOperation::PUFFDIFF:
246       return ExecutePuffDiffOperation(
247           operation, std::move(writer), source_fd, data, count);
248     case InstallOperation::ZUCCHINI:
249       return ExecuteZucchiniOperation(
250           operation, std::move(writer), source_fd, data, count);
251     case InstallOperation::LZ4DIFF_BSDIFF:
252     case InstallOperation::LZ4DIFF_PUFFDIFF:
253       return ExecuteLz4diffOperation(
254           operation, std::move(writer), source_fd, data, count);
255     default:
256       LOG(ERROR) << "Unexpected operation type when executing diff ops "
257                  << operation.type() << " "
258                  << operation.Type_Name(operation.type());
259       return false;
260   }
261 }
262 
ExecuteLz4diffOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd,const void * data,size_t count)263 bool InstallOperationExecutor::ExecuteLz4diffOperation(
264     const InstallOperation& operation,
265     std::unique_ptr<ExtentWriter> writer,
266     FileDescriptorPtr source_fd,
267     const void* data,
268     size_t count) {
269   brillo::Blob src_data;
270 
271   TEST_AND_RETURN_FALSE(utils::ReadExtents(
272       source_fd, operation.src_extents(), &src_data, block_size_));
273   TEST_AND_RETURN_FALSE(Lz4Patch(
274       ToStringView(src_data),
275       ToStringView(data, count),
276       [writer(writer.get())](const uint8_t* data, size_t size) -> size_t {
277         if (!writer->Write(data, size)) {
278           return 0;
279         }
280         return size;
281       }));
282   return true;
283 }
284 
ExecuteSourceBsdiffOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd,const void * data,size_t count)285 bool InstallOperationExecutor::ExecuteSourceBsdiffOperation(
286     const InstallOperation& operation,
287     std::unique_ptr<ExtentWriter> writer,
288     FileDescriptorPtr source_fd,
289     const void* data,
290     size_t count) {
291   auto reader = std::make_unique<DirectExtentReader>();
292   TEST_AND_RETURN_FALSE(
293       reader->Init(source_fd, operation.src_extents(), block_size_));
294   auto src_file = std::make_unique<BsdiffExtentFile>(
295       std::move(reader),
296       utils::BlocksInExtents(operation.src_extents()) * block_size_);
297 
298   auto dst_file = std::make_unique<BsdiffExtentFile>(
299       std::move(writer),
300       utils::BlocksInExtents(operation.dst_extents()) * block_size_);
301 
302   TEST_AND_RETURN_FALSE(bsdiff::bspatch(std::move(src_file),
303                                         std::move(dst_file),
304                                         reinterpret_cast<const uint8_t*>(data),
305                                         count) == 0);
306   return true;
307 }
308 
ExecutePuffDiffOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd,const void * data,size_t count)309 bool InstallOperationExecutor::ExecutePuffDiffOperation(
310     const InstallOperation& operation,
311     std::unique_ptr<ExtentWriter> writer,
312     FileDescriptorPtr source_fd,
313     const void* data,
314     size_t count) {
315   auto reader = std::make_unique<DirectExtentReader>();
316   TEST_AND_RETURN_FALSE(
317       reader->Init(source_fd, operation.src_extents(), block_size_));
318   puffin::UniqueStreamPtr src_stream(new PuffinExtentStream(
319       std::move(reader),
320       utils::BlocksInExtents(operation.src_extents()) * block_size_));
321 
322   puffin::UniqueStreamPtr dst_stream(new PuffinExtentStream(
323       std::move(writer),
324       utils::BlocksInExtents(operation.dst_extents()) * block_size_));
325 
326   constexpr size_t kMaxCacheSize = 5 * 1024 * 1024;  // Total 5MB cache.
327   TEST_AND_RETURN_FALSE(
328       puffin::PuffPatch(std::move(src_stream),
329                         std::move(dst_stream),
330                         reinterpret_cast<const uint8_t*>(data),
331                         count,
332                         kMaxCacheSize));
333   return true;
334 }
335 
ExecuteZucchiniOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd,const void * data,size_t count)336 bool InstallOperationExecutor::ExecuteZucchiniOperation(
337     const InstallOperation& operation,
338     std::unique_ptr<ExtentWriter> writer,
339     FileDescriptorPtr source_fd,
340     const void* data,
341     size_t count) {
342   uint64_t src_size =
343       utils::BlocksInExtents(operation.src_extents()) * block_size_;
344   brillo::Blob source_bytes(src_size);
345 
346   // TODO(197361113) either make zucchini stream the read, or use memory mapped
347   // files.
348   auto reader = std::make_unique<DirectExtentReader>();
349   TEST_AND_RETURN_FALSE(
350       reader->Init(source_fd, operation.src_extents(), block_size_));
351   TEST_AND_RETURN_FALSE(reader->Seek(0));
352   TEST_AND_RETURN_FALSE(reader->Read(source_bytes.data(), src_size));
353 
354   brillo::Blob zucchini_patch;
355   TEST_AND_RETURN_FALSE(puffin::BrotliDecode(
356       static_cast<const uint8_t*>(data), count, &zucchini_patch));
357   auto patch_reader = zucchini::EnsemblePatchReader::Create(
358       {zucchini_patch.data(), zucchini_patch.size()});
359   if (!patch_reader.has_value()) {
360     LOG(ERROR) << "Failed to parse the zucchini patch.";
361     return false;
362   }
363 
364   auto dst_size = patch_reader->header().new_size;
365   TEST_AND_RETURN_FALSE(dst_size ==
366                         utils::BlocksInExtents(operation.dst_extents()) *
367                             block_size_);
368 
369   brillo::Blob patched_data(dst_size);
370   auto status =
371       zucchini::ApplyBuffer({source_bytes.data(), source_bytes.size()},
372                             *patch_reader,
373                             {patched_data.data(), patched_data.size()});
374   if (status != zucchini::status::kStatusSuccess) {
375     LOG(ERROR) << "Failed to apply the zucchini patch: " << status;
376     return false;
377   }
378 
379   TEST_AND_RETURN_FALSE(
380       writer->Write(patched_data.data(), patched_data.size()));
381   return true;
382 }
383 
384 }  // namespace chromeos_update_engine
385