| 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 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724872(v=vs.85).as
px |
| 33 extern const size_t g_kRegMaxPathLen; | 33 constexpr size_t g_kRegMaxPathLen = 255; |
| 34 extern wchar_t HKLM_override[]; | 34 constexpr size_t g_kRegMaxValueName = 16383; |
| 35 extern wchar_t HKCU_override[]; | |
| 36 | 35 |
| 37 // AUTO will choose depending on system install or not. | 36 // AUTO will choose depending on system install or not. |
| 38 // Use HKLM or HKCU to override. | 37 // Use HKLM or HKCU to override. |
| 39 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; | 38 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; |
| 40 | 39 |
| 40 // Used with wrapper functions to request registry redirection override. |
| 41 // Maps to KEY_WOW64_32KEY and KEY_WOW64_64KEY access flags. |
| 42 enum WOW64_OVERRIDE { |
| 43 NONE = 0L, |
| 44 WOW6432 = KEY_WOW64_32KEY, |
| 45 WOW6464 = KEY_WOW64_64KEY |
| 46 }; |
| 47 |
| 48 //------------------------------------------------------------------------------ |
| 49 // Create, open, delete, close functions |
| 50 //------------------------------------------------------------------------------ |
| 51 |
| 41 // Create and/or open a registry key. | 52 // Create and/or open a registry key. |
| 42 // - This function will recursively create multiple sub-keys if required for | 53 // - This function will recursively create multiple sub-keys if required for |
| 43 // |key_path|. | 54 // |key_path|. |
| 44 // - If the key doesn't need to be left open, pass in nullptr for |out_handle|. | 55 // - 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. | 56 // - This function will happily succeed if the key already exists. |
| 46 // - Optional |out_handle|. If nullptr, function will close handle when done. | 57 // - Optional |out_handle|. If nullptr, function will close handle when done. |
| 47 // Otherwise, will hold the open handle to the deepest subkey. | 58 // Otherwise, will hold the open handle to the deepest subkey. |
| 48 // - Caller must call CloseRegKey on returned handle (on success). | 59 // - Caller must call CloseRegKey on returned handle (on success). |
| 49 bool CreateRegKey(ROOT_KEY root, | 60 bool CreateRegKey(ROOT_KEY root, |
| 50 const wchar_t* key_path, | 61 const wchar_t* key_path, |
| 51 ACCESS_MASK access, | 62 ACCESS_MASK access, |
| 52 HANDLE* out_handle OPTIONAL); | 63 HANDLE* out_handle OPTIONAL); |
| 53 | 64 |
| 54 // Open existing registry key. | 65 // Open existing registry key. |
| 55 // - Caller must call CloseRegKey on returned handle (on success). | 66 // - Caller must call CloseRegKey on returned handle (on success). |
| 56 // - Optional error code can be returned on failure for extra detail. | 67 // - Optional error code can be returned on failure for extra detail. |
| 57 bool OpenRegKey(ROOT_KEY root, | 68 bool OpenRegKey(ROOT_KEY root, |
| 58 const wchar_t* key_path, | 69 const wchar_t* key_path, |
| 59 ACCESS_MASK access, | 70 ACCESS_MASK access, |
| 60 HANDLE* out_handle, | 71 HANDLE* out_handle, |
| 61 NTSTATUS* error_code OPTIONAL); | 72 NTSTATUS* error_code OPTIONAL); |
| 62 | 73 |
| 63 // Delete a registry key. | 74 // Delete a registry key. |
| 64 // - Caller must still call CloseRegKey after the delete. | 75 // - Caller must still call CloseRegKey after the delete. |
| 65 bool DeleteRegKey(HANDLE key); | 76 bool DeleteRegKey(HANDLE key); |
| 66 | 77 |
| 67 // Delete a registry key. | 78 // Delete a registry key. |
| 68 // - WRAPPER: Function opens and closes the target key for caller. | 79 // - WRAPPER: Function opens and closes the target key for caller. |
| 69 bool DeleteRegKey(ROOT_KEY root, const wchar_t* key_path); | 80 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 81 bool DeleteRegKey(ROOT_KEY root, |
| 82 WOW64_OVERRIDE wow64_override, |
| 83 const wchar_t* key_path); |
| 70 | 84 |
| 71 // Close a registry key handle that was opened with CreateRegKey or OpenRegKey. | 85 // Close a registry key handle that was opened with CreateRegKey or OpenRegKey. |
| 72 void CloseRegKey(HANDLE key); | 86 void CloseRegKey(HANDLE key); |
| 73 | 87 |
| 74 //------------------------------------------------------------------------------ | 88 //------------------------------------------------------------------------------ |
| 75 // Getter functions | 89 // Getter functions |
| 76 //------------------------------------------------------------------------------ | 90 //------------------------------------------------------------------------------ |
| 77 | 91 |
| 78 // Main function to query a registry value. | 92 // Main function to query a registry value. |
| 79 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 93 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 80 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. | 94 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. |
| 81 // - Caller is responsible for calling "delete[] *out_buffer" (on success). | 95 // - Caller is responsible for calling "delete[] *out_buffer" (on success). |
| 82 bool QueryRegKeyValue(HANDLE key, | 96 bool QueryRegKeyValue(HANDLE key, |
| 83 const wchar_t* value_name, | 97 const wchar_t* value_name, |
| 84 ULONG* out_type, | 98 ULONG* out_type, |
| 85 BYTE** out_buffer, | 99 BYTE** out_buffer, |
| 86 DWORD* out_size); | 100 DWORD* out_size); |
| 87 | 101 |
| 88 // Query DWORD value. | 102 // Query DWORD value. |
| 89 // - WRAPPER: Function works with DWORD data type. | 103 // - WRAPPER: Function works with DWORD data type. |
| 90 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 104 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 91 // - Handle will be left open. Caller must still call CloseRegKey when done. | 105 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 92 bool QueryRegValueDWORD(HANDLE key, | 106 bool QueryRegValueDWORD(HANDLE key, |
| 93 const wchar_t* value_name, | 107 const wchar_t* value_name, |
| 94 DWORD* out_dword); | 108 DWORD* out_dword); |
| 95 | 109 |
| 96 // Query DWORD value. | 110 // Query DWORD value. |
| 97 // - WRAPPER: Function opens and closes the target key for caller, and works | 111 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 98 // with DWORD data type. | 112 // with DWORD data type. |
| 113 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 99 bool QueryRegValueDWORD(ROOT_KEY root, | 114 bool QueryRegValueDWORD(ROOT_KEY root, |
| 115 WOW64_OVERRIDE wow64_override, |
| 100 const wchar_t* key_path, | 116 const wchar_t* key_path, |
| 101 const wchar_t* value_name, | 117 const wchar_t* value_name, |
| 102 DWORD* out_dword); | 118 DWORD* out_dword); |
| 103 | 119 |
| 104 // Query SZ (string) value. | 120 // Query SZ (string) value. |
| 105 // - WRAPPER: Function works with SZ data type. | 121 // - WRAPPER: Function works with SZ data type. |
| 106 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 122 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 107 // - Handle will be left open. Caller must still call CloseRegKey when done. | 123 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 108 bool QueryRegValueSZ(HANDLE key, | 124 bool QueryRegValueSZ(HANDLE key, |
| 109 const wchar_t* value_name, | 125 const wchar_t* value_name, |
| 110 std::wstring* out_sz); | 126 std::wstring* out_sz); |
| 111 | 127 |
| 112 // Query SZ (string) value. | 128 // Query SZ (string) value. |
| 113 // - WRAPPER: Function opens and closes the target key for caller, and works | 129 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 114 // with SZ data type. | 130 // with SZ data type. |
| 131 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 115 bool QueryRegValueSZ(ROOT_KEY root, | 132 bool QueryRegValueSZ(ROOT_KEY root, |
| 133 WOW64_OVERRIDE wow64_override, |
| 116 const wchar_t* key_path, | 134 const wchar_t* key_path, |
| 117 const wchar_t* value_name, | 135 const wchar_t* value_name, |
| 118 std::wstring* out_sz); | 136 std::wstring* out_sz); |
| 119 | 137 |
| 120 // Query MULTI_SZ (multiple strings) value. | 138 // Query MULTI_SZ (multiple strings) value. |
| 121 // - WRAPPER: Function works with MULTI_SZ data type. | 139 // - WRAPPER: Function works with MULTI_SZ data type. |
| 122 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 140 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 123 // - Handle will be left open. Caller must still call CloseRegKey when done. | 141 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 124 bool QueryRegValueMULTISZ(HANDLE key, | 142 bool QueryRegValueMULTISZ(HANDLE key, |
| 125 const wchar_t* value_name, | 143 const wchar_t* value_name, |
| 126 std::vector<std::wstring>* out_multi_sz); | 144 std::vector<std::wstring>* out_multi_sz); |
| 127 | 145 |
| 128 // Query MULTI_SZ (multiple strings) value. | 146 // Query MULTI_SZ (multiple strings) value. |
| 129 // - WRAPPER: Function opens and closes the target key for caller, and works | 147 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 130 // with MULTI_SZ data type. | 148 // with MULTI_SZ data type. |
| 149 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 131 bool QueryRegValueMULTISZ(ROOT_KEY root, | 150 bool QueryRegValueMULTISZ(ROOT_KEY root, |
| 151 WOW64_OVERRIDE wow64_override, |
| 132 const wchar_t* key_path, | 152 const wchar_t* key_path, |
| 133 const wchar_t* value_name, | 153 const wchar_t* value_name, |
| 134 std::vector<std::wstring>* out_multi_sz); | 154 std::vector<std::wstring>* out_multi_sz); |
| 135 | 155 |
| 136 //------------------------------------------------------------------------------ | 156 //------------------------------------------------------------------------------ |
| 137 // Setter functions | 157 // Setter functions |
| 138 //------------------------------------------------------------------------------ | 158 //------------------------------------------------------------------------------ |
| 139 | 159 |
| 140 // Main function to set a registry value. | 160 // Main function to set a registry value. |
| 141 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 161 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 142 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. | 162 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. |
| 143 bool SetRegKeyValue(HANDLE key, | 163 bool SetRegKeyValue(HANDLE key, |
| 144 const wchar_t* value_name, | 164 const wchar_t* value_name, |
| 145 ULONG type, | 165 ULONG type, |
| 146 const BYTE* data, | 166 const BYTE* data, |
| 147 DWORD data_size); | 167 DWORD data_size); |
| 148 | 168 |
| 149 // Set DWORD value. | 169 // Set DWORD value. |
| 150 // - WRAPPER: Function works with DWORD data type. | 170 // - WRAPPER: Function works with DWORD data type. |
| 151 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 171 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 152 // - Handle will be left open. Caller must still call CloseRegKey when done. | 172 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 153 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); | 173 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); |
| 154 | 174 |
| 155 // Set DWORD value. | 175 // Set DWORD value. |
| 156 // - WRAPPER: Function opens and closes the target key for caller, and works | 176 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 157 // with DWORD data type. | 177 // with DWORD data type. |
| 178 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 158 bool SetRegValueDWORD(ROOT_KEY root, | 179 bool SetRegValueDWORD(ROOT_KEY root, |
| 180 WOW64_OVERRIDE wow64_override, |
| 159 const wchar_t* key_path, | 181 const wchar_t* key_path, |
| 160 const wchar_t* value_name, | 182 const wchar_t* value_name, |
| 161 DWORD value); | 183 DWORD value); |
| 162 | 184 |
| 163 // Set SZ (string) value. | 185 // Set SZ (string) value. |
| 164 // - WRAPPER: Function works with SZ data type. | 186 // - WRAPPER: Function works with SZ data type. |
| 165 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 187 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 166 // - Handle will be left open. Caller must still call CloseRegKey when done. | 188 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 167 bool SetRegValueSZ(HANDLE key, | 189 bool SetRegValueSZ(HANDLE key, |
| 168 const wchar_t* value_name, | 190 const wchar_t* value_name, |
| 169 const std::wstring& value); | 191 const std::wstring& value); |
| 170 | 192 |
| 171 // Set SZ (string) value. | 193 // Set SZ (string) value. |
| 172 // - WRAPPER: Function opens and closes the target key for caller, and works | 194 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 173 // with SZ data type. | 195 // with SZ data type. |
| 196 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 174 bool SetRegValueSZ(ROOT_KEY root, | 197 bool SetRegValueSZ(ROOT_KEY root, |
| 198 WOW64_OVERRIDE wow64_override, |
| 175 const wchar_t* key_path, | 199 const wchar_t* key_path, |
| 176 const wchar_t* value_name, | 200 const wchar_t* value_name, |
| 177 const std::wstring& value); | 201 const std::wstring& value); |
| 178 | 202 |
| 179 // Set MULTI_SZ (multiple strings) value. | 203 // Set MULTI_SZ (multiple strings) value. |
| 180 // - WRAPPER: Function works with MULTI_SZ data type. | 204 // - WRAPPER: Function works with MULTI_SZ data type. |
| 181 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 205 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 182 // - Handle will be left open. Caller must still call CloseRegKey when done. | 206 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 183 bool SetRegValueMULTISZ(HANDLE key, | 207 bool SetRegValueMULTISZ(HANDLE key, |
| 184 const wchar_t* value_name, | 208 const wchar_t* value_name, |
| 185 const std::vector<std::wstring>& values); | 209 const std::vector<std::wstring>& values); |
| 186 | 210 |
| 187 // Set MULTI_SZ (multiple strings) value. | 211 // Set MULTI_SZ (multiple strings) value. |
| 188 // - WRAPPER: Function opens and closes the target key for caller, and works | 212 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 189 // with MULTI_SZ data type. | 213 // with MULTI_SZ data type. |
| 214 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 190 bool SetRegValueMULTISZ(ROOT_KEY root, | 215 bool SetRegValueMULTISZ(ROOT_KEY root, |
| 216 WOW64_OVERRIDE wow64_override, |
| 191 const wchar_t* key_path, | 217 const wchar_t* key_path, |
| 192 const wchar_t* value_name, | 218 const wchar_t* value_name, |
| 193 const std::vector<std::wstring>& values); | 219 const std::vector<std::wstring>& values); |
| 194 | 220 |
| 195 //------------------------------------------------------------------------------ | 221 //------------------------------------------------------------------------------ |
| 196 // Utils | 222 // Utils |
| 197 //------------------------------------------------------------------------------ | 223 //------------------------------------------------------------------------------ |
| 198 | 224 |
| 199 // Returns the current user SID in string form. | 225 // Returns the current user SID in string form. |
| 200 const wchar_t* GetCurrentUserSidString(); | 226 const wchar_t* GetCurrentUserSidString(); |
| 201 | 227 |
| 228 // Returns true if this process is WOW64. |
| 229 bool IsCurrentProcWow64(); |
| 230 |
| 231 // Setter function for test suites that use reg redirection. |
| 232 bool SetTestingOverride(ROOT_KEY root, const std::wstring& new_path); |
| 233 |
| 234 // Getter function for test suites that use reg redirection. |
| 235 std::wstring GetTestingOverride(ROOT_KEY root); |
| 236 |
| 202 }; // namespace nt | 237 }; // namespace nt |
| 203 | 238 |
| 204 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ | 239 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ |
| OLD | NEW |