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 // SP-HAL(Sameprocess-HAL)s are the only vendor libraries that are allowed to be
18 // loaded inside system processes. libEGL_<chipset>.so, libGLESv2_<chipset>.so,
19 // android.hardware.graphics.mapper@2.0-impl.so, etc are SP-HALs.
20 //
21 // This namespace is exclusivly for SP-HALs. When the framework tries to
22 // dynamically load SP-HALs, android_dlopen_ext() is used to explicitly specify
23 // that they should be searched and loaded from this namespace.
24 //
25 // Note that there is no link from the default namespace to this namespace.
26 
27 #include "linkerconfig/namespacebuilder.h"
28 
29 using android::linkerconfig::modules::AsanPath;
30 using android::linkerconfig::modules::Namespace;
31 
32 namespace android {
33 namespace linkerconfig {
34 namespace contents {
35 Namespace BuildSphalNamespace([[maybe_unused]] const Context& ctx) {
36   // Visible to allow use with android_dlopen_ext, and with
37   // android_link_namespaces in libnativeloader.
38   Namespace ns("sphal",
39                /*is_isolated=*/!ctx.IsUnrestrictedSection(),
40                /*is_visible=*/true);
41   ns.AddSearchPath("/odm/${LIB}", AsanPath::WITH_DATA_ASAN);
42   ns.AddSearchPath("/vendor/${LIB}", AsanPath::WITH_DATA_ASAN);
43   ns.AddSearchPath("/vendor/${LIB}/hw", AsanPath::NONE);
44 
45   ns.AddPermittedPath("/odm/${LIB}", AsanPath::WITH_DATA_ASAN);
46   ns.AddPermittedPath("/vendor/${LIB}", AsanPath::WITH_DATA_ASAN);
47   ns.AddPermittedPath("/system/vendor/${LIB}", AsanPath::NONE);
48 
49   if (ctx.IsApexBinaryConfig() && !ctx.IsVndkAvailable()) {
50     // If device is legacy, let Sphal libraries access to system lib path for
51     // VNDK-SP libraries
52     ns.AddSearchPath("/system/${LIB}", AsanPath::WITH_DATA_ASAN);
53     ns.AddPermittedPath("/system/${LIB}", AsanPath::WITH_DATA_ASAN);
54   }
55 
56   if (ctx.IsApexBinaryConfig()) {
57     if (ctx.IsVndkAvailable()) {
58       ns.GetLink("vndk").AddSharedLib(
59           Var("VNDK_SAMEPROCESS_LIBRARIES_VENDOR", ""));
60       ns.GetLink(ctx.GetSystemNamespaceName())
61           .AddSharedLib(Var("LLNDK_LIBRARIES_VENDOR", ""));
62     }
63   } else {
64     // Once in this namespace, access to libraries in /system/lib is restricted.
65     // Only libs listed here can be used. Order is important here as the
66     // namespaces are tried in this order. rs should be before vndk because both
67     // are capable of loading libRS_internal.so
68     if (ctx.IsSystemSection() || ctx.IsUnrestrictedSection()) {
69       ns.GetLink("rs").AddSharedLib("libRS_internal.so");
70     }
71     ns.GetLink(ctx.GetSystemNamespaceName())
72         .AddSharedLib(Var("LLNDK_LIBRARIES_VENDOR", ""));
73     ns.GetLink("vndk").AddSharedLib(
74         Var("VNDK_SAMEPROCESS_LIBRARIES_VENDOR", ""));
75     ns.AddRequires(std::vector{"libneuralnetworks.so"});
76   }
77 
78   return ns;
79 }
80 }  // namespace contents
81 }  // namespace linkerconfig
82 }  // namespace android
83