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
|
/*
Copyright (c) 2012-2020 Maarten Baert <maarten-baert@hotmail.com>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "Global.h"
#include "GLInject.h"
#include "GLXFrameGrabber.h"
#include "plthook.h"
#include <dlfcn.h>
#include <link.h>
#include <GL/glx.h>
#include <X11/X.h>
// global variable from the standard library that holds all environment variables
extern char **environ;
// return type of glXGetProcAddressARB
typedef void (*GLXextFuncPtr)(void);
// hook replacement function prototypes
void* glinject_hook_dlsym(void* handle, const char* symbol);
void* glinject_hook_dlvsym(void* handle, const char* symbol, const char* version);
int glinject_hook_execl(const char* filename, const char* arg, ...);
int glinject_hook_execlp(const char* filename, const char* arg, ...);
int glinject_hook_execle(const char* filename, const char* arg, ...);
int glinject_hook_execv(const char* filename, char* const argv[]);
int glinject_hook_execve(const char* filename, char* const argv[], char* const envp[]);
int glinject_hook_execvp(const char* filename, char* const argv[]);
int glinject_hook_execvpe(const char* filename, char* const argv[], char* const envp[]);
GLXWindow glinject_hook_glXCreateWindow(Display* dpy, GLXFBConfig config, Window win, const int* attrib_list);
void glinject_hook_glXDestroyWindow(Display* dpy, GLXWindow win);
int glinject_hook_XDestroyWindow(Display* dpy, Window win);
void glinject_hook_glXSwapBuffers(Display* dpy, GLXDrawable drawable);
GLXextFuncPtr glinject_hook_glXGetProcAddressARB(const GLubyte *proc_name);
// hook table
struct GLInjectHook {
const char *name;
void *address;
};
std::initializer_list<GLInjectHook> glinject_hook_table = {
{"dlsym" , (void*) &glinject_hook_dlsym},
{"dlvsym" , (void*) &glinject_hook_dlvsym},
{"execl" , (void*) &glinject_hook_execl},
{"execlp" , (void*) &glinject_hook_execlp},
{"execle" , (void*) &glinject_hook_execle},
{"execv" , (void*) &glinject_hook_execv},
{"execve" , (void*) &glinject_hook_execve},
{"execvp" , (void*) &glinject_hook_execvp},
{"execvpe" , (void*) &glinject_hook_execvpe},
{"glXCreateWindow" , (void*) &glinject_hook_glXCreateWindow},
{"glXDestroyWindow" , (void*) &glinject_hook_glXDestroyWindow},
{"XDestroyWindow" , (void*) &glinject_hook_XDestroyWindow},
{"glXSwapBuffers" , (void*) &glinject_hook_glXSwapBuffers},
{"glXGetProcAddressARB", (void*) &glinject_hook_glXGetProcAddressARB},
};
// main glinject object and mutex
static GLInject *g_glinject = NULL;
static std::mutex g_glinject_mutex;
// hook initializer
static struct GLInjectHooksInitializer {
GLInjectHooksInitializer() {
// get the link table of the glinject library (we can use any global variable for this)
Dl_info glinject_dlinfo;
struct link_map *glinject_lmap = NULL;
if(dladdr1((void*) &glinject_hook_table, &glinject_dlinfo, (void**) &glinject_lmap, RTLD_DL_LINKMAP) == 0) {
GLINJECT_PRINT("Error: Failed to get link map of glinject library!");
return;
}
// replace PLT entries everywhere except in the glinject library
void *mainhandle = dlopen(NULL, RTLD_NOW);
if(mainhandle == NULL) {
GLINJECT_PRINT("Error: Failed to get main program handle!");
return;
}
struct link_map *lmap = NULL;
if(dlinfo(mainhandle, RTLD_DI_LINKMAP, &lmap) != 0) {
GLINJECT_PRINT("Error: Failed to get link map of main program!");
return;
}
while(lmap) {
if(lmap != glinject_lmap) {
plthook_t *plthook;
if(plthook_open_by_linkmap(&plthook, lmap) == 0) {
for(const GLInjectHook &hook : glinject_hook_table) {
void *oldfunc;
if(plthook_replace(plthook, hook.name, hook.address, &oldfunc) == 0) {
GLINJECT_PRINT("Hooked " << hook.name << " PLT entry in '" << lmap->l_name << "'.");
}
}
plthook_close(plthook);
}
}
lmap = lmap->l_next;
}
dlclose(mainhandle);
}
} glinject_hooks_initializer;
void GLInjectInit();
void GLInjectFree();
void GLInjectInit() {
if(g_glinject != NULL)
return;
g_glinject = new GLInject();
atexit(GLInjectFree);
}
void GLInjectFree() {
if(g_glinject != NULL) {
delete g_glinject;
g_glinject = NULL;
}
}
void FilterEnviron(const char* filename, std::vector<char*>* out, char* const* in) {
const char* exec_blacklist[] = {
"ping",
"/bin/ping",
"/usr/bin/ping",
};
bool filter = false;
for(unsigned int i = 0; i < sizeof(exec_blacklist) / sizeof(const char*); ++i) {
if(strcmp(exec_blacklist[i], filename) == 0) {
filter = true;
break;
}
}
while(*in != NULL) {
if(!filter || strncmp(*in, "LD_PRELOAD=", 11) != 0)
out->push_back(*in);
++in;
}
out->push_back(NULL);
}
void* glinject_hook_dlsym(void* handle, const char* symbol) {
const char *str = "(In glinject_hook_dlsym)\n";
write(2, str, strlen(str));
for(const GLInjectHook &hook : glinject_hook_table) {
if(strcmp(hook.name, symbol) == 0) {
std::lock_guard<std::mutex> lock(g_glinject_mutex);
GLINJECT_PRINT("Hooked dlsym(" << symbol << ").");
return hook.address;
}
}
return dlsym(handle, symbol);
}
void* glinject_hook_dlvsym(void* handle, const char* symbol, const char* version) {
const char *str = "(In glinject_hook_dlvsym)\n";
write(2, str, strlen(str));
for(const GLInjectHook &hook : glinject_hook_table) {
if(strcmp(hook.name, symbol) == 0) {
std::lock_guard<std::mutex> lock(g_glinject_mutex);
GLINJECT_PRINT("Hooked dlvsym(" << symbol << ").");
return hook.address;
}
}
return dlvsym(handle, symbol, version);
}
int glinject_hook_execl(const char* filename, const char* arg, ...) {
const char *str = "(In glinject_hook_execl)\n";
write(2, str, strlen(str));
std::vector<char*> args;
args.push_back((char*) arg);
va_list vl;
va_start(vl, arg);
while(args.back() != NULL) {
args.push_back(va_arg(vl, char*));
}
va_end(vl);
std::vector<char*> filtered_environ;
FilterEnviron(filename, &filtered_environ, environ);
return execve(filename, args.data(), filtered_environ.data());
}
int glinject_hook_execlp(const char* filename, const char* arg, ...) {
const char *str = "(In glinject_hook_execlp)\n";
write(2, str, strlen(str));
std::vector<char*> args;
args.push_back((char*) arg);
va_list vl;
va_start(vl, arg);
while(args.back() != NULL) {
args.push_back(va_arg(vl, char*));
}
va_end(vl);
std::vector<char*> filtered_environ;
FilterEnviron(filename, &filtered_environ, environ);
return execvpe(filename, args.data(), filtered_environ.data());
}
int glinject_hook_execle(const char* filename, const char* arg, ...) {
const char *str = "(In glinject_hook_execle)\n";
write(2, str, strlen(str));
std::vector<char*> args;
args.push_back((char*) arg);
va_list vl;
va_start(vl, arg);
while(args.back() != NULL) {
args.push_back(va_arg(vl, char*));
}
char *const *envp = va_arg(vl, char* const*);
va_end(vl);
std::vector<char*> filtered_environ;
FilterEnviron(filename, &filtered_environ, envp);
return execvpe(filename, args.data(), filtered_environ.data());
}
int glinject_hook_execv(const char* filename, char* const argv[]) {
const char *str = "(In glinject_hook_execv)\n";
write(2, str, strlen(str));
std::vector<char*> filtered_environ;
FilterEnviron(filename, &filtered_environ, environ);
return execve(filename, argv, filtered_environ.data());
}
int glinject_hook_execve(const char* filename, char* const argv[], char* const envp[]) {
const char *str = "(In glinject_hook_execve)\n";
write(2, str, strlen(str));
std::vector<char*> filtered_environ;
FilterEnviron(filename, &filtered_environ, envp);
return execve(filename, argv, filtered_environ.data());
}
int glinject_hook_execvp(const char* filename, char* const argv[]) {
const char *str = "(In glinject_hook_execvp)\n";
write(2, str, strlen(str));
std::vector<char*> filtered_environ;
FilterEnviron(filename, &filtered_environ, environ);
return execvpe(filename, argv, filtered_environ.data());
}
int glinject_hook_execvpe(const char* filename, char* const argv[], char* const envp[]) {
const char *str = "(In glinject_hook_execvpe)\n";
write(2, str, strlen(str));
std::vector<char*> filtered_environ;
FilterEnviron(filename, &filtered_environ, envp);
return execvpe(filename, argv, filtered_environ.data());
}
GLXWindow glinject_hook_glXCreateWindow(Display* dpy, GLXFBConfig config, Window win, const int* attrib_list) {
const char *str = "(In glinject_hook_glXCreateWindow)\n";
write(2, str, strlen(str));
GLXWindow res = glXCreateWindow(dpy, config, win, attrib_list);
if(res == 0)
return 0;
{
std::lock_guard<std::mutex> lock(g_glinject_mutex);
GLInjectInit();
g_glinject->NewGLXFrameGrabber(dpy, win, res);
}
return res;
}
void glinject_hook_glXDestroyWindow(Display* dpy, GLXWindow win) {
const char *str = "(In glinject_hook_glXDestroyWindow)\n";
write(2, str, strlen(str));
glXDestroyWindow(dpy, win);
{
std::lock_guard<std::mutex> lock(g_glinject_mutex);
GLInjectInit();
g_glinject->DeleteGLXFrameGrabberByDrawable(dpy, win);
}
}
int glinject_hook_XDestroyWindow(Display* dpy, Window win) {
const char *str = "(In glinject_hook_XDestroyWindow)\n";
write(2, str, strlen(str));
int res = XDestroyWindow(dpy, win);
{
std::lock_guard<std::mutex> lock(g_glinject_mutex);
GLInjectInit();
g_glinject->DeleteGLXFrameGrabberByWindow(dpy, win);
}
return res;
}
void glinject_hook_glXSwapBuffers(Display* dpy, GLXDrawable drawable) {
const char *str = "(In glinject_hook_glXSwapBuffers)\n";
write(2, str, strlen(str));
{
std::lock_guard<std::mutex> lock(g_glinject_mutex);
GLInjectInit();
GLXFrameGrabber *fg = g_glinject->FindGLXFrameGrabber(dpy, drawable);
if(fg == NULL) {
GLINJECT_PRINT("Warning: glXSwapBuffers called without existing frame grabber, creating one assuming window == drawable.");
fg = g_glinject->NewGLXFrameGrabber(dpy, drawable, drawable);
}
fg->GrabFrame();
}
glXSwapBuffers(dpy, drawable);
}
GLXextFuncPtr glinject_hook_glXGetProcAddressARB(const GLubyte *proc_name) {
const char *str = "(In glinject_hook_glXGetProcAddressARB)\n";
write(2, str, strlen(str));
for(const GLInjectHook &hook : glinject_hook_table) {
if(strcmp(hook.name, (const char*) proc_name) == 0) {
std::lock_guard<std::mutex> lock(g_glinject_mutex);
GLINJECT_PRINT("Hooked glXGetProcAddressARB(" << proc_name << ").");
return (GLXextFuncPtr) hook.address;
}
}
return glXGetProcAddressARB(proc_name);
}
|