1 /*
2 * Copyright (C) 2013 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 #define TLOG_TAG "stats-relayer"
18
19 #include <vector>
20
21 #include <inttypes.h>
22 #include <lib/tipc/tipc.h>
23 #include <lk/err_ptr.h>
24 #include <relayer_consts.h>
25 #include <stdio.h>
26 #include <sys/mman.h>
27 #include <trusty/sys/mman.h>
28 #include <trusty/time.h>
29 #include <trusty_log.h>
30 #include <uapi/err.h>
31 #include <uapi/mm.h>
32
33 #include <binder/IBinder.h>
34 #include <binder/RpcServerTrusty.h>
35 #include <binder/RpcTransportTipcTrusty.h>
36
37 #include <android/frameworks/stats/VendorAtom.h>
38 #include <android/trusty/stats/nw/setter/BnStatsSetter.h>
39 #include <android/trusty/stats/tz/BnStats.h>
40 #include <android/trusty/stats/tz/BnStatsSetter.h>
41
42 using namespace android;
43 using binder::Status;
44 using frameworks::stats::VendorAtom;
45 using frameworks::stats::VendorAtomValue;
46
47 class StatsRelayer : public trusty::stats::tz::BnStats {
48 public:
49 class StatsSetterNormalWorld
50 : public trusty::stats::nw::setter::BnStatsSetter {
51 public:
StatsSetterNormalWorld(sp<StatsRelayer> && statsRelayer)52 StatsSetterNormalWorld(sp<StatsRelayer>&& statsRelayer)
53 : mStatsRelayer(std::move(statsRelayer)) {}
54
setInterface(const sp<frameworks::stats::IStats> & istats)55 Status setInterface(const sp<frameworks::stats::IStats>& istats) {
56 assert(mStatsRelayer.get() != nullptr);
57
58 TLOGD("setInterface from Normal-World Consumer\n");
59 // save iStats facet for asynchronous callback
60 mStatsRelayer->mIStats = istats;
61 return Status::ok();
62 };
63
64 private:
65 sp<StatsRelayer> mStatsRelayer;
66 };
67
68 class StatsSetterSecureWorld : public trusty::stats::tz::BnStatsSetter {
69 public:
StatsSetterSecureWorld(sp<StatsRelayer> && statsRelayer)70 StatsSetterSecureWorld(sp<StatsRelayer>&& statsRelayer)
71 : mStatsRelayer(std::move(statsRelayer)) {}
72
setInterface(const sp<trusty::stats::tz::IStats> & istats)73 Status setInterface(const sp<trusty::stats::tz::IStats>& istats) {
74 assert(mStatsRelayer.get() != nullptr);
75
76 TLOGD("setInterface from Secure World Consumer\n");
77 // callback into iStats synchronously if any atoms were previously
78 // received. Swap so we don't try to report the same atoms twice
79 // if we encounter an error.
80 std::vector<VendorAtom> pending;
81 std::swap(pending, mStatsRelayer->mPendingAtoms);
82
83 for (const VendorAtom& atom : pending) {
84 auto rc = istats->reportVendorAtom(atom);
85 if (!rc.isOk()) {
86 TLOGE("reportVendorAtom error %d\n", rc.exceptionCode());
87 return rc;
88 }
89 }
90 return Status::ok();
91 }
92
93 private:
94 sp<StatsRelayer> mStatsRelayer;
95 };
96
StatsRelayer()97 StatsRelayer() : trusty::stats::tz::BnStats(), mPendingAtoms() {}
98
reportVendorAtom(const VendorAtom & vendorAtom)99 Status reportVendorAtom(const VendorAtom& vendorAtom) {
100 TLOGD("reportVendorAtom atomId=%d.\n", vendorAtom.atomId);
101 if (mIStats) {
102 /*
103 * when the normal-world consumer initialises its binder session
104 * with an incoming thread (setMaxIncomingThreads(1)),
105 * its istats facet is accessible after the
106 * setInterface returns.
107 */
108 Status rc = mIStats->reportVendorAtom(vendorAtom);
109 if (!rc.isOk()) {
110 TLOGD("relaying reportVendorAtom failed=%d.\n",
111 rc.exceptionCode());
112 return rc;
113 }
114 }
115
116 /*
117 * if istats endpoint is in secure-world, the callback path
118 * is NOT persistent, hence istats pointer is valid only through the
119 * setInterface call, so we record the vendorAtom so it
120 * can be shared on a setInterface invocation
121 */
122 mPendingAtoms.push_back(vendorAtom);
123 return Status::ok();
124 }
125
126 private:
127 // the normal-world IStats facet, stored for asynchronous callback
128 sp<frameworks::stats::IStats> mIStats;
129
130 // the vendor atom, stored to be shared to the consumer TA
131 // via the synchronous callback
132 std::vector<VendorAtom> mPendingAtoms;
133 };
134
main(void)135 int main(void) {
136 TLOGI("Starting StatsRelayer\n");
137
138 tipc_hset* hset = tipc_hset_create();
139 if (IS_ERR(hset)) {
140 TLOGE("Failed to create handle set (%d)\n", PTR_ERR(hset));
141 return EXIT_FAILURE;
142 }
143
144 auto statsRelayer = sp<StatsRelayer>::make();
145 auto statsSetterNormalWorld =
146 sp<StatsRelayer::StatsSetterNormalWorld>::make(sp(statsRelayer));
147 auto statsSetterSecureWorld =
148 sp<StatsRelayer::StatsSetterSecureWorld>::make(sp(statsRelayer));
149
150 const auto portAcl_TA = RpcServerTrusty::PortAcl{
151 .flags = IPC_PORT_ALLOW_TA_CONNECT,
152 };
153 const auto portAcl_NS = RpcServerTrusty::PortAcl{
154 .flags = IPC_PORT_ALLOW_NS_CONNECT,
155 };
156
157 // message size needs to be large enough to cover all messages sent by
158 // the tests
159 constexpr size_t maxMsgSize = 4096;
160 TLOGE("Creating Relayer (exposing IStats)\n");
161 auto srvIStats = RpcServerTrusty::make(
162 hset, RELAYER_PORT_ISTATS,
163 std::make_shared<const RpcServerTrusty::PortAcl>(portAcl_TA),
164 maxMsgSize);
165 if (srvIStats == nullptr) {
166 return EXIT_FAILURE;
167 }
168 srvIStats->setRootObject(statsRelayer);
169
170 // expose the test port for the NW accessible IStatsSetter interface
171 // trusty::stats::setter::IStatsSetter
172 auto srvIStatsSetterNormalWorld = RpcServerTrusty::make(
173 hset, RELAYER_PORT_ISTATS_SETTER_NORMAL_WORLD,
174 std::make_shared<const RpcServerTrusty::PortAcl>(portAcl_NS),
175 maxMsgSize);
176 if (srvIStatsSetterNormalWorld == nullptr) {
177 return EXIT_FAILURE;
178 }
179 srvIStatsSetterNormalWorld->setRootObject(statsSetterNormalWorld);
180
181 auto srvIStatsSetterSecureWorld = RpcServerTrusty::make(
182 hset, RELAYER_PORT_ISTATS_SETTER_SECURE_WORLD,
183 std::make_shared<const RpcServerTrusty::PortAcl>(portAcl_TA),
184 maxMsgSize);
185 if (srvIStatsSetterSecureWorld == nullptr) {
186 return EXIT_FAILURE;
187 }
188 srvIStatsSetterSecureWorld->setRootObject(statsSetterSecureWorld);
189
190 int rc = tipc_run_event_loop(hset);
191
192 TLOGE("stats-relayer service died\n");
193
194 return rc;
195 }
196