-
-
Save bmeck/1e4d99199648f5e7e03b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[x@y ~/interposed/fixtures]# gcc -shared -lc -o nix.so nix.c | |
Text relocation remains referenced | |
against symbol offset in file | |
.rodata (section) 0x9 /var/tmp//cca5aac2.o | |
.data (section) 0x26 /var/tmp//cca5aac2.o | |
.rodata (section) 0x2e /var/tmp//cca5aac2.o | |
.data (section) 0x48 /var/tmp//cca5aac2.o | |
.data (section) 0x59 /var/tmp//cca5aac2.o | |
.rodata (section) 0x76 /var/tmp//cca5aac2.o | |
.rodata (section) 0xdb /var/tmp//cca5aac2.o | |
.bss (section) 0xfb /var/tmp//cca5aac2.o | |
.rodata (section) 0x107 /var/tmp//cca5aac2.o | |
.bss (section) 0x118 /var/tmp//cca5aac2.o | |
.bss (section) 0x141 /var/tmp//cca5aac2.o | |
.bss (section) 0x193 /var/tmp//cca5aac2.o | |
.bss (section) 0x1e0 /var/tmp//cca5aac2.o | |
.bss (section) 0x206 /var/tmp//cca5aac2.o | |
.rodata (section) 0x212 /var/tmp//cca5aac2.o | |
.bss (section) 0x223 /var/tmp//cca5aac2.o | |
.bss (section) 0x229 /var/tmp//cca5aac2.o | |
.rodata (section) 0x24a /var/tmp//cca5aac2.o | |
.data (section) 0x265 /var/tmp//cca5aac2.o | |
.rodata (section) 0x2ca /var/tmp//cca5aac2.o | |
.rodata (section) 0x2f8 /var/tmp//cca5aac2.o | |
getenv 0xe /var/tmp//cca5aac2.o | |
htons 0x16e /var/tmp//cca5aac2.o | |
snprintf 0xee /var/tmp//cca5aac2.o | |
snprintf 0x25d /var/tmp//cca5aac2.o | |
snprintf 0x2e0 /var/tmp//cca5aac2.o | |
ntohs 0xc5 /var/tmp//cca5aac2.o | |
ntohs 0x17c /var/tmp//cca5aac2.o | |
errno 0x1b1 /var/tmp//cca5aac2.o | |
errno 0x1bb /var/tmp//cca5aac2.o | |
sscanf 0x39 /var/tmp//cca5aac2.o | |
dlsym 0x113 /var/tmp//cca5aac2.o | |
dlsym 0x21e /var/tmp//cca5aac2.o | |
__interposed_RTLD_SEND 0x2fd /var/tmp//cca5aac2.o | |
dprintf 0x7e /var/tmp//cca5aac2.o | |
sockaddr_json 0x2aa /var/tmp//cca5aac2.o | |
inet_ntop 0xb0 /var/tmp//cca5aac2.o | |
getsockname 0x27a /var/tmp//cca5aac2.o | |
ld: fatal: relocations remain against allocatable but non-writable sections | |
collect2: ld returned 1 exit status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <dlfcn.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <netinet/in.h> | |
#include <errno.h> | |
static int __interposed_CHANNEL_FD = 1; | |
#pragma CALL_ON_MODULE_BIND initptrs | |
#pragma CALL_ON_LOAD initptrs | |
#pragma init(initptrs) | |
__attribute__((constructor)) | |
static void __interposed_init() | |
{ | |
char* env_channel_fd = getenv("NODE_CHANNEL_FD"); | |
int found = 0; | |
if (env_channel_fd) { | |
found = sscanf(env_channel_fd,"%d", &__interposed_CHANNEL_FD); | |
} | |
if (!found) { | |
__interposed_CHANNEL_FD = 1; | |
} | |
} | |
#define RTLD_SEND(params, ret) __interposed_RTLD_SEND(__FUNCTION__, params, ret) | |
void __interposed_RTLD_SEND(const char name[], char* params, char* ret) { | |
dprintf(__interposed_CHANNEL_FD, "{\"method\":\"%s\",\"params\":%s,\"result\":%s}\n",name,params,ret); | |
} | |
void sockaddr_json(struct sockaddr_in *in_addr, char addr_str[256]) { | |
char str[INET_ADDRSTRLEN]; | |
int size = inet_ntop(in_addr->sin_family, &(in_addr->sin_addr), str, INET_ADDRSTRLEN); | |
snprintf(addr_str, 256, "{\"port\":%d,\"address\":\"%s\"}", ntohs(in_addr->sin_port), str); | |
} | |
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) | |
{ | |
static const int (*original_bind) (int, const struct sockaddr *, socklen_t) = NULL; | |
if (!original_bind) { | |
original_bind = dlsym(RTLD_NEXT, "bind"); | |
} | |
if (addr->sa_family == AF_INET) { | |
struct sockaddr_in* in_addr = (struct sockaddr_in*)addr; | |
int desired = in_addr->sin_port; | |
int result = original_bind(sockfd, addr, addrlen); | |
while (result = -1 && (errno == EADDRINUSE || errno == EACCES)) { | |
in_addr->sin_port = ntohs(htons(in_addr->sin_port)+1); | |
in_addr->sin_addr.s_addr = INADDR_ANY; | |
result = original_bind(sockfd, addr, addrlen); | |
} | |
return result; | |
} | |
return original_bind(sockfd, addr, addrlen); | |
} | |
int listen(int sockfd, int backlog) | |
{ | |
static const int (*original_listen) (int, int) = NULL; | |
if (!original_listen) { | |
original_listen = dlsym(RTLD_NEXT, "listen"); | |
} | |
int result = original_listen(sockfd, backlog); | |
char ret[15]; | |
snprintf(ret, 15, "%d", result); | |
char params[512]; | |
struct sockaddr_in addr; | |
static socklen_t addr_len = sizeof addr; | |
int len = getsockname(sockfd, (struct sockaddr*)&addr, &addr_len); | |
if (len <= sizeof addr && addr.sin_family == AF_INET) { | |
char addr_str[256]; | |
sockaddr_json(&addr, addr_str); | |
snprintf(params, 512, "{\"fd\":%d,\"address\":%s,\"backlog\":%d]", sockfd, addr_str, backlog); | |
RTLD_SEND(params,ret); | |
// return the result | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment