1 /*
2  * Copyright 2020 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 #include <algorithm>
18 #include <string>
19 
20 #include "dumpsys/internal/filter_internal.h"
21 #include "flatbuffers/flatbuffers.h"
22 #include "flatbuffers/idl.h"
23 #include "os/log.h"
24 
25 #define DBG 0
26 
27 using namespace bluetooth;
28 using namespace dumpsys;
29 
30 constexpr flatbuffers::voffset_t kErasedFromTable = 0;
31 constexpr bool kFieldIsNotPopulated = true;
32 constexpr bool kFieldHasBeenFiltered = true;
33 constexpr bool kFieldContinueFiltering = false;
34 
ScrubFromTable(flatbuffers::Table * table,flatbuffers::voffset_t field_offset)35 void internal::ScrubFromTable(flatbuffers::Table* table, flatbuffers::voffset_t field_offset) {
36   ASSERT(table != nullptr);
37   uint8_t* vtable = const_cast<uint8_t*>(table->GetVTable());
38   vtable[field_offset] = kErasedFromTable;
39 }
40 
ReplaceInString(flatbuffers::String * string,int c)41 void internal::ReplaceInString(flatbuffers::String* string, int c) {
42   uint8_t* p = const_cast<uint8_t*>(string->Data());
43   memset(p, c, string->size());
44 }
45 
RandomizeInString(flatbuffers::String * string)46 void internal::RandomizeInString(flatbuffers::String* string) {
47   std::size_t hash = std::hash<std::string>{}(string->str());
48   std::string hashed_string = std::to_string(hash);
49   ReplaceInString(string, ' ');
50   size_t len = std::min(static_cast<size_t>(string->size()), hashed_string.size());
51   uint8_t* p = const_cast<uint8_t*>(string->Data());
52   memcpy(p, hashed_string.c_str(), len);
53 }
54 
PrivacyLevelName(PrivacyLevel privacy_level)55 const char* internal::PrivacyLevelName(PrivacyLevel privacy_level) {
56   switch (privacy_level) {
57     case kPrivate:
58       return "Private";
59       break;
60     case kOpaque:
61       return "Opaque";
62       break;
63     case kAnonymized:
64       return "Anonymized";
65       break;
66     case kAny:
67       return "Any";
68       break;
69   }
70 };
GetPrivacyLevelAttribute(const std::string & string)71 internal::PrivacyLevel internal::GetPrivacyLevelAttribute(const std::string& string) {
72   if (string == "Any") {
73     return kAny;
74   } else if (string == "Anonymized") {
75     return kAnonymized;
76   } else if (string == "Opaque") {
77     return kOpaque;
78   } else if (string == "Private") {
79     return kPrivate;
80   }
81   return kDefaultPrivacyLevel;
82 }
83 
FindFieldPrivacyLevel(const reflection::Field & field)84 internal::PrivacyLevel internal::FindFieldPrivacyLevel(const reflection::Field& field) {
85   PrivacyLevel privacy_level = kDefaultPrivacyLevel;
86 
87   if (field.attributes() != nullptr) {
88     auto key = field.attributes()->LookupByKey(kPrivacyAttributeKeyword);
89     if (key != nullptr) {
90       privacy_level = internal::GetPrivacyLevelAttribute(key->value()->str());
91     }
92   }
93   return privacy_level;
94 }
95 
FindReflectionObject(const flatbuffers::Vector<flatbuffers::Offset<reflection::Object>> * objects,const flatbuffers::String * name)96 const reflection::Object* internal::FindReflectionObject(
97     const flatbuffers::Vector<flatbuffers::Offset<reflection::Object>>* objects, const flatbuffers::String* name) {
98   ASSERT(objects != nullptr);
99   ASSERT(name != nullptr);
100   for (auto it = objects->cbegin(); it != objects->cend(); ++it) {
101     if (it->name()->str() == name->str()) {
102       return *it;
103     }
104   }
105   return nullptr;
106 }
107 
FilterTypeInteger(const reflection::Field & field,flatbuffers::Table * table,PrivacyLevel privacy_level)108 bool internal::FilterTypeInteger(
109     const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level) {
110   ASSERT(table != nullptr);
111   ASSERT(flatbuffers::IsInteger(field.type()->base_type()));
112 
113   int32_t default_val = flatbuffers::GetFieldDefaultI<int32_t>(field);
114   flatbuffers::voffset_t field_offset = field.offset();
115   [[maybe_unused]] int32_t val = table->GetField<int32_t>(field_offset, default_val);
116 
117   switch (privacy_level) {
118     case kPrivate:
119       flatbuffers::SetField<int32_t>(table, field, default_val);
120       internal::ScrubFromTable(table, field_offset);
121       break;
122     case kOpaque:
123       flatbuffers::SetField<int32_t>(table, field, default_val);
124       break;
125     case kAnonymized: {
126       auto target_field = flatbuffers::GetFieldI<int32_t>(*table, field);
127       int32_t new_val = static_cast<int32_t>(std::hash<std::string>{}(std::to_string(target_field)));
128       flatbuffers::SetField<int32_t>(table, field, new_val);
129     } break;
130     default:
131     case kAny:
132       break;
133   }
134 
135   if (DBG) {
136     LOG_DEBUG(
137         "Integer Field_name:%s privacy_level:%s old_value:%d / 0x%x ==> new_value:%d\n",
138         field.name()->c_str(),
139         PrivacyLevelName(privacy_level),
140         val,
141         val,
142         table->GetField<int32_t>(field_offset, default_val));
143   }
144   return kFieldHasBeenFiltered;
145 }
146 
FilterTypeFloat(const reflection::Field & field,flatbuffers::Table * table,PrivacyLevel privacy_level)147 bool internal::FilterTypeFloat(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level) {
148   ASSERT(table != nullptr);
149   ASSERT(flatbuffers::IsFloat(field.type()->base_type()));
150 
151   float default_val = flatbuffers::GetFieldDefaultI<float>(field);
152   flatbuffers::voffset_t field_offset = field.offset();
153   [[maybe_unused]] float val = table->GetField<float>(field_offset, default_val);
154   switch (privacy_level) {
155     case kPrivate:
156       flatbuffers::SetField<float>(table, field, default_val);
157       internal::ScrubFromTable(table, field_offset);
158       break;
159     case kOpaque:
160       flatbuffers::SetField<float>(table, field, default_val);
161       break;
162     case kAnonymized: {
163       auto target_field = flatbuffers::GetFieldF<float>(*table, field);
164       int32_t new_val = static_cast<float>(std::hash<std::string>{}(std::to_string(target_field)));
165       flatbuffers::SetField<float>(table, field, new_val);
166     } break;
167     default:
168     case kAny:
169       break;
170   }
171   if (DBG) {
172     LOG_DEBUG(
173         "Float Field_name:%s privacy_level:%s old_value:%f ==> new_value:%f",
174         field.name()->c_str(),
175         PrivacyLevelName(privacy_level),
176         val,
177         table->GetField<float>(field_offset, default_val));
178   }
179   return kFieldHasBeenFiltered;
180 }
181 
FilterTypeString(const reflection::Field & field,flatbuffers::Table * table,PrivacyLevel privacy_level)182 bool internal::FilterTypeString(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level) {
183   ASSERT(table != nullptr);
184   ASSERT(field.type()->base_type() == reflection::BaseType::String);
185 
186   flatbuffers::voffset_t field_offset = field.offset();
187 
188   const flatbuffers::String* string = flatbuffers::GetFieldS(*table, field);
189   if (string == nullptr) {
190     return kFieldIsNotPopulated;
191     // Field is not populated
192   }
193   ASSERT(string != nullptr);
194   flatbuffers::String* mutable_string = const_cast<flatbuffers::String*>(string);
195 
196   [[maybe_unused]] std::string old_string(string->str());
197   switch (privacy_level) {
198     case kPrivate:
199       internal::ReplaceInString(mutable_string, '*');
200       internal::ScrubFromTable(table, field_offset);
201       break;
202     case kOpaque:
203       internal::ReplaceInString(mutable_string, '*');
204       break;
205     case kAnonymized:
206       internal::RandomizeInString(mutable_string);
207       break;
208     default:
209     case kAny:
210       break;
211   }
212   if (DBG) {
213     LOG_DEBUG(
214         "%s Field_name:%s size:%u privacy_level:%s old_string:%s ==> new_string:%s",
215         __func__,
216         field.name()->c_str(),
217         string->size(),
218         PrivacyLevelName(privacy_level),
219         old_string.c_str(),
220         string->c_str());
221   }
222   return kFieldHasBeenFiltered;
223 }
224 
FilterTypeStruct(const reflection::Field & field,flatbuffers::Table * table,PrivacyLevel privacy_level)225 bool internal::FilterTypeStruct(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level) {
226   ASSERT(table != nullptr);
227   ASSERT(!flatbuffers::IsScalar(field.type()->base_type()));
228 
229   flatbuffers::voffset_t field_offset = field.offset();
230 
231   if (privacy_level != kAny) {
232     flatbuffers::SetFieldT(table, field, nullptr);
233     internal::ScrubFromTable(table, field_offset);
234     if (DBG) {
235       LOG_DEBUG(
236           " Table Removing field name:%s privacy_level:%s", field.name()->c_str(), PrivacyLevelName(privacy_level));
237     }
238   }
239   return kFieldContinueFiltering;
240 }
241