• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,size_t count)178 bool InstallOperationExecutor::ExecuteReplaceOperation(
179     const InstallOperation& operation,
180     std::unique_ptr<ExtentWriter> writer,
181     const void* data,
182     size_t count) {
183   TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::REPLACE ||
184                         operation.type() == InstallOperation::REPLACE_BZ ||
185                         operation.type() == InstallOperation::REPLACE_XZ);
186   // Setup the ExtentWriter stack based on the operation type.
187   if (operation.type() == InstallOperation::REPLACE_BZ) {
188     writer.reset(new BzipExtentWriter(std::move(writer)));
189   } else if (operation.type() == InstallOperation::REPLACE_XZ) {
190     writer.reset(new XzExtentWriter(std::move(writer)));
191   }
192   TEST_AND_RETURN_FALSE(writer->Init(operation.dst_extents(), block_size_));
193   TEST_AND_RETURN_FALSE(writer->Write(data, operation.data_length()));
194 
195   return true;
196 }
197 
ExecuteZeroOrDiscardOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer)198 bool InstallOperationExecutor::ExecuteZeroOrDiscardOperation(
199     const InstallOperation& operation, std::unique_ptr<ExtentWriter> writer) {
200   TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::ZERO ||
201                         operation.type() == InstallOperation::DISCARD);
202   using base::MemoryMappedFile;
203   using Access = base::MemoryMappedFile::Access;
204   using Region = base::MemoryMappedFile::Region;
205   writer->Init(operation.dst_extents(), block_size_);
206   // Mmap a region of /dev/zero, as we don't need any actual memory to store
207   // these 0s, so mmap a region of "free memory".
208   base::File dev_zero(base::FilePath("/dev/zero"),
209                       base::File::FLAG_OPEN | base::File::FLAG_READ);
210   MemoryMappedFile buffer;
211   TEST_AND_RETURN_FALSE_ERRNO(buffer.Initialize(
212       std::move(dev_zero),
213       Region{
214           0,
215           static_cast<size_t>(utils::BlocksInExtents(operation.dst_extents()) *
216                               block_size_)},
217       Access::READ_ONLY));
218   writer->Write(buffer.data(), buffer.length());
219   return true;
220 }
221 
ExecuteSourceCopyOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd)222 bool InstallOperationExecutor::ExecuteSourceCopyOperation(
223     const InstallOperation& operation,
224     std::unique_ptr<ExtentWriter> writer,
225     FileDescriptorPtr source_fd) {
226   TEST_AND_RETURN_FALSE(operation.type() == InstallOperation::SOURCE_COPY);
227   TEST_AND_RETURN_FALSE(writer->Init(operation.dst_extents(), block_size_));
228   return fd_utils::CommonHashExtents(
229       source_fd, operation.src_extents(), writer.get(), block_size_, nullptr);
230 }
231 
ExecuteDiffOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd,const void * data,size_t count)232 bool InstallOperationExecutor::ExecuteDiffOperation(
233     const InstallOperation& operation,
234     std::unique_ptr<ExtentWriter> writer,
235     FileDescriptorPtr source_fd,
236     const void* data,
237     size_t count) {
238   TEST_AND_RETURN_FALSE(source_fd != nullptr);
239   TEST_AND_RETURN_FALSE(writer->Init(operation.dst_extents(), block_size_));
240   switch (operation.type()) {
241     case InstallOperation::SOURCE_BSDIFF:
242     case InstallOperation::BSDIFF:
243     case InstallOperation::BROTLI_BSDIFF:
244       return ExecuteSourceBsdiffOperation(
245           operation, std::move(writer), source_fd, data, count);
246     case InstallOperation::PUFFDIFF:
247       return ExecutePuffDiffOperation(
248           operation, std::move(writer), source_fd, data, count);
249     case InstallOperation::ZUCCHINI:
250       return ExecuteZucchiniOperation(
251           operation, std::move(writer), source_fd, data, count);
252     case InstallOperation::LZ4DIFF_BSDIFF:
253     case InstallOperation::LZ4DIFF_PUFFDIFF:
254       return ExecuteLz4diffOperation(
255           operation, std::move(writer), source_fd, data, count);
256     default:
257       LOG(ERROR) << "Unexpected operation type when executing diff ops "
258                  << operation.type() << " "
259                  << operation.Type_Name(operation.type());
260       return false;
261   }
262 }
263 
ExecuteLz4diffOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd,const void * data,size_t count)264 bool InstallOperationExecutor::ExecuteLz4diffOperation(
265     const InstallOperation& operation,
266     std::unique_ptr<ExtentWriter> writer,
267     FileDescriptorPtr source_fd,
268     const void* data,
269     size_t count) {
270   brillo::Blob src_data;
271 
272   TEST_AND_RETURN_FALSE(utils::ReadExtents(
273       source_fd, operation.src_extents(), &src_data, block_size_));
274   TEST_AND_RETURN_FALSE(Lz4Patch(
275       ToStringView(src_data),
276       ToStringView(data, count),
277       [writer(writer.get())](const uint8_t* data, size_t size) -> size_t {
278         if (!writer->Write(data, size)) {
279           return 0;
280         }
281         return size;
282       }));
283   return true;
284 }
285 
ExecuteSourceBsdiffOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd,const void * data,size_t count)286 bool InstallOperationExecutor::ExecuteSourceBsdiffOperation(
287     const InstallOperation& operation,
288     std::unique_ptr<ExtentWriter> writer,
289     FileDescriptorPtr source_fd,
290     const void* data,
291     size_t count) {
292   auto reader = std::make_unique<DirectExtentReader>();
293   TEST_AND_RETURN_FALSE(
294       reader->Init(source_fd, operation.src_extents(), block_size_));
295   auto src_file = std::make_unique<BsdiffExtentFile>(
296       std::move(reader),
297       utils::BlocksInExtents(operation.src_extents()) * block_size_);
298 
299   auto dst_file = std::make_unique<BsdiffExtentFile>(
300       std::move(writer),
301       utils::BlocksInExtents(operation.dst_extents()) * block_size_);
302 
303   TEST_AND_RETURN_FALSE(bsdiff::bspatch(std::move(src_file),
304                                         std::move(dst_file),
305                                         reinterpret_cast<const uint8_t*>(data),
306                                         count) == 0);
307   return true;
308 }
309 
ExecutePuffDiffOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd,const void * data,size_t count)310 bool InstallOperationExecutor::ExecutePuffDiffOperation(
311     const InstallOperation& operation,
312     std::unique_ptr<ExtentWriter> writer,
313     FileDescriptorPtr source_fd,
314     const void* data,
315     size_t count) {
316   auto reader = std::make_unique<DirectExtentReader>();
317   TEST_AND_RETURN_FALSE(
318       reader->Init(source_fd, operation.src_extents(), block_size_));
319   puffin::UniqueStreamPtr src_stream(new PuffinExtentStream(
320       std::move(reader),
321       utils::BlocksInExtents(operation.src_extents()) * block_size_));
322 
323   puffin::UniqueStreamPtr dst_stream(new PuffinExtentStream(
324       std::move(writer),
325       utils::BlocksInExtents(operation.dst_extents()) * block_size_));
326 
327   constexpr size_t kMaxCacheSize = 5 * 1024 * 1024;  // Total 5MB cache.
328   TEST_AND_RETURN_FALSE(
329       puffin::PuffPatch(std::move(src_stream),
330                         std::move(dst_stream),
331                         reinterpret_cast<const uint8_t*>(data),
332                         count,
333                         kMaxCacheSize));
334   return true;
335 }
336 
ExecuteZucchiniOperation(const InstallOperation & operation,std::unique_ptr<ExtentWriter> writer,FileDescriptorPtr source_fd,const void * data,size_t count)337 bool InstallOperationExecutor::ExecuteZucchiniOperation(
338     const InstallOperation& operation,
339     std::unique_ptr<ExtentWriter> writer,
340     FileDescriptorPtr source_fd,
341     const void* data,
342     size_t count) {
343   uint64_t src_size =
344       utils::BlocksInExtents(operation.src_extents()) * block_size_;
345   brillo::Blob source_bytes(src_size);
346 
347   // TODO(197361113) either make zucchini stream the read, or use memory mapped
348   // files.
349   auto reader = std::make_unique<DirectExtentReader>();
350   TEST_AND_RETURN_FALSE(
351       reader->Init(source_fd, operation.src_extents(), block_size_));
352   TEST_AND_RETURN_FALSE(reader->Seek(0));
353   TEST_AND_RETURN_FALSE(reader->Read(source_bytes.data(), src_size));
354 
355   brillo::Blob zucchini_patch;
356   TEST_AND_RETURN_FALSE(puffin::BrotliDecode(
357       static_cast<const uint8_t*>(data), count, &zucchini_patch));
358   auto patch_reader = zucchini::EnsemblePatchReader::Create(
359       {zucchini_patch.data(), zucchini_patch.size()});
360   if (!patch_reader.has_value()) {
361     LOG(ERROR) << "Failed to parse the zucchini patch.";
362     return false;
363   }
364 
365   auto dst_size = patch_reader->header().new_size;
366   TEST_AND_RETURN_FALSE(dst_size ==
367                         utils::BlocksInExtents(operation.dst_extents()) *
368                             block_size_);
369 
370   brillo::Blob patched_data(dst_size);
371   auto status =
372       zucchini::ApplyBuffer({source_bytes.data(), source_bytes.size()},
373                             *patch_reader,
374                             {patched_data.data(), patched_data.size()});
375   if (status != zucchini::status::kStatusSuccess) {
376     LOG(ERROR) << "Failed to apply the zucchini patch: " << status;
377     return false;
378   }
379 
380   TEST_AND_RETURN_FALSE(
381       writer->Write(patched_data.data(), patched_data.size()));
382   return true;
383 }
384 
385 }  // namespace chromeos_update_engine
386