1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * PROJECT:     ReactOS Application compatibility module
 * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
 * PURPOSE:     apphelp entrypoint / generic interface functions
 * COPYRIGHT:   Copyright 2011 André Hentschel
 *              Copyright 2013 Mislav Blaževic
 *              Copyright 2015-2019 Mark Jansen (mark.jansen@reactos.org)
 */

#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "strsafe.h"
#include "apphelp.h"
#include <ndk/rtlfuncs.h>
#include <ndk/cmfuncs.h>
#include <ndk/obfuncs.h>
#include <ndk/kdtypes.h>


ACCESS_MASK Wow64QueryFlag(void);

const UNICODE_STRING InstalledSDBKeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\InstalledSDB");

/* from dpfilter.h */
#define DPFLTR_APPCOMPAT_ID 123

#define MAX_GUID_STRING_LEN     sizeof("{12345678-1234-1234-0123-456789abcdef}")

#ifndef NT_SUCCESS
#define NT_SUCCESS(StatCode)  ((NTSTATUS)(StatCode) >= 0)
#endif

ULONG g_ShimDebugLevel = ~0;
HMODULE g_hInstance;

void ApphelppInitDebugLevel(void)
{
    static const UNICODE_STRING DebugKey = RTL_CONSTANT_STRING(L"SHIM_DEBUG_LEVEL");
    UNICODE_STRING DebugValue;
    NTSTATUS Status;
    ULONG NewLevel = SHIM_ERR;
    WCHAR Buffer[40];

    RtlInitEmptyUnicodeString(&DebugValue, Buffer, sizeof(Buffer));

    Status = RtlQueryEnvironmentVariable_U(NULL, &DebugKey, &DebugValue);

    if (NT_SUCCESS(Status))
    {
        if (!NT_SUCCESS(RtlUnicodeStringToInteger(&DebugValue, 10, &NewLevel)))
            NewLevel = SHIM_ERR;
    }
    g_ShimDebugLevel = NewLevel;
}


BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
{
    switch (reason)
    {
    case DLL_PROCESS_ATTACH:
        g_hInstance = hinst;
        DisableThreadLibraryCalls(hinst);
        SdbpHeapInit();
        break;
    case DLL_PROCESS_DETACH:
        SdbpHeapDeinit();
        break;
    }
    return TRUE;
}

BOOL WINAPI ApphelpCheckInstallShieldPackage(void* ptr, LPCWSTR path)
{
    SHIM_WARN("stub: ptr=%p, path='%S'\n", ptr, path);
    return TRUE;
}


BOOL WINAPI ApphelpCheckShellObject(REFCLSID ObjectCLSID, BOOL bShimIfNecessary, ULONGLONG *pullFlags)
{
    WCHAR GuidString[MAX_GUID_STRING_LEN];
    if (!ObjectCLSID || !SdbGUIDToString(ObjectCLSID, GuidString, RTL_NUMBER_OF(GuidString)))
        GuidString[0] = L'\0';
    SHIM_WARN("stub: ObjectCLSID='%S', bShimIfNecessary=%d, pullFlags=%p)\n", GuidString, bShimIfNecessary, pullFlags);

    if (pullFlags)
        *pullFlags = 0;

    return TRUE;
}

/**
 * Outputs diagnostic info.
 *
 * @param [in]  Level           The level to log this message with, choose any of [SHIM_ERR,
 *                              SHIM_WARN, SHIM_INFO].
 * @param [in]  FunctionName    The function this log should be attributed to.
 * @param [in]  Format          The format string.
 * @param   ...                 Variable arguments providing additional information.
 *
 * @return  Success: TRUE Failure: FALSE.
 */
BOOL WINAPIV ShimDbgPrint(SHIM_LOG_LEVEL Level, PCSTR FunctionName, PCSTR Format, ...)
{
    char Buffer[512];
    va_list ArgList;
    char* Current = Buffer;
    const char* LevelStr;
    size_t Length = sizeof(Buffer);

    if (g_ShimDebugLevel == ~0)
        ApphelppInitDebugLevel();

    if (Level > g_ShimDebugLevel)
        return FALSE;

    switch (Level)
    {
    case SHIM_ERR:
        LevelStr = "Err ";
        Level = DPFLTR_MASK | (1 << DPFLTR_ERROR_LEVEL);
        break;
    case SHIM_WARN:
        LevelStr = "Warn";
        Level = DPFLTR_MASK | (1 << DPFLTR_WARNING_LEVEL);
        break;
    case SHIM_INFO:
        LevelStr = "Info";
        Level = DPFLTR_MASK | (1 << DPFLTR_INFO_LEVEL);
        break;
    default:
        LevelStr = "User";
        Level = DPFLTR_MASK | (1 << DPFLTR_INFO_LEVEL);<--- Assignment of function parameter has no effect outside the function.
        break;
    }
    StringCchPrintfExA(Current, Length, &Current, &Length, STRSAFE_NULL_ON_FAILURE, "[%s][%-20s] ", LevelStr, FunctionName);

    va_start(ArgList, Format);
    StringCchVPrintfExA(Current, Length, &Current, &Length, STRSAFE_NULL_ON_FAILURE, Format, ArgList);
    va_end(ArgList);

#if defined(APPCOMPAT_USE_DBGPRINTEX) && APPCOMPAT_USE_DBGPRINTEX
    return NT_SUCCESS(DbgPrintEx(DPFLTR_APPCOMPAT_ID, Level, "%s", Buffer));
#else
    DbgPrint("%s", Buffer);
    return TRUE;
#endif
}


