95 lines
4.0 KiB
C
95 lines
4.0 KiB
C
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
#pragma once
|
|
#include <windows.h>
|
|
|
|
// Programs whose threads must never receive the native menu/input hooks,
|
|
// named by executable file name. Readers pass one --exclude-hook-program
|
|
// argument per program; the 64-bit helper forwards the same list to the
|
|
// 32-bit hook host through WINE_ACCESS_EXCLUDE_HOOK_PROGRAMS (newline-joined),
|
|
// an internal channel the reader never sets. The bounds mirror the
|
|
// readers' own validation so either side rejects the same names. Newlines
|
|
// are rejected in names, so semicolons in executable names remain intact.
|
|
#define WINE_ACCESS_MAX_HOOK_EXCLUSIONS 256
|
|
#define WINE_ACCESS_MAX_HOOK_EXCLUSION_LENGTH 255
|
|
|
|
typedef struct WineAccessHookExclusions {
|
|
WCHAR names[WINE_ACCESS_MAX_HOOK_EXCLUSIONS][WINE_ACCESS_MAX_HOOK_EXCLUSION_LENGTH + 1];
|
|
int count;
|
|
} WineAccessHookExclusions;
|
|
|
|
static inline BOOL wine_access_hook_exclusion_valid(const WCHAR *name) {
|
|
if (!name) return FALSE;
|
|
int length = 0;
|
|
while (length <= WINE_ACCESS_MAX_HOOK_EXCLUSION_LENGTH && name[length]) ++length;
|
|
if (!length || length > WINE_ACCESS_MAX_HOOK_EXCLUSION_LENGTH) return FALSE;
|
|
if (name[0] <= L' ' || name[length - 1] <= L' ') return FALSE;
|
|
for (int index = 0; index < length; ++index)
|
|
if (name[index] < L' ' || name[index] == L'/' || name[index] == L'\\') return FALSE;
|
|
return TRUE;
|
|
}
|
|
|
|
static inline BOOL wine_access_hook_exclusions_add(WineAccessHookExclusions *list,
|
|
const WCHAR *name) {
|
|
if (!list || !wine_access_hook_exclusion_valid(name)) return FALSE;
|
|
for (int index = 0; index < list->count; ++index)
|
|
if (!lstrcmpiW(list->names[index], name)) return TRUE;
|
|
if (list->count >= WINE_ACCESS_MAX_HOOK_EXCLUSIONS) return FALSE;
|
|
lstrcpyW(list->names[list->count++], name);
|
|
return TRUE;
|
|
}
|
|
|
|
static inline BOOL wine_access_hook_exclusions_match(const WineAccessHookExclusions *list,
|
|
const WCHAR *program) {
|
|
if (!list || !program) return FALSE;
|
|
const WCHAR *name = program;
|
|
for (const WCHAR *cursor = program; *cursor; ++cursor)
|
|
if (*cursor == L'\\' || *cursor == L'/') name = cursor + 1;
|
|
if (!*name) return FALSE;
|
|
for (int index = 0; index < list->count; ++index)
|
|
if (!lstrcmpiW(list->names[index], name)) return TRUE;
|
|
return FALSE;
|
|
}
|
|
|
|
static inline int wine_access_hook_exclusions_parse(WineAccessHookExclusions *list,
|
|
const WCHAR *value) {
|
|
if (!list || !value) return 0;
|
|
const int before = list->count;
|
|
const WCHAR *start = value;
|
|
for (;;) {
|
|
const WCHAR *end = start;
|
|
while (*end && *end != L'\n') ++end;
|
|
const int length = (int)(end - start);
|
|
if (length && length <= WINE_ACCESS_MAX_HOOK_EXCLUSION_LENGTH) {
|
|
WCHAR name[WINE_ACCESS_MAX_HOOK_EXCLUSION_LENGTH + 1];
|
|
for (int index = 0; index < length; ++index) name[index] = start[index];
|
|
name[length] = 0;
|
|
wine_access_hook_exclusions_add(list, name);
|
|
}
|
|
if (!*end) break;
|
|
start = end + 1;
|
|
}
|
|
return list->count - before;
|
|
}
|
|
|
|
static inline BOOL wine_access_process_hook_excluded(const WineAccessHookExclusions *list,
|
|
HANDLE process) {
|
|
if (!list || !list->count || !process) return FALSE;
|
|
WCHAR path[4096];
|
|
DWORD length = ARRAYSIZE(path);
|
|
if (!QueryFullProcessImageNameW(process, 0, path, &length)) return FALSE;
|
|
return wine_access_hook_exclusions_match(list, path);
|
|
}
|
|
|
|
// Hook attachment fails closed, independently of the reader's slower /proc
|
|
// discovery: with exclusions configured, a target whose image cannot be
|
|
// identified is treated as excluded. Both hook hosts use this guard.
|
|
static inline BOOL wine_access_hooks_allowed(const WineAccessHookExclusions *list,
|
|
HANDLE process) {
|
|
if (!list || !list->count) return TRUE;
|
|
if (!process) return FALSE;
|
|
WCHAR path[4096];
|
|
DWORD length = ARRAYSIZE(path);
|
|
if (!QueryFullProcessImageNameW(process, 0, path, &length)) return FALSE;
|
|
return !wine_access_hook_exclusions_match(list, path);
|
|
}
|