• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "install/spl_check.h"
18 
ViolatesSPLDowngrade(const build::tools::releasetools::OtaMetadata & metadata,std::string_view current_spl)19 bool ViolatesSPLDowngrade(const build::tools::releasetools::OtaMetadata& metadata,
20                           std::string_view current_spl) {
21   const auto& post_spl = metadata.postcondition().security_patch_level();
22   if (current_spl.empty()) {
23     LOG(WARNING) << "Failed to get device's current security patch level. Target SPL is "
24                  << post_spl << " permitting OTA install";
25     return false;
26   }
27   // SPL(security patch level) is expected to be in format yyyy-mm-dd, e.g.  2018-05-29. Given this
28   // specific format, comparing two SPL can be done by just regular string comparison. If the format
29   // must lay out year/month/date in the exact order, and must properly prepend dates with 0(for
30   // example, 05 for May). Otherwise this comparison doesn't work. We don't expect SPL date formats
31   // to change, leave this as is.
32   if (post_spl < current_spl) {
33     LOG(ERROR) << "Current SPL: " << current_spl << " Target SPL: " << post_spl
34                << " this is considered a downgrade";
35     if (metadata.spl_downgrade() || metadata.downgrade()) {
36       LOG(WARNING)
37           << "SPL downgrade detected, but OTA package explicitly permitts this(OtaMetadata has "
38              "spl_downgrade / downgrade bit set).Permitting update anyway.Installing a SPL "
39              "downgrade OTA can cause /data fail to decrypt and device fails to boot.";
40       return false;
41     }
42     return true;
43   } else {
44     LOG(INFO) << "old spl: " << current_spl << " new spl: " << post_spl << " CHECK passes";
45   }
46   return false;
47 }
48 
ViolatesSPLDowngrade(ZipArchiveHandle zip,std::string_view current_spl)49 bool ViolatesSPLDowngrade(ZipArchiveHandle zip, std::string_view current_spl) {
50   static constexpr auto&& OTA_OTA_METADATA = "META-INF/com/android/metadata.pb";
51   ZipEntry64 metadata_entry;
52   if (FindEntry(zip, OTA_OTA_METADATA, &metadata_entry) != 0) {
53     LOG(WARNING) << "Failed to find " << OTA_OTA_METADATA
54                  << " treating this as non-spl-downgrade, permit OTA install. If device bricks "
55                     "after installing, check kernel log to see if /data failed to decrypt";
56     return false;
57   }
58   const auto metadata_entry_length = metadata_entry.uncompressed_length;
59   if (metadata_entry_length > std::numeric_limits<size_t>::max()) {
60     LOG(ERROR) << "Failed to extract " << OTA_OTA_METADATA
61                << " because's uncompressed size exceeds size of address space. "
62                << metadata_entry_length;
63     return false;
64   }
65   std::vector<uint8_t> ota_metadata(metadata_entry_length);
66   int32_t err = ExtractToMemory(zip, &metadata_entry, ota_metadata.data(), metadata_entry_length);
67   if (err != 0) {
68     LOG(ERROR) << "Failed to extract " << OTA_OTA_METADATA << ": " << ErrorCodeString(err);
69     return false;
70   }
71   using build::tools::releasetools::OtaMetadata;
72   OtaMetadata metadata;
73   if (!metadata.ParseFromArray(ota_metadata.data(), ota_metadata.size())) {
74     LOG(ERROR) << "Failed to parse ota_medata";
75     return false;
76   }
77   return ViolatesSPLDowngrade(metadata, current_spl);
78 }
79