applypatch: Add backup_source parameter to PatchPartition.

And set it to false when installing recovery image via applypatch. We
only need to back up the source partition when doing in-place update
(e.g. when updating a given partition under recovery). When installing
recovery image via applypatch, we won't touch the source partition (i.e.
/boot).

Removing the backup step also allows dropping the dac_override_allowed
permission. Previously it was needed due to the access to /cache.
Because applypatch runs as root:root, while /cache is owned by
system:cache with 0770.

Bug: 68319577
Test: Invoke the code that installs recovery image; check that recovery
      is installed successfully without denials.
Test: recovery_unit_test passes on taimen.
Change-Id: I549a770b511762189d6672a2835b6e403d695919
This commit is contained in:
Tao Bao
2019-09-23 10:28:54 -07:00
parent 71c35b9fba
commit 5234ad466c
5 changed files with 17 additions and 14 deletions

View File

@@ -47,7 +47,7 @@
using namespace std::string_literals;
static bool GenerateTarget(const Partition& target, const FileContents& source_file,
const Value& patch, const Value* bonus_data);
const Value& patch, const Value* bonus_data, bool backup_source);
bool LoadFileContents(const std::string& filename, FileContents* file) {
// No longer allow loading contents from eMMC partitions.
@@ -266,7 +266,7 @@ int ShowLicenses() {
}
bool PatchPartition(const Partition& target, const Partition& source, const Value& patch,
const Value* bonus) {
const Value* bonus, bool backup_source) {
LOG(INFO) << "Patching " << target.name;
// We try to load and check against the target hash first.
@@ -280,7 +280,7 @@ bool PatchPartition(const Partition& target, const Partition& source, const Valu
FileContents source_file;
if (ReadPartitionToBuffer(source, &source_file, true)) {
return GenerateTarget(target, source_file, patch, bonus);
return GenerateTarget(target, source_file, patch, bonus, backup_source);
}
LOG(ERROR) << "Failed to find any match";
@@ -326,7 +326,7 @@ bool FlashPartition(const Partition& partition, const std::string& source_filena
}
static bool GenerateTarget(const Partition& target, const FileContents& source_file,
const Value& patch, const Value* bonus_data) {
const Value& patch, const Value* bonus_data, bool backup_source) {
uint8_t expected_sha1[SHA_DIGEST_LENGTH];
if (ParseSha1(target.hash, expected_sha1) != 0) {
LOG(ERROR) << "Failed to parse target hash \"" << target.hash << "\"";
@@ -351,11 +351,11 @@ static bool GenerateTarget(const Partition& target, const FileContents& source_f
}
// We write the original source to cache, in case the partition write is interrupted.
if (!CheckAndFreeSpaceOnCache(source_file.data.size())) {
if (backup_source && !CheckAndFreeSpaceOnCache(source_file.data.size())) {
LOG(ERROR) << "Not enough free space on /cache";
return false;
}
if (!SaveFileContents(Paths::Get().cache_temp_source(), &source_file)) {
if (backup_source && !SaveFileContents(Paths::Get().cache_temp_source(), &source_file)) {
LOG(ERROR) << "Failed to back up source file";
return false;
}
@@ -415,7 +415,9 @@ static bool GenerateTarget(const Partition& target, const FileContents& source_f
}
// Delete the backup copy of the source.
unlink(Paths::Get().cache_temp_source().c_str());
if (backup_source) {
unlink(Paths::Get().cache_temp_source().c_str());
}
// Success!
return true;