Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 55ef6167 authored by Ryan Mitchell's avatar Ryan Mitchell
Browse files

Revert "libandroidfw hardening for IncFs"

Revert "Move map_ptr to incfs namspace"

Revert submission 12787270

Reason for revert: b/173250495
Reverted Changes:
I5cd1bc8a2:libandroidfw hardening for IncFs
Ice5dbcfb2:Move map_ptr to incfs namspace
I29ccdc8ed:Do not cache bag parent stack until requested
I1e9e9acaa:Cache resolved theme values

Change-Id: Ib90ef68339710086df41e9abe0833a542d03a74f
parent 6ca48473
Loading
Loading
Loading
Loading
+46 −25
Original line number Diff line number Diff line
@@ -45,8 +45,11 @@ using android::ApkAssets;
using android::ApkAssetsCookie;
using android::AssetManager2;
using android::ConfigDescription;
using android::is_valid_resid;
using android::kInvalidCookie;
using android::Res_value;
using android::ResStringPool;
using android::ResTable_config;
using android::StringPiece16;
using android::base::StringPrintf;
using android::idmap2::CommandLineOptions;
@@ -56,6 +59,7 @@ using android::idmap2::ResourceId;
using android::idmap2::Result;
using android::idmap2::Unit;
using android::idmap2::utils::ExtractOverlayManifestInfo;
using android::util::Utf16ToUtf8;

