Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 // This API is a usability layer for direct registry access via NTDLL. | 5 // This API is a usability layer for direct registry access via NTDLL. |
| 6 // It allows for "advapi32-free" registry access, which is especially | 6 // It allows for "advapi32-free" registry access, which is especially |
| 7 // useful for accessing registy from DllMain (holding loader lock), | 7 // useful for accessing registy from DllMain (holding loader lock), |
| 8 // or if a dependency on/linkage of ADVAPI32.dll is not desired. | 8 // or if a dependency on/linkage of ADVAPI32.dll is not desired. |
| 9 | 9 |
| 10 // The implementation of this API should only use ntdll and kernel32 system | 10 // The implementation of this API should only use ntdll and kernel32 system |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 #ifndef CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ | 22 #ifndef CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ |
| 23 #define CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ | 23 #define CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ |
| 24 | 24 |
| 25 #include <vector> | 25 #include <vector> |
| 26 | 26 |
| 27 #include "sandbox/win/src/nt_internals.h" // NTSTATUS | 27 #include "sandbox/win/src/nt_internals.h" // NTSTATUS |
| 28 | 28 |
| 29 namespace nt { | 29 namespace nt { |
| 30 | 30 |
| 31 // These globals are only used in test suites that use reg redirection | 31 // Windows registry maximum lengths (in chars). Not including null char. |
| 32 // of HKLM and/or HKCU. | 32 constexpr size_t g_kRegMaxPathLen = 255; |
|
grt (UTC plus 2)
2016/09/30 09:32:16
this is max name for a single key, not for a full
penny
2016/10/01 01:44:25
No, this is actually the max for the absolute key
| |
| 33 extern const size_t g_kRegMaxPathLen; | 33 constexpr size_t g_kRegMaxValueName = 16383; |
| 34 extern wchar_t HKLM_override[]; | |
| 35 extern wchar_t HKCU_override[]; | |
| 36 | 34 |
| 37 // AUTO will choose depending on system install or not. | 35 // AUTO will choose depending on system install or not. |
| 38 // Use HKLM or HKCU to override. | 36 // Use HKLM or HKCU to override. |
| 39 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; | 37 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; |
| 40 | 38 |
| 39 // Used with wrapper functions to request registry redirection override. | |
| 40 // Maps to KEY_WOW64_32KEY and KEY_WOW64_64KEY access flags. | |
| 41 enum WOW64_OVERRIDE { | |
| 42 NONE = 0L, | |
| 43 WOW6432 = KEY_WOW64_32KEY, | |
| 44 WOW6464 = KEY_WOW64_64KEY | |
| 45 }; | |
| 46 | |
| 47 //------------------------------------------------------------------------------ | |
| 48 // Create, open, delete, close functions | |
| 49 //------------------------------------------------------------------------------ | |
| 50 | |
| 41 // Create and/or open a registry key. | 51 // Create and/or open a registry key. |
| 42 // - This function will recursively create multiple sub-keys if required for | 52 // - This function will recursively create multiple sub-keys if required for |
| 43 // |key_path|. | 53 // |key_path|. |
| 44 // - If the key doesn't need to be left open, pass in nullptr for |out_handle|. | 54 // - If the key doesn't need to be left open, pass in nullptr for |out_handle|. |
| 45 // - This function will happily succeed if the key already exists. | 55 // - This function will happily succeed if the key already exists. |
| 46 // - Optional |out_handle|. If nullptr, function will close handle when done. | 56 // - Optional |out_handle|. If nullptr, function will close handle when done. |
| 47 // Otherwise, will hold the open handle to the deepest subkey. | 57 // Otherwise, will hold the open handle to the deepest subkey. |
| 48 // - Caller must call CloseRegKey on returned handle (on success). | 58 // - Caller must call CloseRegKey on returned handle (on success). |
| 49 bool CreateRegKey(ROOT_KEY root, | 59 bool CreateRegKey(ROOT_KEY root, |
| 50 const wchar_t* key_path, | 60 const wchar_t* key_path, |
| 51 ACCESS_MASK access, | 61 ACCESS_MASK access, |
| 52 HANDLE* out_handle OPTIONAL); | 62 HANDLE* out_handle OPTIONAL); |
| 53 | 63 |
| 54 // Open existing registry key. | 64 // Open existing registry key. |
| 55 // - Caller must call CloseRegKey on returned handle (on success). | 65 // - Caller must call CloseRegKey on returned handle (on success). |
| 56 // - Optional error code can be returned on failure for extra detail. | 66 // - Optional error code can be returned on failure for extra detail. |
| 57 bool OpenRegKey(ROOT_KEY root, | 67 bool OpenRegKey(ROOT_KEY root, |
| 58 const wchar_t* key_path, | 68 const wchar_t* key_path, |
| 59 ACCESS_MASK access, | 69 ACCESS_MASK access, |
| 60 HANDLE* out_handle, | 70 HANDLE* out_handle, |
| 61 NTSTATUS* error_code OPTIONAL); | 71 NTSTATUS* error_code OPTIONAL); |
| 62 | 72 |
| 63 // Delete a registry key. | 73 // Delete a registry key. |
| 64 // - Caller must still call CloseRegKey after the delete. | 74 // - Caller must still call CloseRegKey after the delete. |
| 65 bool DeleteRegKey(HANDLE key); | 75 bool DeleteRegKey(HANDLE key); |
| 66 | 76 |
| 67 // Delete a registry key. | 77 // Delete a registry key. |
| 68 // - WRAPPER: Function opens and closes the target key for caller. | 78 // - WRAPPER: Function opens and closes the target key for caller. |
| 69 bool DeleteRegKey(ROOT_KEY root, const wchar_t* key_path); | 79 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. |
| 80 bool DeleteRegKey(ROOT_KEY root, | |
| 81 WOW64_OVERRIDE wow64_override, | |
| 82 const wchar_t* key_path); | |
| 70 | 83 |
| 71 // Close a registry key handle that was opened with CreateRegKey or OpenRegKey. | 84 // Close a registry key handle that was opened with CreateRegKey or OpenRegKey. |
| 72 void CloseRegKey(HANDLE key); | 85 void CloseRegKey(HANDLE key); |
| 73 | 86 |
| 74 //------------------------------------------------------------------------------ | 87 //------------------------------------------------------------------------------ |
| 75 // Getter functions | 88 // Getter functions |
| 76 //------------------------------------------------------------------------------ | 89 //------------------------------------------------------------------------------ |
| 77 | 90 |
| 78 // Main function to query a registry value. | 91 // Main function to query a registry value. |
| 79 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 92 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 80 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. | 93 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. |
| 81 // - Caller is responsible for calling "delete[] *out_buffer" (on success). | 94 // - Caller is responsible for calling "delete[] *out_buffer" (on success). |
| 82 bool QueryRegKeyValue(HANDLE key, | 95 bool QueryRegKeyValue(HANDLE key, |
| 83 const wchar_t* value_name, | 96 const wchar_t* value_name, |
| 84 ULONG* out_type, | 97 ULONG* out_type, |
| 85 BYTE** out_buffer, | 98 BYTE** out_buffer, |
| 86 DWORD* out_size); | 99 DWORD* out_size); |
| 87 | 100 |
| 88 // Query DWORD value. | 101 // Query DWORD value. |
| 89 // - WRAPPER: Function works with DWORD data type. | 102 // - WRAPPER: Function works with DWORD data type. |
| 90 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 103 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 91 // - Handle will be left open. Caller must still call CloseRegKey when done. | 104 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 92 bool QueryRegValueDWORD(HANDLE key, | 105 bool QueryRegValueDWORD(HANDLE key, |
| 93 const wchar_t* value_name, | 106 const wchar_t* value_name, |
| 94 DWORD* out_dword); | 107 DWORD* out_dword); |
| 95 | 108 |
| 96 // Query DWORD value. | 109 // Query DWORD value. |
| 97 // - WRAPPER: Function opens and closes the target key for caller, and works | 110 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 98 // with DWORD data type. | 111 // with DWORD data type. |
| 112 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
| 99 bool QueryRegValueDWORD(ROOT_KEY root, | 113 bool QueryRegValueDWORD(ROOT_KEY root, |
| 114 WOW64_OVERRIDE wow64_override, | |
| 100 const wchar_t* key_path, | 115 const wchar_t* key_path, |
| 101 const wchar_t* value_name, | 116 const wchar_t* value_name, |
| 102 DWORD* out_dword); | 117 DWORD* out_dword); |
| 103 | 118 |
| 104 // Query SZ (string) value. | 119 // Query SZ (string) value. |
| 105 // - WRAPPER: Function works with SZ data type. | 120 // - WRAPPER: Function works with SZ data type. |
| 106 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 121 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 107 // - Handle will be left open. Caller must still call CloseRegKey when done. | 122 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 108 bool QueryRegValueSZ(HANDLE key, | 123 bool QueryRegValueSZ(HANDLE key, |
| 109 const wchar_t* value_name, | 124 const wchar_t* value_name, |
| 110 std::wstring* out_sz); | 125 std::wstring* out_sz); |
| 111 | 126 |
| 112 // Query SZ (string) value. | 127 // Query SZ (string) value. |
| 113 // - WRAPPER: Function opens and closes the target key for caller, and works | 128 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 114 // with SZ data type. | 129 // with SZ data type. |
| 130 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
| 115 bool QueryRegValueSZ(ROOT_KEY root, | 131 bool QueryRegValueSZ(ROOT_KEY root, |
| 132 WOW64_OVERRIDE wow64_override, | |
| 116 const wchar_t* key_path, | 133 const wchar_t* key_path, |
| 117 const wchar_t* value_name, | 134 const wchar_t* value_name, |
| 118 std::wstring* out_sz); | 135 std::wstring* out_sz); |
| 119 | 136 |
| 120 // Query MULTI_SZ (multiple strings) value. | 137 // Query MULTI_SZ (multiple strings) value. |
| 121 // - WRAPPER: Function works with MULTI_SZ data type. | 138 // - WRAPPER: Function works with MULTI_SZ data type. |
| 122 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 139 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 123 // - Handle will be left open. Caller must still call CloseRegKey when done. | 140 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 124 bool QueryRegValueMULTISZ(HANDLE key, | 141 bool QueryRegValueMULTISZ(HANDLE key, |
| 125 const wchar_t* value_name, | 142 const wchar_t* value_name, |
| 126 std::vector<std::wstring>* out_multi_sz); | 143 std::vector<std::wstring>* out_multi_sz); |
| 127 | 144 |
| 128 // Query MULTI_SZ (multiple strings) value. | 145 // Query MULTI_SZ (multiple strings) value. |
| 129 // - WRAPPER: Function opens and closes the target key for caller, and works | 146 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 130 // with MULTI_SZ data type. | 147 // with MULTI_SZ data type. |
| 148 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
| 131 bool QueryRegValueMULTISZ(ROOT_KEY root, | 149 bool QueryRegValueMULTISZ(ROOT_KEY root, |
| 150 WOW64_OVERRIDE wow64_override, | |
| 132 const wchar_t* key_path, | 151 const wchar_t* key_path, |
| 133 const wchar_t* value_name, | 152 const wchar_t* value_name, |
| 134 std::vector<std::wstring>* out_multi_sz); | 153 std::vector<std::wstring>* out_multi_sz); |
| 135 | 154 |
| 136 //------------------------------------------------------------------------------ | 155 //------------------------------------------------------------------------------ |
| 137 // Setter functions | 156 // Setter functions |
| 138 //------------------------------------------------------------------------------ | 157 //------------------------------------------------------------------------------ |
| 139 | 158 |
| 140 // Main function to set a registry value. | 159 // Main function to set a registry value. |
| 141 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 160 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 142 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. | 161 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. |
| 143 bool SetRegKeyValue(HANDLE key, | 162 bool SetRegKeyValue(HANDLE key, |
| 144 const wchar_t* value_name, | 163 const wchar_t* value_name, |
| 145 ULONG type, | 164 ULONG type, |
| 146 const BYTE* data, | 165 const BYTE* data, |
| 147 DWORD data_size); | 166 DWORD data_size); |
| 148 | 167 |
| 149 // Set DWORD value. | 168 // Set DWORD value. |
| 150 // - WRAPPER: Function works with DWORD data type. | 169 // - WRAPPER: Function works with DWORD data type. |
| 151 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 170 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 152 // - Handle will be left open. Caller must still call CloseRegKey when done. | 171 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 153 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); | 172 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); |
| 154 | 173 |
| 155 // Set DWORD value. | 174 // Set DWORD value. |
| 156 // - WRAPPER: Function opens and closes the target key for caller, and works | 175 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 157 // with DWORD data type. | 176 // with DWORD data type. |
| 177 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
| 158 bool SetRegValueDWORD(ROOT_KEY root, | 178 bool SetRegValueDWORD(ROOT_KEY root, |
| 179 WOW64_OVERRIDE wow64_override, | |
| 159 const wchar_t* key_path, | 180 const wchar_t* key_path, |
| 160 const wchar_t* value_name, | 181 const wchar_t* value_name, |
| 161 DWORD value); | 182 DWORD value); |
| 162 | 183 |
| 163 // Set SZ (string) value. | 184 // Set SZ (string) value. |
| 164 // - WRAPPER: Function works with SZ data type. | 185 // - WRAPPER: Function works with SZ data type. |
| 165 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 186 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 166 // - Handle will be left open. Caller must still call CloseRegKey when done. | 187 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 167 bool SetRegValueSZ(HANDLE key, | 188 bool SetRegValueSZ(HANDLE key, |
| 168 const wchar_t* value_name, | 189 const wchar_t* value_name, |
| 169 const std::wstring& value); | 190 const std::wstring& value); |
| 170 | 191 |
| 171 // Set SZ (string) value. | 192 // Set SZ (string) value. |
| 172 // - WRAPPER: Function opens and closes the target key for caller, and works | 193 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 173 // with SZ data type. | 194 // with SZ data type. |
| 195 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
| 174 bool SetRegValueSZ(ROOT_KEY root, | 196 bool SetRegValueSZ(ROOT_KEY root, |
| 197 WOW64_OVERRIDE wow64_override, | |
| 175 const wchar_t* key_path, | 198 const wchar_t* key_path, |
| 176 const wchar_t* value_name, | 199 const wchar_t* value_name, |
| 177 const std::wstring& value); | 200 const std::wstring& value); |
| 178 | 201 |
| 179 // Set MULTI_SZ (multiple strings) value. | 202 // Set MULTI_SZ (multiple strings) value. |
| 180 // - WRAPPER: Function works with MULTI_SZ data type. | 203 // - WRAPPER: Function works with MULTI_SZ data type. |
| 181 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 204 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 182 // - Handle will be left open. Caller must still call CloseRegKey when done. | 205 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 183 bool SetRegValueMULTISZ(HANDLE key, | 206 bool SetRegValueMULTISZ(HANDLE key, |
| 184 const wchar_t* value_name, | 207 const wchar_t* value_name, |
| 185 const std::vector<std::wstring>& values); | 208 const std::vector<std::wstring>& values); |
| 186 | 209 |
| 187 // Set MULTI_SZ (multiple strings) value. | 210 // Set MULTI_SZ (multiple strings) value. |
| 188 // - WRAPPER: Function opens and closes the target key for caller, and works | 211 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 189 // with MULTI_SZ data type. | 212 // with MULTI_SZ data type. |
| 213 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
| 190 bool SetRegValueMULTISZ(ROOT_KEY root, | 214 bool SetRegValueMULTISZ(ROOT_KEY root, |
| 215 WOW64_OVERRIDE wow64_override, | |
| 191 const wchar_t* key_path, | 216 const wchar_t* key_path, |
| 192 const wchar_t* value_name, | 217 const wchar_t* value_name, |
| 193 const std::vector<std::wstring>& values); | 218 const std::vector<std::wstring>& values); |
| 194 | 219 |
| 195 //------------------------------------------------------------------------------ | 220 //------------------------------------------------------------------------------ |
| 196 // Utils | 221 // Utils |
| 197 //------------------------------------------------------------------------------ | 222 //------------------------------------------------------------------------------ |
| 198 | 223 |
| 199 // Returns the current user SID in string form. | 224 // Returns the current user SID in string form. |
| 200 const wchar_t* GetCurrentUserSidString(); | 225 const wchar_t* GetCurrentUserSidString(); |
| 201 | 226 |
| 227 // Returns true if this process is WOW64. | |
| 228 bool IsCurrentProcWow64(); | |
| 229 | |
| 230 // Setter function for test suites that use reg redirection | |
| 231 // of HKLM and/or HKCU. | |
| 232 bool SetTestingOverride(nt::ROOT_KEY root, const std::wstring& new_path); | |
| 233 | |
| 234 // Getter function for test suites that use reg redirection | |
| 235 // of HKLM and/or HKCU. | |
| 236 void GetTestingOverride(nt::ROOT_KEY root, std::wstring* out_path); | |
|
grt (UTC plus 2)
2016/09/30 09:32:16
nit: we generally return strings as values rather
penny
2016/10/01 01:44:25
Good point, well made.
| |
| 237 | |
| 202 }; // namespace nt | 238 }; // namespace nt |
| 203 | 239 |
| 204 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ | 240 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ |
| OLD | NEW |