[tor-commits] [torsocks/master] Add basic interface for libc overload
dgoulet at torproject.org
dgoulet at torproject.org
Fri Apr 4 22:40:25 UTC 2014
commit 9acdf15bf1bc7de354dff59c611c44d58cfc83c3
Author: David Goulet <dgoulet at ev0ke.net>
Date: Thu Jun 6 20:37:55 2013 -0400
Add basic interface for libc overload
Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
src/common/compat.h | 6 ++++++
src/lib/Makefile.am | 6 +++---
src/lib/torsocks.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
src/lib/torsocks.h | 14 ++++++++++++++
4 files changed, 73 insertions(+), 3 deletions(-)
diff --git a/src/common/compat.h b/src/common/compat.h
index 3ac4941..020fe8e 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -18,6 +18,12 @@
#ifndef TORSOCKS_COMPAT_H
#define TORSOCKS_COMPAT_H
+#if (defined(__linux__) || defined(__FreeBSD__) || defined(__darwin__))
+
+#define RTLD_NEXT ((void *) -1)
+
+#endif /* __linux__, __FreeBSD__, __darwin__ */
+
#ifdef __linux__
#include <pthread.h>
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index f578d3f..7a1586d 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -1,4 +1,5 @@
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(builddir)
+AM_CFLAGS = -fno-strict-aliasing
libdir = @libdir@/torsocks
@@ -11,7 +12,6 @@ lib_LTLIBRARIES = libtorsocks.la
libtorsocks_la_SOURCES = torsocks.c torsocks.h
-libtorsocks_la_LDFLAGS = $(TORSOCKSLDFLAGS)
-
libtorsocks_la_LIBADD = \
- $(top_builddir)/src/common/libcommon.la
+ $(top_builddir)/src/common/libcommon.la \
+ -ldl
diff --git a/src/lib/torsocks.c b/src/lib/torsocks.c
index c0450ad..f5029e7 100644
--- a/src/lib/torsocks.c
+++ b/src/lib/torsocks.c
@@ -17,6 +17,8 @@
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include <assert.h>
+#include <dlfcn.h>
#include <stdlib.h>
#include <common/defaults.h>
@@ -31,6 +33,27 @@
static int is_suid;
/*
+ * Lookup symbol in the loaded libraries of the binary.
+ *
+ * Return the function pointer or NULL on error.
+ */
+static void *find_libc_symbol(const char *symbol)
+{
+ void *fct_ptr = NULL;
+
+ assert(symbol);
+
+ fct_ptr = dlsym(RTLD_NEXT, symbol);
+ if (!fct_ptr) {
+ ERR("Unable to find %s", symbol);
+ goto end;
+ }
+
+end:
+ return fct_ptr;
+}
+
+/*
* Initialize logging subsytem using either the default values or the one given
* by the environment variables.
*/
@@ -85,3 +108,30 @@ static void __attribute__((constructor)) init()
init_logging();
}
+
+/*
+ * Cleanup and exit with the given status.
+ */
+static void clean_exit(int status)
+{
+ exit(status);
+}
+
+/*
+ * Libc hijacked symbol connect(2).
+ */
+int connect(LIBC_CONNECT_SIG)
+{
+ static int (*libc_connect)(LIBC_CONNECT_SIG) = NULL;
+
+ /* Find symbol if not already set. */
+ if (!libc_connect) {
+ libc_connect = find_libc_symbol("connect");
+ if (!libc_connect) {
+ ERR("This is critical for torsocks. Exiting");
+ clean_exit(EXIT_FAILURE);
+ }
+ }
+
+ return libc_connect(_sockfd, _addr, _addrlen);
+}
diff --git a/src/lib/torsocks.h b/src/lib/torsocks.h
index 7e6eec1..122d711 100644
--- a/src/lib/torsocks.h
+++ b/src/lib/torsocks.h
@@ -22,4 +22,18 @@
#include <common/compat.h>
+#if (defined(__linux__) || defined(__FreeBSD__) || defined(__darwin__))
+
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#ifndef LIBC_CONNECT_SIG
+#define LIBC_CONNECT_SIG \
+ int _sockfd, const struct sockaddr *_addr, socklen_t _addrlen
+#endif /* LIBC_CONNECT_SIG */
+
+#else
+#error "OS not supported."
+#endif /* __linux__ , __FreeBSD__, __darwin__ */
+
#endif /* TORSOCKS_H */
More information about the tor-commits
mailing list