1 /*
2  * Copyright (C) 2017 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 #pragma once
18 
19 #include <gtest/gtest_prod.h>
20 #include <src/uid_data.pb.h>
21 #include <stdio.h>
22 #include <utils/RefBase.h>
23 #include <utils/String16.h>
24 
25 #include <list>
26 #include <mutex>
27 #include <set>
28 #include <string>
29 #include <unordered_map>
30 
31 #include "config/ConfigKey.h"
32 #include "packages/PackageInfoListener.h"
33 #include "stats_util.h"
34 
35 using namespace android;
36 using namespace std;
37 
38 using android::util::ProtoOutputStream;
39 
40 namespace android {
41 namespace os {
42 namespace statsd {
43 
44 struct AppData {
45     int64_t versionCode;
46     string versionString;
47     string installer;
48     bool deleted;
49     string certificateHash;
50 
51     // Empty constructor needed for unordered map.
AppDataAppData52     AppData() {
53     }
54 
AppDataAppData55     AppData(const int64_t v, const string& versionString, const string& installer,
56             const string& certificateHash)
57         : versionCode(v),
58           versionString(versionString),
59           installer(installer),
60           deleted(false),
61           certificateHash(certificateHash){};
62 };
63 
64 // When calling appendUidMap, we retrieve all the ChangeRecords since the last
65 // timestamp we called appendUidMap for this configuration key.
66 struct ChangeRecord {
67     const bool deletion;
68     const int64_t timestampNs;
69     const string package;
70     const int32_t uid;
71     const int64_t version;
72     const int64_t prevVersion;
73     const string versionString;
74     const string prevVersionString;
75 
ChangeRecordChangeRecord76     ChangeRecord(const bool isDeletion, int64_t timestampNs, const string& package,
77                  const int32_t uid, int64_t version, const string& versionString,
78                  const int64_t prevVersion, const string& prevVersionString)
79         : deletion(isDeletion),
80           timestampNs(timestampNs),
81           package(package),
82           uid(uid),
83           version(version),
84           prevVersion(prevVersion),
85           versionString(versionString),
86           prevVersionString(prevVersionString) {
87     }
88 };
89 
90 struct UidMapOptions {
91     bool includeVersionStrings = false;
92     bool includeInstaller = false;
93     uint8_t truncatedCertificateHashSize = 0;
94     bool omitSystemUids = false;
95     bool omitUnusedUids = false;
96     set<int32_t> usedUids = {};
97     set<string> allowlistedPackages = {};
98 };
99 
100 const unsigned int kBytesChangeRecord = sizeof(struct ChangeRecord);
101 
102 // UidMap keeps track of what the corresponding app name (APK name) and version code for every uid
103 // at any given moment. This map must be updated by StatsCompanionService.
104 class UidMap : public virtual RefBase {
105 public:
106     UidMap();
107     ~UidMap();
108     static const std::map<std::string, uint32_t> sAidToUidMapping;
109 
110     static sp<UidMap> getInstance();
111 
112     void updateMap(const int64_t timestamp, const UidData& uidData);
113 
114     void updateApp(const int64_t timestamp, const string& appName, const int32_t uid,
115                    const int64_t versionCode, const string& versionString, const string& installer,
116                    const vector<uint8_t>& certificateHash);
117     void removeApp(const int64_t timestamp, const string& app, const int32_t uid);
118 
119     // Returns true if the given uid contains the specified app (eg. com.google.android.gms).
120     bool hasApp(int uid, const string& packageName) const;
121 
122     // Returns the app names from uid.
123     std::set<string> getAppNamesFromUid(int32_t uid, bool returnNormalized) const;
124 
125     int64_t getAppVersion(int uid, const string& packageName) const;
126 
127     // Helper for debugging contents of this uid map. Can be triggered with:
128     // adb shell cmd stats print-uid-map [--with_certificate_hash]
129     void printUidMap(int outFd, bool includeCertificateHash) const;
130 
131     // Command for indicating to the map that StatsLogProcessor should be notified if an app is
132     // updated. This allows metric producers and managers to distinguish when the same uid or app
133     // represents a different version of an app.
134     void setListener(const wp<PackageInfoListener>& listener);
135 
136     // Informs uid map that a config is added/updated. Used for keeping mConfigKeys up to date.
137     void OnConfigUpdated(const ConfigKey& key);
138 
139     // Informs uid map that a config is removed. Used for keeping mConfigKeys up to date.
140     void OnConfigRemoved(const ConfigKey& key);
141 
142     void assignIsolatedUid(int isolatedUid, int parentUid);
143     void removeIsolatedUid(int isolatedUid);
144 
145     // Returns the host uid if it exists. Otherwise, returns the same uid that was passed-in.
146     virtual int getHostUidOrSelf(int uid) const;
147 
148     // Gets all snapshots and changes that have occurred since the last output.
149     // If every config key has received a change or snapshot record, then this
150     // record is deleted.
151     void appendUidMap(int64_t timestamp, const ConfigKey& key, const UidMapOptions& options,
152                       std::set<string>* str_set, ProtoOutputStream* proto);
153 
154     // Forces the output to be cleared. We still generate a snapshot based on the current state.
155     // This results in extra data uploaded but helps us reconstruct the uid mapping on the server
156     // in case we lose a previous upload.
157     void clearOutput();
158 
159     // Get currently cached value of memory used by UID map.
160     size_t getBytesUsed() const;
161 
162     virtual std::set<int32_t> getAppUid(const string& package) const;
163 
164     // Write current PackageInfoSnapshot to ProtoOutputStream.
165     // interestingUids: If not empty, only write the package info for these uids. If empty, write
166     //                  package info for all uids.
167     // str_set: if not null, add new string to the set and write str_hash to proto
168     //          if null, write string to proto.
169     void writeUidMapSnapshot(int64_t timestamp, const UidMapOptions& options,
170                              const std::set<int32_t>& interestingUids,
171                              std::map<string, int>* installerIndices, std::set<string>* str_set,
172                              ProtoOutputStream* proto) const;
173 
174 private:
175     std::set<string> getAppNamesFromUidLocked(int32_t uid, bool returnNormalized) const;
176     string normalizeAppName(const string& appName) const;
177 
178     void writeUidMapSnapshotLocked(const int64_t timestamp, const UidMapOptions& options,
179                                    const std::set<int32_t>& interestingUids,
180                                    std::map<string, int>* installerIndices,
181                                    std::set<string>* str_set, ProtoOutputStream* proto) const;
182 
183     mutable mutex mMutex;
184     mutable mutex mIsolatedMutex;
185 
186     struct PairHash {
operatorPairHash187         size_t operator()(const std::pair<int, string>& p) const noexcept {
188             std::hash<std::string> hash_fn;
189             return hash_fn(std::to_string(p.first) + p.second);
190         }
191     };
192     // Maps uid and package name to application data.
193     std::unordered_map<std::pair<int, string>, AppData, PairHash> mMap;
194 
195     // Maps isolated uid to the parent uid. Any metrics for an isolated uid will instead contribute
196     // to the parent uid.
197     std::unordered_map<int, int> mIsolatedUidMap;
198 
199     // Record the changes that can be provided with the uploads.
200     std::list<ChangeRecord> mChanges;
201 
202     // Store which uid and apps represent deleted ones.
203     std::list<std::pair<int, string>> mDeletedApps;
204 
205     // Notify StatsLogProcessor if there's an upgrade/removal in any app.
206     wp<PackageInfoListener> mSubscriber;
207 
208     // Mapping of config keys we're aware of to the epoch time they last received an update. This
209     // lets us know it's safe to delete events older than the oldest update. The value is nanosec.
210     // Value of -1 denotes this config key has never received an upload.
211     std::unordered_map<ConfigKey, int64_t> mLastUpdatePerConfigKey;
212 
213     // Returns the minimum value from mConfigKeys.
214     int64_t getMinimumTimestampNs();
215 
216     // If our current used bytes is above the limit, then we clear out the earliest snapshot. If
217     // there are no more snapshots, then we clear out the earliest delta. We repeat the deletions
218     // until the memory consumed by mOutput is below the specified limit.
219     void ensureBytesUsedBelowLimit();
220 
221     // Override used for testing the max memory allowed by uid map. 0 means we use the value
222     // specified in StatsdStats.h with the rest of the guardrails.
223     size_t maxBytesOverride = 0;
224 
225     // Cache the size of mOutput;
226     size_t mBytesUsed;
227 
228     // Allows unit-test to access private methods.
229     FRIEND_TEST(RestrictedEventMetricE2eTest, TestRestrictedConfigUpdateDoesNotUpdateUidMap);
230     FRIEND_TEST(RestrictedEventMetricE2eTest,
231                 TestRestrictedConfigUpdateAddsDelegateRemovesUidMapEntry);
232     FRIEND_TEST(UidMapTest, TestClearingOutput);
233     FRIEND_TEST(UidMapTest, TestRemovedAppRetained);
234     FRIEND_TEST(UidMapTest, TestRemovedAppOverGuardrail);
235     FRIEND_TEST(UidMapTest, TestOutputIncludesAtLeastOneSnapshot);
236     FRIEND_TEST(UidMapTest, TestMemoryComputed);
237     FRIEND_TEST(UidMapTest, TestMemoryGuardrail);
238 };
239 
240 }  // namespace statsd
241 }  // namespace os
242 }  // namespace android
243