OSDN Git Service

Maintain directory to section map in manual
authorKiyoung Kim <kiyoungkim@google.com>
Tue, 24 Sep 2019 06:35:54 +0000 (15:35 +0900)
committerKiyoung Kim <kiyoungkim@google.com>
Tue, 24 Sep 2019 06:45:26 +0000 (15:45 +0900)
Order of directory to section map was maintained automatically in
reverse dictionary order. However, this made a problem that /vendor/bin
comes first which caused sepolicy errors from system binaries. To
resolve this kinds of issues, order of map should be maintained in
manual.

Bug: 141524071
Test: m -j && atest passed
Test: Tested from Cuttlefish
Change-Id: Ibb4364e15eee63ac0af30c20e808847f2cdcf1e8

13 files changed:
contents/configuration/baseconfig.cc
contents/configuration/legacy.cc
contents/section/legacy.cc
contents/section/postinstall.cc
contents/section/system.cc
contents/section/unrestricted.cc
contents/section/vendor.cc
modules/configuration.cc
modules/include/linkerconfig/configuration.h
modules/include/linkerconfig/section.h
modules/section.cc
modules/tests/configuration_test.cc
modules/tests/section_test.cc

index 63b3713..0e49b15 100644 (file)
  */
 
 #include "linkerconfig/baseconfig.h"
-
 #include "linkerconfig/sectionbuilder.h"
 
+using android::linkerconfig::modules::DirToSection;
 using android::linkerconfig::modules::Section;
 
