• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
FilterTypeBool(const reflection::Field & field,flatbuffers::Table * table,PrivacyLevel privacy_level)108 bool internal::FilterTypeBool(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level) {
109   ASSERT(table != nullptr);
110 
111   // TODO(cmanton) Figure out boolean filtering
112   return kFieldHasBeenFiltered;
113 }
114 
FilterTypeInteger(const reflection::Field & field,flatbuffers::Table * table,PrivacyLevel privacy_level)115 bool internal::FilterTypeInteger(
116     const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level) {
117   ASSERT(table != nullptr);
118   ASSERT(flatbuffers::IsInteger(field.type()->base_type()));
119 
120   int32_t default_val = flatbuffers::GetFieldDefaultI<int32_t>(field);
121   flatbuffers::voffset_t field_offset = field.offset();
122   [[maybe_unused]] int32_t val = table->GetField<int32_t>(field_offset, default_val);
123 
124   switch (privacy_level) {
125     case kPrivate:
126       flatbuffers::SetField<int32_t>(table, field, default_val);
127       internal::ScrubFromTable(table, field_offset);
128       break;
129     case kOpaque:
130       flatbuffers::SetField<int32_t>(table, field, default_val);
131       break;
132     case kAnonymized: {
133       auto target_field = flatbuffers::GetFieldI<int32_t>(*table, field);
134       int32_t new_val = static_cast<int32_t>(std::hash<std::string>{}(std::to_string(target_field)));
135       flatbuffers::SetField<int32_t>(table, field, new_val);
136     } break;
137     default:
138     case kAny:
139       break;
140   }
141 
142   if (DBG) {
143     LOG_INFO(
144         "Integer Field_name:%s privacy_level:%s old_value:%d / 0x%x ==> new_value:%d\n",
145         field.name()->c_str(),
146         PrivacyLevelName(privacy_level),
147         val,
148         val,
149         table->GetField<int32_t>(field_offset, default_val));
150   }
151   return kFieldHasBeenFiltered;
152 }
153 
FilterTypeFloat(const reflection::Field & field,flatbuffers::Table * table,PrivacyLevel privacy_level)154 bool internal::FilterTypeFloat(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level) {
155   ASSERT(table != nullptr);
156   ASSERT(flatbuffers::IsFloat(field.type()->base_type()));
157 
158   float default_val = flatbuffers::GetFieldDefaultI<float>(field);
159   flatbuffers::voffset_t field_offset = field.offset();
160   [[maybe_unused]] float val = table->GetField<float>(field_offset, default_val);
161   switch (privacy_level) {
162     case kPrivate:
163       flatbuffers::SetField<float>(table, field, default_val);
164       internal::ScrubFromTable(table, field_offset);
165       break;
166     case kOpaque:
167       flatbuffers::SetField<float>(table, field, default_val);
168       break;
169     case kAnonymized: {
170       auto target_field = flatbuffers::GetFieldF<float>(*table, field);
171       int32_t new_val = static_cast<float>(std::hash<std::string>{}(std::to_string(target_field)));
172       flatbuffers::SetField<float>(table, field, new_val);
173     } break;
174     default:
175     case kAny:
176       break;
177   }
178   if (DBG) {
179     LOG_INFO(
180         "Float Field_name:%s privacy_level:%s old_value:%f ==> new_value:%f",
181         field.name()->c_str(),
182         PrivacyLevelName(privacy_level),
183         val,
184         table->GetField<float>(field_offset, default_val));
185   }
186   return kFieldHasBeenFiltered;
187 }
188 
FilterTypeString(const reflection::Field & field,flatbuffers::Table * table,PrivacyLevel privacy_level)189 bool internal::FilterTypeString(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level) {
190   ASSERT(table != nullptr);
191   ASSERT(field.type()->base_type() == reflection::BaseType::String);
192 
193   flatbuffers::voffset_t field_offset = field.offset();
194 
195   const flatbuffers::String* string = flatbuffers::GetFieldS(*table, field);
196   if (string == nullptr) {
197     return kFieldIsNotPopulated;
198     // Field is not populated
199   }
200   ASSERT(string != nullptr);
201   flatbuffers::String* mutable_string = const_cast<flatbuffers::String*>(string);
202 
203   [[maybe_unused]] std::string old_string(string->str());
204   switch (privacy_level) {
205     case kPrivate:
206       internal::ReplaceInString(mutable_string, '*');
207       internal::ScrubFromTable(table, field_offset);
208       break;
209     case kOpaque:
210       internal::ReplaceInString(mutable_string, '*');
211       break;
212     case kAnonymized:
213       internal::RandomizeInString(mutable_string);
214       break;
215     default:
216     case kAny:
217       break;
218   }
219   if (DBG) {
220     LOG_INFO(
221         "%s Field_name:%s size:%u privacy_level:%s old_string:%s ==> new_string:%s",
222         __func__,
223         field.name()->c_str(),
224         string->size(),
225         PrivacyLevelName(privacy_level),
226         old_string.c_str(),
227         string->c_str());
228   }
229   return kFieldHasBeenFiltered;
230 }
231 
FilterTypeStruct(const reflection::Field & field,flatbuffers::Table * table,PrivacyLevel privacy_level)232 bool internal::FilterTypeStruct(const reflection::Field& field, flatbuffers::Table* table, PrivacyLevel privacy_level) {
233   ASSERT(table != nullptr);
234   ASSERT(!flatbuffers::IsScalar(field.type()->base_type()));
235 
236   flatbuffers::voffset_t field_offset = field.offset();
237 
238   if (privacy_level != kAny) {
239     flatbuffers::SetFieldT(table, field, nullptr);
240     internal::ScrubFromTable(table, field_offset);
241     if (DBG) {
242       LOG_INFO(
243           " Table Removing field name:%s privacy_level:%s", field.name()->c_str(), PrivacyLevelName(privacy_level));
244     }
245   }
246   return kFieldContinueFiltering;
247 }
248