source: zzuf/trunk/src/libzzuf/sys.c @ 4809

Last change on this file since 4809 was 4809, checked in by Sam Hocevar, 8 years ago

Fix a weird problem with lib6 versioned symbols.

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  This program is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/*
14 *  sys.c: system-dependent initialisation
15 */
16
17#include "config.h"
18
19/* Need this for RTLD_NEXT */
20#define _GNU_SOURCE
21
22#if defined HAVE_STDINT_H
23#   include <stdint.h>
24#elif defined HAVE_INTTYPES_H
25#   include <inttypes.h>
26#endif
27
28#ifdef HAVE_DLFCN_H
29#   include <dlfcn.h>
30#endif
31
32#if defined HAVE_WINDOWS_H
33#   include <windows.h>
34#   include <imagehlp.h>
35#   include <tlhelp32.h>
36#   define import_t PIMAGE_IMPORT_DESCRIPTOR
37#   define thunk_t PIMAGE_THUNK_DATA
38#endif
39
40#include <stdio.h>
41
42#include "sys.h"
43#include "lib-load.h"
44
45#if defined HAVE_DLFCN_H
46void *_zz_dl_lib = RTLD_NEXT;
47#endif
48
49#if defined HAVE_WINDOWS_H
50static void insert_funcs(void *);
51
52/* TODO: get rid of this later */
53HINSTANCE (WINAPI *LoadLibraryA_orig)(LPCSTR);
54HINSTANCE WINAPI LoadLibraryA_new(LPCSTR path)
55{
56    return LoadLibraryA_orig(path);
57}
58
59BOOL (WINAPI *AllocConsole_orig)(void);
60BOOL WINAPI AllocConsole_new(void)
61{
62    return AllocConsole_orig();
63}
64
65BOOL (WINAPI *AttachConsole_orig)(DWORD);
66BOOL WINAPI AttachConsole_new(DWORD d)
67{
68    return AttachConsole_orig(d);
69}
70#endif
71
72void _zz_sys_init(void)
73{
74#if defined HAVE_WINDOWS_H
75    MEMORY_BASIC_INFORMATION mbi;
76    MODULEENTRY32 entry;
77    void *list;
78    int k;
79
80    VirtualQuery(_zz_sys_init, &mbi, sizeof(mbi));
81    list = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
82    entry.dwSize = sizeof(entry);
83    for(k = Module32First(list, &entry); k; k = Module32Next(list, &entry))
84    {
85        if(entry.hModule == mbi.AllocationBase)
86            continue; /* Don't replace our own functions */
87
88        insert_funcs(entry.hModule);
89    }
90    CloseHandle(list);
91#elif defined HAVE_DLFCN_H
92    /* If glibc is recent enough, we use dladdr() to get its address. This
93     * way we are sure that the symbols we load are the most recent version,
94     * or we may get weird problems. We choose fileno as a random symbol to
95     * get, because we know we don't divert it. */
96#   if HAVE_DLADDR
97    Dl_info di;
98    if (dladdr(&fileno, &di) != 0)
99    {
100        void *lib = dlopen(di.dli_fname, RTLD_NOW);
101        if (lib)
102            _zz_dl_lib = lib;
103    }
104#   endif
105#else
106    /* Nothing to do on our platform */
107#endif
108}
109
110#if defined HAVE_WINDOWS_H
111static void insert_funcs(void *module)
112{
113    static zzuf_table_t *list[] =
114    {
115        table_win32,
116    };
117
118    zzuf_table_t *diversion;
119    void *lib;
120    unsigned long dummy;
121    import_t import;
122    thunk_t thunk;
123    int k, j, i;
124
125    import = (import_t)
126        ImageDirectoryEntryToData(module, TRUE,
127                                  IMAGE_DIRECTORY_ENTRY_IMPORT, &dummy);
128    if(!import)
129        return;
130
131    for (k = 0, diversion = NULL; k < sizeof(list) / sizeof(*list); )
132    {
133        if (!diversion)
134            diversion = list[k];
135
136        if (!diversion->lib)
137        {
138            k++;
139            diversion = NULL;
140            continue;
141        }
142
143        lib = GetModuleHandleA(diversion->lib);
144        *diversion->old = (void *)GetProcAddress(lib, diversion->name);
145
146        for(j = 0; import[j].Name; j++)
147        {
148            char *name = (char *)module + import[j].Name;
149            if(lstrcmpiA(name, diversion->lib) != 0)
150                continue;
151
152            thunk = (thunk_t)((char *)module + import->FirstThunk);
153            for(i = 0; thunk[i].u1.Function; i++)
154            {
155                void **func = (void **)&thunk[i].u1.Function;
156                if(*func != *diversion->old)
157                    continue;
158
159                /* FIXME: The StarCraft 2 hack uses two methods for function
160                 * diversion. See HookSsdt() and HookHotPatch(). */
161                VirtualProtect(func, sizeof(func), PAGE_EXECUTE_READWRITE, &dummy);
162                WriteProcessMemory(GetCurrentProcess(), func, &diversion->new,
163                                    sizeof(diversion->new), NULL);
164            }
165        }
166
167        diversion++;
168    }
169}
170#endif
Note: See TracBrowser for help on using the repository browser.