1 //
2 // Copyright (C) 2017 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_generator/squashfs_filesystem.h"
18 
19 #include <fcntl.h>
20 
21 #include <algorithm>
22 #include <string>
23 #include <utility>
24 
25 #include <base/files/file_util.h>
26 #include <base/files/scoped_temp_dir.h>
27 #include <base/logging.h>
28 #include <base/strings/string_number_conversions.h>
29 #include <base/strings/string_split.h>
30 #include <brillo/streams/file_stream.h>
31 
32 #include "update_engine/common/subprocess.h"
33 #include "update_engine/common/utils.h"
34 #include "update_engine/payload_generator/deflate_utils.h"
35 #include "update_engine/payload_generator/delta_diff_generator.h"
36 #include "update_engine/payload_generator/extent_ranges.h"
37 #include "update_engine/payload_generator/extent_utils.h"
38 #include "update_engine/update_metadata.pb.h"
39 
40 using base::FilePath;
41 using base::ScopedTempDir;
42 using std::string;
43 using std::unique_ptr;
44 using std::vector;
45 
46 namespace chromeos_update_engine {
47 
48 namespace {
49 
50 // The size of the squashfs super block.
51 constexpr size_t kSquashfsSuperBlockSize = 96;
52 constexpr uint64_t kSquashfsCompressedBit = 1 << 24;
53 constexpr uint32_t kSquashfsZlibCompression = 1;
54 
55 constexpr char kUpdateEngineConf[] = "etc/update_engine.conf";
56 
ReadSquashfsHeader(const brillo::Blob blob,SquashfsFilesystem::SquashfsHeader * header)57 bool ReadSquashfsHeader(const brillo::Blob blob,
58                         SquashfsFilesystem::SquashfsHeader* header) {
59   if (blob.size() < kSquashfsSuperBlockSize) {
60     return false;
61   }
62 
63   memcpy(&header->magic, blob.data(), 4);
64   memcpy(&header->block_size, blob.data() + 12, 4);
65   memcpy(&header->compression_type, blob.data() + 20, 2);
66   memcpy(&header->major_version, blob.data() + 28, 2);
67   return true;
68 }
69 
CheckHeader(const SquashfsFilesystem::SquashfsHeader & header)70 bool CheckHeader(const SquashfsFilesystem::SquashfsHeader& header) {
71   return header.magic == 0x73717368 && header.major_version == 4;
72 }
73 
GetFileMapContent(const string & sqfs_path,string * map)74 bool GetFileMapContent(const string& sqfs_path, string* map) {
75   // Create a tmp file
76   string map_file;
77   TEST_AND_RETURN_FALSE(
78       utils::MakeTempFile("squashfs_file_map.XXXXXX", &map_file, nullptr));
79   ScopedPathUnlinker map_unlinker(map_file);
80 
81   // Run unsquashfs to get the system file map.
82   // unsquashfs -m <map-file> <squashfs-file>
83   vector<string> cmd = {"unsquashfs", "-m", map_file, sqfs_path};
84   string stdout, stderr;
85   int exit_code;
86   if (!Subprocess::SynchronousExec(cmd, &exit_code, &stdout, &stderr) ||
87       exit_code != 0) {
88     LOG(ERROR) << "Failed to run `unsquashfs -m` with stdout content: "
89                << stdout << " and stderr content: " << stderr;
90     return false;
91   }
92   TEST_AND_RETURN_FALSE(utils::ReadFile(map_file, map));
93   return true;
94 }
95 
GetUpdateEngineConfig(const std::string & sqfs_path,string * config)96 bool GetUpdateEngineConfig(const std::string& sqfs_path, string* config) {
97   ScopedTempDir unsquash_dir;
98   if (!unsquash_dir.CreateUniqueTempDir()) {
99     PLOG(ERROR) << "Failed to create a temporary directory.";
100     return false;
101   }
102 
103   // Run unsquashfs to extract update_engine.conf
104   // -f: To force overriding if the target directory exists.
105   // -d: The directory to unsquash the files.
106   vector<string> cmd = {"unsquashfs",
107                         "-f",
108                         "-d",
109                         unsquash_dir.GetPath().value(),
110                         sqfs_path,
111                         kUpdateEngineConf};
112   string stdout, stderr;
113   int exit_code;
114   if (!Subprocess::SynchronousExec(cmd, &exit_code, &stdout, &stderr) ||
115       exit_code != 0) {
116     PLOG(ERROR) << "Failed to unsquashfs etc/update_engine.conf with stdout: "
117                 << stdout << " and stderr: " << stderr;
118     return false;
119   }
120 
121   auto config_path = unsquash_dir.GetPath().Append(kUpdateEngineConf);
122   string config_content;
123   if (!utils::ReadFile(config_path.value(), &config_content)) {
124     PLOG(ERROR) << "Failed to read " << config_path.value();
125     return false;
126   }
127 
128   if (config_content.empty()) {
129     LOG(ERROR) << "update_engine config file was empty!!";
130     return false;
131   }
132 
133   *config = std::move(config_content);
134   return true;
135 }
136 
137 }  // namespace
138 
Init(const string & map,const string & sqfs_path,size_t size,const SquashfsHeader & header,bool extract_deflates)139 bool SquashfsFilesystem::Init(const string& map,
140                               const string& sqfs_path,
141                               size_t size,
142                               const SquashfsHeader& header,
143                               bool extract_deflates) {
144   size_ = size;
145 
146   bool is_zlib = header.compression_type == kSquashfsZlibCompression;
147   if (!is_zlib) {
148     LOG(WARNING) << "Filesystem is not Gzipped. Not filling deflates!";
149   }
150   vector<puffin::ByteExtent> zlib_blks;
151 
152   // Reading files map. For the format of the file map look at the comments for
153   // |CreateFromFileMap()|.
154   auto lines = base::SplitStringPiece(map,
155                                       "\n",
156                                       base::WhitespaceHandling::KEEP_WHITESPACE,
157                                       base::SplitResult::SPLIT_WANT_NONEMPTY);
158   for (const auto& line : lines) {
159     auto splits =
160         base::SplitStringPiece(line,
161                                " \t",
162                                base::WhitespaceHandling::TRIM_WHITESPACE,
163                                base::SplitResult::SPLIT_WANT_NONEMPTY);
164     // Only filename is invalid.
165     TEST_AND_RETURN_FALSE(splits.size() > 1);
166     uint64_t start;
167     TEST_AND_RETURN_FALSE(base::StringToUint64(splits[1], &start));
168     uint64_t cur_offset = start;
169     bool is_compressed = false;
170     for (size_t i = 2; i < splits.size(); ++i) {
171       uint64_t blk_size;
172       TEST_AND_RETURN_FALSE(base::StringToUint64(splits[i], &blk_size));
173       // TODO(ahassani): For puffin push it into a proper list if uncompressed.
174       auto new_blk_size = blk_size & ~kSquashfsCompressedBit;
175       TEST_AND_RETURN_FALSE(new_blk_size <= header.block_size);
176       if (new_blk_size > 0 && !(blk_size & kSquashfsCompressedBit)) {
177         // It is a compressed block.
178         if (is_zlib && extract_deflates) {
179           zlib_blks.emplace_back(cur_offset, new_blk_size);
180         }
181         is_compressed = true;
182       }
183       cur_offset += new_blk_size;
184     }
185 
186     // If size is zero do not add the file.
187     if (cur_offset - start > 0) {
188       File file;
189       file.name = splits[0].as_string();
190       file.extents = {ExtentForBytes(kBlockSize, start, cur_offset - start)};
191       file.is_compressed = is_compressed;
192       files_.emplace_back(file);
193     }
194   }
195 
196   // Sort all files by their offset in the squashfs.
197   std::sort(files_.begin(), files_.end(), [](const File& a, const File& b) {
198     return a.extents[0].start_block() < b.extents[0].start_block();
199   });
200   // If there is any overlap between two consecutive extents, remove them. Here
201   // we are assuming all files have exactly one extent. If this assumption
202   // changes then this implementation needs to change too.
203   for (auto first = files_.begin(),
204             second = first + (first == files_.end() ? 0 : 1);
205        first != files_.end() && second != files_.end();
206        second = first + 1) {
207     auto first_begin = first->extents[0].start_block();
208     auto first_end = first_begin + first->extents[0].num_blocks();
209     auto second_begin = second->extents[0].start_block();
210     auto second_end = second_begin + second->extents[0].num_blocks();
211     // Remove the first file if the size is zero.
212     if (first_end == first_begin) {
213       first = files_.erase(first);
214     } else if (first_end > second_begin) {  // We found a collision.
215       if (second_end <= first_end) {
216         // Second file is inside the first file, remove the second file.
217         second = files_.erase(second);
218       } else if (first_begin == second_begin) {
219         // First file is inside the second file, remove the first file.
220         first = files_.erase(first);
221       } else {
222         // Remove overlapping extents from the first file.
223         first->extents[0].set_num_blocks(second_begin - first_begin);
224         ++first;
225       }
226     } else {
227       ++first;
228     }
229   }
230 
231   // Find all the metadata including superblock and add them to the list of
232   // files.
233   ExtentRanges file_extents;
234   for (const auto& file : files_) {
235     file_extents.AddExtents(file.extents);
236   }
237   vector<Extent> full = {ExtentForBytes(kBlockSize, 0, size_)};
238   auto metadata_extents = FilterExtentRanges(full, file_extents);
239   // For now there should be at most two extents. One for superblock and one for
240   // metadata at the end. Just create appropriate files with <metadata-i> name.
241   // We can add all these extents as one metadata too, but that violates the
242   // contiguous write optimization.
243   for (size_t i = 0; i < metadata_extents.size(); i++) {
244     File file;
245     file.name = "<metadata-" + std::to_string(i) + ">";
246     file.extents = {metadata_extents[i]};
247     files_.emplace_back(file);
248   }
249 
250   // Do one last sort before returning.
251   std::sort(files_.begin(), files_.end(), [](const File& a, const File& b) {
252     return a.extents[0].start_block() < b.extents[0].start_block();
253   });
254 
255   if (is_zlib && extract_deflates) {
256     // If it is infact gzipped, then the sqfs_path should be valid to read its
257     // content.
258     TEST_AND_RETURN_FALSE(!sqfs_path.empty());
259     if (zlib_blks.empty()) {
260       return true;
261     }
262 
263     // Sort zlib blocks.
264     std::sort(zlib_blks.begin(),
265               zlib_blks.end(),
266               [](const puffin::ByteExtent& a, const puffin::ByteExtent& b) {
267                 return a.offset < b.offset;
268               });
269 
270     // Sometimes a squashfs can have a two files that are hard linked. In this
271     // case both files will have the same starting offset in the image and hence
272     // the same zlib blocks. So we need to remove these duplicates to eliminate
273     // further potential probems. As a matter of fact the next statement will
274     // fail if there are duplicates (there will be overlap between two blocks).
275     auto last = std::unique(zlib_blks.begin(), zlib_blks.end());
276     zlib_blks.erase(last, zlib_blks.end());
277 
278     // Make sure zlib blocks are not overlapping.
279     auto result = std::adjacent_find(
280         zlib_blks.begin(),
281         zlib_blks.end(),
282         [](const puffin::ByteExtent& a, const puffin::ByteExtent& b) {
283           return (a.offset + a.length) > b.offset;
284         });
285     TEST_AND_RETURN_FALSE(result == zlib_blks.end());
286 
287     vector<puffin::BitExtent> deflates;
288     TEST_AND_RETURN_FALSE(
289         puffin::LocateDeflatesInZlibBlocks(sqfs_path, zlib_blks, &deflates));
290 
291     // Add deflates for each file.
292     for (auto& file : files_) {
293       file.deflates = deflate_utils::FindDeflates(file.extents, deflates);
294     }
295   }
296   return true;
297 }
298 
CreateFromFile(const string & sqfs_path,bool extract_deflates,bool load_settings)299 unique_ptr<SquashfsFilesystem> SquashfsFilesystem::CreateFromFile(
300     const string& sqfs_path, bool extract_deflates, bool load_settings) {
301   if (sqfs_path.empty())
302     return nullptr;
303 
304   brillo::StreamPtr sqfs_file =
305       brillo::FileStream::Open(FilePath(sqfs_path),
306                                brillo::Stream::AccessMode::READ,
307                                brillo::FileStream::Disposition::OPEN_EXISTING,
308                                nullptr);
309   if (!sqfs_file) {
310     LOG(ERROR) << "Unable to open " << sqfs_path << " for reading.";
311     return nullptr;
312   }
313 
314   SquashfsHeader header;
315   brillo::Blob blob(kSquashfsSuperBlockSize);
316   if (!sqfs_file->ReadAllBlocking(blob.data(), blob.size(), nullptr)) {
317     LOG(ERROR) << "Unable to read from file: " << sqfs_path;
318     return nullptr;
319   }
320   if (!ReadSquashfsHeader(blob, &header) || !CheckHeader(header)) {
321     // This is not necessary an error.
322     return nullptr;
323   }
324 
325   // Read the map file.
326   string filemap;
327   if (!GetFileMapContent(sqfs_path, &filemap)) {
328     LOG(ERROR) << "Failed to produce squashfs map file: " << sqfs_path;
329     return nullptr;
330   }
331 
332   unique_ptr<SquashfsFilesystem> sqfs(new SquashfsFilesystem());
333   if (!sqfs->Init(
334           filemap, sqfs_path, sqfs_file->GetSize(), header, extract_deflates)) {
335     LOG(ERROR) << "Failed to initialized the Squashfs file system";
336     return nullptr;
337   }
338 
339   if (load_settings) {
340     if (!GetUpdateEngineConfig(sqfs_path, &sqfs->update_engine_config_)) {
341       return nullptr;
342     }
343   }
344 
345   return sqfs;
346 }
347 
CreateFromFileMap(const string & filemap,size_t size,const SquashfsHeader & header)348 unique_ptr<SquashfsFilesystem> SquashfsFilesystem::CreateFromFileMap(
349     const string& filemap, size_t size, const SquashfsHeader& header) {
350   if (!CheckHeader(header)) {
351     LOG(ERROR) << "Invalid Squashfs super block!";
352     return nullptr;
353   }
354 
355   unique_ptr<SquashfsFilesystem> sqfs(new SquashfsFilesystem());
356   if (!sqfs->Init(filemap, "", size, header, false)) {
357     LOG(ERROR) << "Failed to initialize the Squashfs file system using filemap";
358     return nullptr;
359   }
360   // TODO(ahassani): Add a function that initializes the puffin related extents.
361   return sqfs;
362 }
363 
GetBlockSize() const364 size_t SquashfsFilesystem::GetBlockSize() const {
365   return kBlockSize;
366 }
367 
GetBlockCount() const368 size_t SquashfsFilesystem::GetBlockCount() const {
369   return size_ / kBlockSize;
370 }
371 
GetFiles(vector<File> * files) const372 bool SquashfsFilesystem::GetFiles(vector<File>* files) const {
373   files->insert(files->end(), files_.begin(), files_.end());
374   return true;
375 }
376 
LoadSettings(brillo::KeyValueStore * store) const377 bool SquashfsFilesystem::LoadSettings(brillo::KeyValueStore* store) const {
378   if (!store->LoadFromString(update_engine_config_)) {
379     LOG(ERROR) << "Failed to load the settings with config: "
380                << update_engine_config_;
381     return false;
382   }
383   return true;
384 }
385 
IsSquashfsImage(const brillo::Blob & blob)386 bool SquashfsFilesystem::IsSquashfsImage(const brillo::Blob& blob) {
387   SquashfsHeader header;
388   return ReadSquashfsHeader(blob, &header) && CheckHeader(header);
389 }
390 }  // namespace chromeos_update_engine
391