Fix potential OOM in update_verifier

Limit the size of each read to 1024 * BLOCKSIZE. (Same as the I/O limit
of each transfer command for block based OTA).

Bug: 37729708
Test: U_V sets slot successfully on sailfish, and it takes about ~20s
(no noticeable time increase)
Change-Id: I7a6cdc744fe4c0760e09e0afed75b89c16d8eac3
This commit is contained in:
Tianjie Xu 2017-04-27 11:47:35 -07:00
parent c99bb23955
commit 8fa8f0b16c

View file

@ -44,6 +44,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <algorithm>
#include <string> #include <string>
#include <vector> #include <vector>
@ -142,17 +143,21 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
return false; return false;
} }
static constexpr int BLOCKSIZE = 4096; static constexpr size_t BLOCKSIZE = 4096;
if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) { if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) {
PLOG(ERROR) << "lseek to " << range_start << " failed"; PLOG(ERROR) << "lseek to " << range_start << " failed";
return false; return false;
} }
size_t size = (range_end - range_start) * BLOCKSIZE; size_t remain = (range_end - range_start) * BLOCKSIZE;
std::vector<uint8_t> buf(size); while (remain > 0) {
if (!android::base::ReadFully(fd.get(), buf.data(), size)) { size_t to_read = std::min(remain, 1024 * BLOCKSIZE);
PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end; std::vector<uint8_t> buf(to_read);
return false; if (!android::base::ReadFully(fd.get(), buf.data(), to_read)) {
PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end;
return false;
}
remain -= to_read;
} }
blk_count += (range_end - range_start); blk_count += (range_end - range_start);
} }