1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "applypatch/block_set.h"
17 #include <linux/fs.h>
18 #include <sys/ioctl.h>
19 #include <openssl/sha.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include "applypatch/command.h"
24 #include "applypatch/store.h"
25 #include "applypatch/transfer_manager.h"
26 #include "log/log.h"
27 #include "patch/update_patch.h"
28 #include "securec.h"
29 #include "utils.h"
30
31 using namespace Updater;
32 using namespace Updater::Utils;
33
34 namespace Updater {
BlockSet(std::vector<BlockPair> && pairs)35 BlockSet::BlockSet(std::vector<BlockPair> &&pairs)
36 {
37 blockSize_ = 0;
38 if (pairs.empty()) {
39 LOG(ERROR) << "Invalid block.";
40 return;
41 }
42
43 for (const auto &pair : pairs) {
44 if (!CheckReliablePair(pair)) {
45 return;
46 }
47 PushBack(pair);
48 }
49 }
50
CheckReliablePair(BlockPair pair)51 bool BlockSet::CheckReliablePair(BlockPair pair)
52 {
53 if (pair.first >= pair.second) {
54 LOG(ERROR) << "Invalid number of block size";
55 return false;
56 }
57 size_t size = pair.second - pair.first;
58 if (blockSize_ >= (SIZE_MAX - size)) {
59 LOG(ERROR) << "Block size overflow";
60 return false;
61 }
62 return true;
63 }
64
PushBack(BlockPair blockPair)65 void BlockSet::PushBack(BlockPair blockPair)
66 {
67 blocks_.push_back(std::move(blockPair));
68 blockSize_ += (blockPair.second - blockPair.first);
69 }
70
ClearBlocks()71 void BlockSet::ClearBlocks()
72 {
73 blockSize_ = 0;
74 blocks_.clear();
75 }
76
ParserAndInsert(const std::string & blockStr)77 bool BlockSet::ParserAndInsert(const std::string &blockStr)
78 {
79 if (blockStr == "") {
80 LOG(ERROR) << "Invalid argument, this argument is empty";
81 return false;
82 }
83 std::vector<std::string> pairs = SplitString(blockStr, ",");
84 return ParserAndInsert(pairs);
85 }
86
ParserAndInsert(const std::vector<std::string> & blockToken)87 bool BlockSet::ParserAndInsert(const std::vector<std::string> &blockToken)
88 {
89 ClearBlocks();
90 if (blockToken.empty()) {
91 LOG(ERROR) << "Invalid block token argument";
92 return false;
93 }
94 if (blockToken.size() < 3) { // 3:blockToken.size() < 3 means too small blocks_ in argument
95 LOG(ERROR) << "Too small blocks_ in argument";
96 return false;
97 }
98 // Get number of blockToken
99 unsigned long long int blockPairSize;
100 std::vector<std::string> bt = blockToken;
101 std::vector<std::string>::iterator bp = bt.begin();
102 blockPairSize = String2Int<unsigned long long int>(*bp, N_DEC);
103 if (blockPairSize == 0 || blockPairSize % 2 != 0 || // 2:Check whether blockPairSize is valid.
104 blockPairSize != bt.size() - 1) {
105 LOG(ERROR) << "Invalid number in block token";
106 return false;
107 }
108
109 while (++bp != bt.end()) {
110 size_t first = String2Int<size_t>(*bp++, N_DEC);
111 size_t second = String2Int<size_t>(*bp, N_DEC);
112 blocks_.push_back(BlockPair {
113 first, second
114 });
115 blockSize_ += (second - first);
116 }
117 return true;
118 }
119
Begin()120 std::vector<BlockPair>::iterator BlockSet::Begin()
121 {
122 return blocks_.begin();
123 }
124
End()125 std::vector<BlockPair>::iterator BlockSet::End()
126 {
127 return blocks_.end();
128 }
129
CBegin() const130 std::vector<BlockPair>::const_iterator BlockSet::CBegin() const
131 {
132 return blocks_.cbegin();
133 }
134
CEnd() const135 std::vector<BlockPair>::const_iterator BlockSet::CEnd() const
136 {
137 return blocks_.cend();
138 }
139
CrBegin() const140 std::vector<BlockPair>::const_reverse_iterator BlockSet::CrBegin() const
141 {
142 return blocks_.crbegin();
143 }
144
CrEnd() const145 std::vector<BlockPair>::const_reverse_iterator BlockSet::CrEnd() const
146 {
147 return blocks_.crend();
148 }
149
ReadDataFromBlock(int fd,std::vector<uint8_t> & buffer)150 size_t BlockSet::ReadDataFromBlock(int fd, std::vector<uint8_t> &buffer)
151 {
152 size_t pos = 0;
153 std::vector<BlockPair>::iterator it = blocks_.begin();
154 int ret;
155 for (; it != blocks_.end(); ++it) {
156 ret = lseek64(fd, static_cast<off64_t>(it->first * H_BLOCK_SIZE), SEEK_SET);
157 if (ret == -1) {
158 LOG(ERROR) << "Fail to seek";
159 return 0;
160 }
161 size_t size = (it->second - it->first) * H_BLOCK_SIZE;
162 if (!Utils::ReadFully(fd, buffer.data() + pos, size)) {
163 LOG(ERROR) << "Fail to read";
164 return 0;
165 }
166 pos += size;
167 }
168 return pos;
169 }
170
WriteDataToBlock(int fd,std::vector<uint8_t> & buffer)171 size_t BlockSet::WriteDataToBlock(int fd, std::vector<uint8_t> &buffer)
172 {
173 size_t pos = 0;
174 std::vector<BlockPair>::iterator it = blocks_.begin();
175 int ret = 0;
176 for (; it != blocks_.end(); ++it) {
177 off64_t offset = static_cast<off64_t>(it->first * H_BLOCK_SIZE);
178 size_t writeSize = (it->second - it->first) * H_BLOCK_SIZE;
179 #ifndef UPDATER_UT
180 uint64_t arguments[] = {static_cast<uint64_t>(offset), writeSize};
181 ret = ioctl(fd, BLKDISCARD, &arguments);
182 if (ret == -1 && errno != EOPNOTSUPP) {
183 LOG(ERROR) << "Error to write block set to memory";
184 return 0;
185 }
186 #endif
187 ret = lseek64(fd, offset, SEEK_SET);
188 if (ret == -1) {
189 LOG(ERROR) << "BlockSet::WriteDataToBlock Fail to seek";
190 return 0;
191 }
192 if (Utils::WriteFully(fd, buffer.data() + pos, writeSize) == false) {
193 LOG(ERROR) << "Write data to block error, errno : " << errno;
194 return 0;
195 }
196 pos += writeSize;
197 }
198 if (fsync(fd) == -1) {
199 LOG(ERROR) << "Failed to fsync";
200 return 0;
201 }
202 return pos;
203 }
204
CountOfRanges() const205 size_t BlockSet::CountOfRanges() const
206 {
207 return blocks_.size();
208 }
209
TotalBlockSize() const210 size_t BlockSet::TotalBlockSize() const
211 {
212 return blockSize_;
213 }
214
VerifySha256(const std::vector<uint8_t> & buffer,const size_t size,const std::string & expected)215 int32_t BlockSet::VerifySha256(const std::vector<uint8_t> &buffer, const size_t size, const std::string &expected)
216 {
217 uint8_t digest[SHA256_DIGEST_LENGTH];
218 SHA256(buffer.data(), size * H_BLOCK_SIZE, digest);
219 std::string hexdigest = Utils::ConvertSha256Hex(digest, SHA256_DIGEST_LENGTH);
220 if (hexdigest == expected) {
221 return 0;
222 }
223 return -1;
224 }
225
IsTwoBlocksOverlap(const BlockSet & source,BlockSet & target)226 bool BlockSet::IsTwoBlocksOverlap(const BlockSet &source, BlockSet &target)
227 {
228 auto firstIter = source.CBegin();
229 for (; firstIter != source.CEnd(); ++firstIter) {
230 std::vector<BlockPair>::iterator secondIter = target.Begin();
231 for (; secondIter != target.End(); ++secondIter) {
232 if (!(secondIter->first >= firstIter->second ||
233 firstIter->first >= secondIter->second)) {
234 return true;
235 }
236 }
237 }
238 return false;
239 }
240
MoveBlock(std::vector<uint8_t> & target,const BlockSet & locations,const std::vector<uint8_t> & source)241 void BlockSet::MoveBlock(std::vector<uint8_t> &target, const BlockSet& locations,
242 const std::vector<uint8_t>& source)
243 {
244 const uint8_t *sd = source.data();
245 uint8_t *td = target.data();
246 size_t start = locations.TotalBlockSize();
247 for (auto it = locations.CrBegin(); it != locations.CrEnd(); it++) {
248 size_t blocks = it->second - it->first;
249 start -= blocks;
250 if (memmove_s(td + (it->first * H_BLOCK_SIZE), blocks * H_BLOCK_SIZE, sd + (start *
251 H_BLOCK_SIZE), blocks * H_BLOCK_SIZE) != EOK) {
252 LOG(ERROR) << "MoveBlock memmove_s failed!";
253 return;
254 }
255 }
256 }
257
LoadSourceBuffer(const Command & cmd,size_t & pos,std::vector<uint8_t> & sourceBuffer,bool & isOverlap,size_t & srcBlockSize)258 int32_t BlockSet::LoadSourceBuffer(const Command &cmd, size_t &pos, std::vector<uint8_t> &sourceBuffer,
259 bool &isOverlap, size_t &srcBlockSize)
260 {
261 std::string blockLen = cmd.GetArgumentByPos(pos++);
262 srcBlockSize = String2Int<size_t>(blockLen, N_DEC);
263 sourceBuffer.resize(srcBlockSize * H_BLOCK_SIZE);
264 std::string targetCmd = cmd.GetArgumentByPos(pos++);
265 std::string storeBase = TransferManager::GetTransferManagerInstance()->GetGlobalParams()->storeBase;
266 if (targetCmd != "-") {
267 BlockSet srcBlk;
268 srcBlk.ParserAndInsert(targetCmd);
269 isOverlap = IsTwoBlocksOverlap(srcBlk, *this);
270 // read source data
271 if (srcBlk.ReadDataFromBlock(cmd.GetFileDescriptor(), sourceBuffer) == 0) {
272 LOG(ERROR) << "ReadDataFromBlock failed";
273 return -1;
274 }
275 std::string nextArgv = cmd.GetArgumentByPos(pos++);
276 if (nextArgv == "") {
277 return 1;
278 }
279 BlockSet locations;
280 locations.ParserAndInsert(nextArgv);
281 MoveBlock(sourceBuffer, locations, sourceBuffer);
282 }
283
284 std::string lastArg = cmd.GetArgumentByPos(pos++);
285 while (lastArg != "") {
286 std::vector<std::string> tokens = SplitString(lastArg, ":");
287 if (tokens.size() != H_CMD_ARGS_LIMIT) {
288 LOG(ERROR) << "invalid parameter";
289 return -1;
290 }
291 std::vector<uint8_t> stash;
292 auto ret = Store::LoadDataFromStore(storeBase, tokens[H_ZERO_NUMBER], stash);
293 if (ret == -1) {
294 LOG(ERROR) << "Failed to load tokens";
295 return -1;
296 }
297 BlockSet locations;
298 locations.ParserAndInsert(tokens[1]);
299 MoveBlock(sourceBuffer, locations, stash);
300
301 lastArg = cmd.GetArgumentByPos(pos++);
302 }
303 return 1;
304 }
305
LoadTargetBuffer(const Command & cmd,std::vector<uint8_t> & buffer,size_t & blockSize,size_t pos,std::string & srcHash)306 int32_t BlockSet::LoadTargetBuffer(const Command &cmd, std::vector<uint8_t> &buffer, size_t &blockSize,
307 size_t pos, std::string &srcHash)
308 {
309 bool isOverlap = false;
310 auto ret = LoadSourceBuffer(cmd, pos, buffer, isOverlap, blockSize);
311 if (ret != 1) {
312 return ret;
313 }
314 std::string storeBase = TransferManager::GetTransferManagerInstance()->GetGlobalParams()->storeBase;
315 std::string storePath = storeBase + "/" + srcHash;
316 struct stat storeStat {};
317 int res = stat(storePath.c_str(), &storeStat);
318 if (VerifySha256(buffer, blockSize, srcHash) == 0) {
319 if (isOverlap && res != 0) {
320 TransferManager::GetTransferManagerInstance()->GetGlobalParams()->freeStash = srcHash;
321 ret = Store::WriteDataToStore(storeBase, srcHash, buffer, blockSize * H_BLOCK_SIZE);
322 if (ret != 0) {
323 LOG(ERROR) << "failed to stash overlapping source blocks";
324 return -1;
325 }
326 }
327 return 0;
328 }
329 if (Store::LoadDataFromStore(storeBase, srcHash, buffer) == 0) {
330 return 0;
331 }
332 return -1;
333 }
334
WriteZeroToBlock(int fd,bool isErase)335 int32_t BlockSet::WriteZeroToBlock(int fd, bool isErase)
336 {
337 std::vector<uint8_t> buffer;
338 buffer.resize(H_BLOCK_SIZE);
339 if (memset_s(buffer.data(), H_BLOCK_SIZE, 0, H_BLOCK_SIZE) != EOK) {
340 LOG(ERROR) << "memset_s failed";
341 return -1;
342 }
343
344 auto iter = blocks_.begin();
345 while (iter != blocks_.end()) {
346 off64_t offset = static_cast<off64_t>(iter->first * H_BLOCK_SIZE);
347 int ret = 0;
348 #ifndef UPDATER_UT
349 size_t writeSize = (iter->second - iter->first) * H_BLOCK_SIZE;
350 uint64_t arguments[2] = {static_cast<uint64_t>(offset), writeSize};
351 ret = ioctl(fd, BLKDISCARD, &arguments);
352 if (ret == -1 && errno != EOPNOTSUPP) {
353 LOG(ERROR) << "Error to write block set to memory";
354 return -1;
355 }
356 #endif
357 if (isErase) {
358 iter++;
359 continue;
360 }
361 ret = lseek64(fd, offset, SEEK_SET);
362 if (ret == -1) {
363 LOG(ERROR) << "BlockSet::WriteZeroToBlock Fail to seek";
364 return -1;
365 }
366 for (size_t pos = iter->first; pos < iter->second; pos++) {
367 if (Utils::WriteFully(fd, buffer.data(), H_BLOCK_SIZE)) {
368 continue;
369 }
370 if (errno == EIO) {
371 return 1;
372 }
373 LOG(ERROR) << "BlockSet::WriteZeroToBlock Write 0 to block error";
374 return -1;
375 }
376 iter++;
377 }
378 return 0;
379 }
380
WriteDiffToBlock(const Command & cmd,std::vector<uint8_t> & srcBuffer,const size_t srcBlockSize,bool isImgDiff)381 int32_t BlockSet::WriteDiffToBlock(const Command &cmd, std::vector<uint8_t> &srcBuffer,
382 const size_t srcBlockSize, bool isImgDiff)
383 {
384 size_t pos = H_MOVE_CMD_ARGS_START;
385 size_t offset = Utils::String2Int<size_t>(cmd.GetArgumentByPos(pos++), Utils::N_DEC);
386 size_t length = Utils::String2Int<size_t>(cmd.GetArgumentByPos(pos++), Utils::N_DEC);
387 // Get patch buffer
388 auto globalParams = TransferManager::GetTransferManagerInstance()->GetGlobalParams();
389 auto patchBuff = globalParams->patchDataBuffer + offset;
390 size_t srcBuffSize = srcBlockSize * H_BLOCK_SIZE;
391 if (isImgDiff) {
392 std::vector<uint8_t> empty;
393 UpdatePatch::PatchParam patchParam = {
394 reinterpret_cast<u_char*>(srcBuffer.data()), srcBuffSize, reinterpret_cast<u_char*>(patchBuff), length
395 };
396 std::unique_ptr<BlockWriter> writer = std::make_unique<BlockWriter>(cmd.GetFileDescriptor(), *this);
397 if (writer.get() == nullptr) {
398 LOG(ERROR) << "Cannot create block writer, pkgdiff patch abort!";
399 return -1;
400 }
401 int32_t ret = UpdatePatch::UpdateApplyPatch::ApplyImagePatch(patchParam, empty,
402 [&](size_t start, const UpdatePatch::BlockBuffer &data, size_t size) -> int {
403 return (writer->Write(data.buffer, size, nullptr)) ? 0 : -1;
404 }, cmd.GetArgumentByPos(pos + 1));
405 writer.reset();
406 if (ret != 0) {
407 LOG(ERROR) << "Fail to ApplyImagePatch; offset: " << offset << ", length: " << length;
408 return -1;
409 }
410 } else {
411 LOG(DEBUG) << "Run bsdiff patch.";
412 UpdatePatch::PatchBuffer patchInfo = {patchBuff, 0, length};
413 std::unique_ptr<BlockWriter> writer = std::make_unique<BlockWriter>(cmd.GetFileDescriptor(), *this);
414 if (writer.get() == nullptr) {
415 LOG(ERROR) << "Cannot create block writer, pkgdiff patch abort!";
416 return -1;
417 }
418 auto ret = UpdatePatch::UpdateApplyPatch::ApplyBlockPatch(patchInfo, {srcBuffer.data(), srcBuffSize},
419 [&](size_t start, const UpdatePatch::BlockBuffer &data, size_t size) -> int {
420 return (writer->Write(data.buffer, size, nullptr)) ? 0 : -1;
421 }, cmd.GetArgumentByPos(pos + 1));
422 writer.reset();
423 if (ret != 0) {
424 LOG(ERROR) << "Fail to ApplyBlockPatch";
425 return -1;
426 }
427 }
428 if (fsync(cmd.GetFileDescriptor()) == -1) {
429 LOG(ERROR) << "Failed to sync restored data";
430 return -1;
431 }
432 return 0;
433 }
434 } // namespace Updater
435