namespace {

@@ -65,23 +69,25 @@ Result<ResourceId> WARN_UNUSED ParseResReference(const AssetManager2& am, const

  // first, try to parse as a hex number
  char* endptr = nullptr;
  const ResourceId parsed_resid = strtol(res.c_str(), &endptr, kBaseHex);
  ResourceId resid;
  resid = strtol(res.c_str(), &endptr, kBaseHex);
  if (*endptr == '\0') {
    return parsed_resid;
    return resid;
  }

  // next, try to parse as a package:type/name string
  if (auto resid = am.GetResourceId(res, "", fallback_package)) {
    return *resid;
  resid = am.GetResourceId(res, "", fallback_package);
  if (is_valid_resid(resid)) {
    return resid;
  }

  // end of the road: res could not be parsed
  return Error("failed to obtain resource id for %s", res.c_str());
}

void PrintValue(AssetManager2* const am, const AssetManager2::SelectedValue& value,
void PrintValue(AssetManager2* const am, const Res_value& value, const ApkAssetsCookie& cookie,
                std::string* const out) {
  switch (value.type) {
  switch (value.dataType) {
    case Res_value::TYPE_INT_DEC:
      out->append(StringPrintf("%d", value.data));
      break;
@@ -92,21 +98,30 @@ void PrintValue(AssetManager2* const am, const AssetManager2::SelectedValue& val
      out->append(value.data != 0 ? "true" : "false");
      break;
    case Res_value::TYPE_STRING: {
      const ResStringPool* pool = am->GetStringPoolForCookie(value.cookie);
      const ResStringPool* pool = am->GetStringPoolForCookie(cookie);
      out->append("\"");
      if (auto str = pool->string8ObjectAt(value.data)) {
        out->append(*str);
      size_t len;
      if (pool->isUTF8()) {
        const char* str = pool->string8At(value.data, &len);
        out->append(str, len);
      } else {
        const char16_t* str16 = pool->stringAt(value.data, &len);
        out->append(Utf16ToUtf8(StringPiece16(str16, len)));
      }
      out->append("\"");
    } break;
    default:
      out->append(StringPrintf("dataType=0x%02x data=0x%08x", value.type, value.data));
      out->append(StringPrintf("dataType=0x%02x data=0x%08x", value.dataType, value.data));
      break;
  }
}

Result<std::string> WARN_UNUSED GetValue(AssetManager2* const am, ResourceId resid) {
  auto value = am->GetResource(resid);
  if (!value.has_value()) {
  Res_value value;
  ResTable_config config;
  uint32_t flags;
  ApkAssetsCookie cookie = am->GetResource(resid, true, 0, &value, &config, &flags);
  if (cookie == kInvalidCookie) {
    return Error("no resource 0x%08x in asset manager", resid);
  }

@@ -114,35 +129,41 @@ Result<std::string> WARN_UNUSED GetValue(AssetManager2* const am, ResourceId res

  // TODO(martenkongstad): use optional parameter GetResource(..., std::string*
  // stacktrace = NULL) instead
  out.append(StringPrintf("cookie=%d ", value->cookie));
  out.append(StringPrintf("cookie=%d ", cookie));

  out.append("config='");
  out.append(value->config.toString().c_str());
  out.append(config.toString().c_str());
  out.append("' value=");

  if (value->type == Res_value::TYPE_REFERENCE) {
    auto bag_result = am->GetBag(static_cast<uint32_t>(value->data));
    if (!bag_result.has_value()) {
      out.append(StringPrintf("dataType=0x%02x data=0x%08x", value->type, value->data));
  if (value.dataType == Res_value::TYPE_REFERENCE) {
    const android::ResolvedBag* bag = am->GetBag(static_cast<uint32_t>(value.data));
    if (bag == nullptr) {
      out.append(StringPrintf("dataType=0x%02x data=0x%08x", value.dataType, value.data));
      return out;
    }

    out.append("[");
    const android::ResolvedBag* bag = bag_result.value();
    Res_value bag_val;
    ResTable_config selected_config;
    uint32_t flags;
    uint32_t ref;
    ApkAssetsCookie bag_cookie;
    for (size_t i = 0; i < bag->entry_count; ++i) {
      AssetManager2::SelectedValue entry(bag, bag->entries[i]);
      if (am->ResolveReference(entry).has_value()) {
        out.append(StringPrintf("Error: dataType=0x%02x data=0x%08x", entry.type, entry.data));
      const android::ResolvedBag::Entry& entry = bag->entries[i];
      bag_val = entry.value;
      bag_cookie = am->ResolveReference(entry.cookie, &bag_val, &selected_config, &flags, &ref);
      if (bag_cookie == kInvalidCookie) {
        out.append(
            StringPrintf("Error: dataType=0x%02x data=0x%08x", bag_val.dataType, bag_val.data));
        continue;
      }
      PrintValue(am, entry, &out);
      PrintValue(am, bag_val, bag_cookie, &out);
      if (i != bag->entry_count - 1) {
        out.append(", ");
      }
    }
    out.append("]");
  } else {
    PrintValue(am, *value, &out);
    PrintValue(am, value, cookie, &out);
  }

  return out;
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
#ifndef IDMAP2_IDMAP2D_IDMAP2SERVICE_H_
#define IDMAP2_IDMAP2D_IDMAP2SERVICE_H_

#include <string>

#include <android-base/unique_fd.h>
#include <binder/BinderService.h>

+1 −2
Original line number Diff line number Diff line
@@ -117,8 +117,7 @@ class ResourceMapping {
  static Result<ResourceMapping> CreateResourceMappingLegacy(const AssetManager2* target_am,
                                                             const AssetManager2* overlay_am,
                                                             const LoadedPackage* target_package,
                                                             const LoadedPackage* overlay_package,
                                                             LogInfo& log_info);
                                                             const LoadedPackage* overlay_package);

  // Removes resources that do not pass policy or overlayable checks of the target package.
  void FilterOverlayableResources(const AssetManager2* target_am,
+3 −2
Original line number Diff line number Diff line
@@ -100,9 +100,10 @@ void PrettyPrintVisitor::visit(const IdmapData& data) {
    stream_ << TAB << base::StringPrintf("0x%08x -> ", target_entry.target_id)
            << utils::DataTypeToString(target_entry.value.data_type);

    size_t unused;
    if (target_entry.value.data_type == Res_value::TYPE_STRING) {
      auto str = string_pool.stringAt(target_entry.value.data_value - string_pool_offset);
      stream_ << " \"" << str.value_or(StringPiece16(u"")) << "\"";
      auto str = string_pool.stringAt(target_entry.value.data_value - string_pool_offset, &unused);
      stream_ << " \"" << StringPiece16(str) << "\"";
    } else {
      stream_ << " " << base::StringPrintf("0x%08x", target_entry.value.data_value);
    }
+28 −26
Original line number Diff line number Diff line
@@ -71,8 +71,7 @@ Result<Unit> CheckOverlayable(const LoadedPackage& target_package,
  if (!target_package.DefinesOverlayable()) {
    return (sDefaultPolicies & fulfilled_policies) != 0
               ? Result<Unit>({})
               : Error(
                     "overlay must be preinstalled, signed with the same signature as the target,"
               : Error("overlay must be preinstalled, signed with the same signature as the target,"
                       " or signed with the same signature as the package referenced through"
                       " <overlay-config-signature>.");
  }
@@ -120,25 +119,32 @@ const LoadedPackage* GetPackageAtIndex0(const LoadedArsc& loaded_arsc) {

Result<std::unique_ptr<Asset>> OpenNonAssetFromResource(const ResourceId& resource_id,
                                                        const AssetManager2& asset_manager) {
  auto value = asset_manager.GetResource(resource_id);
  if (!value.has_value()) {
  Res_value value{};
  ResTable_config selected_config{};
  uint32_t flags;
  auto cookie =
      asset_manager.GetResource(resource_id, /* may_be_bag */ false,
                                /* density_override */ 0U, &value, &selected_config, &flags);
  if (cookie == kInvalidCookie) {
    return Error("failed to find resource for id 0x%08x", resource_id);
  }

  if (value->type != Res_value::TYPE_STRING) {
  if (value.dataType != Res_value::TYPE_STRING) {
    return Error("resource for is 0x%08x is not a file", resource_id);
  }

  auto string_pool = asset_manager.GetStringPoolForCookie(value->cookie);
  auto file = string_pool->string8ObjectAt(value->data);
  if (!file.has_value()) {
    return Error("failed to find string for index %d", value->data);
  auto string_pool = asset_manager.GetStringPoolForCookie(cookie);
  size_t len;
  auto file_path16 = string_pool->stringAt(value.data, &len);
  if (file_path16 == nullptr) {
    return Error("failed to find string for index %d", value.data);
  }

  // Load the overlay resource mappings from the file specified using android:resourcesMap.
  auto asset = asset_manager.OpenNonAsset(file->c_str(), Asset::AccessMode::ACCESS_BUFFER);
  auto file_path = String8(String16(file_path16));
  auto asset = asset_manager.OpenNonAsset(file_path.c_str(), Asset::AccessMode::ACCESS_BUFFER);
  if (asset == nullptr) {
    return Error("file \"%s\" not found", file->c_str());
    return Error("file \"%s\" not found", file_path.c_str());
  }

  return asset;
@@ -184,16 +190,16 @@ Result<ResourceMapping> ResourceMapping::CreateResourceMapping(const AssetManage
      return Error(R"(<item> tag missing expected attribute "value")");
    }

    auto target_id_result =
    ResourceId target_id =
        target_am->GetResourceId(*target_resource, "", target_package->GetPackageName());
    if (!target_id_result.has_value()) {
    if (target_id == 0U) {
      log_info.Warning(LogMessage() << "failed to find resource \"" << *target_resource
                                    << "\" in target resources");
      continue;
    }

    // Retrieve the compile-time resource id of the target resource.
    uint32_t target_id = REWRITE_PACKAGE(*target_id_result, target_package_id);
    target_id = REWRITE_PACKAGE(target_id, target_package_id);

    if (overlay_resource->dataType == Res_value::TYPE_STRING) {
      overlay_resource->data += string_pool_offset;
@@ -214,7 +220,7 @@ Result<ResourceMapping> ResourceMapping::CreateResourceMapping(const AssetManage

Result<ResourceMapping> ResourceMapping::CreateResourceMappingLegacy(
    const AssetManager2* target_am, const AssetManager2* overlay_am,
    const LoadedPackage* target_package, const LoadedPackage* overlay_package, LogInfo& log_info) {
    const LoadedPackage* target_package, const LoadedPackage* overlay_package) {
  ResourceMapping resource_mapping;
  const uint8_t target_package_id = target_package->GetPackageId();
  const auto end = overlay_package->end();
@@ -228,15 +234,13 @@ Result<ResourceMapping> ResourceMapping::CreateResourceMappingLegacy(
    // Find the resource with the same type and entry name within the target package.
    const std::string full_name =
        base::StringPrintf("%s:%s", target_package->GetPackageName().c_str(), name->c_str());
    auto target_resource_result = target_am->GetResourceId(full_name);
    if (!target_resource_result.has_value()) {
      log_info.Warning(LogMessage() << "failed to find resource \"" << full_name
                                    << "\" in target resources");
    ResourceId target_resource = target_am->GetResourceId(full_name);
    if (target_resource == 0U) {
      continue;
    }

    // Retrieve the compile-time resource id of the target resource.
    ResourceId target_resource = REWRITE_PACKAGE(*target_resource_result, target_package_id);
    target_resource = REWRITE_PACKAGE(target_resource, target_package_id);
    resource_mapping.AddMapping(target_resource, overlay_resid,
                                false /* rewrite_overlay_reference */);
  }
@@ -343,9 +347,7 @@ Result<ResourceMapping> ResourceMapping::FromApkAssets(const ApkAssets& target_a
    auto& string_pool = (*parser)->get_strings();
    string_pool_data_length = string_pool.bytes();
    string_pool_data.reset(new uint8_t[string_pool_data_length]);

    // Overlays should not be incrementally installed, so calling unsafe_ptr is fine here.
    memcpy(string_pool_data.get(), string_pool.data().unsafe_ptr(), string_pool_data_length);
    memcpy(string_pool_data.get(), string_pool.data(), string_pool_data_length);

    // Offset string indices by the size of the overlay resource table string pool.
    string_pool_offset = overlay_arsc->GetStringPool()->size();
@@ -356,7 +358,7 @@ Result<ResourceMapping> ResourceMapping::FromApkAssets(const ApkAssets& target_a
    // If no file is specified using android:resourcesMap, it is assumed that the overlay only
    // defines resources intended to override target resources of the same type and name.
    resource_mapping = CreateResourceMappingLegacy(&target_asset_manager, &overlay_asset_manager,
                                                   target_pkg, overlay_pkg, log_info);
                                                   target_pkg, overlay_pkg);
  }

  if (!resource_mapping) {
Loading