| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/installer/util/delete_reg_key_work_item.h" | 5 #include "chrome/installer/util/delete_reg_key_work_item.h" |
| 6 | 6 |
| 7 #include <shlwapi.h> | 7 #include <shlwapi.h> |
| 8 #include <algorithm> | |
| 9 #include <limits> | |
| 10 #include <vector> | |
| 11 | 8 |
| 12 #include "base/logging.h" | 9 #include "base/logging.h" |
| 13 #include "base/rand_util.h" | |
| 14 #include "base/stringprintf.h" | |
| 15 #include "base/win/registry.h" | 10 #include "base/win/registry.h" |
| 16 #include "chrome/installer/util/logging_installer.h" | |
| 17 | 11 |
| 18 using base::win::RegKey; | 12 using base::win::RegKey; |
| 19 | 13 |
| 20 namespace { | |
| 21 const REGSAM kKeyReadNoNotify = (KEY_READ) & ~(KEY_NOTIFY); | |
| 22 } | |
| 23 | |
| 24 // A container for a registry key, its values, and its subkeys. We don't use | |
| 25 // more obvious methods for various reasons: | |
| 26 // - RegCopyTree isn't supported pre-Vista, so we'd have to do something | |
| 27 // different for XP anyway. | |
| 28 // - SHCopyKey can't copy subkeys into a volatile destination, so we'd have to | |
| 29 // worry about polluting the registry. | |
| 30 // We don't persist security attributes since we only delete keys that we own, | |
| 31 // and we don't set custom attributes on them anyway. | |
| 32 class DeleteRegKeyWorkItem::RegKeyBackup { | |
| 33 public: | |
| 34 RegKeyBackup(); | |
| 35 bool Initialize(const RegKey& key); | |
| 36 bool WriteTo(RegKey* key) const; | |
| 37 | |
| 38 private: | |
| 39 // A container for a registry value. | |
| 40 class RegValueBackup { | |
| 41 public: | |
| 42 RegValueBackup(); | |
| 43 void Initialize(const wchar_t* name_buffer, DWORD name_size, | |
| 44 DWORD type, const uint8* data, DWORD data_size); | |
| 45 const std::wstring& name_str() const { return name_; } | |
| 46 const wchar_t* name() const { return name_.empty() ? NULL : name_.c_str(); } | |
| 47 DWORD type() const { return type_; } | |
| 48 const uint8* data() const { return data_.empty() ? NULL : &data_[0]; } | |
| 49 DWORD data_len() const { return static_cast<DWORD>(data_.size()); } | |
| 50 | |
| 51 private: | |
| 52 std::wstring name_; | |
| 53 std::vector<uint8> data_; | |
| 54 DWORD type_; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(RegValueBackup); | |
| 57 }; | |
| 58 | |
| 59 scoped_array<RegValueBackup> values_; | |
| 60 scoped_array<std::wstring> subkey_names_; | |
| 61 scoped_array<RegKeyBackup> subkeys_; | |
| 62 ptrdiff_t num_values_; | |
| 63 ptrdiff_t num_subkeys_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(RegKeyBackup); | |
| 66 }; | |
| 67 | |
| 68 DeleteRegKeyWorkItem::RegKeyBackup::RegValueBackup::RegValueBackup() | |
| 69 : type_(REG_NONE) { | |
| 70 } | |
| 71 | |
| 72 void DeleteRegKeyWorkItem::RegKeyBackup::RegValueBackup::Initialize( | |
| 73 const wchar_t* name_buffer, | |
| 74 DWORD name_size, | |
| 75 DWORD type, const uint8* data, | |
| 76 DWORD data_size) { | |
| 77 name_.assign(name_buffer, name_size); | |
| 78 type_ = type; | |
| 79 data_.assign(data, data + data_size); | |
| 80 } | |
| 81 | |
| 82 DeleteRegKeyWorkItem::RegKeyBackup::RegKeyBackup() | |
| 83 : num_values_(0), | |
| 84 num_subkeys_(0) { | |
| 85 } | |
| 86 | |
| 87 // Initializes this object by reading the values and subkeys of |key|. | |
| 88 // Security descriptors are not backed up. | |
| 89 bool DeleteRegKeyWorkItem::RegKeyBackup::Initialize(const RegKey& key) { | |
| 90 DCHECK(key.Valid()); | |
| 91 | |
| 92 scoped_array<RegValueBackup> values; | |
| 93 scoped_array<std::wstring> subkey_names; | |
| 94 scoped_array<RegKeyBackup> subkeys; | |
| 95 | |
| 96 DWORD num_subkeys = 0; | |
| 97 DWORD max_subkey_name_len = 0; | |
| 98 DWORD num_values = 0; | |
| 99 DWORD max_value_name_len = 0; | |
| 100 DWORD max_value_len = 0; | |
| 101 LONG result = RegQueryInfoKey(key.Handle(), NULL, NULL, NULL, | |
| 102 &num_subkeys, &max_subkey_name_len, NULL, | |
| 103 &num_values, &max_value_name_len, | |
| 104 &max_value_len, NULL, NULL); | |
| 105 if (result != ERROR_SUCCESS) { | |
| 106 LOG(ERROR) << "Failed getting info of key to backup, result: " << result; | |
| 107 return false; | |
| 108 } | |
| 109 if (max_subkey_name_len >= std::numeric_limits<DWORD>::max() - 1 || | |
| 110 max_value_name_len >= std::numeric_limits<DWORD>::max() - 1) { | |
| 111 LOG(ERROR) | |
| 112 << "Failed backing up key; subkeys and/or names are out of range."; | |
| 113 return false; | |
| 114 } | |
| 115 DWORD max_name_len = std::max(max_subkey_name_len, max_value_name_len) + 1; | |
| 116 scoped_array<wchar_t> name_buffer(new wchar_t[max_name_len]); | |
| 117 | |
| 118 // Backup the values. | |
| 119 if (num_values != 0) { | |
| 120 values.reset(new RegValueBackup[num_values]); | |
| 121 scoped_array<uint8> value_buffer(new uint8[max_value_len]); | |
| 122 DWORD name_size = 0; | |
| 123 DWORD value_type = REG_NONE; | |
| 124 DWORD value_size = 0; | |
| 125 | |
| 126 for (DWORD i = 0; i < num_values; ) { | |
| 127 name_size = max_name_len; | |
| 128 value_size = max_value_len; | |
| 129 result = RegEnumValue(key.Handle(), i, name_buffer.get(), &name_size, | |
| 130 NULL, &value_type, value_buffer.get(), &value_size); | |
| 131 switch (result) { | |
| 132 case ERROR_NO_MORE_ITEMS: | |
| 133 num_values = i; | |
| 134 break; | |
| 135 case ERROR_SUCCESS: | |
| 136 values[i].Initialize(name_buffer.get(), name_size, value_type, | |
| 137 value_buffer.get(), value_size); | |
| 138 ++i; | |
| 139 break; | |
| 140 case ERROR_MORE_DATA: | |
| 141 if (value_size > max_value_len) { | |
| 142 max_value_len = value_size; | |
| 143 value_buffer.reset(new uint8[max_value_len]); | |
| 144 } else { | |
| 145 DCHECK(max_name_len - 1 < name_size); | |
| 146 if (name_size >= std::numeric_limits<DWORD>::max() - 1) { | |
| 147 LOG(ERROR) << "Failed backing up key; value name out of range."; | |
| 148 return false; | |
| 149 } | |
| 150 max_name_len = name_size + 1; | |
| 151 name_buffer.reset(new wchar_t[max_name_len]); | |
| 152 } | |
| 153 break; | |
| 154 default: | |
| 155 LOG(ERROR) << "Failed backing up value " << i << ", result: " | |
| 156 << result; | |
| 157 return false; | |
| 158 } | |
| 159 } | |
| 160 DLOG_IF(WARNING, RegEnumValue(key.Handle(), num_values, name_buffer.get(), | |
| 161 &name_size, NULL, &value_type, NULL, | |
| 162 NULL) != ERROR_NO_MORE_ITEMS) | |
| 163 << "Concurrent modifications to registry key during backup operation."; | |
| 164 } | |
| 165 | |
| 166 // Backup the subkeys. | |
| 167 if (num_subkeys != 0) { | |
| 168 subkey_names.reset(new std::wstring[num_subkeys]); | |
| 169 subkeys.reset(new RegKeyBackup[num_subkeys]); | |
| 170 DWORD name_size = 0; | |
| 171 | |
| 172 // Get the names of them. | |
| 173 for (DWORD i = 0; i < num_subkeys; ) { | |
| 174 name_size = max_name_len; | |
| 175 result = RegEnumKeyEx(key.Handle(), i, name_buffer.get(), &name_size, | |
| 176 NULL, NULL, NULL, NULL); | |
| 177 switch (result) { | |
| 178 case ERROR_NO_MORE_ITEMS: | |
| 179 num_subkeys = i; | |
| 180 break; | |
| 181 case ERROR_SUCCESS: | |
| 182 subkey_names[i].assign(name_buffer.get(), name_size); | |
| 183 ++i; | |
| 184 break; | |
| 185 case ERROR_MORE_DATA: | |
| 186 if (name_size >= std::numeric_limits<DWORD>::max() - 1) { | |
| 187 LOG(ERROR) << "Failed backing up key; subkey name out of range."; | |
| 188 return false; | |
| 189 } | |
| 190 max_name_len = name_size + 1; | |
| 191 name_buffer.reset(new wchar_t[max_name_len]); | |
| 192 break; | |
| 193 default: | |
| 194 LOG(ERROR) << "Failed getting name of subkey " << i | |
| 195 << " for backup, result: " << result; | |
| 196 return false; | |
| 197 } | |
| 198 } | |
| 199 DLOG_IF(WARNING, | |
| 200 RegEnumKeyEx(key.Handle(), num_subkeys, NULL, &name_size, NULL, | |
| 201 NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS) | |
| 202 << "Concurrent modifications to registry key during backup operation."; | |
| 203 | |
| 204 // Get their values. | |
| 205 RegKey subkey; | |
| 206 for (DWORD i = 0; i < num_subkeys; ++i) { | |
| 207 result = subkey.Open(key.Handle(), subkey_names[i].c_str(), | |
| 208 kKeyReadNoNotify); | |
| 209 if (result != ERROR_SUCCESS) { | |
| 210 LOG(ERROR) << "Failed opening subkey \"" << subkey_names[i] | |
| 211 << "\" for backup, result: " << result; | |
| 212 return false; | |
| 213 } | |
| 214 if (!subkeys[i].Initialize(subkey)) { | |
| 215 LOG(ERROR) << "Failed backing up subkey \"" << subkey_names[i] << "\""; | |
| 216 return false; | |
| 217 } | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 values_.swap(values); | |
| 222 subkey_names_.swap(subkey_names); | |
| 223 subkeys_.swap(subkeys); | |
| 224 num_values_ = num_values; | |
| 225 num_subkeys_ = num_subkeys; | |
| 226 | |
| 227 return true; | |
| 228 } | |
| 229 | |
| 230 // Writes the values and subkeys of this object into |key|. | |
| 231 bool DeleteRegKeyWorkItem::RegKeyBackup::WriteTo(RegKey* key) const { | |
| 232 LONG result = ERROR_SUCCESS; | |
| 233 | |
| 234 // Write the values. | |
| 235 for (int i = 0; i < num_values_; ++i) { | |
| 236 const RegValueBackup& value = values_[i]; | |
| 237 result = RegSetValueEx(key->Handle(), value.name(), 0, value.type(), | |
| 238 value.data(), value.data_len()); | |
| 239 if (result != ERROR_SUCCESS) { | |
| 240 LOG(ERROR) << "Failed writing value \"" << value.name_str() | |
| 241 << "\", result: " << result; | |
| 242 return false; | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 // Write the subkeys. | |
| 247 RegKey subkey; | |
| 248 for (int i = 0; i < num_subkeys_; ++i) { | |
| 249 const std::wstring& name = subkey_names_[i]; | |
| 250 | |
| 251 result = subkey.Create(key->Handle(), name.c_str(), KEY_WRITE); | |
| 252 if (result != ERROR_SUCCESS) { | |
| 253 LOG(ERROR) << "Failed creating subkey \"" << name << "\", result: " | |
| 254 << result; | |
| 255 return false; | |
| 256 } | |
| 257 if (!subkeys_[i].WriteTo(&subkey)) { | |
| 258 LOG(ERROR) << "Failed writing subkey \"" << name << "\", result: " | |
| 259 << result; | |
| 260 return false; | |
| 261 } | |
| 262 } | |
| 263 | |
| 264 return true; | |
| 265 } | |
| 266 | |
| 267 DeleteRegKeyWorkItem::~DeleteRegKeyWorkItem() { | 14 DeleteRegKeyWorkItem::~DeleteRegKeyWorkItem() { |
| 268 } | 15 } |
| 269 | 16 |
| 270 DeleteRegKeyWorkItem::DeleteRegKeyWorkItem(HKEY predefined_root, | 17 DeleteRegKeyWorkItem::DeleteRegKeyWorkItem(HKEY predefined_root, |
| 271 const std::wstring& path) | 18 const std::wstring& path) |
| 272 : predefined_root_(predefined_root), | 19 : predefined_root_(predefined_root), |
| 273 path_(path) { | 20 path_(path) { |
| 21 DCHECK(predefined_root); |
| 274 // It's a safe bet that we don't want to delete one of the root trees. | 22 // It's a safe bet that we don't want to delete one of the root trees. |
| 275 DCHECK(!path.empty()); | 23 DCHECK(!path.empty()); |
| 276 } | 24 } |
| 277 | 25 |
| 278 bool DeleteRegKeyWorkItem::Do() { | 26 bool DeleteRegKeyWorkItem::Do() { |
| 279 scoped_ptr<RegKeyBackup> backup; | 27 if (path_.empty()) |
| 28 return false; |
| 29 |
| 30 RegistryKeyBackup backup; |
| 280 | 31 |
| 281 // Only try to make a backup if we're not configured to ignore failures. | 32 // Only try to make a backup if we're not configured to ignore failures. |
| 282 if (!ignore_failure_) { | 33 if (!ignore_failure_) { |
| 283 RegKey original_key; | 34 if (!backup.Initialize(predefined_root_, path_.c_str())) { |
| 284 | 35 LOG(ERROR) << "Failed to backup destination for registry key copy."; |
| 285 // Does the key exist? | 36 return false; |
| 286 LONG result = original_key.Open(predefined_root_, path_.c_str(), | |
| 287 kKeyReadNoNotify); | |
| 288 if (result == ERROR_SUCCESS) { | |
| 289 backup.reset(new RegKeyBackup()); | |
| 290 if (!backup->Initialize(original_key)) { | |
| 291 LOG(ERROR) << "Failed to backup key at " << path_; | |
| 292 return ignore_failure_; | |
| 293 } | |
| 294 } else if (result != ERROR_FILE_NOT_FOUND) { | |
| 295 LOG(ERROR) << "Failed to open key at " << path_ | |
| 296 << " to create backup, result: " << result; | |
| 297 return ignore_failure_; | |
| 298 } | 37 } |
| 299 } | 38 } |
| 300 | 39 |
| 301 // Delete the key. | 40 // Delete the key. |
| 302 LONG result = SHDeleteKey(predefined_root_, path_.c_str()); | 41 LONG result = SHDeleteKey(predefined_root_, path_.c_str()); |
| 303 if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) { | 42 if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) { |
| 304 LOG(ERROR) << "Failed to delete key at " << path_ << ", result: " | 43 LOG(ERROR) << "Failed to delete key at " << path_ << ", result: " |
| 305 << result; | 44 << result; |
| 306 return ignore_failure_; | 45 return ignore_failure_; |
| 307 } | 46 } |
| 308 | 47 |
| 309 // We've succeeded, so remember any backup we may have made. | 48 // We've succeeded, so remember any backup we may have made. |
| 310 backup_.swap(backup); | 49 backup_.swap(backup); |
| 311 | 50 |
| 312 return true; | 51 return true; |
| 313 } | 52 } |
| 314 | 53 |
| 315 void DeleteRegKeyWorkItem::Rollback() { | 54 void DeleteRegKeyWorkItem::Rollback() { |
| 316 if (ignore_failure_ || backup_.get() == NULL) | 55 if (ignore_failure_) |
| 317 return; | 56 return; |
| 318 | 57 |
| 319 // Delete anything in the key before restoring the backup in case someone else | 58 // Delete anything in the key before restoring the backup in case someone else |
| 320 // put new data in the key after Do(). | 59 // put new data in the key after Do(). |
| 321 LONG result = SHDeleteKey(predefined_root_, path_.c_str()); | 60 LONG result = SHDeleteKey(predefined_root_, path_.c_str()); |
| 322 if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) { | 61 if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) { |
| 323 LOG(ERROR) << "Failed to delete key at " << path_ << " in rollback, " | 62 LOG(ERROR) << "Failed to delete key at " << path_ << " in rollback, " |
| 324 "result: " << result; | 63 "result: " << result; |
| 325 } | 64 } |
| 326 | 65 |
| 327 // Restore the old contents. The restoration takes on its default security | 66 // Restore the old contents. The restoration takes on its default security |
| 328 // attributes; any custom attributes are lost. | 67 // attributes; any custom attributes are lost. |
| 329 RegKey original_key; | 68 if (!backup_.WriteTo(predefined_root_, path_.c_str())) |
| 330 result = original_key.Create(predefined_root_, path_.c_str(), KEY_WRITE); | 69 LOG(ERROR) << "Failed to restore key in rollback."; |
| 331 if (result != ERROR_SUCCESS) { | |
| 332 LOG(ERROR) << "Failed to create original key at " << path_ | |
| 333 << " in rollback, result: " << result; | |
| 334 } else { | |
| 335 if (!backup_->WriteTo(&original_key)) | |
| 336 LOG(ERROR) << "Failed to restore key in rollback, result: " << result; | |
| 337 } | |
| 338 } | 70 } |
| OLD | NEW |