1 /*
2  * Copyright (C) 2018 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 "Common.h"
18 
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include <algorithm>
24 #include <cctype>
25 #include <cerrno>
26 #include <cmath>
27 #include <cstdlib>
28 #include <cstring>
29 #include <initializer_list>
30 #include <memory>
31 #include <regex>
32 #include <string>
33 #include <unordered_set>
34 #include <vector>
35 
36 #include <android-base/file.h>
37 #include <android-base/logging.h>
38 #include <android-base/strings.h>
39 #include <google/protobuf/text_format.h>
40 
41 #include "sysprop.pb.h"
42 
43 using android::base::Result;
44 
45 namespace {
46 
47 std::string GenerateDefaultPropName(const sysprop::Properties& props,
48                                     const sysprop::Property& prop);
49 bool IsCorrectIdentifier(const std::string& name);
50 Result<void> ValidateProp(const sysprop::Properties& props,
51                           const sysprop::Property& prop);
52 Result<void> ValidateProps(const sysprop::Properties& props);
53 
GenerateDefaultPropName(const sysprop::Properties & props,const sysprop::Property & prop)54 std::string GenerateDefaultPropName(const sysprop::Properties& props,
55                                     const sysprop::Property& prop) {
56   std::string ret;
57 
58   if (prop.access() != sysprop::ReadWrite) ret = "ro.";
59 
60   switch (props.owner()) {
61     case sysprop::Vendor:
62       ret += "vendor.";
63       break;
64     case sysprop::Odm:
65       ret += "odm.";
66       break;
67     default:
68       break;
69   }
70 
71   ret += prop.api_name();
72 
73   return ret;
74 }
75 
IsCorrectIdentifier(const std::string & name)76 bool IsCorrectIdentifier(const std::string& name) {
77   if (name.empty()) return false;
78   if (std::isalpha(name[0]) == 0 && name[0] != '_') return false;
79 
80   return std::all_of(name.begin() + 1, name.end(), [](char ch) {
81     return std::isalnum(ch) != 0 || ch == '_';
82   });
83 }
84 
IsCorrectName(const std::string & name,const std::unordered_set<char> & allowed_chars)85 bool IsCorrectName(const std::string& name,
86                    const std::unordered_set<char>& allowed_chars) {
87   if (name.empty()) return false;
88   if (!std::isalpha(*name.begin())) return false;
89 
90   return std::all_of(name.begin(), name.end(), [allowed_chars](char ch) {
91     return std::isalnum(ch) != 0 || allowed_chars.count(ch) != 0;
92   });
93 }
94 
IsCorrectPropertyName(const std::string & name)95 bool IsCorrectPropertyName(const std::string& name) {
96   std::unordered_set<char> allowed{'_', '-', '.'};
97   if (android::base::StartsWith(name, "ctl.")) {
98     allowed.emplace('$');
99   }
100   return IsCorrectName(name, allowed);
101 }
102 
IsCorrectApiName(const std::string & name)103 bool IsCorrectApiName(const std::string& name) {
104   static std::unordered_set<char> allowed{'_', '-'};
105   return IsCorrectName(name, allowed);
106 }
107 
ValidateProp(const sysprop::Properties & props,const sysprop::Property & prop)108 Result<void> ValidateProp(const sysprop::Properties& props,
109                           const sysprop::Property& prop) {
110   if (!IsCorrectApiName(prop.api_name())) {
111     return Errorf("Invalid API name \"{}\"", prop.api_name());
112   }
113 
114   if (prop.type() == sysprop::Enum || prop.type() == sysprop::EnumList) {
115     std::vector<std::string> names =
116         android::base::Split(prop.enum_values(), "|");
117     if (names.empty()) {
118       return Errorf("Enum values are empty for API \"{}\"", prop.api_name());
119     }
120 
121     for (const std::string& name : names) {
122       if (!IsCorrectIdentifier(name)) {
123         return Errorf("Invalid enum value \"{}\" for API \"{}\"", name,
124                       prop.api_name());
125       }
126     }
127 
128     std::unordered_set<std::string> name_set;
129     for (const std::string& name : names) {
130       if (!name_set.insert(ToUpper(name)).second) {
131         return Errorf("Duplicated enum value \"{}\" for API \"{}\"", name,
132                       prop.api_name());
133       }
134     }
135   }
136 
137   std::string prop_name = prop.prop_name();
138   if (prop_name.empty()) prop_name = GenerateDefaultPropName(props, prop);
139 
140   if (!IsCorrectPropertyName(prop_name)) {
141     return Errorf("Invalid prop name \"{}\"", prop.prop_name());
142   }
143 
144   std::string legacy_name = prop.legacy_prop_name();
145   if (!legacy_name.empty()) {
146     if (!IsCorrectPropertyName(legacy_name)) {
147       return Errorf("Invalid legacy prop name \"{}\"", legacy_name);
148     }
149     if (prop.access() != sysprop::Readonly) {
150       return Errorf("Prop \"{}\" which has legacy_prop_name must be Readonly",
151                     prop.prop_name());
152     }
153   }
154 
155   static const std::regex vendor_regex(
156       "(init\\.svc\\.|ro\\.|persist\\.)?vendor\\..+|ro\\.hardware\\..+");
157   static const std::regex odm_regex(
158       "(init\\.svc\\.|ro\\.|persist\\.)?odm\\..+|ro\\.hardware\\..+");
159 
160   switch (props.owner()) {
161     case sysprop::Platform:
162       if (std::regex_match(prop_name, vendor_regex) ||
163           std::regex_match(prop_name, odm_regex)) {
164         return Errorf(
165             "Prop \"{}\" owned by platform cannot have vendor. or odm. "
166             "namespace",
167             prop_name);
168       }
169       break;
170     case sysprop::Vendor:
171       if (!std::regex_match(prop_name, vendor_regex)) {
172         return Errorf(
173             "Prop \"{}\" owned by vendor should have vendor. namespace",
174             prop_name);
175       }
176       break;
177     case sysprop::Odm:
178       if (!std::regex_match(prop_name, odm_regex)) {
179         return Errorf("Prop \"{}\" owned by odm should have odm. namespace",
180                       prop_name);
181       }
182       break;
183     default:
184       break;
185   }
186 
187   if (prop.access() == sysprop::ReadWrite &&
188       android::base::StartsWith(prop_name, "ro.")) {
189     return Errorf("Prop \"{}\" is ReadWrite and also have prefix \"ro.\"",
190                   prop_name);
191   }
192 
193   if (prop.integer_as_bool() && !(prop.type() == sysprop::Boolean ||
194                                   prop.type() == sysprop::BooleanList)) {
195     return Errorf("Prop \"{}\" has integer_as_bool: true, but not a boolean",
196                   prop_name);
197   }
198 
199   return {};
200 }
201 
ValidateProps(const sysprop::Properties & props)202 Result<void> ValidateProps(const sysprop::Properties& props) {
203   std::vector<std::string> names = android::base::Split(props.module(), ".");
204   if (names.size() <= 1) {
205     return Errorf("Invalid module name \"{}\"", props.module());
206   }
207 
208   for (const auto& name : names) {
209     if (!IsCorrectIdentifier(name)) {
210       return Errorf("Invalid name \"{}\" in module", name);
211     }
212   }
213 
214   if (props.prop_size() == 0) {
215     return Errorf("There is no defined property");
216   }
217 
218   for (int i = 0; i < props.prop_size(); ++i) {
219     const auto& prop = props.prop(i);
220     if (auto res = ValidateProp(props, prop); !res.ok()) return res;
221   }
222 
223   std::unordered_set<std::string> prop_names;
224   std::unordered_map<std::string, sysprop::Type> prop_types;
225 
226   for (int i = 0; i < props.prop_size(); ++i) {
227     const auto& prop = props.prop(i);
228     auto res = prop_names.insert(ApiNameToIdentifier(prop.api_name()));
229 
230     if (!res.second) {
231       return Errorf("Duplicated API name \"{}\"", prop.api_name());
232     }
233 
234     std::vector<std::string> prop_names{prop.prop_name()};
235     std::string legacy_name = prop.legacy_prop_name();
236     if (!legacy_name.empty()) prop_names.push_back(legacy_name);
237 
238     sysprop::Type type = prop.type();
239 
240     for (auto& name : prop_names) {
241       // get type if already exists. inserts mine if not.
242       sysprop::Type prev_type = prop_types.emplace(name, type).first->second;
243       if (prev_type != type) {
244         return Errorf("Type error on prop \"{}\": it's {} but was {}", name,
245                       sysprop::Type_Name(type), sysprop::Type_Name(prev_type));
246       }
247     }
248   }
249 
250   return {};
251 }
252 
SetDefaultValues(sysprop::Properties * props)253 void SetDefaultValues(sysprop::Properties* props) {
254   for (int i = 0; i < props->prop_size(); ++i) {
255     // set each optional field to its default value
256     sysprop::Property& prop = *props->mutable_prop(i);
257     if (prop.prop_name().empty())
258       prop.set_prop_name(GenerateDefaultPropName(*props, prop));
259     if (prop.scope() == sysprop::Scope::System) {
260       LOG(WARNING) << "Sysprop API " << prop.api_name()
261                    << ": System scope is deprecated."
262                    << " Please use Public scope instead.";
263       prop.set_scope(sysprop::Scope::Public);
264     }
265   }
266 }
267 
268 }  // namespace
269 
IsListProp(const sysprop::Property & prop)270 bool IsListProp(const sysprop::Property& prop) {
271   switch (prop.type()) {
272     case sysprop::BooleanList:
273     case sysprop::IntegerList:
274     case sysprop::LongList:
275     case sysprop::DoubleList:
276     case sysprop::StringList:
277     case sysprop::EnumList:
278       return true;
279     default:
280       return false;
281   }
282 }
283 
GetModuleName(const sysprop::Properties & props)284 std::string GetModuleName(const sysprop::Properties& props) {
285   const std::string& module = props.module();
286   return module.substr(module.rfind('.') + 1);
287 }
288 
ParseEnumValues(const std::string & enum_values)289 std::vector<std::string> ParseEnumValues(const std::string& enum_values) {
290   return android::base::Split(enum_values, "|");
291 }
292 
ParseProps(const std::string & input_file_path)293 Result<sysprop::Properties> ParseProps(const std::string& input_file_path) {
294   sysprop::Properties ret;
295   std::string file_contents;
296 
297   if (!android::base::ReadFileToString(input_file_path, &file_contents, true)) {
298     return ErrnoErrorf("Error reading file {}", input_file_path);
299   }
300 
301   if (!google::protobuf::TextFormat::ParseFromString(file_contents, &ret)) {
302     return Errorf("Error parsing file {}", input_file_path);
303   }
304 
305   SetDefaultValues(&ret);
306 
307   // validate after filling default values such as prop_name
308   if (auto res = ValidateProps(ret); !res.ok()) {
309     return res.error();
310   }
311 
312   return ret;
313 }
314 
ParseApiFile(const std::string & input_file_path)315 Result<sysprop::SyspropLibraryApis> ParseApiFile(
316     const std::string& input_file_path) {
317   sysprop::SyspropLibraryApis ret;
318   std::string file_contents;
319 
320   if (!android::base::ReadFileToString(input_file_path, &file_contents, true)) {
321     return ErrnoErrorf("Error reading file {}", input_file_path);
322   }
323 
324   if (!google::protobuf::TextFormat::ParseFromString(file_contents, &ret)) {
325     return Errorf("Error parsing file {}", input_file_path);
326   }
327 
328   std::unordered_set<std::string> modules;
329 
330   for (int i = 0; i < ret.props_size(); ++i) {
331     sysprop::Properties* props = ret.mutable_props(i);
332 
333     if (!modules.insert(props->module()).second) {
334       return Errorf("Error parsing file {}: duplicated module {}",
335                     input_file_path, props->module());
336     }
337 
338     SetDefaultValues(props);
339 
340     // validate after filling default values such as prop_name
341     if (auto res = ValidateProps(*props); !res.ok()) {
342       return res.error();
343     }
344   }
345 
346   return ret;
347 }
348 
ToUpper(std::string str)349 std::string ToUpper(std::string str) {
350   for (char& ch : str) {
351     ch = toupper(ch);
352   }
353   return str;
354 }
355 
ApiNameToIdentifier(const std::string & name)356 std::string ApiNameToIdentifier(const std::string& name) {
357   static const std::regex kRegexAllowed{"-|\\."};
358   return (isdigit(name[0]) ? "_" : "") +
359          std::regex_replace(name, kRegexAllowed, "_");
360 }
361