#define APPHELP_DONTWRITE_REASON    2
#define APPHELP_CLEARBITS           0x100   /* TODO: Investigate */
#define APPHELP_IGNORE_ENVIRONMENT  0x400

#define APPHELP_VALID_RESULT        0x10000
#define APPHELP_RESULT_NOTFOUND     0x20000
#define APPHELP_RESULT_FOUND        0x40000

/**
 * Lookup Shims / Fixes for the specified application
 *
 * @param [in]  FileHandle                  Handle to the file to check.
 * @param [in]  Unk1
 * @param [in]  Unk2
 * @param [in]  ApplicationName             Exe to check
 * @param [in]  Environment                 The environment variables to use, or NULL to use the current environment.
 * @param [in]  ExeType                     Exe type (MACHINE_TYPE_XXXX)
 * @param [in,out]  Reason                  Input/output flags
 * @param [in]  SdbQueryAppCompatData       The resulting data.
 * @param [in]  SdbQueryAppCompatDataSize   The resulting data size.
 * @param [in]  SxsData                     TODO
 * @param [in]  SxsDataSize                 TODO
 * @param [in]  FusionFlags                 TODO
 * @param [in]  SomeFlag1                   TODO
 * @param [in]  SomeFlag2                   TODO
 *
 * @return  TRUE if the application is allowed to run.
 */
BOOL
WINAPI
ApphelpCheckRunAppEx(
    _In_ HANDLE FileHandle,
    _In_opt_ PVOID Unk1,
    _In_opt_ PVOID Unk2,
    _In_opt_z_ PWCHAR ApplicationName,
    _In_opt_ PVOID Environment,
    _In_opt_ USHORT ExeType,
    _Inout_opt_ PULONG Reason,
    _Out_opt_ PVOID* SdbQueryAppCompatData,
    _Out_opt_ PULONG SdbQueryAppCompatDataSize,
    _Out_opt_ PVOID* SxsData,
    _Out_opt_ PULONG SxsDataSize,
    _Out_opt_ PULONG FusionFlags,
    _Out_opt_ PULONG64 SomeFlag1,
    _Out_opt_ PULONG SomeFlag2)
{
    SDBQUERYRESULT* result = NULL;
    HSDB hsdb = NULL;
    DWORD dwFlags = 0;

    if (SxsData)
        *SxsData = NULL;
    if (SxsDataSize)
        *SxsDataSize = 0;
    if (FusionFlags)
        *FusionFlags = 0;
    if (SomeFlag1)
        *SomeFlag1 = 0;
    if (SomeFlag2)
        *SomeFlag2 = 0;
    if (Reason)
        dwFlags = *Reason;

    dwFlags &= ~APPHELP_CLEARBITS;

    *SdbQueryAppCompatData = result = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SDBQUERYRESULT));
    if (SdbQueryAppCompatDataSize)
        *SdbQueryAppCompatDataSize = sizeof(*result);


    hsdb = SdbInitDatabase(HID_DOS_PATHS | SDB_DATABASE_MAIN_SHIM, NULL);
    if (hsdb)
    {
        BOOL FoundMatch;
        DWORD MatchingExeFlags = 0;

        if (dwFlags & APPHELP_IGNORE_ENVIRONMENT)
            MatchingExeFlags |= SDBGMEF_IGNORE_ENVIRONMENT;

        FoundMatch = SdbGetMatchingExe(hsdb, ApplicationName, NULL, Environment, MatchingExeFlags, result);
        if (FileHandle != INVALID_HANDLE_VALUE)
        {
            dwFlags |= APPHELP_VALID_RESULT;
            dwFlags |= (FoundMatch ? APPHELP_RESULT_FOUND : APPHELP_RESULT_NOTFOUND);
        }

        SdbReleaseDatabase(hsdb);
    }

    if (Reason && !(dwFlags & APPHELP_DONTWRITE_REASON))
        *Reason = dwFlags;


    /* We should _ALWAYS_ return TRUE here, unless we want to block an application from starting! */
    return TRUE;
}


