Skip to content

Instantly share code, notes, and snippets.

@MaskRay
Created May 7, 2016 07:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaskRay/9e1c57642bedd8b2b965e39b2d58fc82 to your computer and use it in GitHub Desktop.
Save MaskRay/9e1c57642bedd8b2b965e39b2d58fc82 to your computer and use it in GitHub Desktop.
LD_PRELOAD termite to support filesystem:https:// style URL patterns
// Add support for filesystem+https:// style URL
// url_regex.hh scheme:// -> (scheme:)+//
// gcc preload.c -ldl -fPIC -shared -o preload.so
// LD_PRELOAD=./preload.so /usr/bin/termite
#ifndef _GNU_SOURCE
# define _GNU_SOURCE 1
#endif
#include <assert.h>
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
/*
http://permalink.gmane.org/gmane.comp.lib.glibc.alpha/61520
- Library initializers and finalizers (Run in dep order)
- Library constructors and destructors (Run in dep order)
- Prioritized constructors and destructors (Run in dep order, and # order)
- LD_PRELOAD initializer run last before the application is initialized.
- Non-zero initialized data from .data in the ELF image.
.init / .init_array sections in LD_PRELOAD shared objects are called after DT_NEEDED shared objects, thus we need to preload a function called by .init/.init_array sections in libvte.so
https://developer.gnome.org/gio/stable/GResource.html#g-static-resource-get-resource
*/
void g_static_resource_init(void* static_resource)
{
static long initialized = 0;
if (! initialized) {
initialized = 1;
size_t size = 0x200000;
mprotect((void*)0x400000, size, PROT_READ | PROT_WRITE | PROT_EXEC);
// scheme:// -> (scheme:)+// i.e. append a '+'
const char orig[] = "(?:[[:alpha:]][+-.[:alnum:]]*:)";
char *p = (char*)memmem((char*)0x400000, size, orig, strlen(orig));
assert(p);
p += strlen(orig);
char *q = strchr(p, '\0');
assert(q[1] == '\0');
for (; p < q; q--)
*q = q[-1];
*p = '+';
mprotect((void*)0x400000, size, PROT_READ | PROT_EXEC);
}
void (*real)(void*) = (void(*)(void*))dlsym(RTLD_NEXT, __func__);
real(static_resource);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment