2017-09-08 08:02:43 +00:00
|
|
|
/* Copyright (c) Mark Harmstone 2016-17
|
|
|
|
*
|
2016-09-04 15:27:46 +00:00
|
|
|
* This file is part of WinBtrfs.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2016-09-04 15:27:46 +00:00
|
|
|
* WinBtrfs is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public Licence as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the Licence, or
|
|
|
|
* (at your option) any later version.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2016-09-04 15:27:46 +00:00
|
|
|
* WinBtrfs is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public Licence for more details.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2016-09-04 15:27:46 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public Licence
|
|
|
|
* along with WinBtrfs. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
#include "btrfs_drv.h"
|
2018-12-16 11:03:16 +00:00
|
|
|
#include "zstd/zstd.h"
|
2016-07-27 19:24:26 +00:00
|
|
|
|
|
|
|
extern UNICODE_STRING log_device, log_file, registry_path;
|
2017-09-08 08:02:43 +00:00
|
|
|
extern LIST_ENTRY uid_map_list, gid_map_list;
|
|
|
|
extern ERESOURCE mapping_lock;
|
2016-07-27 19:24:26 +00:00
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
extern HANDLE log_handle;
|
|
|
|
extern ERESOURCE log_lock;
|
|
|
|
extern PFILE_OBJECT comfo;
|
|
|
|
extern PDEVICE_OBJECT comdo;
|
|
|
|
#endif
|
2016-07-27 19:24:26 +00:00
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
WORK_QUEUE_ITEM wqi;
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
static const WCHAR option_mounted[] = L"Mounted";
|
2016-07-27 19:24:26 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
NTSTATUS registry_load_volume_options(device_extension* Vcb) {
|
|
|
|
BTRFS_UUID* uuid = &Vcb->superblock.uuid;
|
|
|
|
mount_options* options = &Vcb->options;
|
|
|
|
UNICODE_STRING path, ignoreus, compressus, compressforceus, compresstypeus, readonlyus, zliblevelus, flushintervalus,
|
2019-11-12 18:32:46 +00:00
|
|
|
maxinlineus, subvolidus, skipbalanceus, nobarrierus, notrimus, clearcacheus, allowdegradedus, zstdlevelus,
|
2022-09-28 16:08:10 +00:00
|
|
|
norootdirus, nodatacowus;
|
2016-07-27 19:24:26 +00:00
|
|
|
OBJECT_ATTRIBUTES oa;
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG i, j, kvfilen, index, retlen;
|
|
|
|
KEY_VALUE_FULL_INFORMATION* kvfi = NULL;
|
|
|
|
HANDLE h;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
options->compress = mount_compress;
|
|
|
|
options->compress_force = mount_compress_force;
|
2018-12-16 11:03:16 +00:00
|
|
|
options->compress_type = mount_compress_type > BTRFS_COMPRESSION_ZSTD ? 0 : mount_compress_type;
|
2017-09-08 08:02:43 +00:00
|
|
|
options->readonly = mount_readonly;
|
2016-09-04 15:27:46 +00:00
|
|
|
options->zlib_level = mount_zlib_level;
|
2018-12-16 11:03:16 +00:00
|
|
|
options->zstd_level = mount_zstd_level;
|
2016-09-04 15:27:46 +00:00
|
|
|
options->flush_interval = mount_flush_interval;
|
|
|
|
options->max_inline = min(mount_max_inline, Vcb->superblock.node_size - sizeof(tree_header) - sizeof(leaf_node) - sizeof(EXTENT_DATA) + 1);
|
2017-01-01 17:12:12 +00:00
|
|
|
options->skip_balance = mount_skip_balance;
|
2017-09-08 08:02:43 +00:00
|
|
|
options->no_barrier = mount_no_barrier;
|
|
|
|
options->no_trim = mount_no_trim;
|
|
|
|
options->clear_cache = mount_clear_cache;
|
|
|
|
options->allow_degraded = mount_allow_degraded;
|
2016-09-04 15:27:46 +00:00
|
|
|
options->subvol_id = 0;
|
2022-09-28 16:08:10 +00:00
|
|
|
options->no_root_dir = mount_no_root_dir;
|
|
|
|
options->nodatacow = mount_nodatacow;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
path.Length = path.MaximumLength = registry_path.Length + (37 * sizeof(WCHAR));
|
|
|
|
path.Buffer = ExAllocatePoolWithTag(PagedPool, path.Length, ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!path.Buffer) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RtlCopyMemory(path.Buffer, registry_path.Buffer, registry_path.Length);
|
|
|
|
i = registry_path.Length / sizeof(WCHAR);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
path.Buffer[i] = '\\';
|
|
|
|
i++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
path.Buffer[i] = hex_digit((uuid->uuid[j] & 0xF0) >> 4);
|
|
|
|
path.Buffer[i+1] = hex_digit(uuid->uuid[j] & 0xF);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
i += 2;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (j == 3 || j == 5 || j == 7 || j == 9) {
|
|
|
|
path.Buffer[i] = '-';
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
kvfilen = sizeof(KEY_VALUE_FULL_INFORMATION) - sizeof(WCHAR) + (255 * sizeof(WCHAR));
|
|
|
|
kvfi = ExAllocatePoolWithTag(PagedPool, kvfilen, ALLOC_TAG);
|
|
|
|
if (!kvfi) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
InitializeObjectAttributes(&oa, &path, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwOpenKey(&h, KEY_QUERY_VALUE, &oa);
|
|
|
|
if (Status == STATUS_OBJECT_NAME_NOT_FOUND) {
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
goto end;
|
|
|
|
} else if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwOpenKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
index = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
RtlInitUnicodeString(&ignoreus, L"Ignore");
|
|
|
|
RtlInitUnicodeString(&compressus, L"Compress");
|
|
|
|
RtlInitUnicodeString(&compressforceus, L"CompressForce");
|
|
|
|
RtlInitUnicodeString(&compresstypeus, L"CompressType");
|
|
|
|
RtlInitUnicodeString(&readonlyus, L"Readonly");
|
|
|
|
RtlInitUnicodeString(&zliblevelus, L"ZlibLevel");
|
|
|
|
RtlInitUnicodeString(&flushintervalus, L"FlushInterval");
|
|
|
|
RtlInitUnicodeString(&maxinlineus, L"MaxInline");
|
|
|
|
RtlInitUnicodeString(&subvolidus, L"SubvolId");
|
2017-01-01 17:12:12 +00:00
|
|
|
RtlInitUnicodeString(&skipbalanceus, L"SkipBalance");
|
2017-09-08 08:02:43 +00:00
|
|
|
RtlInitUnicodeString(&nobarrierus, L"NoBarrier");
|
|
|
|
RtlInitUnicodeString(¬rimus, L"NoTrim");
|
|
|
|
RtlInitUnicodeString(&clearcacheus, L"ClearCache");
|
|
|
|
RtlInitUnicodeString(&allowdegradedus, L"AllowDegraded");
|
2018-12-16 11:03:16 +00:00
|
|
|
RtlInitUnicodeString(&zstdlevelus, L"ZstdLevel");
|
2019-11-12 18:32:46 +00:00
|
|
|
RtlInitUnicodeString(&norootdirus, L"NoRootDir");
|
2022-09-28 16:08:10 +00:00
|
|
|
RtlInitUnicodeString(&nodatacowus, L"NoDataCOW");
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
do {
|
|
|
|
Status = ZwEnumerateValueKey(h, index, KeyValueFullInformation, kvfi, kvfilen, &retlen);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
index++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (NT_SUCCESS(Status)) {
|
|
|
|
UNICODE_STRING us;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
us.Length = us.MaximumLength = (USHORT)kvfi->NameLength;
|
2016-07-27 19:24:26 +00:00
|
|
|
us.Buffer = kvfi->Name;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
if (FsRtlAreNamesEqual(&ignoreus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
options->ignore = *val != 0 ? true : false;
|
|
|
|
} else if (FsRtlAreNamesEqual(&compressus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
options->compress = *val != 0 ? true : false;
|
|
|
|
} else if (FsRtlAreNamesEqual(&compressforceus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
options->compress_force = *val != 0 ? true : false;
|
|
|
|
} else if (FsRtlAreNamesEqual(&compresstypeus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
options->compress_type = (uint8_t)(*val > BTRFS_COMPRESSION_ZSTD ? 0 : *val);
|
|
|
|
} else if (FsRtlAreNamesEqual(&readonlyus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
options->readonly = *val != 0 ? true : false;
|
|
|
|
} else if (FsRtlAreNamesEqual(&zliblevelus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
options->zlib_level = *val;
|
2019-09-01 12:53:20 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(&flushintervalus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
options->flush_interval = *val;
|
2019-09-01 12:53:20 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(&maxinlineus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
options->max_inline = min(*val, Vcb->superblock.node_size - sizeof(tree_header) - sizeof(leaf_node) - sizeof(EXTENT_DATA) + 1);
|
2019-09-01 12:53:20 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(&subvolidus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_QWORD) {
|
|
|
|
uint64_t* val = (uint64_t*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
options->subvol_id = *val;
|
2019-09-01 12:53:20 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(&skipbalanceus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
options->skip_balance = *val;
|
2019-09-01 12:53:20 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(&nobarrierus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
options->no_barrier = *val;
|
2019-09-01 12:53:20 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(¬rimus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
options->no_trim = *val;
|
2019-09-01 12:53:20 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(&clearcacheus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
options->clear_cache = *val;
|
2019-09-01 12:53:20 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(&allowdegradedus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
options->allow_degraded = *val;
|
2019-09-01 12:53:20 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(&zstdlevelus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
2018-12-16 11:03:16 +00:00
|
|
|
|
|
|
|
options->zstd_level = *val;
|
2019-11-12 18:32:46 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(&norootdirus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
|
|
|
|
|
|
|
options->no_root_dir = *val;
|
2022-09-28 16:08:10 +00:00
|
|
|
} else if (FsRtlAreNamesEqual(&nodatacowus, &us, true, NULL) && kvfi->DataOffset > 0 && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
|
|
|
DWORD* val = (DWORD*)((uint8_t*)kvfi + kvfi->DataOffset);
|
|
|
|
|
|
|
|
options->nodatacow = *val;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
|
|
|
} else if (Status != STATUS_NO_MORE_ENTRIES) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwEnumerateValueKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto end2;
|
|
|
|
}
|
|
|
|
} while (NT_SUCCESS(Status));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
if (!options->compress && options->compress_force)
|
2019-09-01 12:53:20 +00:00
|
|
|
options->compress = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
if (options->zlib_level > 9)
|
|
|
|
options->zlib_level = 9;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
if (options->zstd_level > (uint32_t)ZSTD_maxCLevel())
|
2018-12-16 11:03:16 +00:00
|
|
|
options->zstd_level = ZSTD_maxCLevel();
|
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
if (options->flush_interval == 0)
|
|
|
|
options->flush_interval = mount_flush_interval;
|
2016-07-27 19:24:26 +00:00
|
|
|
|
|
|
|
Status = STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
end2:
|
|
|
|
ZwClose(h);
|
|
|
|
|
|
|
|
end:
|
|
|
|
ExFreePool(path.Buffer);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (kvfi)
|
|
|
|
ExFreePool(kvfi);
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS registry_mark_volume_mounted(BTRFS_UUID* uuid) {
|
|
|
|
UNICODE_STRING path, mountedus;
|
|
|
|
ULONG i, j;
|
|
|
|
NTSTATUS Status;
|
|
|
|
OBJECT_ATTRIBUTES oa;
|
|
|
|
HANDLE h;
|
|
|
|
DWORD data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
path.Length = path.MaximumLength = registry_path.Length + (37 * sizeof(WCHAR));
|
|
|
|
path.Buffer = ExAllocatePoolWithTag(PagedPool, path.Length, ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!path.Buffer) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RtlCopyMemory(path.Buffer, registry_path.Buffer, registry_path.Length);
|
|
|
|
i = registry_path.Length / sizeof(WCHAR);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
path.Buffer[i] = '\\';
|
|
|
|
i++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
path.Buffer[i] = hex_digit((uuid->uuid[j] & 0xF0) >> 4);
|
|
|
|
path.Buffer[i+1] = hex_digit(uuid->uuid[j] & 0xF);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
i += 2;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (j == 3 || j == 5 || j == 7 || j == 9) {
|
|
|
|
path.Buffer[i] = '-';
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InitializeObjectAttributes(&oa, &path, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwCreateKey(&h, KEY_SET_VALUE, &oa, 0, NULL, REG_OPTION_NON_VOLATILE, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwCreateKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
mountedus.Buffer = (WCHAR*)option_mounted;
|
|
|
|
mountedus.Length = mountedus.MaximumLength = sizeof(option_mounted) - sizeof(WCHAR);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
data = 1;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwSetValueKey(h, &mountedus, 0, REG_DWORD, &data, sizeof(DWORD));
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwSetValueKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto end2;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
end2:
|
|
|
|
ZwClose(h);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
end:
|
|
|
|
ExFreePool(path.Buffer);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS registry_mark_volume_unmounted_path(PUNICODE_STRING path) {
|
|
|
|
HANDLE h;
|
|
|
|
OBJECT_ATTRIBUTES oa;
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG index, kvbilen = sizeof(KEY_VALUE_BASIC_INFORMATION) - sizeof(WCHAR) + (255 * sizeof(WCHAR)), retlen;
|
|
|
|
KEY_VALUE_BASIC_INFORMATION* kvbi;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool has_options = false;
|
2016-07-27 19:24:26 +00:00
|
|
|
UNICODE_STRING mountedus;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// If a volume key has any options in it, we set Mounted to 0 and return. Otherwise,
|
|
|
|
// we delete the whole thing.
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
kvbi = ExAllocatePoolWithTag(PagedPool, kvbilen, ALLOC_TAG);
|
|
|
|
if (!kvbi) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
InitializeObjectAttributes(&oa, path, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwOpenKey(&h, KEY_QUERY_VALUE | KEY_SET_VALUE | DELETE, &oa);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwOpenKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
index = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
mountedus.Buffer = (WCHAR*)option_mounted;
|
|
|
|
mountedus.Length = mountedus.MaximumLength = sizeof(option_mounted) - sizeof(WCHAR);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
do {
|
|
|
|
Status = ZwEnumerateValueKey(h, index, KeyValueBasicInformation, kvbi, kvbilen, &retlen);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
index++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (NT_SUCCESS(Status)) {
|
|
|
|
UNICODE_STRING us;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
us.Length = us.MaximumLength = (USHORT)kvbi->NameLength;
|
2016-07-27 19:24:26 +00:00
|
|
|
us.Buffer = kvbi->Name;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
if (!FsRtlAreNamesEqual(&mountedus, &us, true, NULL)) {
|
|
|
|
has_options = true;
|
2016-07-27 19:24:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (Status != STATUS_NO_MORE_ENTRIES) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwEnumerateValueKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto end2;
|
|
|
|
}
|
|
|
|
} while (NT_SUCCESS(Status));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (has_options) {
|
|
|
|
DWORD data = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwSetValueKey(h, &mountedus, 0, REG_DWORD, &data, sizeof(DWORD));
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwSetValueKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto end2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Status = ZwDeleteKey(h);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwDeleteKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto end2;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
end2:
|
|
|
|
ZwClose(h);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
end:
|
|
|
|
ExFreePool(kvbi);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS registry_mark_volume_unmounted(BTRFS_UUID* uuid) {
|
|
|
|
UNICODE_STRING path;
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG i, j;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
path.Length = path.MaximumLength = registry_path.Length + (37 * sizeof(WCHAR));
|
|
|
|
path.Buffer = ExAllocatePoolWithTag(PagedPool, path.Length, ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!path.Buffer) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RtlCopyMemory(path.Buffer, registry_path.Buffer, registry_path.Length);
|
|
|
|
i = registry_path.Length / sizeof(WCHAR);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
path.Buffer[i] = '\\';
|
|
|
|
i++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
path.Buffer[i] = hex_digit((uuid->uuid[j] & 0xF0) >> 4);
|
|
|
|
path.Buffer[i+1] = hex_digit(uuid->uuid[j] & 0xF);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
i += 2;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (j == 3 || j == 5 || j == 7 || j == 9) {
|
|
|
|
path.Buffer[i] = '-';
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = registry_mark_volume_unmounted_path(&path);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("registry_mark_volume_unmounted_path returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
end:
|
|
|
|
ExFreePool(path.Buffer);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define is_hex(c) ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static bool is_uuid(ULONG namelen, WCHAR* name) {
|
2016-07-27 19:24:26 +00:00
|
|
|
ULONG i;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (namelen != 36 * sizeof(WCHAR))
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
for (i = 0; i < 36; i++) {
|
|
|
|
if (i == 8 || i == 13 || i == 18 || i == 23) {
|
|
|
|
if (name[i] != '-')
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2016-07-27 19:24:26 +00:00
|
|
|
} else if (!is_hex(name[i]))
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
return true;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
UNICODE_STRING name;
|
|
|
|
LIST_ENTRY list_entry;
|
|
|
|
} key_name;
|
|
|
|
|
|
|
|
static void reset_subkeys(HANDLE h, PUNICODE_STRING reg_path) {
|
|
|
|
NTSTATUS Status;
|
|
|
|
KEY_BASIC_INFORMATION* kbi;
|
|
|
|
ULONG kbilen = sizeof(KEY_BASIC_INFORMATION) - sizeof(WCHAR) + (255 * sizeof(WCHAR)), retlen, index = 0;
|
|
|
|
LIST_ENTRY key_names, *le;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
InitializeListHead(&key_names);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
kbi = ExAllocatePoolWithTag(PagedPool, kbilen, ALLOC_TAG);
|
|
|
|
if (!kbi) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
do {
|
|
|
|
Status = ZwEnumerateKey(h, index, KeyBasicInformation, kbi, kbilen, &retlen);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
index++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (NT_SUCCESS(Status)) {
|
|
|
|
key_name* kn;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2020-04-23 02:38:57 +00:00
|
|
|
TRACE("key: %.*S\n", (int)(kbi->NameLength / sizeof(WCHAR)), kbi->Name);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (is_uuid(kbi->NameLength, kbi->Name)) {
|
|
|
|
kn = ExAllocatePoolWithTag(PagedPool, sizeof(key_name), ALLOC_TAG);
|
|
|
|
if (!kn) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
kn->name.Length = kn->name.MaximumLength = (USHORT)min(0xffff, kbi->NameLength);
|
|
|
|
kn->name.Buffer = ExAllocatePoolWithTag(PagedPool, kn->name.MaximumLength, ALLOC_TAG);
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!kn->name.Buffer) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
ExFreePool(kn);
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
RtlCopyMemory(kn->name.Buffer, kbi->Name, kn->name.Length);
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
InsertTailList(&key_names, &kn->list_entry);
|
|
|
|
}
|
|
|
|
} else if (Status != STATUS_NO_MORE_ENTRIES)
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwEnumerateKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
} while (NT_SUCCESS(Status));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = key_names.Flink;
|
|
|
|
while (le != &key_names) {
|
|
|
|
key_name* kn = CONTAINING_RECORD(le, key_name, list_entry);
|
|
|
|
UNICODE_STRING path;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
path.Length = path.MaximumLength = reg_path->Length + sizeof(WCHAR) + kn->name.Length;
|
|
|
|
path.Buffer = ExAllocatePoolWithTag(PagedPool, path.Length, ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!path.Buffer) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RtlCopyMemory(path.Buffer, reg_path->Buffer, reg_path->Length);
|
|
|
|
path.Buffer[reg_path->Length / sizeof(WCHAR)] = '\\';
|
|
|
|
RtlCopyMemory(&path.Buffer[(reg_path->Length / sizeof(WCHAR)) + 1], kn->name.Buffer, kn->name.Length);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = registry_mark_volume_unmounted_path(&path);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
WARN("registry_mark_volume_unmounted_path returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(path.Buffer);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
end:
|
|
|
|
while (!IsListEmpty(&key_names)) {
|
|
|
|
key_name* kn;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = RemoveHeadList(&key_names);
|
|
|
|
kn = CONTAINING_RECORD(le, key_name, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (kn->name.Buffer)
|
|
|
|
ExFreePool(kn->name.Buffer);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(kn);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExFreePool(kbi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_mappings(PUNICODE_STRING regpath) {
|
|
|
|
WCHAR* path;
|
|
|
|
UNICODE_STRING us;
|
|
|
|
HANDLE h;
|
|
|
|
OBJECT_ATTRIBUTES oa;
|
|
|
|
ULONG dispos;
|
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
static const WCHAR mappings[] = L"\\Mappings";
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
while (!IsListEmpty(&uid_map_list)) {
|
|
|
|
uid_map* um = CONTAINING_RECORD(RemoveHeadList(&uid_map_list), uid_map, listentry);
|
|
|
|
|
|
|
|
if (um->sid) ExFreePool(um->sid);
|
|
|
|
ExFreePool(um);
|
|
|
|
}
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
path = ExAllocatePoolWithTag(PagedPool, regpath->Length + sizeof(mappings) - sizeof(WCHAR), ALLOC_TAG);
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!path) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RtlCopyMemory(path, regpath->Buffer, regpath->Length);
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlCopyMemory((uint8_t*)path + regpath->Length, mappings, sizeof(mappings) - sizeof(WCHAR));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
us.Buffer = path;
|
2018-12-16 11:03:16 +00:00
|
|
|
us.Length = us.MaximumLength = regpath->Length + sizeof(mappings) - sizeof(WCHAR);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
InitializeObjectAttributes(&oa, &us, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwCreateKey(&h, KEY_QUERY_VALUE, &oa, 0, NULL, REG_OPTION_NON_VOLATILE, &dispos);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwCreateKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dispos == REG_OPENED_EXISTING_KEY) {
|
2017-09-08 08:02:43 +00:00
|
|
|
KEY_VALUE_FULL_INFORMATION* kvfi;
|
|
|
|
ULONG kvfilen, retlen, i;
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
kvfilen = sizeof(KEY_VALUE_FULL_INFORMATION) + 256;
|
|
|
|
kvfi = ExAllocatePoolWithTag(PagedPool, kvfilen, ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!kvfi) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
ExFreePool(path);
|
|
|
|
ZwClose(h);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
Status = ZwEnumerateValueKey(h, i, KeyValueFullInformation, kvfi, kvfilen, &retlen);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (NT_SUCCESS(Status) && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint32_t val = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlCopyMemory(&val, (uint8_t*)kvfi + kvfi->DataOffset, min(kvfi->DataLength, sizeof(uint32_t)));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2020-04-23 02:38:57 +00:00
|
|
|
TRACE("entry %lu = %.*S = %u\n", i, (int)(kvfi->NameLength / sizeof(WCHAR)), kvfi->Name, val);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
add_user_mapping(kvfi->Name, kvfi->NameLength / sizeof(WCHAR), val);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
i = i + 1;
|
|
|
|
} while (Status != STATUS_NO_MORE_ENTRIES);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExFreePool(kvfi);
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ZwClose(h);
|
|
|
|
|
|
|
|
ExFreePool(path);
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static void read_group_mappings(PUNICODE_STRING regpath) {
|
|
|
|
WCHAR* path;
|
|
|
|
UNICODE_STRING us;
|
|
|
|
HANDLE h;
|
|
|
|
OBJECT_ATTRIBUTES oa;
|
|
|
|
ULONG dispos;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
static const WCHAR mappings[] = L"\\GroupMappings";
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
while (!IsListEmpty(&gid_map_list)) {
|
|
|
|
gid_map* gm = CONTAINING_RECORD(RemoveHeadList(&gid_map_list), gid_map, listentry);
|
|
|
|
|
|
|
|
if (gm->sid) ExFreePool(gm->sid);
|
|
|
|
ExFreePool(gm);
|
|
|
|
}
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
path = ExAllocatePoolWithTag(PagedPool, regpath->Length + sizeof(mappings) - sizeof(WCHAR), ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!path) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RtlCopyMemory(path, regpath->Buffer, regpath->Length);
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlCopyMemory((uint8_t*)path + regpath->Length, mappings, sizeof(mappings) - sizeof(WCHAR));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
us.Buffer = path;
|
2018-12-16 11:03:16 +00:00
|
|
|
us.Length = us.MaximumLength = regpath->Length + sizeof(mappings) - sizeof(WCHAR);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
InitializeObjectAttributes(&oa, &us, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
|
|
|
|
|
|
|
|
Status = ZwCreateKey(&h, KEY_QUERY_VALUE, &oa, 0, NULL, REG_OPTION_NON_VOLATILE, &dispos);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwCreateKey returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExFreePool(path);
|
|
|
|
|
|
|
|
if (dispos == REG_OPENED_EXISTING_KEY) {
|
|
|
|
KEY_VALUE_FULL_INFORMATION* kvfi;
|
|
|
|
ULONG kvfilen, retlen, i;
|
|
|
|
|
|
|
|
kvfilen = sizeof(KEY_VALUE_FULL_INFORMATION) + 256;
|
|
|
|
kvfi = ExAllocatePoolWithTag(PagedPool, kvfilen, ALLOC_TAG);
|
|
|
|
|
|
|
|
if (!kvfi) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
ZwClose(h);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
Status = ZwEnumerateValueKey(h, i, KeyValueFullInformation, kvfi, kvfilen, &retlen);
|
|
|
|
|
|
|
|
if (NT_SUCCESS(Status) && kvfi->DataLength > 0 && kvfi->Type == REG_DWORD) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint32_t val = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlCopyMemory(&val, (uint8_t*)kvfi + kvfi->DataOffset, min(kvfi->DataLength, sizeof(uint32_t)));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2020-04-23 02:38:57 +00:00
|
|
|
TRACE("entry %lu = %.*S = %u\n", i, (int)(kvfi->NameLength / sizeof(WCHAR)), kvfi->Name, val);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
add_group_mapping(kvfi->Name, kvfi->NameLength / sizeof(WCHAR), val);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = i + 1;
|
|
|
|
} while (Status != STATUS_NO_MORE_ENTRIES);
|
|
|
|
|
|
|
|
ExFreePool(kvfi);
|
|
|
|
} else if (dispos == REG_CREATED_NEW_KEY) {
|
2020-04-23 02:38:57 +00:00
|
|
|
static const WCHAR builtin_users[] = L"S-1-5-32-545";
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
UNICODE_STRING us2;
|
|
|
|
DWORD val;
|
|
|
|
|
|
|
|
// If we're creating the key for the first time, we add a default mapping of
|
|
|
|
// BUILTIN\Users to gid 100, which ought to correspond to the "users" group on Linux.
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
us2.Length = us2.MaximumLength = sizeof(builtin_users) - sizeof(WCHAR);
|
2017-09-08 08:02:43 +00:00
|
|
|
us2.Buffer = ExAllocatePoolWithTag(PagedPool, us2.MaximumLength, ALLOC_TAG);
|
|
|
|
|
|
|
|
if (us2.Buffer) {
|
|
|
|
RtlCopyMemory(us2.Buffer, builtin_users, us2.Length);
|
|
|
|
|
|
|
|
val = 100;
|
|
|
|
Status = ZwSetValueKey(h, &us2, 0, REG_DWORD, &val, sizeof(DWORD));
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwSetValueKey returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ZwClose(h);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_group_mapping(us2.Buffer, us2.Length / sizeof(WCHAR), val);
|
|
|
|
|
|
|
|
ExFreePool(us2.Buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ZwClose(h);
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
static void get_registry_value(HANDLE h, WCHAR* string, ULONG type, void* val, ULONG size) {
|
2016-07-27 19:24:26 +00:00
|
|
|
ULONG kvfilen;
|
|
|
|
KEY_VALUE_FULL_INFORMATION* kvfi;
|
2016-09-04 15:27:46 +00:00
|
|
|
UNICODE_STRING us;
|
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
RtlInitUnicodeString(&us, string);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
kvfi = NULL;
|
|
|
|
kvfilen = 0;
|
|
|
|
Status = ZwQueryValueKey(h, &us, KeyValueFullInformation, kvfi, kvfilen, &kvfilen);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if ((Status == STATUS_BUFFER_TOO_SMALL || Status == STATUS_BUFFER_OVERFLOW) && kvfilen > 0) {
|
|
|
|
kvfi = ExAllocatePoolWithTag(PagedPool, kvfilen, ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!kvfi) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
ZwClose(h);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwQueryValueKey(h, &us, KeyValueFullInformation, kvfi, kvfilen, &kvfilen);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (NT_SUCCESS(Status)) {
|
2016-09-04 15:27:46 +00:00
|
|
|
if (kvfi->Type == type && kvfi->DataLength >= size) {
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlCopyMemory(val, ((uint8_t*)kvfi) + kvfi->DataOffset, size);
|
2016-07-27 19:24:26 +00:00
|
|
|
} else {
|
|
|
|
Status = ZwDeleteValueKey(h, &us);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwDeleteValueKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
Status = ZwSetValueKey(h, &us, 0, type, val, size);
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwSetValueKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(kvfi);
|
|
|
|
} else if (Status == STATUS_OBJECT_NAME_NOT_FOUND) {
|
2016-09-04 15:27:46 +00:00
|
|
|
Status = ZwSetValueKey(h, &us, 0, type, val, size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwSetValueKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwQueryValueKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2016-09-04 15:27:46 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
void read_registry(PUNICODE_STRING regpath, bool refresh) {
|
2016-09-04 15:27:46 +00:00
|
|
|
OBJECT_ATTRIBUTES oa;
|
|
|
|
NTSTATUS Status;
|
|
|
|
HANDLE h;
|
|
|
|
ULONG dispos;
|
2017-09-08 08:02:43 +00:00
|
|
|
#ifdef _DEBUG
|
2016-09-04 15:27:46 +00:00
|
|
|
KEY_VALUE_FULL_INFORMATION* kvfi;
|
2017-09-08 08:02:43 +00:00
|
|
|
ULONG kvfilen, old_debug_log_level = debug_log_level;
|
|
|
|
UNICODE_STRING us, old_log_file, old_log_device;
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
static const WCHAR def_log_file[] = L"\\??\\C:\\btrfs.log";
|
2016-09-04 15:27:46 +00:00
|
|
|
#endif
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&mapping_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
read_mappings(regpath);
|
2017-09-08 08:02:43 +00:00
|
|
|
read_group_mappings(regpath);
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&mapping_lock);
|
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
InitializeObjectAttributes(&oa, regpath, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
Status = ZwCreateKey(&h, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &oa, 0, NULL, REG_OPTION_NON_VOLATILE, &dispos);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwCreateKey returned %08lx\n", Status);
|
2016-09-04 15:27:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!refresh)
|
|
|
|
reset_subkeys(h, regpath);
|
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
get_registry_value(h, L"Compress", REG_DWORD, &mount_compress, sizeof(mount_compress));
|
|
|
|
get_registry_value(h, L"CompressForce", REG_DWORD, &mount_compress_force, sizeof(mount_compress_force));
|
|
|
|
get_registry_value(h, L"CompressType", REG_DWORD, &mount_compress_type, sizeof(mount_compress_type));
|
|
|
|
get_registry_value(h, L"ZlibLevel", REG_DWORD, &mount_zlib_level, sizeof(mount_zlib_level));
|
|
|
|
get_registry_value(h, L"FlushInterval", REG_DWORD, &mount_flush_interval, sizeof(mount_flush_interval));
|
|
|
|
get_registry_value(h, L"MaxInline", REG_DWORD, &mount_max_inline, sizeof(mount_max_inline));
|
2017-01-01 17:12:12 +00:00
|
|
|
get_registry_value(h, L"SkipBalance", REG_DWORD, &mount_skip_balance, sizeof(mount_skip_balance));
|
2017-09-08 08:02:43 +00:00
|
|
|
get_registry_value(h, L"NoBarrier", REG_DWORD, &mount_no_barrier, sizeof(mount_no_barrier));
|
|
|
|
get_registry_value(h, L"NoTrim", REG_DWORD, &mount_no_trim, sizeof(mount_no_trim));
|
|
|
|
get_registry_value(h, L"ClearCache", REG_DWORD, &mount_clear_cache, sizeof(mount_clear_cache));
|
|
|
|
get_registry_value(h, L"AllowDegraded", REG_DWORD, &mount_allow_degraded, sizeof(mount_allow_degraded));
|
|
|
|
get_registry_value(h, L"Readonly", REG_DWORD, &mount_readonly, sizeof(mount_readonly));
|
2018-12-16 11:03:16 +00:00
|
|
|
get_registry_value(h, L"ZstdLevel", REG_DWORD, &mount_zstd_level, sizeof(mount_zstd_level));
|
2019-11-12 18:32:46 +00:00
|
|
|
get_registry_value(h, L"NoRootDir", REG_DWORD, &mount_no_root_dir, sizeof(mount_no_root_dir));
|
2022-09-28 16:08:10 +00:00
|
|
|
get_registry_value(h, L"NoDataCOW", REG_DWORD, &mount_nodatacow, sizeof(mount_nodatacow));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!refresh)
|
|
|
|
get_registry_value(h, L"NoPNP", REG_DWORD, &no_pnp, sizeof(no_pnp));
|
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
if (mount_flush_interval == 0)
|
|
|
|
mount_flush_interval = 1;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
get_registry_value(h, L"DebugLogLevel", REG_DWORD, &debug_log_level, sizeof(debug_log_level));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RtlInitUnicodeString(&us, L"LogDevice");
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
kvfi = NULL;
|
|
|
|
kvfilen = 0;
|
|
|
|
Status = ZwQueryValueKey(h, &us, KeyValueFullInformation, kvfi, kvfilen, &kvfilen);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
old_log_device = log_device;
|
|
|
|
|
|
|
|
log_device.Length = log_device.MaximumLength = 0;
|
|
|
|
log_device.Buffer = NULL;
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if ((Status == STATUS_BUFFER_TOO_SMALL || Status == STATUS_BUFFER_OVERFLOW) && kvfilen > 0) {
|
|
|
|
kvfi = ExAllocatePoolWithTag(PagedPool, kvfilen, ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!kvfi) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
ZwClose(h);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwQueryValueKey(h, &us, KeyValueFullInformation, kvfi, kvfilen, &kvfilen);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (NT_SUCCESS(Status)) {
|
|
|
|
if ((kvfi->Type == REG_SZ || kvfi->Type == REG_EXPAND_SZ) && kvfi->DataLength >= sizeof(WCHAR)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
log_device.Length = log_device.MaximumLength = (USHORT)min(0xffff, kvfi->DataLength);
|
|
|
|
log_device.Buffer = ExAllocatePoolWithTag(PagedPool, log_device.MaximumLength, ALLOC_TAG);
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!log_device.Buffer) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
ExFreePool(kvfi);
|
|
|
|
ZwClose(h);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlCopyMemory(log_device.Buffer, ((uint8_t*)kvfi) + kvfi->DataOffset, log_device.Length);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (log_device.Buffer[(log_device.Length / sizeof(WCHAR)) - 1] == 0)
|
|
|
|
log_device.Length -= sizeof(WCHAR);
|
|
|
|
} else {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("LogDevice was type %lu, length %lu\n", kvfi->Type, kvfi->DataLength);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwDeleteValueKey(h, &us);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwDeleteValueKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(kvfi);
|
|
|
|
} else if (Status != STATUS_OBJECT_NAME_NOT_FOUND) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwQueryValueKey returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&log_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (refresh && (log_device.Length != old_log_device.Length || RtlCompareMemory(log_device.Buffer, old_log_device.Buffer, log_device.Length) != log_device.Length ||
|
|
|
|
(!comfo && log_device.Length > 0) || (old_debug_log_level == 0 && debug_log_level > 0) || (old_debug_log_level > 0 && debug_log_level == 0))) {
|
|
|
|
if (comfo)
|
|
|
|
ObDereferenceObject(comfo);
|
|
|
|
|
|
|
|
if (log_handle) {
|
|
|
|
ZwClose(log_handle);
|
|
|
|
log_handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
comfo = NULL;
|
|
|
|
comdo = NULL;
|
|
|
|
|
|
|
|
if (log_device.Length > 0 && debug_log_level > 0) {
|
|
|
|
Status = IoGetDeviceObjectPointer(&log_device, FILE_WRITE_DATA, &comfo, &comdo);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
DbgPrint("IoGetDeviceObjectPointer returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&log_lock);
|
|
|
|
|
|
|
|
if (old_log_device.Buffer)
|
|
|
|
ExFreePool(old_log_device.Buffer);
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RtlInitUnicodeString(&us, L"LogFile");
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
kvfi = NULL;
|
|
|
|
kvfilen = 0;
|
|
|
|
Status = ZwQueryValueKey(h, &us, KeyValueFullInformation, kvfi, kvfilen, &kvfilen);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
old_log_file = log_file;
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if ((Status == STATUS_BUFFER_TOO_SMALL || Status == STATUS_BUFFER_OVERFLOW) && kvfilen > 0) {
|
|
|
|
kvfi = ExAllocatePoolWithTag(PagedPool, kvfilen, ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!kvfi) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
ZwClose(h);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwQueryValueKey(h, &us, KeyValueFullInformation, kvfi, kvfilen, &kvfilen);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (NT_SUCCESS(Status)) {
|
|
|
|
if ((kvfi->Type == REG_SZ || kvfi->Type == REG_EXPAND_SZ) && kvfi->DataLength >= sizeof(WCHAR)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
log_file.Length = log_file.MaximumLength = (USHORT)min(0xffff, kvfi->DataLength);
|
|
|
|
log_file.Buffer = ExAllocatePoolWithTag(PagedPool, log_file.MaximumLength, ALLOC_TAG);
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!log_file.Buffer) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
ExFreePool(kvfi);
|
|
|
|
ZwClose(h);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlCopyMemory(log_file.Buffer, ((uint8_t*)kvfi) + kvfi->DataOffset, log_file.Length);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (log_file.Buffer[(log_file.Length / sizeof(WCHAR)) - 1] == 0)
|
|
|
|
log_file.Length -= sizeof(WCHAR);
|
|
|
|
} else {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("LogFile was type %lu, length %lu\n", kvfi->Type, kvfi->DataLength);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = ZwDeleteValueKey(h, &us);
|
2018-05-26 08:44:36 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwDeleteValueKey returned %08lx\n", Status);
|
2018-05-26 08:44:36 +00:00
|
|
|
|
|
|
|
log_file.Length = 0;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2018-05-26 08:44:36 +00:00
|
|
|
} else {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwQueryValueKey returned %08lx\n", Status);
|
2018-05-26 08:44:36 +00:00
|
|
|
log_file.Length = 0;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(kvfi);
|
|
|
|
} else if (Status == STATUS_OBJECT_NAME_NOT_FOUND) {
|
2018-12-16 11:03:16 +00:00
|
|
|
Status = ZwSetValueKey(h, &us, 0, REG_SZ, (void*)def_log_file, sizeof(def_log_file));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-05-26 08:44:36 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwSetValueKey returned %08lx\n", Status);
|
2018-05-26 08:44:36 +00:00
|
|
|
|
|
|
|
log_file.Length = 0;
|
2016-07-27 19:24:26 +00:00
|
|
|
} else {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwQueryValueKey returned %08lx\n", Status);
|
2018-05-26 08:44:36 +00:00
|
|
|
log_file.Length = 0;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (log_file.Length == 0) {
|
2018-12-16 11:03:16 +00:00
|
|
|
log_file.Length = log_file.MaximumLength = sizeof(def_log_file) - sizeof(WCHAR);
|
2016-07-27 19:24:26 +00:00
|
|
|
log_file.Buffer = ExAllocatePoolWithTag(PagedPool, log_file.MaximumLength, ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!log_file.Buffer) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
ZwClose(h);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RtlCopyMemory(log_file.Buffer, def_log_file, log_file.Length);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&log_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (refresh && (log_file.Length != old_log_file.Length || RtlCompareMemory(log_file.Buffer, old_log_file.Buffer, log_file.Length) != log_file.Length ||
|
|
|
|
(!log_handle && log_file.Length > 0) || (old_debug_log_level == 0 && debug_log_level > 0) || (old_debug_log_level > 0 && debug_log_level == 0))) {
|
|
|
|
if (log_handle) {
|
|
|
|
ZwClose(log_handle);
|
|
|
|
log_handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!comfo && log_file.Length > 0 && refresh && debug_log_level > 0) {
|
|
|
|
IO_STATUS_BLOCK iosb;
|
|
|
|
|
|
|
|
InitializeObjectAttributes(&oa, &log_file, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
|
|
|
|
|
|
|
|
Status = ZwCreateFile(&log_handle, FILE_WRITE_DATA, &oa, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
|
|
|
|
FILE_OPEN_IF, FILE_NON_DIRECTORY_FILE | FILE_WRITE_THROUGH | FILE_SYNCHRONOUS_IO_ALERT, NULL, 0);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
DbgPrint("ZwCreateFile returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
log_handle = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&log_lock);
|
|
|
|
|
|
|
|
if (old_log_file.Buffer)
|
|
|
|
ExFreePool(old_log_file.Buffer);
|
2016-07-27 19:24:26 +00:00
|
|
|
#endif
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ZwClose(h);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
_Function_class_(WORKER_THREAD_ROUTINE)
|
2019-09-01 12:53:20 +00:00
|
|
|
static void __stdcall registry_work_item(PVOID Parameter) {
|
2017-09-08 08:02:43 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
HANDLE regh = (HANDLE)Parameter;
|
|
|
|
IO_STATUS_BLOCK iosb;
|
|
|
|
|
|
|
|
TRACE("registry changed\n");
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
read_registry(®istry_path, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = ZwNotifyChangeKey(regh, NULL, (PVOID)&wqi, (PVOID)DelayedWorkQueue, &iosb, REG_NOTIFY_CHANGE_LAST_SET, true, NULL, 0, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwNotifyChangeKey returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void watch_registry(HANDLE regh) {
|
|
|
|
NTSTATUS Status;
|
|
|
|
IO_STATUS_BLOCK iosb;
|
|
|
|
|
|
|
|
ExInitializeWorkItem(&wqi, registry_work_item, regh);
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = ZwNotifyChangeKey(regh, NULL, (PVOID)&wqi, (PVOID)DelayedWorkQueue, &iosb, REG_NOTIFY_CHANGE_LAST_SET, true, NULL, 0, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("ZwNotifyChangeKey returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|