/**
 * @name SdbRegisterDatabaseEx
 * Register an application compatibility database
 *
 * @param pszDatabasePath   The database. Required
 * @param dwDatabaseType    The database type. SDB_DATABASE_*
 * @param pTimeStamp        The timestamp. When this argument is not provided, the system time is used.
 * @return                  TRUE on success, or FALSE on failure.
 */
BOOL WINAPI SdbRegisterDatabaseEx(
    _In_ LPCWSTR pszDatabasePath,
    _In_ DWORD dwDatabaseType,
    _In_opt_ const PULONGLONG pTimeStamp)
{
    PDB pdb;
    DB_INFORMATION Information;
    WCHAR GuidBuffer[MAX_GUID_STRING_LEN];
    UNICODE_STRING KeyName;
    ACCESS_MASK KeyAccess;
    OBJECT_ATTRIBUTES ObjectKey = RTL_INIT_OBJECT_ATTRIBUTES(&KeyName, OBJ_CASE_INSENSITIVE);
    NTSTATUS Status;
    HANDLE InstalledSDBKey;

    pdb = SdbOpenDatabase(pszDatabasePath, DOS_PATH);
    if (!pdb)
    {
        SHIM_ERR("Unable to open DB %S\n", pszDatabasePath);
        return FALSE;
    }

    if (!SdbGetDatabaseInformation(pdb, &Information) ||
        !(Information.dwFlags & DB_INFO_FLAGS_VALID_GUID))
    {
        SHIM_ERR("Unable to retrieve DB info\n");
        SdbCloseDatabase(pdb);
        return FALSE;
    }

    if (!SdbGUIDToString(&Information.Id, GuidBuffer, RTL_NUMBER_OF(GuidBuffer)))
    {
        SHIM_ERR("Unable to Convert GUID to string\n");
        SdbFreeDatabaseInformation(&Information);
        SdbCloseDatabase(pdb);
        return FALSE;
    }

    KeyName = InstalledSDBKeyName;
    KeyAccess = Wow64QueryFlag() | KEY_WRITE | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS;
    Status = NtCreateKey(&InstalledSDBKey, KeyAccess, &ObjectKey, 0, NULL, 0, NULL);
    if (NT_SUCCESS(Status))
    {
        HANDLE DbKey;

        RtlInitUnicodeString(&KeyName, GuidBuffer);
        ObjectKey.RootDirectory = InstalledSDBKey;

        Status = NtCreateKey(&DbKey, KeyAccess, &ObjectKey, 0, NULL, 0, NULL);
        if (NT_SUCCESS(Status))
        {
            UNICODE_STRING DatabasePathKey = RTL_CONSTANT_STRING(L"DatabasePath");
            UNICODE_STRING DatabaseInstallTimeStampKey = RTL_CONSTANT_STRING(L"DatabaseInstallTimeStamp");
            UNICODE_STRING DatabaseTypeKey = RTL_CONSTANT_STRING(L"DatabaseType");
            UNICODE_STRING DatabaseDescriptionKey = RTL_CONSTANT_STRING(L"DatabaseDescription");

            Status = NtSetValueKey(DbKey, &DatabasePathKey, 0, REG_SZ,
                                   (PVOID)pszDatabasePath, ((ULONG)wcslen(pszDatabasePath) + 1) * sizeof(WCHAR));
            if (!NT_SUCCESS(Status))
                SHIM_ERR("Unable to write %wZ\n", &DatabasePathKey);

            if (NT_SUCCESS(Status))
            {
                ULARGE_INTEGER ulTimeStamp;
                if (pTimeStamp)
                {
                    ulTimeStamp.QuadPart = *pTimeStamp;
                }
                else
                {
                    FILETIME fi;
                    GetSystemTimeAsFileTime(&fi);
                    ulTimeStamp.LowPart = fi.dwLowDateTime;
                    ulTimeStamp.HighPart = fi.dwHighDateTime;
                }
                Status = NtSetValueKey(DbKey, &DatabaseInstallTimeStampKey, 0, REG_QWORD,
                                       &ulTimeStamp.QuadPart, sizeof(ulTimeStamp));
                if (!NT_SUCCESS(Status))
                    SHIM_ERR("Unable to write %wZ\n", &DatabaseInstallTimeStampKey);
            }

            if (NT_SUCCESS(Status))
            {
                Status = NtSetValueKey(DbKey, &DatabaseTypeKey, 0, REG_DWORD,
                                       &dwDatabaseType, sizeof(dwDatabaseType));
                if (!NT_SUCCESS(Status))
                    SHIM_ERR("Unable to write %wZ\n", &DatabaseTypeKey);
            }

            if (NT_SUCCESS(Status) && Information.Description)
            {
                Status = NtSetValueKey(DbKey, &DatabaseDescriptionKey, 0, REG_SZ,
                                       (PVOID)Information.Description, ((ULONG)wcslen(Information.Description) + 1) * sizeof(WCHAR));
                if (!NT_SUCCESS(Status))
                    SHIM_ERR("Unable to write %wZ\n", &DatabaseDescriptionKey);
            }

            NtClose(DbKey);

            if (NT_SUCCESS(Status))
            {
                SHIM_INFO("Installed %wS as %wZ\n", pszDatabasePath, &KeyName);
            }
        }
        else
        {
            SHIM_ERR("Unable to create key %wZ\n", &KeyName);
        }

        NtClose(InstalledSDBKey);
    }
    else
    {
        SHIM_ERR("Unable to create key %wZ\n", &KeyName);
    }

    SdbFreeDatabaseInformation(&Information);
    SdbCloseDatabase(pdb);

    return NT_SUCCESS(Status);
}


/**
 * @name SdbRegisterDatabase
 * Register an application compatibility database
 *
 * @param pszDatabasePath   The database. Required
 * @param dwDatabaseType    The database type. SDB_DATABASE_*
 * @return                  TRUE on success, or FALSE on failure.
 */
BOOL WINAPI SdbRegisterDatabase(
    _In_ LPCWSTR pszDatabasePath,
    _In_ DWORD dwDatabaseType)
{
    return SdbRegisterDatabaseEx(pszDatabasePath, dwDatabaseType, NULL);
}


/**
 * @name SdbUnregisterDatabase
 * 
 *
 * @param pguidDB
 * @return 
 */
BOOL WINAPI SdbUnregisterDatabase(_In_ const GUID *pguidDB)
{
    WCHAR KeyBuffer[MAX_PATH], GuidBuffer[50];
    UNICODE_STRING KeyName;
    ACCESS_MASK KeyAccess;
    OBJECT_ATTRIBUTES ObjectKey = RTL_INIT_OBJECT_ATTRIBUTES(&KeyName, OBJ_CASE_INSENSITIVE);
    NTSTATUS Status;
    HANDLE DbKey;

    if (!SdbGUIDToString(pguidDB, GuidBuffer, RTL_NUMBER_OF(GuidBuffer)))
    {
        SHIM_ERR("Unable to Convert GUID to string\n");
        return FALSE;
    }

    RtlInitEmptyUnicodeString(&KeyName, KeyBuffer, sizeof(KeyBuffer));
    RtlAppendUnicodeStringToString(&KeyName, &InstalledSDBKeyName);
    RtlAppendUnicodeToString(&KeyName, L"\\");
    RtlAppendUnicodeToString(&KeyName, GuidBuffer);

    KeyAccess = Wow64QueryFlag() | DELETE;
    Status = NtCreateKey(&DbKey, KeyAccess, &ObjectKey, 0, NULL, 0, NULL);
    if (!NT_SUCCESS(Status))
    {
        SHIM_ERR("Unable to open %wZ\n", &KeyName);
        return FALSE;
    }

    Status = NtDeleteKey(DbKey);
    if (!NT_SUCCESS(Status))
        SHIM_ERR("Unable to delete %wZ\n", &KeyName);

    NtClose(DbKey);
    return NT_SUCCESS(Status);
}


/* kernel32.dll */
BOOL WINAPI BaseDumpAppcompatCache(VOID);
BOOL WINAPI BaseFlushAppcompatCache(VOID);


/**
 * @name ShimDumpCache
 * Dump contents of the shim cache.
 *
 * @param hwnd          Unused, pass 0
 * @param hInstance     Unused, pass 0
 * @param lpszCmdLine   Unused, pass 0
 * @param nCmdShow      Unused, pass 0
 * @return 
 */
BOOL WINAPI ShimDumpCache(HWND hwnd, HINSTANCE hInstance, LPCSTR lpszCmdLine, int nCmdShow)
{
    return BaseDumpAppcompatCache();
}

/**
* @name ShimFlushCache
* Flush the shim cache. Call this after installing a new shim database
*
* @param hwnd          Unused, pass 0
* @param hInstance     Unused, pass 0
* @param lpszCmdLine   Unused, pass 0
* @param nCmdShow      Unused, pass 0
* @return 
*/
BOOL WINAPI ShimFlushCache(HWND hwnd, HINSTANCE hInstance, LPCSTR lpszCmdLine, int nCmdShow)
{
    return BaseFlushAppcompatCache();
}