Xiaochu Liu | 6da1727 | 2018-11-06 14:04:01 -0800 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "dlcservice/utils.h" |
| 6 | |
Jae Hoon Kim | e7db33a | 2020-01-30 12:43:54 -0800 | [diff] [blame] | 7 | #include <fcntl.h> |
Jae Hoon Kim | a050ad7 | 2020-01-30 19:30:21 -0800 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <unistd.h> |
Jae Hoon Kim | e7db33a | 2020-01-30 12:43:54 -0800 | [diff] [blame] | 10 | |
Jae Hoon Kim | a050ad7 | 2020-01-30 19:30:21 -0800 | [diff] [blame] | 11 | #include <algorithm> |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 12 | #include <memory> |
Jae Hoon Kim | 873404c | 2019-08-08 15:43:49 -0700 | [diff] [blame] | 13 | #include <utility> |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 14 | #include <vector> |
Jae Hoon Kim | 873404c | 2019-08-08 15:43:49 -0700 | [diff] [blame] | 15 | |
Amin Hassani | 69dfe6e | 2019-03-20 14:58:48 -0700 | [diff] [blame] | 16 | #include <base/files/file_enumerator.h> |
Colin Howes | 56ecef1 | 2018-12-11 09:55:39 -0800 | [diff] [blame] | 17 | #include <base/files/file_util.h> |
| 18 | #include <base/logging.h> |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 19 | #include <base/strings/string_number_conversions.h> |
Jae Hoon Kim | e7db33a | 2020-01-30 12:43:54 -0800 | [diff] [blame] | 20 | #include <brillo/file_utils.h> |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 21 | #include <crypto/secure_hash.h> |
| 22 | #include <crypto/sha2.h> |
Colin Howes | 56ecef1 | 2018-12-11 09:55:39 -0800 | [diff] [blame] | 23 | |
Jae Hoon Kim | 5d095de | 2019-07-31 13:44:42 -0700 | [diff] [blame] | 24 | using base::FilePath; |
Amin Hassani | 5bbbf21 | 2020-04-30 09:43:17 -0700 | [diff] [blame] | 25 | using crypto::SecureHash; |
Jae Hoon Kim | 5d095de | 2019-07-31 13:44:42 -0700 | [diff] [blame] | 26 | using std::set; |
| 27 | using std::string; |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 28 | using std::unique_ptr; |
| 29 | using std::vector; |
Jae Hoon Kim | 5d095de | 2019-07-31 13:44:42 -0700 | [diff] [blame] | 30 | |
Xiaochu Liu | 6da1727 | 2018-11-06 14:04:01 -0800 | [diff] [blame] | 31 | namespace dlcservice { |
| 32 | |
Jae Hoon Kim | 074a910 | 2020-01-29 17:38:27 -0800 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | bool SetFilePermissions(const base::FilePath& path, int perms) { |
Amin Hassani | c0867b6 | 2020-04-16 09:44:34 -0700 | [diff] [blame] | 36 | // Do not try to set the permission if the permissions are already correct. If |
| 37 | // it failed to get the permissions, go ahead and set them. |
| 38 | int tmp_perms; |
| 39 | if (base::GetPosixFilePermissions(path, &tmp_perms) && perms == tmp_perms) |
| 40 | return true; |
| 41 | |
Jae Hoon Kim | 074a910 | 2020-01-29 17:38:27 -0800 | [diff] [blame] | 42 | if (!base::SetPosixFilePermissions(path, perms)) { |
| 43 | PLOG(ERROR) << "Failed to set permissions for: " << path.value(); |
| 44 | return false; |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | |
Amin Hassani | 7c6543f | 2020-06-02 16:16:43 -0700 | [diff] [blame] | 49 | bool WriteFile(const FilePath& path, const string& data, bool truncate) { |
| 50 | int flags = O_CREAT | O_WRONLY; |
| 51 | if (truncate) |
| 52 | flags |= O_TRUNC; |
| 53 | |
| 54 | base::ScopedFD fd(brillo::OpenSafely(path, flags, kDlcFilePerms)); |
| 55 | if (!fd.is_valid()) { |
| 56 | LOG(ERROR) << "Failed to open file for writting " << path.value(); |
| 57 | return false; |
| 58 | } |
| 59 | if (data.empty()) |
| 60 | return true; |
| 61 | return base::WriteFileDescriptor(fd.get(), data.c_str(), data.size()); |
| 62 | } |
| 63 | |
Jae Hoon Kim | 074a910 | 2020-01-29 17:38:27 -0800 | [diff] [blame] | 64 | } // namespace |
| 65 | |
Jae Hoon Kim | 9ecbeba | 2020-01-16 16:32:32 -0800 | [diff] [blame] | 66 | char kDlcDirAName[] = "dlc_a"; |
| 67 | char kDlcDirBName[] = "dlc_b"; |
Jae Hoon Kim | 9ecbeba | 2020-01-16 16:32:32 -0800 | [diff] [blame] | 68 | |
| 69 | char kDlcImageFileName[] = "dlc.img"; |
| 70 | char kManifestName[] = "imageloader.json"; |
| 71 | |
| 72 | char kRootDirectoryInsideDlcModule[] = "root"; |
| 73 | |
Jae Hoon Kim | 074a910 | 2020-01-29 17:38:27 -0800 | [diff] [blame] | 74 | const int kDlcFilePerms = 0644; |
| 75 | const int kDlcDirectoryPerms = 0755; |
Jae Hoon Kim | 373cd65 | 2020-01-21 10:03:39 -0800 | [diff] [blame] | 76 | |
Jae Hoon Kim | e7db33a | 2020-01-30 12:43:54 -0800 | [diff] [blame] | 77 | bool WriteToFile(const FilePath& path, const string& data) { |
Amin Hassani | 7c6543f | 2020-06-02 16:16:43 -0700 | [diff] [blame] | 78 | return WriteFile(path, data, /*truncate=*/true); |
Jae Hoon Kim | e7db33a | 2020-01-30 12:43:54 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Amin Hassani | 7c6543f | 2020-06-02 16:16:43 -0700 | [diff] [blame] | 81 | bool WriteToImage(const FilePath& path, const string& data) { |
| 82 | return WriteFile(path, data, /*truncate=*/false); |
| 83 | } |
| 84 | |
Jae Hoon Kim | a050ad7 | 2020-01-30 19:30:21 -0800 | [diff] [blame] | 85 | bool ResizeFile(const base::FilePath& path, int64_t size) { |
| 86 | int64_t prev_size; |
| 87 | base::File f(path, base::File::FLAG_OPEN | base::File::FLAG_WRITE); |
| 88 | if (!f.IsValid()) { |
| 89 | LOG(ERROR) << "Failed to open file to resize '" << path.value() |
| 90 | << "': " << base::File::ErrorToString(f.error_details()); |
| 91 | return false; |
| 92 | } |
| 93 | prev_size = f.GetLength(); |
| 94 | if (prev_size < 0) { |
| 95 | PLOG(ERROR) << "Failed to get file size for resizing " << path.value(); |
| 96 | return false; |
| 97 | } |
| 98 | if (!f.SetLength(size)) { |
Tom Hughes | ff48365 | 2020-08-24 17:58:00 -0700 | [diff] [blame] | 99 | PLOG(ERROR) << "Failed to set length (" << size << ") for " << path.value(); |
Jae Hoon Kim | a050ad7 | 2020-01-30 19:30:21 -0800 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | // When shrinking files, there is no need to unsparse as it's not certainly |
| 103 | // safe to unsparse potentially used portions of the file. |
| 104 | if (size <= prev_size) |
| 105 | return true; |
| 106 | |
| 107 | // Otherwise, unsparse the increased portion of the file. |
| 108 | if (f.Seek(base::File::Whence::FROM_BEGIN, prev_size) < 0) { |
| 109 | PLOG(ERROR) << "Failed to lseek() to offset " << prev_size << " for " |
| 110 | << path.value(); |
| 111 | return false; |
| 112 | } |
| 113 | size -= prev_size; |
| 114 | |
| 115 | constexpr int64_t kMaxBufSize = 4096; |
| 116 | constexpr char buf[kMaxBufSize] = {'\0'}; |
| 117 | for (; size > 0; size -= kMaxBufSize) { |
| 118 | // Set the lesser of either |kMaxBufSize| or |size| bytes. |
| 119 | const size_t len = std::min(size, kMaxBufSize); |
| 120 | // Write out |len| from |buf| to |fd|. |
| 121 | if (f.WriteAtCurrentPos(buf, len) != len) { |
| 122 | PLOG(ERROR) << "Failed to write zero to " << path.value(); |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
Jae Hoon Kim | 074a910 | 2020-01-29 17:38:27 -0800 | [diff] [blame] | 129 | bool CreateDir(const base::FilePath& path) { |
Jae Hoon Kim | 373cd65 | 2020-01-21 10:03:39 -0800 | [diff] [blame] | 130 | base::File::Error file_err; |
| 131 | if (!base::CreateDirectoryAndGetError(path, &file_err)) { |
Amin Hassani | a69f32e | 2020-03-30 15:20:42 -0700 | [diff] [blame] | 132 | PLOG(ERROR) << "Failed to create directory '" << path.value() |
| 133 | << "': " << base::File::ErrorToString(file_err); |
Jae Hoon Kim | 373cd65 | 2020-01-21 10:03:39 -0800 | [diff] [blame] | 134 | return false; |
| 135 | } |
Jae Hoon Kim | 074a910 | 2020-01-29 17:38:27 -0800 | [diff] [blame] | 136 | return SetFilePermissions(path, kDlcDirectoryPerms); |
Amin Hassani | 69dfe6e | 2019-03-20 14:58:48 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Jae Hoon Kim | a050ad7 | 2020-01-30 19:30:21 -0800 | [diff] [blame] | 139 | // TODO(crbug.com/976074): When creating a file, provide the flexibility to be |
| 140 | // able to unsparse in |ResizeFile()| up to the actual size necessary and not |
| 141 | // the preallocated size from the manifest as is the |size| here for DLC to |
| 142 | // install successfully. |
Jae Hoon Kim | 373cd65 | 2020-01-21 10:03:39 -0800 | [diff] [blame] | 143 | bool CreateFile(const base::FilePath& path, int64_t size) { |
Jae Hoon Kim | 074a910 | 2020-01-29 17:38:27 -0800 | [diff] [blame] | 144 | if (!CreateDir(path.DirName())) |
Jae Hoon Kim | 373cd65 | 2020-01-21 10:03:39 -0800 | [diff] [blame] | 145 | return false; |
Jae Hoon Kim | a050ad7 | 2020-01-30 19:30:21 -0800 | [diff] [blame] | 146 | // Keep scoped to not explicitly close file. |
| 147 | { |
Amin Hassani | c0867b6 | 2020-04-16 09:44:34 -0700 | [diff] [blame] | 148 | base::File f(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); |
Jae Hoon Kim | a050ad7 | 2020-01-30 19:30:21 -0800 | [diff] [blame] | 149 | if (!f.IsValid()) { |
| 150 | LOG(ERROR) << "Failed to create file at " << path.value() |
| 151 | << " reason: " << base::File::ErrorToString(f.error_details()); |
| 152 | return false; |
| 153 | } |
Jae Hoon Kim | 373cd65 | 2020-01-21 10:03:39 -0800 | [diff] [blame] | 154 | } |
Jae Hoon Kim | a050ad7 | 2020-01-30 19:30:21 -0800 | [diff] [blame] | 155 | return ResizeFile(path, size) && SetFilePermissions(path, kDlcFilePerms); |
Jae Hoon Kim | 373cd65 | 2020-01-21 10:03:39 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 158 | bool HashFile(const base::FilePath& path, |
| 159 | int64_t size, |
| 160 | vector<uint8_t>* sha256) { |
Jae Hoon Kim | 8ab2649 | 2020-05-07 13:49:36 -0700 | [diff] [blame] | 161 | base::File f(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 162 | if (!f.IsValid()) { |
| 163 | PLOG(ERROR) << "Failed to read file at " << path.value() |
| 164 | << ", reason: " << base::File::ErrorToString(f.error_details()); |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | auto length = f.GetLength(); |
| 169 | if (length < 0) { |
| 170 | LOG(ERROR) << "Failed to get length for file at " << path.value(); |
| 171 | return false; |
| 172 | } |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 173 | if (length < size) { |
| 174 | LOG(ERROR) << "File size " << length |
| 175 | << " is smaller than intended file size " << size; |
| 176 | return false; |
| 177 | } |
Jae Hoon Kim | 8ab2649 | 2020-05-07 13:49:36 -0700 | [diff] [blame] | 178 | |
| 179 | constexpr int64_t kMaxBufSize = 4096; |
| 180 | unique_ptr<SecureHash> hash(SecureHash::Create(SecureHash::SHA256)); |
| 181 | |
| 182 | vector<char> buf(kMaxBufSize); |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 183 | for (; size > 0; size -= kMaxBufSize) { |
| 184 | int bytes = std::min(kMaxBufSize, size); |
Jae Hoon Kim | 8ab2649 | 2020-05-07 13:49:36 -0700 | [diff] [blame] | 185 | if (f.ReadAtCurrentPos(buf.data(), bytes) != bytes) { |
| 186 | PLOG(ERROR) << "Failed to read from file at " << path.value(); |
| 187 | return false; |
| 188 | } |
| 189 | hash->Update(buf.data(), bytes); |
| 190 | } |
| 191 | sha256->resize(crypto::kSHA256Length); |
| 192 | hash->Finish(sha256->data(), sha256->size()); |
| 193 | return true; |
| 194 | } |
| 195 | |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 196 | bool CopyAndHashFile(const base::FilePath& from, |
| 197 | const base::FilePath& to, |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 198 | int64_t size, |
Amin Hassani | 0d3ab93 | 2020-04-26 17:35:04 -0700 | [diff] [blame] | 199 | vector<uint8_t>* sha256) { |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 200 | base::File f_from(from, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 201 | if (!f_from.IsValid()) { |
| 202 | PLOG(ERROR) << "Failed to read file at " << from.value() << " reason: " |
| 203 | << base::File::ErrorToString(f_from.error_details()); |
Jae Hoon Kim | 373cd65 | 2020-01-21 10:03:39 -0800 | [diff] [blame] | 204 | return false; |
| 205 | } |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 206 | base::File f_to(to, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); |
| 207 | if (!f_to.IsValid()) { |
| 208 | PLOG(ERROR) << "Failed to open file at " << to.value() << " reason: " |
| 209 | << base::File::ErrorToString(f_to.error_details()); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | auto from_length = f_from.GetLength(); |
| 214 | if (from_length < 0) { |
| 215 | LOG(ERROR) << "Failed to get length for file at " << from.value(); |
| 216 | return false; |
| 217 | } |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 218 | if (from_length < size) { |
| 219 | LOG(ERROR) << "Preloaded file size " << from_length |
| 220 | << " is smaller than intended file size " << size; |
| 221 | return false; |
| 222 | } |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 223 | |
| 224 | constexpr int64_t kMaxBufSize = 4096; |
Amin Hassani | 5bbbf21 | 2020-04-30 09:43:17 -0700 | [diff] [blame] | 225 | unique_ptr<SecureHash> hash(SecureHash::Create(SecureHash::SHA256)); |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 226 | |
| 227 | vector<char> buf(kMaxBufSize); |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 228 | for (; size > 0; size -= kMaxBufSize) { |
| 229 | int bytes = std::min(kMaxBufSize, size); |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 230 | if (f_from.ReadAtCurrentPos(buf.data(), bytes) != bytes) { |
| 231 | PLOG(ERROR) << "Failed to read from file at " << from.value(); |
| 232 | return false; |
| 233 | } |
| 234 | if (f_to.WriteAtCurrentPos(buf.data(), bytes) != bytes) { |
| 235 | PLOG(ERROR) << "Failed to write to file at " << from.value(); |
| 236 | return false; |
| 237 | } |
| 238 | hash->Update(buf.data(), bytes); |
| 239 | } |
Amin Hassani | 0d3ab93 | 2020-04-26 17:35:04 -0700 | [diff] [blame] | 240 | sha256->resize(crypto::kSHA256Length); |
| 241 | hash->Finish(sha256->data(), sha256->size()); |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 242 | |
Jae Hoon Kim | 760a694 | 2020-04-15 10:01:02 -0700 | [diff] [blame] | 243 | return SetFilePermissions(to, kDlcFilePerms); |
Xiaochu Liu | 6da1727 | 2018-11-06 14:04:01 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Jae Hoon Kim | ace79b7 | 2020-03-10 18:00:15 +0000 | [diff] [blame] | 246 | FilePath GetDlcImagePath(const FilePath& dlc_module_root_path, |
| 247 | const string& id, |
| 248 | const string& package, |
| 249 | BootSlot::Slot slot) { |
Jae Hoon Kim | e567b9e | 2020-04-08 14:43:59 -0700 | [diff] [blame] | 250 | return JoinPaths(dlc_module_root_path, id, package, BootSlot::ToString(slot), |
| 251 | kDlcImageFileName); |
Colin Howes | 56ecef1 | 2018-12-11 09:55:39 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // Extract details about a DLC module from its manifest file. |
Jae Hoon Kim | ace79b7 | 2020-03-10 18:00:15 +0000 | [diff] [blame] | 255 | bool GetDlcManifest(const FilePath& dlc_manifest_path, |
| 256 | const string& id, |
| 257 | const string& package, |
| 258 | imageloader::Manifest* manifest_out) { |
Jae Hoon Kim | 5d095de | 2019-07-31 13:44:42 -0700 | [diff] [blame] | 259 | string dlc_json_str; |
| 260 | FilePath dlc_manifest_file = |
Jae Hoon Kim | 373cd65 | 2020-01-21 10:03:39 -0800 | [diff] [blame] | 261 | JoinPaths(dlc_manifest_path, id, package, kManifestName); |
Colin Howes | 56ecef1 | 2018-12-11 09:55:39 -0800 | [diff] [blame] | 262 | |
| 263 | if (!base::ReadFileToString(dlc_manifest_file, &dlc_json_str)) { |
| 264 | LOG(ERROR) << "Failed to read DLC manifest file '" |
| 265 | << dlc_manifest_file.value() << "'."; |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | if (!manifest_out->ParseManifest(dlc_json_str)) { |
Andrew | 6a55cf4 | 2020-01-02 15:01:36 -0800 | [diff] [blame] | 270 | LOG(ERROR) << "Failed to parse DLC manifest for DLC:" << id << "."; |
Colin Howes | 56ecef1 | 2018-12-11 09:55:39 -0800 | [diff] [blame] | 271 | return false; |
| 272 | } |
| 273 | |
| 274 | return true; |
Xiaochu Liu | 6da1727 | 2018-11-06 14:04:01 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Jae Hoon Kim | 5d095de | 2019-07-31 13:44:42 -0700 | [diff] [blame] | 277 | set<string> ScanDirectory(const FilePath& dir) { |
| 278 | set<string> result; |
Amin Hassani | 69dfe6e | 2019-03-20 14:58:48 -0700 | [diff] [blame] | 279 | base::FileEnumerator file_enumerator(dir, false, |
| 280 | base::FileEnumerator::DIRECTORIES); |
Jae Hoon Kim | 5d095de | 2019-07-31 13:44:42 -0700 | [diff] [blame] | 281 | for (FilePath dir_path = file_enumerator.Next(); !dir_path.empty(); |
Amin Hassani | 69dfe6e | 2019-03-20 14:58:48 -0700 | [diff] [blame] | 282 | dir_path = file_enumerator.Next()) { |
Jae Hoon Kim | 013d58a | 2019-07-15 16:12:05 -0700 | [diff] [blame] | 283 | result.emplace(dir_path.BaseName().value()); |
Amin Hassani | 69dfe6e | 2019-03-20 14:58:48 -0700 | [diff] [blame] | 284 | } |
| 285 | return result; |
| 286 | } |
| 287 | |
Xiaochu Liu | 6da1727 | 2018-11-06 14:04:01 -0800 | [diff] [blame] | 288 | } // namespace dlcservice |