+namespace {
+const std::vector<DirToSection> kDirToSection = {
+    {"/system/bin/", "system"},
+    {"/system/xbin/", "system"},
+    {"/@{SYSTEM_EXT:system_ext}/bin/", "system"},
+    {"/@{PRODUCT:product}/bin/", "system"},
+
+    {"/odm/bin/", "vendor"},
+    {"/vendor/bin/", "vendor"},
+    {"/data/nativetest/odm", "vendor"},
+    {"/data/nativetest64/odm", "vendor"},
+    {"/data/benchmarktest/odm", "vendor"},
+    {"/data/benchmarktest64/odm", "vendor"},
+    {"/data/nativetest/vendor", "vendor"},
+    {"/data/nativetest64/vendor", "vendor"},
+    {"/data/benchmarktest/vendor", "vendor"},
+    {"/data/benchmarktest64/vendor", "vendor"},
+
+    {"/data/nativetest/unrestricted", "unrestricted"},
+    {"/data/nativetest64/unrestricted", "unrestricted"},
+
+    // TODO(b/123864775): Ensure tests are run from /data/nativetest{,64} or (if
+    // necessary) the unrestricted subdirs above. Then clean this up.
+    {"/data/local/tmp", "unrestricted"},
+
+    {"/postinstall", "postinstall"},
+    // Fallback entry to provide APEX namespace lookups for binaries anywhere
+    // else. This must be last.
+    {"/data", "system"},
+};
+}  // namespace
+
 namespace android {
 namespace linkerconfig {
 namespace contents {
@@ -32,7 +64,8 @@ android::linkerconfig::modules::Configuration CreateBaseConfiguration() {
   sections.emplace_back(BuildUnrestrictedSection(current_context));
   sections.emplace_back(BuildPostInstallSection(current_context));
 
-  return android::linkerconfig::modules::Configuration(std::move(sections));
+  return android::linkerconfig::modules::Configuration(std::move(sections),
+                                                       kDirToSection);
 }
 }  // namespace contents
 }  // namespace linkerconfig
index b15be16..aa9a1b6 100644 (file)
  */
 
 #include "linkerconfig/legacy.h"
-
 #include "linkerconfig/sectionbuilder.h"
 
+using android::linkerconfig::modules::DirToSection;
 using android::linkerconfig::modules::Section;
 
+namespace {
+const std::vector<DirToSection> kDirToSection = {
+    // All binaries gets the same configuration 'legacy'
+    {"/system", "legacy"},
+    {"/product", "legacy"},
+    {"/vendor", "legacy"},
+    {"/odm", "legacy"},
+    {"/sbin", "legacy"},
+    // Except for /postinstall, where only /system and /product are searched
+    {"/postinstall", "postinstall"},
+    // Fallback entry to provide APEX namespace lookups for binaries anywhere
+    // else. This must be last.
+    {"/data", "legacy"},
+};
+}  // namespace
+
 namespace android {
 namespace linkerconfig {
 namespace contents {
@@ -30,7 +46,8 @@ android::linkerconfig::modules::Configuration CreateLegacyConfiguration() {
   sections.emplace_back(BuildLegacySection(current_context));
   sections.emplace_back(BuildPostInstallSection(current_context));
 
-  return android::linkerconfig::modules::Configuration(std::move(sections));
+  return android::linkerconfig::modules::Configuration(std::move(sections),
+                                                       kDirToSection);
 }
 }  // namespace contents
 }  // namespace linkerconfig
index a8d74fd..76144df 100644 (file)
@@ -23,16 +23,6 @@ using android::linkerconfig::contents::SectionType;
 using android::linkerconfig::modules::Namespace;
 using android::linkerconfig::modules::Section;
 
-namespace {
-const std::vector<std::string> kLegacyBinaryPath = {
-    "/system",
-    "/product",
-    "/vendor",
-    "/odm",
-    "/sbin",
-};
-}  // namespace
-
 namespace android {
 namespace linkerconfig {
 namespace contents {
@@ -47,7 +37,7 @@ Section BuildLegacySection(Context& ctx) {
   namespaces.emplace_back(BuildResolvNamespace(ctx));
   namespaces.emplace_back(BuildNeuralNetworksNamespace(ctx));
 
-  return Section("legacy", kLegacyBinaryPath, std::move(namespaces));
+  return Section("legacy", std::move(namespaces));
 }
 }  // namespace contents
 }  // namespace linkerconfig
index 8c51203..0f54c45 100644 (file)
@@ -22,10 +22,6 @@ using android::linkerconfig::contents::SectionType;
 using android::linkerconfig::modules::Namespace;
 using android::linkerconfig::modules::Section;
 
-namespace {
-const std::vector<std::string> kBinaryPath = {"/postinstall"};
-}  // namespace
-
 namespace android {
 namespace linkerconfig {
 namespace contents {
@@ -35,7 +31,7 @@ Section BuildPostInstallSection(Context& ctx) {
 
   namespaces.emplace_back(BuildPostInstallNamespace(ctx));
 
-  return Section("postinstall", kBinaryPath, std::move(namespaces));
+  return Section("postinstall", std::move(namespaces));
 }
 }  // namespace contents
 }  // namespace linkerconfig
index db1ed7c..df4c99c 100644 (file)
@@ -24,16 +24,6 @@ using android::linkerconfig::contents::SectionType;
 using android::linkerconfig::modules::Namespace;
 using android::linkerconfig::modules::Section;
 
-namespace {
-const std::vector<std::string> kBinaryPath = {
-    "/system/bin/",
-    "/system/xbin/",
-    "/@{SYSTEM_EXT:system_ext}/bin/",
-    "/@{PRODUCT:product}/bin/",
-    "/data",
-};
-}  // namespace
-
 namespace android {
 namespace linkerconfig {
 namespace contents {
@@ -51,7 +41,7 @@ Section BuildSystemSection(Context& ctx) {
   namespaces.emplace_back(BuildVndkNamespace(ctx));
   namespaces.emplace_back(BuildNeuralNetworksNamespace(ctx));
 
-  return Section("system", kBinaryPath, std::move(namespaces));
+  return Section("system", std::move(namespaces));
 }
 }  // namespace contents
 }  // namespace linkerconfig
index e79c659..efcc69a 100644 (file)
@@ -24,14 +24,6 @@ using android::linkerconfig::contents::SectionType;
 using android::linkerconfig::modules::Namespace;
 using android::linkerconfig::modules::Section;
 
-namespace {
-const std::vector<std::string> kBinaryPath = {
-    "/data/nativetest/unrestricted",
-    "/data/nativetest64/unrestricted",
-    "/data/local/tmp",
-};
-}  // namespace
-
 namespace android {
 namespace linkerconfig {
 namespace contents {
@@ -46,7 +38,7 @@ Section BuildUnrestrictedSection(Context& ctx) {
   namespaces.emplace_back(BuildResolvNamespace(ctx));
   namespaces.emplace_back(BuildNeuralNetworksNamespace(ctx));
 
-  return Section("unrestricted", kBinaryPath, std::move(namespaces));
+  return Section("unrestricted", std::move(namespaces));
 }
 }  // namespace contents
 }  // namespace linkerconfig
index 54517ad..ce264d1 100644 (file)
@@ -24,21 +24,6 @@ using android::linkerconfig::contents::SectionType;
 using android::linkerconfig::modules::Namespace;
 using android::linkerconfig::modules::Section;
 
-namespace {
-const std::vector<std::string> kBinaryPath = {
-    "/odm/bin/",
-    "/vendor/bin/",
-    "/data/nativetest/odm",
-    "/data/nativetest64/odm",
-    "/data/benchmarktest/odm",
-    "/data/benchmarktest64/odm",
-    "/data/nativetest/vendor",
-    "/data/nativetest64/vendor",
-    "/data/benchmarktest/vendor",
-    "/data/benchmarktest64/vendor",
-};
-}  // namespace
-
 namespace android {
 namespace linkerconfig {
 namespace contents {
@@ -56,7 +41,7 @@ Section BuildVendorSection(Context& ctx) {
     namespaces.emplace_back(BuildVndkInSystemNamespace(ctx));
   }
 
-  return Section("vendor", kBinaryPath, std::move(namespaces));
+  return Section("vendor", std::move(namespaces));
 }
 }  // namespace contents
 }  // namespace linkerconfig
index c55d965..555ca99 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "linkerconfig/configuration.h"
 
+#include <unordered_map>
+
 #include "linkerconfig/log.h"
 #include "linkerconfig/variables.h"
 
@@ -23,29 +25,22 @@ namespace android {
 namespace linkerconfig {
 namespace modules {
 void Configuration::WriteConfig(ConfigWriter& writer) {
-  std::map<std::string, std::string> resolved_binary_paths;
+  std::unordered_map<std::string, std::string> resolved_dirs;
 
-  for (auto& section : sections_) {
-    auto binary_paths = section.GetBinaryPaths();
-    auto section_name = section.GetName();
-    for (auto& path : binary_paths) {
-      auto resolved_path = Variables::ResolveVariables(path);
-      auto it = resolved_binary_paths.find(resolved_path);
-      if (it != resolved_binary_paths.end()) {
-        LOG(WARNING) << "Binary path " << path << " already found from "
-                     << it->second << ". Path from " << section_name
-                     << " will be ignored.";
-      } else {
-        resolved_binary_paths[resolved_path] = section_name;
-      }
-    }
-  }
+  for (auto& dir_to_section : dir_to_section_list_) {
+    auto resolved_dir = Variables::ResolveVariables(dir_to_section.first);
+
+    auto it = resolved_dirs.find(resolved_dir);
 
-  // Navigate in reverse order to keep sub directories on top of parent directory
-  for (auto it = resolved_binary_paths.rbegin();
-       it != resolved_binary_paths.rend();
-       it++) {
-    writer.WriteLine("dir.%s = %s", it->second.c_str(), it->first.c_str());
+    if (it != resolved_dirs.end()) {
+      LOG(WARNING) << "Binary path " << resolved_dir << " already found from "
+                   << it->second << ". Path from " << dir_to_section.second
+                   << " will be ignored.";
+    } else {
+      resolved_dirs[resolved_dir] = dir_to_section.second;
+      writer.WriteLine(
+          "dir.%s = %s", dir_to_section.second.c_str(), resolved_dir.c_str());
+    }
   }
 
   for (auto& section : sections_) {
index 8cff279..e264f99 100644 (file)
@@ -16,6 +16,7 @@
 #pragma once
 
 #include <string>
+#include <utility>
 #include <vector>
 
 #include "linkerconfig/configwriter.h"
 namespace android {
 namespace linkerconfig {
 namespace modules {
+using DirToSection = std::pair<std::string, std::string>;
+
 class Configuration {
  public:
-  explicit Configuration(std::vector<Section> sections)
-      : sections_(std::move(sections)) {
+  explicit Configuration(std::vector<Section> sections,
+                         std::vector<DirToSection> dir_to_sections)
+      : sections_(std::move(sections)),
+        dir_to_section_list_(std::move(dir_to_sections)) {
   }
   Configuration(const Configuration&) = delete;
   Configuration(Configuration&&) = default;
@@ -39,6 +44,7 @@ class Configuration {
 
  private:
   std::vector<Section> sections_;
+  std::vector<DirToSection> dir_to_section_list_;
 };
 }  // namespace modules
 }  // namespace linkerconfig
index a511f72..5aecbe7 100644 (file)
@@ -29,11 +29,8 @@ namespace modules {
 
 class Section {
  public:
-  Section(std::string name, std::vector<std::string> binary_paths,
-          std::vector<Namespace> namespaces)
-      : name_(std::move(name)),
-        binary_paths_(std::move(binary_paths)),
-        namespaces_(std::move(namespaces)) {
+  Section(std::string name, std::vector<Namespace> namespaces)
+      : name_(std::move(name)), namespaces_(std::move(namespaces)) {
   }
 
   Section(const Section&) = delete;
@@ -48,7 +45,6 @@ class Section {
 
  private:
   const std::string name_;
-  std::vector<std::string> binary_paths_;
   std::vector<Namespace> namespaces_;
 };
 }  // namespace modules
index 837f13f..6929c43 100644 (file)
@@ -45,10 +45,6 @@ void Section::WriteConfig(ConfigWriter& writer) {
   }
 }
 
-std::vector<std::string> Section::GetBinaryPaths() {
-  return binary_paths_;
-}
-
 Namespace* Section::GetNamespace(const std::string& namespace_name) {
   for (auto& ns : namespaces_) {
     if (ns.GetName() == namespace_name) {
index 7042aab..abd2505 100644 (file)
 using namespace android::linkerconfig::modules;
 
 constexpr const char* kExpectedConfiguration =
-    R"(dir.vendor = /vendor/bin
+    R"(dir.system = /system/bin
 dir.system = /system/xbin
-dir.vendor = /system/bin/vendor
-dir.system = /system/bin
-dir.vendor = /product/bin/vendor
 dir.system = /product/bin
 dir.vendor = /odm/bin
+dir.vendor = /vendor/bin
+dir.vendor = /system/bin/vendor
+dir.vendor = /product/bin/vendor
 [system]
 additional.namespaces = namespace1,namespace2
 namespace.default.isolated = false
@@ -97,13 +97,19 @@ TEST(linkerconfig_configuration, generate_configuration) {
   Variables::AddValue("PRODUCT", "product");
   std::vector<Section> sections;
 
-  std::vector<Namespace> system_namespaces;
-  std::vector<std::string> system_binary_path = {
-      "/system/bin",
-      "/system/xbin",
-      "/@{PRODUCT}/bin",
+  std::vector<DirToSection> dir_to_sections = {
+      {"/system/bin", "system"},
+      {"/system/xbin", "system"},
+      {"/@{PRODUCT}/bin", "system"},
+      {"/odm/bin", "vendor"},
+      {"/vendor/bin", "vendor"},
+      {"/system/bin/vendor", "vendor"},
+      {"/product/bin/vendor", "vendor"},
+      {"/product/bin", "vendor"},
   };
 
+  std::vector<Namespace> system_namespaces;
+
   system_namespaces.emplace_back(CreateNamespaceWithLinks(
       "default", false, false, "namespace1", "namespace2"));
   system_namespaces.emplace_back(
@@ -111,28 +117,21 @@ TEST(linkerconfig_configuration, generate_configuration) {
   system_namespaces.emplace_back(
       CreateNamespaceWithPaths("namespace2", false, false));
 
-  Section system_section(
-      "system", system_binary_path, std::move(system_namespaces));
+  Section system_section("system", std::move(system_namespaces));
   sections.emplace_back(std::move(system_section));
 
   std::vector<Namespace> vendor_namespaces;
-  std::vector<std::string> vendor_binary_path = {"/odm/bin",
-                                                 "/vendor/bin",
-                                                 "/system/bin/vendor",
-                                                 "/product/bin/vendor",
-                                                 "/product/bin"};
 
   vendor_namespaces.emplace_back(
       CreateNamespaceWithPaths("default", false, false));
 
-  Section vendor_section(
-      "vendor", vendor_binary_path, std::move(vendor_namespaces));
+  Section vendor_section("vendor", std::move(vendor_namespaces));
   sections.emplace_back(std::move(vendor_section));
 
-  Configuration conf(std::move(sections));
+  Configuration conf(std::move(sections), dir_to_sections);
 
   android::linkerconfig::modules::ConfigWriter writer;
   conf.WriteConfig(writer);
 
-  ASSERT_EQ(writer.ToString(), kExpectedConfiguration);
+  ASSERT_EQ(kExpectedConfiguration, writer.ToString());
 }
\ No newline at end of file
index 6b6e529..854c3c0 100644 (file)
@@ -105,13 +105,11 @@ TEST(linkerconfig_section, section_with_namespaces) {
                                                    "default", "namespace2"));
   namespaces.emplace_back(CreateNamespaceWithPaths("namespace2", false, false));
 
-  std::vector<std::string> empty_list;
-
-  Section section("test_section", empty_list, std::move(namespaces));
+  Section section("test_section", std::move(namespaces));
 
   section.WriteConfig(writer);
   auto config = writer.ToString();
-  ASSERT_EQ(config, kSectionWithNamespacesExpectedResult);
+  ASSERT_EQ(kSectionWithNamespacesExpectedResult, config);
 }
 
 TEST(linkerconfig_section, section_with_one_namespace) {
@@ -120,20 +118,8 @@ TEST(linkerconfig_section, section_with_one_namespace) {
   std::vector<Namespace> namespaces;
   namespaces.emplace_back(CreateNamespaceWithPaths("default", false, false));
 
-  std::vector<std::string> empty_list;
-
-  Section section("test_section", empty_list, std::move(namespaces));
+  Section section("test_section", std::move(namespaces));
   section.WriteConfig(writer);
   auto config = writer.ToString();
-  ASSERT_EQ(config, kSectionWithOneNamespaceExpectedResult);
-}
-
-TEST(linkerconfig_section, binary_paths) {
-  std::vector<std::string> binary_paths = {"/root/a", "/root/a/b", "/root/b"};
-  std::vector<Namespace> empty_namespace;
-  Section section("test_section", binary_paths, std::move(empty_namespace));
-
-  auto section_binary_paths = section.GetBinaryPaths();
-
-  ASSERT_EQ(section_binary_paths, binary_paths);
+  ASSERT_EQ(kSectionWithOneNamespaceExpectedResult, config);
 }
\ No newline at end of file