1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "components/policy/core/common/registry_dict.h"
#include <memory>
#include <optional>
#include <utility>
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/numerics/byte_conversions.h"
#include "base/strings/cstring_view.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/policy/core/common/schema.h"
#if BUILDFLAG(IS_WIN)
#include "base/win/registry.h"
#include "base/win/win_util.h"
using base::win::RegistryKeyIterator;
using base::win::RegistryValueIterator;
#endif // BUILDFLAG(IS_WIN)
namespace policy {
namespace {
// Validates that a key is numerical. Used for lists below.
bool IsKeyNumerical(const std::string& key) {
int temp = 0;
return base::StringToInt(key, &temp);
}
} // namespace
std::optional<base::Value> ConvertRegistryValue(const base::Value& value,
const Schema& schema) {
if (!schema.valid()) {
return value.Clone();
}
// If the type is good already, go with it.
if (value.type() == schema.type()) {
// Recurse for complex types.
if (value.is_dict()) {
base::Value::Dict result;
for (auto entry : value.GetDict()) {
std::optional<base::Value> converted =
ConvertRegistryValue(entry.second, schema.GetProperty(entry.first));
if (converted.has_value()) {
result.Set(entry.first, std::move(converted.value()));
}
}
return base::Value(std::move(result));
} else if (value.is_list()) {
base::Value::List result;
for (const auto& entry : value.GetList()) {
std::optional<base::Value> converted =
ConvertRegistryValue(entry, schema.GetItems());
if (converted.has_value()) {
result.Append(std::move(converted.value()));
}
}
return base::Value(std::move(result));
}
return value.Clone();
}
// Else, do some conversions to map windows registry data types to JSON types.
int int_value = 0;
switch (schema.type()) {
case base::Value::Type::NONE: {
return base::Value();
}
case base::Value::Type::BOOLEAN: {
// Accept booleans encoded as either string or integer.
if (value.is_int())
return base::Value(value.GetInt() != 0);
if (value.is_string() &&
base::StringToInt(value.GetString(), &int_value)) {
return base::Value(int_value != 0);
}
break;
}
case base::Value::Type::INTEGER: {
// Integers may be string-encoded.
if (value.is_string() &&
base::StringToInt(value.GetString(), &int_value)) {
return base::Value(int_value);
}
break;
}
case base::Value::Type::DOUBLE: {
// Doubles may be string-encoded or integer-encoded.
if (value.is_double() || value.is_int())
return base::Value(value.GetDouble());
double double_value = 0;
if (value.is_string() &&
base::StringToDouble(value.GetString(), &double_value)) {
return base::Value(double_value);
}
break;
}
case base::Value::Type::LIST: {
// Lists are encoded as subkeys with numbered value in the registry
// (non-numerical keys are ignored).
if (value.is_dict()) {
base::Value::List result;
for (auto it : value.GetDict()) {
if (!IsKeyNumerical(it.first)) {
continue;
}
std::optional<base::Value> converted =
ConvertRegistryValue(it.second, schema.GetItems());
if (converted.has_value()) {
result.Append(std::move(converted.value()));
}
}
return base::Value(std::move(result));
}
// Fall through in order to accept lists encoded as JSON strings.
[[fallthrough]];
}
case base::Value::Type::DICT: {
// Dictionaries may be encoded as JSON strings.
if (value.is_string()) {
std::optional<base::Value> result = base::JSONReader::Read(
value.GetString(),
base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
if (result.has_value() && result.value().type() == schema.type()) {
return std::move(result.value());
}
}
break;
}
case base::Value::Type::STRING:
case base::Value::Type::BINARY:
// No conversion possible.
break;
}
LOG(WARNING) << "Failed to convert " << value.type() << " to "
<< schema.type();
return std::nullopt;
}
bool CaseInsensitiveStringCompare::operator()(const std::string& a,
const std::string& b) const {
return base::CompareCaseInsensitiveASCII(a, b) < 0;
}
RegistryDict::RegistryDict() = default;
RegistryDict::~RegistryDict() {
ClearKeys();
ClearValues();
}
RegistryDict* RegistryDict::GetKey(const std::string& name) {
auto entry = keys_.find(name);
return entry != keys_.end() ? entry->second.get() : nullptr;
}
const RegistryDict* RegistryDict::GetKey(const std::string& name) const {
auto entry = keys_.find(name);
return entry != keys_.end() ? entry->second.get() : nullptr;
}
void RegistryDict::SetKey(const std::string& name,
std::unique_ptr<RegistryDict> dict) {
if (!dict) {
RemoveKey(name);
return;
}
keys_[name] = std::move(dict);
}
std::unique_ptr<RegistryDict> RegistryDict::RemoveKey(const std::string& name) {
std::unique_ptr<RegistryDict> result;
auto entry = keys_.find(name);
if (entry != keys_.end()) {
result = std::move(entry->second);
keys_.erase(entry);
}
return result;
}
void RegistryDict::ClearKeys() {
keys_.clear();
}
base::Value* RegistryDict::GetValue(const std::string& name) {
auto entry = values_.find(name);
return entry != values_.end() ? &entry->second : nullptr;
}
const base::Value* RegistryDict::GetValue(const std::string& name) const {
auto entry = values_.find(name);
return entry != values_.end() ? &entry->second : nullptr;
}
void RegistryDict::SetValue(const std::string& name, base::Value&& dict) {
values_[name] = std::move(dict);
}
std::optional<base::Value> RegistryDict::RemoveValue(const std::string& name) {
std::optional<base::Value> result;
auto entry = values_.find(name);
if (entry != values_.end()) {
result = std::move(entry->second);
values_.erase(entry);
}
return result;
}
void RegistryDict::ClearValues() {
values_.clear();
}
void RegistryDict::Merge(const RegistryDict& other) {
for (auto entry(other.keys_.begin()); entry != other.keys_.end(); ++entry) {
std::unique_ptr<RegistryDict>& subdict = keys_[entry->first];
if (!subdict)
subdict = std::make_unique<RegistryDict>();
subdict->Merge(*entry->second);
}
for (auto entry(other.values_.begin()); entry != other.values_.end();
++entry) {
SetValue(entry->first, entry->second.Clone());
}
}
void RegistryDict::Swap(RegistryDict* other) {
keys_.swap(other->keys_);
values_.swap(other->values_);
}
#if BUILDFLAG(IS_WIN)
void RegistryDict::ReadRegistry(HKEY hive, const std::wstring& root) {
ClearKeys();
ClearValues();
// First, read all the values of the key.
for (RegistryValueIterator it(hive, root.c_str()); it.Valid(); ++it) {
const std::string name = base::WideToUTF8(it.Name());
switch (it.Type()) {
case REG_EXPAND_SZ:
if (auto expanded_path = base::win::ExpandEnvironmentVariables(
base::wcstring_view(it.Value()))) {
SetValue(name, base::Value(base::WideToUTF8(*expanded_path)));
continue;
}
[[fallthrough]];
case REG_SZ:
SetValue(name, base::Value(base::WideToUTF8(it.Value())));
continue;
case REG_DWORD_LITTLE_ENDIAN:
case REG_DWORD_BIG_ENDIAN:
if (it.ValueSize() == sizeof(DWORD)) {
auto value =
// TODO(crbug.com/40284755): it.Value() should return a
// wcstring_view which will be usable as a span directly. The
// ValueSize() here is the number of non-NUL *bytes* in the
// Value() string, so we cast the Value() to bytes which is what
// we want in the end anyway.
UNSAFE_TODO(
base::span(reinterpret_cast<const uint8_t*>(it.Value()),
it.ValueSize()))
.first<sizeof(DWORD)>();
DWORD dword_value = it.Type() == REG_DWORD_BIG_ENDIAN
? base::U32FromBigEndian(value)
: base::U32FromLittleEndian(value);
SetValue(name, base::Value(static_cast<int>(dword_value)));
continue;
}
[[fallthrough]];
case REG_NONE:
case REG_LINK:
case REG_MULTI_SZ:
case REG_RESOURCE_LIST:
case REG_FULL_RESOURCE_DESCRIPTOR:
case REG_RESOURCE_REQUIREMENTS_LIST:
case REG_QWORD_LITTLE_ENDIAN:
// Unsupported type, message gets logged below.
break;
}
LOG(WARNING) << "Failed to read hive " << hive << " at " << root << "\\"
<< name << " type " << it.Type();
}
// Recurse for all subkeys.
for (RegistryKeyIterator it(hive, root.c_str()); it.Valid(); ++it) {
std::string name(base::WideToUTF8(it.Name()));
std::unique_ptr<RegistryDict> subdict(new RegistryDict());
subdict->ReadRegistry(hive, root + L"\\" + it.Name());
SetKey(name, std::move(subdict));
}
}
std::optional<base::Value> RegistryDict::ConvertToJSON(
const Schema& schema) const {
base::Value::Type type =
schema.valid() ? schema.type() : base::Value::Type::DICT;
switch (type) {
case base::Value::Type::DICT: {
base::Value::Dict result;
for (RegistryDict::ValueMap::const_iterator entry(values_.begin());
entry != values_.end(); ++entry) {
SchemaList matching_schemas =
schema.valid() ? schema.GetMatchingProperties(entry->first)
: SchemaList();
// Always try the empty schema if no other schemas exist.
if (matching_schemas.empty())
matching_schemas.push_back(Schema());
for (const Schema& subschema : matching_schemas) {
std::optional<base::Value> converted =
ConvertRegistryValue(entry->second, subschema);
if (converted.has_value()) {
result.Set(entry->first, std::move(converted.value()));
break;
}
}
}
for (RegistryDict::KeyMap::const_iterator entry(keys_.begin());
entry != keys_.end(); ++entry) {
SchemaList matching_schemas =
schema.valid() ? schema.GetMatchingProperties(entry->first)
: SchemaList();
// Always try the empty schema if no other schemas exist.
if (matching_schemas.empty())
matching_schemas.push_back(Schema());
for (const Schema& subschema : matching_schemas) {
std::optional<base::Value> converted =
entry->second->ConvertToJSON(subschema);
if (converted) {
result.Set(entry->first, std::move(*converted));
break;
}
}
}
return base::Value(std::move(result));
}
case base::Value::Type::LIST: {
base::Value::List result;
Schema item_schema = schema.valid() ? schema.GetItems() : Schema();
for (RegistryDict::KeyMap::const_iterator entry(keys_.begin());
entry != keys_.end(); ++entry) {
if (!IsKeyNumerical(entry->first))
continue;
std::optional<base::Value> converted =
entry->second->ConvertToJSON(item_schema);
if (converted)
result.Append(std::move(*converted));
}
for (RegistryDict::ValueMap::const_iterator entry(values_.begin());
entry != values_.end(); ++entry) {
if (!IsKeyNumerical(entry->first))
continue;
std::optional<base::Value> converted =
ConvertRegistryValue(entry->second, item_schema);
if (converted.has_value())
result.Append(std::move(*converted));
}
return base::Value(std::move(result));
}
default:
LOG(WARNING) << "Can't convert registry key to schema type " << type;
}
return std::nullopt;
}
#endif // #if BUILDFLAG(IS_WIN)
} // namespace policy
|