Merge "Trim root path from output"
diff --git a/contents/include/linkerconfig/namespacebuilder.h b/contents/include/linkerconfig/namespacebuilder.h
index d621325..eeb2cbf 100644
--- a/contents/include/linkerconfig/namespacebuilder.h
+++ b/contents/include/linkerconfig/namespacebuilder.h
@@ -23,12 +23,16 @@
 namespace linkerconfig {
 namespace contents {
 
+enum class VndkUserPartition {
+  Vendor,
+  Product,
+};
+
 typedef modules::Namespace NamespaceBuilder(const Context& ctx);
 
 NamespaceBuilder BuildSystemDefaultNamespace;
 NamespaceBuilder BuildSphalNamespace;
 NamespaceBuilder BuildRsNamespace;
-NamespaceBuilder BuildVndkNamespace;
 NamespaceBuilder BuildVendorDefaultNamespace;
 NamespaceBuilder BuildSystemNamespace;
 NamespaceBuilder BuildVndkInSystemNamespace;
@@ -37,6 +41,9 @@
 NamespaceBuilder BuildPostInstallNamespace;
 NamespaceBuilder BuildRecoveryDefaultNamespace;
 
+modules::Namespace BuildVndkNamespace(const Context& ctx,
+                                      VndkUserPartition vndk_user);
+
 modules::Namespace BuildArtNamespace(const Context& ctx,
                                      const modules::ApexInfo& apex_info);
 
diff --git a/contents/namespace/vndk.cc b/contents/namespace/vndk.cc
index 9f36bcc..ef390b8 100644
--- a/contents/namespace/vndk.cc
+++ b/contents/namespace/vndk.cc
@@ -16,9 +16,8 @@
 
 // This namespace is exclusively for vndk-sp libs.
 
-#include "linkerconfig/namespacebuilder.h"
-
 #include "linkerconfig/environment.h"
+#include "linkerconfig/namespacebuilder.h"
 
 using android::linkerconfig::modules::AsanPath;
 using android::linkerconfig::modules::Namespace;
@@ -26,41 +25,51 @@
 namespace android {
 namespace linkerconfig {
 namespace contents {
-Namespace BuildVndkNamespace([[maybe_unused]] const Context& ctx) {
+Namespace BuildVndkNamespace([[maybe_unused]] const Context& ctx,
+                             VndkUserPartition vndk_user) {
   bool is_system_section = ctx.IsSystemSection() || ctx.IsApexBinaryConfig();
-  bool is_product_section = ctx.IsProductSection();
   bool is_vndklite = ctx.IsVndkliteConfig();
+  // In the system section, we need to have an additional vndk namespace for
+  // product apps. We must have a different name "vndk_product" for this
+  // namespace. "vndk_product" namespace is used only from the native_loader for
+  // product apps.
+  const char* name;
+  if (is_system_section && vndk_user == VndkUserPartition::Product) {
+    name = "vndk_product";
+  } else {
+    name = "vndk";
+  }
+
   // Isolated but visible when used in the [system] section to allow links to be
   // created at runtime, e.g. through android_link_namespaces in
   // libnativeloader. Otherwise it isn't isolated, so visibility doesn't matter.
-  Namespace ns("vndk",
+  Namespace ns(name,
                /*is_isolated=*/is_system_section,
                /*is_visible=*/is_system_section);
 
-  if (is_system_section) {
-    // It is linked from vendor HAL. It must use vendor vndk libs.
-    ns.AddSearchPath("/odm/${LIB}/vndk-sp", AsanPath::WITH_DATA_ASAN);
-    ns.AddSearchPath("/vendor/${LIB}/vndk-sp", AsanPath::WITH_DATA_ASAN);
-    ns.AddSearchPath(
-        "/apex/com.android.vndk.v" + Var("VENDOR_VNDK_VERSION") + "/${LIB}",
-        AsanPath::SAME_PATH);
-  } else if (is_product_section) {
-    ns.AddSearchPath("/product/${LIB}/vndk-sp", AsanPath::WITH_DATA_ASAN);
-    ns.AddSearchPath("/product/${LIB}/vndk", AsanPath::WITH_DATA_ASAN);
-    ns.AddSearchPath(
-        "/apex/com.android.vndk.v" + Var("PRODUCT_VNDK_VERSION") + "/${LIB}",
-        AsanPath::SAME_PATH);
+  std::vector<std::string> lib_paths;
+  std::vector<std::string> vndk_paths;
+  std::string vndk_version;
+  if (vndk_user == VndkUserPartition::Product) {
+    lib_paths = {"/product/${LIB}/"};
+    vndk_version = Var("PRODUCT_VNDK_VERSION");
   } else {
-    ns.AddSearchPath("/odm/${LIB}/vndk-sp", AsanPath::WITH_DATA_ASAN);
-    ns.AddSearchPath("/odm/${LIB}/vndk", AsanPath::WITH_DATA_ASAN);
-    ns.AddSearchPath("/vendor/${LIB}/vndk-sp", AsanPath::WITH_DATA_ASAN);
-    ns.AddSearchPath("/vendor/${LIB}/vndk", AsanPath::WITH_DATA_ASAN);
-    ns.AddSearchPath(
-        "/apex/com.android.vndk.v" + Var("VENDOR_VNDK_VERSION") + "/${LIB}",
-        AsanPath::SAME_PATH);
+    // default for vendor
+    lib_paths = {"/odm/${LIB}/", "/vendor/${LIB}/"};
+    vndk_version = Var("VENDOR_VNDK_VERSION");
   }
 
-  if (is_system_section) {
+  for (const auto& lib_path : lib_paths) {
+    ns.AddSearchPath(lib_path + "vndk-sp", AsanPath::WITH_DATA_ASAN);
+    if (!is_system_section) {
+      ns.AddSearchPath(lib_path + "vndk", AsanPath::WITH_DATA_ASAN);
+    }
+  }
+  ns.AddSearchPath("/apex/com.android.vndk.v" + vndk_version + "/${LIB}",
+                   AsanPath::SAME_PATH);
+
+  if (is_system_section && vndk_user == VndkUserPartition::Vendor) {
+    // It is for vendor sp-hal
     ns.AddPermittedPath("/odm/${LIB}/hw", AsanPath::WITH_DATA_ASAN);
     ns.AddPermittedPath("/odm/${LIB}/egl", AsanPath::WITH_DATA_ASAN);
     ns.AddPermittedPath("/vendor/${LIB}/hw", AsanPath::WITH_DATA_ASAN);
@@ -76,10 +85,9 @@
         AsanPath::SAME_PATH);
   }
 
-  // For the [vendor] section, the links should be identical to that of the
+  // For the non-system section, the links should be identical to that of the
   // 'vndk_in_system' namespace, except the links to 'default' and 'vndk_in_system'.
-
-  if (is_product_section) {
+  if (vndk_user == VndkUserPartition::Product) {
     ns.GetLink(ctx.GetSystemNamespaceName())
         .AddSharedLib({Var("LLNDK_LIBRARIES_PRODUCT")});
   } else {
@@ -89,10 +97,12 @@
 
   if (!is_vndklite) {
     if (is_system_section) {
-      // The "vndk" namespace links to the system namespace for LLNDK libs above
-      // and links to "sphal" namespace for vendor libs. The ordering matters;
-      // the system namespace has higher priority than the "sphal" namespace.
-      ns.GetLink("sphal").AllowAllSharedLibs();
+      if (vndk_user == VndkUserPartition::Vendor) {
+        // The "vndk" namespace links to the system namespace for LLNDK libs above
+        // and links to "sphal" namespace for vendor libs. The ordering matters;
+        // the system namespace has higher priority than the "sphal" namespace.
+        ns.GetLink("sphal").AllowAllSharedLibs();
+      }
     } else {
       // [vendor] or [product] section
       ns.GetLink("default").AllowAllSharedLibs();
diff --git a/contents/section/apexdefault.cc b/contents/section/apexdefault.cc
index 42948c5..041fe80 100644
--- a/contents/section/apexdefault.cc
+++ b/contents/section/apexdefault.cc
@@ -43,7 +43,7 @@
   // namespace(s)
   if (apex_info.name == "com.android.media.swcodec") {
     namespaces.emplace_back(BuildSphalNamespace(ctx));
-    namespaces.emplace_back(BuildVndkNamespace(ctx));
+    namespaces.emplace_back(BuildVndkNamespace(ctx, VndkUserPartition::Vendor));
   }
 
   return BuildSection(ctx, apex_info.name, std::move(namespaces), {});
diff --git a/contents/section/product.cc b/contents/section/product.cc
index 68da270..ab7b25a 100644
--- a/contents/section/product.cc
+++ b/contents/section/product.cc
@@ -35,7 +35,7 @@
   std::vector<Namespace> namespaces;
 
   namespaces.emplace_back(BuildProductDefaultNamespace(ctx));
-  namespaces.emplace_back(BuildVndkNamespace(ctx));
+  namespaces.emplace_back(BuildVndkNamespace(ctx, VndkUserPartition::Product));
   namespaces.emplace_back(BuildSystemNamespace(ctx));
 
   if (android::linkerconfig::modules::IsVndkInSystemNamespace()) {
diff --git a/contents/section/system.cc b/contents/section/system.cc
index cf190e6..31ba6de 100644
--- a/contents/section/system.cc
+++ b/contents/section/system.cc
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "linkerconfig/sectionbuilder.h"
-
 #include "linkerconfig/common.h"
 #include "linkerconfig/context.h"
+#include "linkerconfig/environment.h"
 #include "linkerconfig/namespacebuilder.h"
 #include "linkerconfig/section.h"
+#include "linkerconfig/sectionbuilder.h"
 
 using android::linkerconfig::contents::SectionType;
 using android::linkerconfig::modules::Namespace;
@@ -36,7 +36,11 @@
   if (ctx.IsVndkAvailable()) {
     namespaces.emplace_back(BuildSphalNamespace(ctx));
     namespaces.emplace_back(BuildRsNamespace(ctx));
-    namespaces.emplace_back(BuildVndkNamespace(ctx));
+    namespaces.emplace_back(BuildVndkNamespace(ctx, VndkUserPartition::Vendor));
+    if (android::linkerconfig::modules::IsProductVndkVersionDefined()) {
+      namespaces.emplace_back(
+          BuildVndkNamespace(ctx, VndkUserPartition::Product));
+    }
   }
   return BuildSection(ctx,
                       "system",
diff --git a/contents/section/vendor.cc b/contents/section/vendor.cc
index 8a0d755..fdff758 100644
--- a/contents/section/vendor.cc
+++ b/contents/section/vendor.cc
@@ -41,7 +41,7 @@
   // section. Instead they (except libraries from APEX) will be loaded from
   // default namespace, so VNDK libraries can access private platform libraries.
   if (!is_vndklite) {
-    namespaces.emplace_back(BuildVndkNamespace(ctx));
+    namespaces.emplace_back(BuildVndkNamespace(ctx, VndkUserPartition::Vendor));
     namespaces.emplace_back(BuildSystemNamespace(ctx));
   }
 
diff --git a/contents/tests/configuration/vndk_test.cc b/contents/tests/configuration/vndk_test.cc
index a7b9ee9..c9045e2 100644
--- a/contents/tests/configuration/vndk_test.cc
+++ b/contents/tests/configuration/vndk_test.cc
@@ -50,7 +50,7 @@
 TEST(vndk_namespace, vndk_ext) {
   Context vendor_context;
   vendor_context.SetCurrentSection(SectionType::Vendor);
-  auto vndk_ns = BuildVndkNamespace(vendor_context);
+  auto vndk_ns = BuildVndkNamespace(vendor_context, VndkUserPartition::Vendor);
 
   auto libvndk = "libvndk.so";
   auto libvndksp = "libvndksp.so";
diff --git a/main.cc b/main.cc
index de7ba23..4f08cf4 100644
--- a/main.cc
+++ b/main.cc
@@ -310,6 +310,11 @@
   LoadVariables(args);
   Context ctx = GetContext(args);
 
+  // when exec'ed from init, this is 0x0077, which makes the subdirectories
+  // inaccessible for others. set umask to 0x0022 so that they can be
+  // accessible.
+  umask(0x0022);
+
   if (args.is_recovery) {
     ExitOnFailure(
         GenerateRecoveryLinkerConfiguration(ctx, args.target_directory));