2015-10-14 17:47:35 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef USE_GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Name used in err msgs */
|
|
|
|
char *progname = "";
|
|
|
|
|
2015-10-21 23:27:17 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <pthread.h>
|
2015-10-14 17:47:35 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <errno.h>
|
2015-10-28 00:12:32 +00:00
|
|
|
#include <linux/errno.h>
|
2015-10-14 17:47:35 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2015-10-28 00:12:32 +00:00
|
|
|
#include <sys/poll.h>
|
2015-10-14 17:47:35 +00:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "Intercept.h"
|
|
|
|
#include "Common.h"
|
|
|
|
|
|
|
|
#ifdef CHECKS
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <linux/net.h> /* for NPROTO */
|
|
|
|
#define SOCK_MAX (SOCK_PACKET + 1)
|
|
|
|
#define SOCK_TYPE_MASK 0xf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Global Declarations */
|
|
|
|
#ifdef USE_SOCKS_DNS
|
|
|
|
static int (*realresinit)(void);
|
|
|
|
#endif
|
|
|
|
static int (*realconnect)(CONNECT_SIG);
|
|
|
|
static int (*realselect)(SELECT_SIG);
|
|
|
|
static int (*realbind)(BIND_SIG);
|
|
|
|
static int (*realaccept)(ACCEPT_SIG);
|
|
|
|
static int (*reallisten)(LISTEN_SIG);
|
|
|
|
static int (*realsocket)(SOCKET_SIG);
|
|
|
|
static int (*realsetsockopt)(SETSOCKOPT_SIG);
|
|
|
|
static int (*realgetsockopt)(GETSOCKOPT_SIG);
|
|
|
|
static int (*realaccept4)(ACCEPT4_SIG);
|
2015-10-21 23:27:17 +00:00
|
|
|
static long (*realsyscall)(SYSCALL_SIG);
|
2015-11-12 16:21:05 +00:00
|
|
|
static int (*realclose)(CLOSE_SIG);
|
2015-10-28 00:12:32 +00:00
|
|
|
//static int (*realclone)(CLONE_SIG);
|
|
|
|
//static int (*realpoll)(POLL_SIG);
|
2015-11-12 16:21:05 +00:00
|
|
|
static int (*realdup2)(DUP2_SIG);
|
|
|
|
static int (*realdup3)(DUP3_SIG);
|
2015-10-14 17:47:35 +00:00
|
|
|
|
|
|
|
/* Exported Function Prototypes */
|
|
|
|
void my_init(void);
|
|
|
|
int connect(CONNECT_SIG);
|
|
|
|
int select(SELECT_SIG);
|
|
|
|
int bind(BIND_SIG);
|
|
|
|
int accept(ACCEPT_SIG);
|
|
|
|
int listen(LISTEN_SIG);
|
|
|
|
int socket(SOCKET_SIG);
|
|
|
|
int setsockopt(SETSOCKOPT_SIG);
|
|
|
|
int getsockopt(GETSOCKOPT_SIG);
|
|
|
|
int accept4(ACCEPT4_SIG);
|
2015-10-21 23:27:17 +00:00
|
|
|
long syscall(SYSCALL_SIG);
|
2015-11-12 16:21:05 +00:00
|
|
|
int close(CLOSE_SIG);
|
2015-10-28 00:12:32 +00:00
|
|
|
//int clone(CLONE_SIG);
|
|
|
|
//int poll(POLL_SIG);
|
2015-11-12 16:21:05 +00:00
|
|
|
int dup2(DUP2_SIG);
|
|
|
|
int dup3(DUP3_SIG);
|
|
|
|
|
2015-10-14 17:47:35 +00:00
|
|
|
|
|
|
|
#ifdef USE_SOCKS_DNS
|
2015-10-14 23:55:21 +00:00
|
|
|
int res_init(void);
|
2015-10-14 17:47:35 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int connect_to_service(void);
|
|
|
|
int init_service_connection();
|
|
|
|
void dwr(const char *fmt, ...);
|
|
|
|
void load_symbols(void);
|
|
|
|
void set_up_intercept();
|
|
|
|
int checkpid();
|
|
|
|
|
|
|
|
#define SERVICE_CONNECT_ATTEMPTS 30
|
2015-11-12 16:21:05 +00:00
|
|
|
#define RPC_FD 1023
|
2015-10-14 17:47:35 +00:00
|
|
|
|
|
|
|
ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd);
|
|
|
|
|
|
|
|
/* threading */
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
pthread_mutex_t loglock;
|
|
|
|
|
2015-10-28 00:12:32 +00:00
|
|
|
void handle_error(char *name, char *info, int err)
|
|
|
|
{
|
|
|
|
#ifdef ERRORS_ARE_FATAL
|
|
|
|
if(err < 0) {
|
|
|
|
dwr("handle_error(%s)=%d: FATAL: %s\n", name, err, info);
|
|
|
|
//exit(-1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef VERBOSE
|
2015-11-12 16:21:05 +00:00
|
|
|
dwr("%s()=%d\n", name, err);
|
2015-10-28 00:12:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
2015-10-14 17:47:35 +00:00
|
|
|
|
2015-11-12 16:21:05 +00:00
|
|
|
static unsigned long rpc_count = 0;
|
|
|
|
|
2015-10-14 17:47:35 +00:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
------------------- Intercept<--->Service Comm mechanisms-----------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static int is_initialized = 0;
|
|
|
|
static int fdret_sock; // used for fd-transfers
|
|
|
|
static int newfd; // used for "this_end" socket
|
|
|
|
static int thispid;
|
|
|
|
static char* af_sock_name = "/tmp/.ztnc_e5cd7a9e1c5311ab";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for forking
|
|
|
|
*/
|
|
|
|
int checkpid() {
|
|
|
|
if(thispid != getpid()) {
|
|
|
|
printf("clone/fork detected. re-initializing this instance.\n");
|
|
|
|
set_up_intercept();
|
|
|
|
fdret_sock = init_service_connection();
|
|
|
|
thispid = getpid();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sends an RPC command to the service
|
|
|
|
*/
|
2015-11-02 23:31:57 +00:00
|
|
|
int send_command(int rpc_fd, char *cmd)
|
2015-10-14 17:47:35 +00:00
|
|
|
{
|
2015-11-12 16:21:05 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
/*
|
|
|
|
dwr(" - IDX_PID = %d\n", IDX_PID);
|
|
|
|
dwr(" - IDX_TID = %d\n", IDX_TID);
|
|
|
|
dwr(" - IDX_COUNT = %d\n", IDX_COUNT);
|
|
|
|
dwr(" - IDX_TIME = %d\n", IDX_TIME);
|
|
|
|
dwr(" - IDX_PAYLOAD = %d\n", IDX_PAYLOAD);
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
#define IDX_PID 0
|
|
|
|
#define IDX_TID sizeof(pid_t)
|
|
|
|
#define IDX_COUNT IDX_TID + sizeof(pid_t)
|
|
|
|
#define IDX_TIME IDX_COUNT + sizeof(int)
|
|
|
|
#define IDX_CMD IDX_TIME + 20 // 20 being the length of the timestamp string
|
|
|
|
#define IDX_PAYLOAD IDX_TIME + sizeof(char)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// [pid_t] [pid_t] [rpc_count] [int] [...]
|
|
|
|
char metabuf[BUF_SZ]; // portion of buffer which contains RPC meta-data for debugging
|
|
|
|
memset(metabuf, '\0', BUF_SZ);
|
|
|
|
pid_t pid = syscall(SYS_getpid);
|
|
|
|
pid_t tid = syscall(SYS_gettid);
|
|
|
|
int payload_idx = sizeof(pid_t)*2 + sizeof(rpc_count);
|
|
|
|
rpc_count++;
|
|
|
|
char timestring[20];
|
|
|
|
time_t timestamp;
|
|
|
|
timestamp = time(NULL);
|
|
|
|
strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(×tamp));
|
|
|
|
|
|
|
|
memcpy(&metabuf[IDX_PID], &pid, sizeof(pid_t) ); // pid
|
|
|
|
memcpy(&metabuf[IDX_TID], &tid, sizeof(pid_t) ); // tid
|
|
|
|
memcpy(&metabuf[IDX_COUNT], &rpc_count, sizeof(rpc_count) ); // rpc_count
|
|
|
|
memcpy(&metabuf[IDX_TIME], ×tring, 20 ); // timestamp
|
|
|
|
#endif
|
|
|
|
// copy payload into final command buffer
|
|
|
|
int copied = BUF_SZ-IDX_PAYLOAD;
|
|
|
|
memcpy(&metabuf[IDX_PAYLOAD], cmd, 200);
|
|
|
|
//dwr(" RX: (pid=%d, tid=%d, rpc_count=%d, timestamp=%s, cmd=%d\n", pid, tid, rpc_count, timestring, cmd[0]);
|
|
|
|
int n_write = write(rpc_fd, &metabuf, BUF_SZ);
|
2015-10-14 17:47:35 +00:00
|
|
|
if(n_write < 0){
|
|
|
|
dwr("Error writing command to service (CMD = %d)\n", cmd[0]);
|
|
|
|
errno = 0;
|
2015-11-02 23:31:57 +00:00
|
|
|
return -1;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
2015-11-02 23:31:57 +00:00
|
|
|
return 0;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
2015-10-14 23:55:21 +00:00
|
|
|
|
2015-10-14 17:47:35 +00:00
|
|
|
/*
|
|
|
|
* Reads a return value from the service and sets errno (if applicable)
|
|
|
|
*/
|
|
|
|
int get_retval()
|
|
|
|
{
|
2015-10-28 21:37:46 +00:00
|
|
|
dwr("get_retval()\n");
|
2015-10-14 17:47:35 +00:00
|
|
|
if(fdret_sock >= 0) {
|
|
|
|
int retval;
|
|
|
|
int sz = sizeof(char) + sizeof(retval) + sizeof(errno);
|
|
|
|
char retbuf[BUF_SZ];
|
|
|
|
memset(&retbuf, '\0', sz);
|
|
|
|
int n_read = read(fdret_sock, &retbuf, sz);
|
|
|
|
if(n_read > 0) {
|
|
|
|
memcpy(&retval, &retbuf[1], sizeof(retval));
|
|
|
|
memcpy(&errno, &retbuf[1+sizeof(retval)], sizeof(errno));
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 19:40:01 +00:00
|
|
|
dwr("unable to read return value\n");
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-28 21:37:46 +00:00
|
|
|
/* Check whether the socket is mapped to the service or not. We
|
|
|
|
need to know if this is a regular AF_LOCAL socket or an end of a socketpair
|
|
|
|
that the service uses. We don't want to keep state in the intercept, so
|
|
|
|
we simply ask the service via an RPC */
|
|
|
|
int is_mapped_to_service(int sockfd)
|
|
|
|
{
|
|
|
|
dwr("is_mapped_to_service()\n");
|
|
|
|
char cmd[BUF_SZ];
|
|
|
|
memset(cmd, '\0', BUF_SZ);
|
|
|
|
cmd[0] = RPC_MAP_REQ;
|
|
|
|
memcpy(&cmd[1], &sockfd, sizeof(sockfd));
|
2015-11-12 16:21:05 +00:00
|
|
|
pthread_mutex_lock(&lock);
|
2015-11-02 23:31:57 +00:00
|
|
|
if(send_command(fdret_sock, cmd) < 0)
|
|
|
|
return -1;
|
2015-11-12 16:21:05 +00:00
|
|
|
int err = get_retval();
|
|
|
|
pthread_mutex_unlock(&lock);
|
|
|
|
return err;
|
2015-10-28 21:37:46 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 17:47:35 +00:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
---------- Unix-domain socket lazy initializer (for fd-transfers)--------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* Sets up the connection pipes and sockets to the service */
|
|
|
|
int init_service_connection()
|
|
|
|
{
|
2015-10-28 21:37:46 +00:00
|
|
|
dwr("init_service_connection()\n");
|
2015-10-21 23:27:17 +00:00
|
|
|
if(!is_initialized) {
|
2015-10-14 17:47:35 +00:00
|
|
|
struct sockaddr_un addr;
|
2015-10-14 23:55:21 +00:00
|
|
|
int tfd = -1, attempts = 0, conn_err = -1;
|
2015-10-14 17:47:35 +00:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path, af_sock_name, sizeof(addr.sun_path)-1);
|
|
|
|
|
|
|
|
if ( (tfd = realsocket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
|
|
|
perror("socket error");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2015-10-21 23:27:17 +00:00
|
|
|
while(conn_err < 0 && attempts < SERVICE_CONNECT_ATTEMPTS) {
|
2015-10-14 17:47:35 +00:00
|
|
|
conn_err = realconnect(tfd, (struct sockaddr*)&addr, sizeof(addr));
|
|
|
|
if(conn_err < 0) {
|
|
|
|
dwr("re-attempting connection in %ds\n", 1+attempts);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dwr("AF_UNIX connection established: %d\n", tfd);
|
|
|
|
is_initialized = 1;
|
2015-11-12 16:21:05 +00:00
|
|
|
int newtfd = realdup2(tfd, 1023);
|
|
|
|
dwr("dup'd to rpc_fd = %d\n", newtfd);
|
|
|
|
close(tfd);
|
|
|
|
return newtfd;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
|
|
|
attempts++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
------------------------ ctors and dtors (and friends)-------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void my_dest(void) __attribute__ ((destructor));
|
|
|
|
void my_dest(void) {
|
2015-10-21 23:27:17 +00:00
|
|
|
//dwr("closing connections to service...\n");
|
2015-10-14 17:47:35 +00:00
|
|
|
close(fdret_sock);
|
|
|
|
pthread_mutex_destroy(&lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_symbols(void)
|
|
|
|
{
|
|
|
|
#ifdef USE_OLD_DLSYM
|
|
|
|
void *lib;
|
|
|
|
#endif
|
|
|
|
/* possibly add check to beginning of each method to avoid needing to cll the constructor */
|
|
|
|
if(thispid == getpid()) {
|
|
|
|
dwr("detected duplicate call to global ctor (pid=%d).\n", thispid);
|
|
|
|
}
|
2015-10-14 23:55:21 +00:00
|
|
|
//dwr(" -- pid = %d\n", getpid());
|
|
|
|
//dwr(" -- uid = %d\n", getuid());
|
2015-10-14 17:47:35 +00:00
|
|
|
thispid = getpid();
|
|
|
|
|
|
|
|
#ifndef USE_OLD_DLSYM
|
|
|
|
realconnect = dlsym(RTLD_NEXT, "connect");
|
|
|
|
realbind = dlsym(RTLD_NEXT, "bind");
|
|
|
|
realaccept = dlsym(RTLD_NEXT, "accept");
|
|
|
|
reallisten = dlsym(RTLD_NEXT, "listen");
|
|
|
|
realsocket = dlsym(RTLD_NEXT, "socket");
|
|
|
|
realbind = dlsym(RTLD_NEXT, "bind");
|
|
|
|
realselect = dlsym(RTLD_NEXT, "select");
|
|
|
|
realsetsockopt = dlsym(RTLD_NEXT, "setsockopt");
|
|
|
|
realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
|
|
|
|
realaccept4 = dlsym(RTLD_NEXT, "accept4");
|
2015-10-21 23:27:17 +00:00
|
|
|
//realclone = dlsym(RTLD_NEXT, "clone");
|
2015-11-12 16:21:05 +00:00
|
|
|
realclose = dlsym(RTLD_NEXT, "close");
|
2015-10-21 23:27:17 +00:00
|
|
|
realsyscall = dlsym(RTLD_NEXT, "syscall");
|
2015-10-28 00:12:32 +00:00
|
|
|
//realsyscall = dlsym(RTLD_NEXT, "poll");
|
2015-11-12 16:21:05 +00:00
|
|
|
realdup2 = dlsym(RTLD_NEXT, "dup2");
|
|
|
|
realdup3 = dlsym(RTLD_NEXT, "dup3");
|
2015-10-14 23:55:21 +00:00
|
|
|
#ifdef USE_SOCKS_DNS
|
2015-10-14 17:47:35 +00:00
|
|
|
realresinit = dlsym(RTLD_NEXT, "res_init");
|
2015-10-14 23:55:21 +00:00
|
|
|
#endif
|
2015-10-14 17:47:35 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
lib = dlopen(LIBCONNECT, RTLD_LAZY);
|
|
|
|
realconnect = dlsym(lib, "connect");
|
|
|
|
realbind = dlsym(lib, "bind");
|
|
|
|
realaccept = dlsym(lib, "accept");
|
|
|
|
reallisten = dlsym(lib, "listen");
|
|
|
|
realsocket = dlsym(lib, "socket");
|
|
|
|
realselect = dlsym(lib, "select");
|
|
|
|
realsetsockopt = dlsym(lib, "setsockopt");
|
|
|
|
realgetsockopt = dlsym(lib, "getsockopt");
|
|
|
|
realaccept4 = dlsym(lib), "accept4");
|
2015-10-21 23:27:17 +00:00
|
|
|
//realclone = dlsym(lib, "clone");
|
2015-11-12 16:21:05 +00:00
|
|
|
realclose = dlsym(lib, "close");
|
2015-10-21 23:27:17 +00:00
|
|
|
realsyscall = dlsym(lib, "syscall");
|
2015-10-28 00:12:32 +00:00
|
|
|
//realsyscall = dlsym(lib, "poll");
|
2015-11-12 16:21:05 +00:00
|
|
|
realdup2 = dlsym(RTLD_NEXT, "dup2");
|
|
|
|
realdup3 = dlsym(RTLD_NEXT, "dup3");
|
2015-10-14 23:55:21 +00:00
|
|
|
#ifdef USE_SOCKS_DNS
|
2015-10-14 17:47:35 +00:00
|
|
|
realresinit = dlsym(lib, "res_init");
|
2015-10-14 23:55:21 +00:00
|
|
|
#endif
|
2015-10-14 17:47:35 +00:00
|
|
|
dlclose(lib);
|
|
|
|
lib = dlopen(LIBC, RTLD_LAZY);
|
|
|
|
dlclose(lib);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Private Function Prototypes */
|
|
|
|
void _init(void) __attribute__ ((constructor));
|
|
|
|
void _init(void) {
|
|
|
|
set_up_intercept();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get symbols and initialize mutexes */
|
|
|
|
void set_up_intercept()
|
|
|
|
{
|
|
|
|
load_symbols();
|
|
|
|
if(pthread_mutex_init(&lock, NULL) != 0) {
|
|
|
|
printf("error while initializing service call mutex\n");
|
|
|
|
}
|
|
|
|
if(pthread_mutex_init(&loglock, NULL) != 0) {
|
|
|
|
printf("error while initializing log mutex mutex\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
--------------------------------- setsockopt() ---------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
2015-10-28 21:37:46 +00:00
|
|
|
|
2015-10-14 17:47:35 +00:00
|
|
|
/* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
|
|
|
|
int setsockopt(SETSOCKOPT_SIG)
|
|
|
|
{
|
2015-11-12 16:21:05 +00:00
|
|
|
dwr("\n\nsetsockopt(%d)\n", socket);
|
2015-11-02 23:52:12 +00:00
|
|
|
/*
|
2015-11-02 23:31:57 +00:00
|
|
|
if(is_mapped_to_service(socket) < 0) { // First, check if the service manages this
|
|
|
|
return realsetsockopt(socket, level, option_name, option_value, option_len);
|
|
|
|
}
|
2015-11-02 23:52:12 +00:00
|
|
|
*/
|
2015-10-28 00:12:32 +00:00
|
|
|
//return(realsetsockopt(socket, level, option_name, option_value, option_len));
|
2015-10-31 00:00:30 +00:00
|
|
|
if(level == SOL_IPV6 && option_name == IPV6_V6ONLY)
|
|
|
|
return 0;
|
|
|
|
if(level == SOL_IP && option_name == IP_TTL)
|
|
|
|
return 0;
|
2015-11-02 23:31:57 +00:00
|
|
|
if(level == IPPROTO_TCP || (level == SOL_SOCKET && option_name == SO_KEEPALIVE))
|
2015-10-21 23:27:17 +00:00
|
|
|
return 0;
|
2015-10-14 17:47:35 +00:00
|
|
|
/* make sure we don't touch any standard outputs */
|
|
|
|
if(socket == STDIN_FILENO || socket == STDOUT_FILENO || socket == STDERR_FILENO)
|
|
|
|
return(realsetsockopt(socket, level, option_name, option_value, option_len));
|
|
|
|
int err = realsetsockopt(socket, level, option_name, option_value, option_len);
|
|
|
|
if(err < 0){
|
|
|
|
//perror("setsockopt():\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
--------------------------------- getsockopt() ---------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
2015-10-28 21:37:46 +00:00
|
|
|
/* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
|
2015-10-14 17:47:35 +00:00
|
|
|
int getsockopt(GETSOCKOPT_SIG)
|
|
|
|
{
|
2015-11-12 16:21:05 +00:00
|
|
|
dwr("\n\ngetsockopt(%d)\n", sockfd);
|
2015-11-02 23:52:12 +00:00
|
|
|
/*
|
2015-11-02 23:31:57 +00:00
|
|
|
if(is_mapped_to_service(sockfd) < 0) { // First, check if the service manages this
|
|
|
|
return realgetsockopt(sockfd, level, optname, optval, optlen);
|
|
|
|
}
|
2015-11-02 23:52:12 +00:00
|
|
|
*/
|
2015-10-14 17:47:35 +00:00
|
|
|
int err = realgetsockopt(sockfd, level, optname, optval, optlen);
|
|
|
|
// FIXME: this condition will need a little more intelligence later on
|
|
|
|
// -- we will need to know if this fd is a local we are spoofing, or a true local
|
|
|
|
if(optname == SO_TYPE)
|
|
|
|
{
|
|
|
|
int* val = (int*)optval;
|
|
|
|
*val = 2;
|
|
|
|
optval = (void*)val;
|
|
|
|
}
|
|
|
|
if(err < 0){
|
|
|
|
//perror("setsockopt():\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
----------------------------------- socket() -----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* int socket_family, int socket_type, int protocol
|
|
|
|
socket() intercept function */
|
|
|
|
int socket(SOCKET_SIG)
|
|
|
|
{
|
2015-11-12 16:21:05 +00:00
|
|
|
dwr("\n\nsocket()*:\n");
|
2015-10-14 17:47:35 +00:00
|
|
|
int err;
|
|
|
|
#ifdef CHECKS
|
|
|
|
/* Check that type makes sense */
|
|
|
|
int flags = socket_type & ~SOCK_TYPE_MASK;
|
2015-10-14 19:46:22 +00:00
|
|
|
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
|
|
|
|
errno = EINVAL;
|
2015-10-31 00:00:30 +00:00
|
|
|
handle_error("socket1", "", -1);
|
2015-10-14 19:46:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-14 17:47:35 +00:00
|
|
|
socket_type &= SOCK_TYPE_MASK;
|
|
|
|
/* Check protocol is in range */
|
2015-10-14 19:46:22 +00:00
|
|
|
if (socket_family < 0 || socket_family >= NPROTO){
|
|
|
|
errno = EAFNOSUPPORT;
|
2015-10-31 00:00:30 +00:00
|
|
|
handle_error("socket2", "", -1);
|
2015-10-14 19:46:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (socket_type < 0 || socket_type >= SOCK_MAX) {
|
|
|
|
errno = EINVAL;
|
2015-10-31 00:00:30 +00:00
|
|
|
handle_error("socket3", "", -1);
|
2015-10-14 19:46:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-14 17:47:35 +00:00
|
|
|
/* Check that we haven't hit the soft-limit file descriptors allowed */
|
|
|
|
/* FIXME: Find number of open fds
|
|
|
|
struct rlimit rl;
|
|
|
|
getrlimit(RLIMIT_NOFILE, &rl);
|
|
|
|
if(sockfd >= rl.rlim_cur){
|
|
|
|
errno = EMFILE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/* FIXME: detect ENFILE condition */
|
|
|
|
#endif
|
|
|
|
char cmd[BUF_SZ];
|
|
|
|
fdret_sock = !is_initialized ? init_service_connection() : fdret_sock;
|
2015-10-22 20:20:50 +00:00
|
|
|
if(fdret_sock < 0) {
|
2015-10-22 19:40:01 +00:00
|
|
|
dwr("BAD service connection. exiting.\n");
|
2015-10-31 00:00:30 +00:00
|
|
|
handle_error("socket4", "", -1);
|
2015-10-22 19:40:01 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
2015-10-14 17:47:35 +00:00
|
|
|
if(socket_family == AF_LOCAL
|
|
|
|
|| socket_family == AF_NETLINK
|
|
|
|
|| socket_family == AF_UNIX) {
|
2015-10-28 00:12:32 +00:00
|
|
|
int err = realsocket(socket_family, socket_type, protocol);
|
|
|
|
dwr("realsocket, err = %d\n", err);
|
2015-10-31 00:00:30 +00:00
|
|
|
handle_error("socket5", "", err);
|
2015-10-28 00:12:32 +00:00
|
|
|
return err;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
2015-10-21 23:27:17 +00:00
|
|
|
/* Assemble and send RPC */
|
2015-10-14 17:47:35 +00:00
|
|
|
struct socket_st rpc_st;
|
|
|
|
rpc_st.socket_family = socket_family;
|
|
|
|
rpc_st.socket_type = socket_type;
|
|
|
|
rpc_st.protocol = protocol;
|
|
|
|
rpc_st.__tid = syscall(SYS_gettid);
|
|
|
|
memset(cmd, '\0', BUF_SZ);
|
|
|
|
cmd[0] = RPC_SOCKET;
|
2015-10-30 00:57:59 +00:00
|
|
|
dwr("pid = %d\n", thispid);
|
|
|
|
memcpy(&cmd[1]+sizeof(pid_t), &rpc_st, sizeof(struct socket_st));
|
2015-10-14 17:47:35 +00:00
|
|
|
pthread_mutex_lock(&lock);
|
2015-10-14 19:46:22 +00:00
|
|
|
send_command(fdret_sock, cmd);
|
2015-10-14 17:47:35 +00:00
|
|
|
|
|
|
|
/* get new fd */
|
2015-10-14 19:46:22 +00:00
|
|
|
char rbuf[16];
|
|
|
|
ssize_t sz = sock_fd_read(fdret_sock, rbuf, sizeof(rbuf), &newfd);
|
2015-11-12 16:21:05 +00:00
|
|
|
int tmp = newfd;
|
2015-10-31 00:00:30 +00:00
|
|
|
dwr("read %d bytes (%s)\n", sz, &rbuf);
|
2015-10-14 19:46:22 +00:00
|
|
|
if(sz > 0)
|
2015-10-14 17:47:35 +00:00
|
|
|
{
|
2015-11-12 16:21:05 +00:00
|
|
|
dwr("sending fd = %d to Service over (%d)\n", newfd, fdret_sock);
|
2015-10-14 17:47:35 +00:00
|
|
|
/* send our local-fd number back to service so
|
|
|
|
it can complete its mapping table entry */
|
|
|
|
memset(cmd, '\0', BUF_SZ);
|
2015-10-28 00:12:32 +00:00
|
|
|
cmd[0] = RPC_MAP;
|
2015-10-14 17:47:35 +00:00
|
|
|
memcpy(&cmd[1], &newfd, sizeof(newfd));
|
2015-10-22 19:40:01 +00:00
|
|
|
|
2015-10-31 00:00:30 +00:00
|
|
|
if(newfd > -1) { // FIXME: check logic
|
2015-10-14 17:47:35 +00:00
|
|
|
send_command(fdret_sock, cmd);
|
|
|
|
pthread_mutex_unlock(&lock);
|
|
|
|
errno = ERR_OK; // OK
|
2015-10-31 00:00:30 +00:00
|
|
|
handle_error("socket6", "", newfd);
|
2015-10-14 17:47:35 +00:00
|
|
|
return newfd;
|
2015-10-31 00:00:30 +00:00
|
|
|
}
|
2015-10-14 17:47:35 +00:00
|
|
|
else { // Try to read retval+errno since we RXed a bad fd
|
|
|
|
dwr("Error, service sent bad fd.\n");
|
|
|
|
err = get_retval();
|
|
|
|
pthread_mutex_unlock(&lock);
|
2015-10-31 00:00:30 +00:00
|
|
|
handle_error("socket7", "", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return err;
|
|
|
|
}
|
2015-10-31 00:00:30 +00:00
|
|
|
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
dwr("Error while receiving new FD.\n");
|
|
|
|
err = get_retval();
|
|
|
|
pthread_mutex_unlock(&lock);
|
2015-10-31 00:00:30 +00:00
|
|
|
handle_error("socket8", "", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
---------------------------------- connect() -----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* int __fd, const struct sockaddr * __addr, socklen_t __len
|
|
|
|
connect() intercept function */
|
|
|
|
int connect(CONNECT_SIG)
|
|
|
|
{
|
2015-11-12 16:21:05 +00:00
|
|
|
dwr("\n\nconnect(%d):\n", __fd);
|
2015-10-28 00:12:32 +00:00
|
|
|
print_addr(__addr);
|
2015-10-14 17:47:35 +00:00
|
|
|
struct sockaddr_in *connaddr;
|
|
|
|
connaddr = (struct sockaddr_in *) __addr;
|
|
|
|
|
|
|
|
#ifdef CHECKS
|
|
|
|
/* Check that this is a valid fd */
|
|
|
|
if(fcntl(__fd, F_GETFD) < 0) {
|
|
|
|
errno = EBADF;
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("connect", "EBADF", -1);
|
|
|
|
return -1;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
|
|
|
/* Check that it is a socket */
|
|
|
|
int sock_type;
|
|
|
|
socklen_t sock_type_len = sizeof(sock_type);
|
|
|
|
if(getsockopt(__fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
|
|
|
|
errno = ENOTSOCK;
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("connect", "ENOTSOCK", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Check family */
|
|
|
|
if (connaddr->sin_family < 0 || connaddr->sin_family >= NPROTO){
|
|
|
|
errno = EAFNOSUPPORT;
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("connect", "EAFNOSUPPORT", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* FIXME: Check that address is in user space, return EFAULT ? */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* make sure we don't touch any standard outputs */
|
|
|
|
if(__fd == STDIN_FILENO || __fd == STDOUT_FILENO || __fd == STDERR_FILENO){
|
|
|
|
if (realconnect == NULL) {
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("connect", "Unresolved symbol [connect]", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
return(realconnect(__fd, __addr, __len));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(__addr != NULL && (connaddr->sin_family == AF_LOCAL
|
|
|
|
|| connaddr->sin_family == PF_NETLINK
|
|
|
|
|| connaddr->sin_family == AF_NETLINK
|
|
|
|
|| connaddr->sin_family == AF_UNIX)) {
|
|
|
|
int err = realconnect(__fd, __addr, __len);
|
2015-10-28 00:12:32 +00:00
|
|
|
perror("connect():");
|
|
|
|
//handle_error("connect", "Cannot connect to local socket", err);
|
2015-10-14 17:47:35 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:27:17 +00:00
|
|
|
/* Assemble and send RPC */
|
2015-10-14 17:47:35 +00:00
|
|
|
int err;
|
|
|
|
char cmd[BUF_SZ];
|
|
|
|
memset(cmd, '\0', BUF_SZ);
|
|
|
|
struct connect_st rpc_st;
|
|
|
|
rpc_st.__tid = syscall(SYS_gettid);
|
|
|
|
rpc_st.__fd = __fd;
|
|
|
|
memcpy(&rpc_st.__addr, __addr, sizeof(struct sockaddr));
|
|
|
|
memcpy(&rpc_st.__len, &__len, sizeof(socklen_t));
|
|
|
|
cmd[0] = RPC_CONNECT;
|
|
|
|
memcpy(&cmd[1], &rpc_st, sizeof(struct connect_st));
|
|
|
|
pthread_mutex_lock(&lock);
|
|
|
|
send_command(fdret_sock, cmd);
|
2015-10-21 23:27:17 +00:00
|
|
|
/*
|
2015-10-16 22:02:07 +00:00
|
|
|
if(sock_type && O_NONBLOCK) {
|
|
|
|
//pthread_mutex_unlock(&lock);
|
|
|
|
//return EINPROGRESS;
|
|
|
|
}
|
2015-10-21 23:27:17 +00:00
|
|
|
*/
|
2015-10-14 17:47:35 +00:00
|
|
|
err = get_retval();
|
|
|
|
pthread_mutex_unlock(&lock);
|
2015-10-28 00:12:32 +00:00
|
|
|
//handle_error("connect", "", err);
|
2015-10-14 17:47:35 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
---------------------------------- select() ------------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* int n, fd_set *readfds, fd_set *writefds,
|
|
|
|
fd_set *exceptfds, struct timeval *timeout */
|
|
|
|
int select(SELECT_SIG)
|
|
|
|
{
|
2015-10-28 21:37:46 +00:00
|
|
|
//dwr("select():\n");
|
2015-10-14 17:47:35 +00:00
|
|
|
return realselect(n, readfds, writefds, exceptfds, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
------------------------------------ bind() ------------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* int sockfd, const struct sockaddr *addr, socklen_t addrlen
|
|
|
|
bind() intercept function */
|
|
|
|
int bind(BIND_SIG)
|
|
|
|
{
|
2015-11-12 16:21:05 +00:00
|
|
|
dwr("\n\nbind(%d):\n", sockfd);
|
2015-10-28 00:12:32 +00:00
|
|
|
print_addr(addr);
|
2015-10-14 17:47:35 +00:00
|
|
|
#ifdef CHECKS
|
|
|
|
/* Check that this is a valid fd */
|
|
|
|
if(fcntl(sockfd, F_GETFD) < 0) {
|
|
|
|
errno = EBADF;
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("bind", "EBADF", -1);
|
|
|
|
return -1;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
|
|
|
/* Check that it is a socket */
|
2015-10-21 23:27:17 +00:00
|
|
|
int opt = -1;
|
|
|
|
socklen_t opt_len;
|
|
|
|
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
|
2015-10-14 17:47:35 +00:00
|
|
|
errno = ENOTSOCK;
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("bind", "ENOTSOCK", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int err;
|
|
|
|
/* make sure we don't touch any standard outputs */
|
|
|
|
if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
|
|
|
|
return(realbind(sockfd, addr, addrlen));
|
|
|
|
|
|
|
|
/* If local, just use normal syscall */
|
|
|
|
struct sockaddr_in *connaddr;
|
2015-10-28 21:37:46 +00:00
|
|
|
connaddr = (struct sockaddr_in *)addr;
|
2015-10-14 17:47:35 +00:00
|
|
|
|
2015-10-28 21:37:46 +00:00
|
|
|
if(connaddr->sin_family == AF_LOCAL
|
2015-10-14 17:47:35 +00:00
|
|
|
|| connaddr->sin_family == AF_NETLINK
|
2015-10-28 21:37:46 +00:00
|
|
|
|| connaddr->sin_family == AF_UNIX) {
|
|
|
|
int err = realbind(sockfd, addr, addrlen);
|
|
|
|
dwr("realbind, err = %d\n", err);
|
|
|
|
return err;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
2015-10-28 21:37:46 +00:00
|
|
|
|
2015-10-21 23:27:17 +00:00
|
|
|
/* Assemble and send RPC */
|
2015-10-14 17:47:35 +00:00
|
|
|
char cmd[BUF_SZ];
|
|
|
|
struct bind_st rpc_st;
|
|
|
|
rpc_st.sockfd = sockfd;
|
|
|
|
rpc_st.__tid = syscall(SYS_gettid);
|
|
|
|
memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr));
|
|
|
|
memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
|
|
|
|
cmd[0]=RPC_BIND;
|
|
|
|
memcpy(&cmd[1], &rpc_st, sizeof(struct bind_st));
|
|
|
|
pthread_mutex_lock(&lock);
|
|
|
|
send_command(fdret_sock, cmd);
|
|
|
|
err = get_retval();
|
|
|
|
pthread_mutex_unlock(&lock);
|
|
|
|
errno = ERR_OK;
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("bind", "", err);
|
2015-10-14 17:47:35 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
----------------------------------- accept4() ----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
/* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
|
|
|
|
int accept4(ACCEPT4_SIG)
|
|
|
|
{
|
2015-11-12 16:21:05 +00:00
|
|
|
dwr("\n\naccept4(%d):\n", sockfd);
|
2015-10-14 17:47:35 +00:00
|
|
|
#ifdef CHECKS
|
2015-10-14 19:46:22 +00:00
|
|
|
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-14 17:47:35 +00:00
|
|
|
#endif
|
2015-10-21 23:27:17 +00:00
|
|
|
int newfd = accept(sockfd, addr, addrlen);
|
|
|
|
if(newfd > 0) {
|
|
|
|
if(flags & SOCK_CLOEXEC)
|
|
|
|
fcntl(newfd, F_SETFL, FD_CLOEXEC);
|
|
|
|
if(flags & SOCK_NONBLOCK)
|
|
|
|
fcntl(newfd, F_SETFL, O_NONBLOCK);
|
|
|
|
}
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("accept4", "", newfd);
|
2015-10-21 23:27:17 +00:00
|
|
|
return newfd;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
----------------------------------- accept() -----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* int sockfd struct sockaddr *addr, socklen_t *addrlen
|
|
|
|
accept() intercept function */
|
|
|
|
int accept(ACCEPT_SIG)
|
|
|
|
{
|
2015-11-12 16:21:05 +00:00
|
|
|
dwr("\n\naccept(%d):\n", sockfd);
|
2015-10-14 17:47:35 +00:00
|
|
|
#ifdef CHECKS
|
|
|
|
/* Check that this is a valid fd */
|
|
|
|
if(fcntl(sockfd, F_GETFD) < 0) {
|
|
|
|
return -1;
|
|
|
|
errno = EBADF;
|
2015-10-28 00:12:32 +00:00
|
|
|
dwr("EBADF\n");
|
|
|
|
handle_error("accept", "EBADF", -1);
|
|
|
|
return -1;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
|
|
|
/* Check that it is a socket */
|
2015-10-21 23:27:17 +00:00
|
|
|
int opt;
|
|
|
|
socklen_t opt_len;
|
|
|
|
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
|
2015-10-14 17:47:35 +00:00
|
|
|
errno = ENOTSOCK;
|
2015-10-28 00:12:32 +00:00
|
|
|
dwr("ENOTSOCK\n");
|
|
|
|
handle_error("accept", "ENOTSOCK", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Check that this socket supports accept() */
|
2015-10-21 23:27:17 +00:00
|
|
|
if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) {
|
2015-10-14 17:47:35 +00:00
|
|
|
errno = EOPNOTSUPP;
|
2015-10-28 00:12:32 +00:00
|
|
|
dwr("EOPNOTSUPP\n");
|
|
|
|
handle_error("accept", "EOPNOTSUPP", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Check that we haven't hit the soft-limit file descriptors allowed */
|
|
|
|
struct rlimit rl;
|
|
|
|
getrlimit(RLIMIT_NOFILE, &rl);
|
|
|
|
if(sockfd >= rl.rlim_cur){
|
|
|
|
errno = EMFILE;
|
2015-10-28 00:12:32 +00:00
|
|
|
dwr("EMFILE\n");
|
|
|
|
handle_error("accept", "EMFILE", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-16 23:30:01 +00:00
|
|
|
/* Check address length */
|
|
|
|
if(addrlen < 0) {
|
|
|
|
errno = EINVAL;
|
2015-10-28 00:12:32 +00:00
|
|
|
dwr("EINVAL\n");
|
|
|
|
handle_error("accept", "EINVAL", -1);
|
2015-10-16 23:30:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-14 17:47:35 +00:00
|
|
|
#endif
|
|
|
|
|
2015-10-21 23:27:17 +00:00
|
|
|
/* redirect calls for standard I/O descriptors to kernel */
|
2015-10-28 00:12:32 +00:00
|
|
|
if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO){
|
|
|
|
dwr("realaccept():\n");
|
2015-10-14 17:47:35 +00:00
|
|
|
return(realaccept(sockfd, addr, addrlen));
|
2015-10-28 00:12:32 +00:00
|
|
|
}
|
2015-10-14 17:47:35 +00:00
|
|
|
|
2015-10-21 23:27:17 +00:00
|
|
|
if(addr)
|
|
|
|
addr->sa_family = AF_INET;
|
|
|
|
/* TODO: also get address info */
|
2015-10-14 17:47:35 +00:00
|
|
|
|
|
|
|
char cmd[BUF_SZ];
|
|
|
|
if(realaccept == NULL) {
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("accept", "Unresolved symbol [accept]", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-21 23:27:17 +00:00
|
|
|
//if(opt & O_NONBLOCK)
|
2015-10-23 20:37:41 +00:00
|
|
|
//fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
2015-10-21 23:27:17 +00:00
|
|
|
|
2015-10-14 19:46:22 +00:00
|
|
|
char rbuf[16], c[1];
|
|
|
|
int new_conn_socket;
|
2015-10-28 00:12:32 +00:00
|
|
|
|
2015-10-14 19:46:22 +00:00
|
|
|
int n = read(sockfd, c, sizeof(c)); // Read signal byte
|
2015-10-14 17:47:35 +00:00
|
|
|
if(n > 0)
|
|
|
|
{
|
2015-10-14 19:46:22 +00:00
|
|
|
ssize_t size = sock_fd_read(fdret_sock, rbuf, sizeof(rbuf), &new_conn_socket);
|
2015-10-14 17:47:35 +00:00
|
|
|
if(size > 0) {
|
|
|
|
/* Send our local-fd number back to service so it can complete its mapping table */
|
|
|
|
memset(cmd, '\0', BUF_SZ);
|
2015-10-28 00:12:32 +00:00
|
|
|
cmd[0] = RPC_MAP;
|
2015-10-14 17:47:35 +00:00
|
|
|
memcpy(&cmd[1], &new_conn_socket, sizeof(new_conn_socket));
|
|
|
|
pthread_mutex_lock(&lock);
|
|
|
|
int n_write = write(fdret_sock, cmd, BUF_SZ);
|
|
|
|
if(n_write < 0) {
|
2015-10-14 19:46:22 +00:00
|
|
|
errno = ECONNABORTED; // FIXME: Closest match, service unreachable
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("accept", "ECONNABORTED - Error sending perceived FD to service", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&lock);
|
2015-10-21 23:27:17 +00:00
|
|
|
errno = ERR_OK;
|
2015-11-02 23:31:57 +00:00
|
|
|
dwr("*accept()=%d\n", new_conn_socket);
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("accept", "", new_conn_socket);
|
2015-10-14 17:47:35 +00:00
|
|
|
return new_conn_socket; // OK
|
|
|
|
}
|
|
|
|
else {
|
2015-10-14 19:46:22 +00:00
|
|
|
errno = ECONNABORTED; // FIXME: Closest match, service unreachable
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("accept", "ECONNABORTED - Error receiving new FD from service", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2015-11-02 23:31:57 +00:00
|
|
|
errno = EBADF;
|
2015-10-31 00:00:30 +00:00
|
|
|
handle_error("accept", "EBADF - Error reading signal byte from service", -1);
|
|
|
|
return -1;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
------------------------------------- listen()----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* int sockfd, int backlog
|
|
|
|
listen() intercept function */
|
|
|
|
int listen(LISTEN_SIG)
|
|
|
|
{
|
2015-11-12 16:21:05 +00:00
|
|
|
dwr("\n\nlisten(%d):\n", sockfd);
|
2015-10-28 21:37:46 +00:00
|
|
|
int sock_type;
|
|
|
|
socklen_t sock_type_len = sizeof(sock_type);
|
|
|
|
|
2015-10-14 17:47:35 +00:00
|
|
|
#ifdef CHECKS
|
|
|
|
/* Check that this is a valid fd */
|
|
|
|
if(fcntl(sockfd, F_GETFD) < 0) {
|
|
|
|
errno = EBADF;
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("listen", "EBADF", -1);
|
|
|
|
return -1;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
|
|
|
/* Check that it is a socket */
|
|
|
|
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
|
|
|
|
errno = ENOTSOCK;
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("listen", "ENOTSOCK", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Check that this socket supports accept() */
|
|
|
|
if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) {
|
|
|
|
errno = EOPNOTSUPP;
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("listen", "EOPNOTSUPP", -1);
|
2015-10-14 17:47:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* make sure we don't touch any standard outputs */
|
|
|
|
if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
|
|
|
|
return(reallisten(sockfd, backlog));
|
|
|
|
|
2015-11-02 23:31:57 +00:00
|
|
|
if(is_mapped_to_service(sockfd) < 0) {
|
2015-10-28 21:37:46 +00:00
|
|
|
// We now know this socket is not one of our socketpairs
|
|
|
|
int err = reallisten(sockfd, backlog);
|
|
|
|
dwr("reallisten()=%d\n", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:27:17 +00:00
|
|
|
/* Assemble and send RPC */
|
2015-10-14 17:47:35 +00:00
|
|
|
char cmd[BUF_SZ];
|
|
|
|
memset(cmd, '\0', BUF_SZ);
|
|
|
|
struct listen_st rpc_st;
|
|
|
|
rpc_st.sockfd = sockfd;
|
|
|
|
rpc_st.backlog = backlog;
|
|
|
|
rpc_st.__tid = syscall(SYS_gettid);
|
|
|
|
cmd[0] = RPC_LISTEN;
|
|
|
|
memcpy(&cmd[1], &rpc_st, sizeof(struct listen_st));
|
|
|
|
pthread_mutex_lock(&lock);
|
|
|
|
send_command(fdret_sock, cmd);
|
2015-10-31 00:00:30 +00:00
|
|
|
int err = get_retval();
|
2015-10-14 17:47:35 +00:00
|
|
|
pthread_mutex_unlock(&lock);
|
2015-10-28 00:12:32 +00:00
|
|
|
handle_error("listen", "", ERR_OK);
|
2015-10-16 18:43:38 +00:00
|
|
|
return ERR_OK;
|
2015-10-14 17:47:35 +00:00
|
|
|
}
|
2015-10-21 23:27:17 +00:00
|
|
|
|
2015-10-28 00:12:32 +00:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
-------------------------------------- clone()----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// int (*fn)(void *), void *child_stack, int flags, void *arg, ...
|
|
|
|
/*
|
|
|
|
int clone(CLONE_SIG)
|
|
|
|
{
|
|
|
|
dwr("clone()\n");
|
|
|
|
return realclone(fn, child_stack, flags, arg);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
-------------------------------------- poll()-----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// struct pollfd *fds, nfds_t nfds, int timeout
|
|
|
|
/*
|
|
|
|
int poll(POLL_SIG)
|
|
|
|
{
|
|
|
|
dwr("poll()\n");
|
|
|
|
return realpoll(fds, nfds, timeout);
|
|
|
|
//return ERESTART_RESTARTBLOCK;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2015-11-12 16:21:05 +00:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
-------------------------------------- close()-----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// int fd
|
|
|
|
int close(CLOSE_SIG)
|
|
|
|
{
|
|
|
|
dwr("close(%d)\n", fd);
|
|
|
|
if(fd == fdret_sock)
|
|
|
|
return 0; // FIXME: Ignore request to shut down our rpc fd, this is *almost always* safe
|
|
|
|
if(fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO){
|
|
|
|
return realclose(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
-------------------------------------- dup2()-----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// int oldfd, int newfd
|
|
|
|
int dup2(DUP2_SIG)
|
|
|
|
{
|
|
|
|
dwr("dup2(%d, %d)\n", oldfd, newfd);
|
|
|
|
if(oldfd == fdret_sock) {
|
|
|
|
dwr("client application attempted to dup2 RPC socket (%d). This is not allowed.\n", oldfd);
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(oldfd != STDIN_FILENO && oldfd != STDOUT_FILENO && oldfd != STDERR_FILENO) {
|
|
|
|
return realdup2(oldfd, newfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
-------------------------------------- dup3()-----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// int oldfd, int newfd, int flags
|
|
|
|
int dup3(DUP3_SIG)
|
|
|
|
{
|
|
|
|
dwr("dup3(%d, %d, %d)\n", oldfd, newfd, flags);
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Only do this check if we want to debug the intercept, otherwise, dont mess with
|
|
|
|
// the client application's logging methods
|
|
|
|
if(newfd == STDIN_FILENO || newfd == STDOUT_FILENO || newfd == STDERR_FILENO)
|
|
|
|
return newfd; // FIXME: This is to prevent httpd from dup'ing over our stderr
|
|
|
|
//and preventing us from debugging
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return realdup3(oldfd, newfd, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-21 23:27:17 +00:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
------------------------------------ syscall()----------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
long syscall(SYSCALL_SIG)
|
|
|
|
{
|
2015-10-31 00:00:30 +00:00
|
|
|
dwr("syscall(%u, ...):\n", number);
|
|
|
|
|
2015-10-21 23:27:17 +00:00
|
|
|
va_list ap;
|
|
|
|
uintptr_t a,b,c,d,e,f;
|
|
|
|
va_start(ap, number);
|
|
|
|
a=va_arg(ap, uintptr_t);
|
|
|
|
b=va_arg(ap, uintptr_t);
|
|
|
|
c=va_arg(ap, uintptr_t);
|
|
|
|
d=va_arg(ap, uintptr_t);
|
|
|
|
e=va_arg(ap, uintptr_t);
|
|
|
|
f=va_arg(ap, uintptr_t);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
#if defined(__i386__)
|
|
|
|
/* TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args);
|
|
|
|
args[0] = (unsigned long) fd;
|
|
|
|
args[1] = (unsigned long) addr;
|
|
|
|
args[2] = (unsigned long) addrlen;
|
|
|
|
args[3] = (unsigned long) flags;
|
|
|
|
*/
|
|
|
|
#else
|
|
|
|
if(number == __NR_accept4) {
|
|
|
|
int sockfd = a;
|
|
|
|
struct sockaddr * addr = (struct sockaddr*)b;
|
|
|
|
socklen_t * addrlen = (socklen_t*)c;
|
|
|
|
int flags = d;
|
2015-10-31 00:00:30 +00:00
|
|
|
int old_errno = errno;
|
|
|
|
int err = accept4(sockfd, addr, addrlen, flags);
|
|
|
|
errno = old_errno;
|
|
|
|
|
|
|
|
if(err == -EBADF) {
|
|
|
|
//errno = EAGAIN;
|
|
|
|
err = -EAGAIN;
|
|
|
|
//exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2015-10-21 23:27:17 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return realsyscall(number,a,b,c,d,e,f);
|
|
|
|
}
|