1 /*
2  * Copyright (C) 2019 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 // This is the default linker namespace for a vendor process (a process started
18 // from /vendor/bin/*).
19 
20 #include "linkerconfig/namespacebuilder.h"
21 
22 #include <android-base/strings.h>
23 
24 #include "linkerconfig/common.h"
25 #include "linkerconfig/environment.h"
26 
27 using android::linkerconfig::modules::AllowAllSharedLibs;
28 using android::linkerconfig::modules::LibProvider;
29 using android::linkerconfig::modules::LibProviders;
30 using android::linkerconfig::modules::Namespace;
31 
32 namespace android {
33 namespace linkerconfig {
34 namespace contents {
BuildVendorDefaultNamespace(const Context & ctx)35 Namespace BuildVendorDefaultNamespace(const Context& ctx) {
36   return BuildVendorNamespace(ctx, "default");
37 }
38 
BuildVendorNamespace(const Context & ctx,const std::string & name)39 Namespace BuildVendorNamespace([[maybe_unused]] const Context& ctx,
40                                const std::string& name) {
41   Namespace ns(name, /*is_isolated=*/true, /*is_visible=*/true);
42 
43   ns.AddSearchPath("/odm/${LIB}");
44   ns.AddSearchPath("/vendor/${LIB}");
45   ns.AddSearchPath("/vendor/${LIB}/hw");
46   ns.AddSearchPath("/vendor/${LIB}/egl");
47 
48   ns.AddPermittedPath("/odm");
49   ns.AddPermittedPath("/vendor");
50   ns.AddPermittedPath("/system/vendor");
51 
52   ns.GetLink("rs").AddSharedLib("libRS_internal.so");
53   ns.AddRequires(base::Split(Var("LLNDK_LIBRARIES_VENDOR", ""), ":"));
54 
55   if (android::linkerconfig::modules::IsVendorVndkVersionDefined()) {
56     ns.GetLink(ctx.GetSystemNamespaceName())
57         .AddSharedLib(Var("SANITIZER_DEFAULT_VENDOR"));
58     ns.GetLink("vndk").AddSharedLib({Var("VNDK_SAMEPROCESS_LIBRARIES_VENDOR"),
59                                      Var("VNDK_CORE_LIBRARIES_VENDOR")});
60     if (android::linkerconfig::modules::IsVndkInSystemNamespace()) {
61       ns.GetLink("vndk_in_system")
62           .AddSharedLib(Var("VNDK_USING_CORE_VARIANT_LIBRARIES"));
63     }
64   }
65 
66   ns.AddRequires(ctx.GetVendorRequireLibs());
67   ns.AddProvides(ctx.GetVendorProvideLibs());
68   return ns;
69 }
70 
BuildVendorSubdirNamespace(const Context &,const std::string & name,const std::string & subdir)71 static Namespace BuildVendorSubdirNamespace(const Context&,
72                                             const std::string& name,
73                                             const std::string& subdir) {
74   Namespace ns(name, /*is_isolated=*/true, /*is_visible=*/true);
75   ns.AddSearchPath("/vendor/${LIB}/" + subdir);
76   ns.AddPermittedPath("/vendor/${LIB}/" + subdir);
77   ns.AddPermittedPath("/system/vendor/${LIB}/" + subdir);
78 
79   // Common requirements for vendor libraries:
80   ns.AddRequires(base::Split(Var("LLNDK_LIBRARIES_VENDOR", ""), ":"));
81   ns.AddRequires(std::vector{":vendorall"});
82   if (android::linkerconfig::modules::IsVendorVndkVersionDefined()) {
83     ns.AddRequires(std::vector{":vndk"});
84   }
85 
86   return ns;
87 }
88 
AddVendorSubdirNamespaceProviders(const Context & ctx,LibProviders & providers)89 void AddVendorSubdirNamespaceProviders(const Context& ctx,
90                                        LibProviders& providers) {
91   // Export known vendor subdirs as linker namespaces
92 
93   // /vendor/lib/mediacas is for CAS HAL to open CAS plugins
94   providers[":mediacas"] = {LibProvider{
95       "mediacas",
96       std::bind(BuildVendorSubdirNamespace, ctx, "mediacas", "mediacas"),
97       AllowAllSharedLibs{},
98   }};
99 
100   // Vendor subdir namespace should be able to access all /vendor/libs.
101   std::string vendor_namespace_name = "default";
102   if (ctx.IsApexBinaryConfig()) {
103     vendor_namespace_name = "vendor";
104   }
105   providers[":vendorall"] = {LibProvider{
106       "vendor",
107       std::bind(BuildVendorNamespace, ctx, vendor_namespace_name),
108       AllowAllSharedLibs{},
109   }};
110 }
111 
112 }  // namespace contents
113 }  // namespace linkerconfig
114 }  // namespace android
115