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