[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [torsocks/master] Improve libc symbol interface and add destructor
commit c7d9b5c8f53f42b424ad0d3ee1e5256932a66357
Author: David Goulet <dgoulet@xxxxxxxxx>
Date: Sun Jun 9 11:35:27 2013 -0400
Improve libc symbol interface and add destructor
This commit adds a *_DECL macro which builds the libc call declaration
using the returned type and signature defined on a per OS basis.
Signed-off-by: David Goulet <dgoulet@xxxxxxxxx>
---
src/lib/torsocks.c | 54 +++++++++++++++++++++++++++++++++-------------------
src/lib/torsocks.h | 26 ++++++++++++++++++++++---
2 files changed, 57 insertions(+), 23 deletions(-)
diff --git a/src/lib/torsocks.c b/src/lib/torsocks.c
index f5029e7..b927201 100644
--- a/src/lib/torsocks.c
+++ b/src/lib/torsocks.c
@@ -33,11 +33,21 @@
static int is_suid;
/*
+ * Cleanup and exit with the given status. Note that the lib destructor will be
+ * called after this call.
+ */
+static void clean_exit(int status)
+{
+ exit(status);
+}
+
+/*
* 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)
+static void *find_libc_symbol(const char *symbol,
+ enum tsocks_sym_action action)
{
void *fct_ptr = NULL;
@@ -46,10 +56,12 @@ static void *find_libc_symbol(const char *symbol)
fct_ptr = dlsym(RTLD_NEXT, symbol);
if (!fct_ptr) {
ERR("Unable to find %s", symbol);
- goto end;
+ if (action == TSOCKS_SYM_EXIT_NOT_FOUND) {
+ ERR("This is critical for torsocks. Exiting");
+ clean_exit(EXIT_FAILURE);
+ }
}
-end:
return fct_ptr;
}
@@ -101,7 +113,7 @@ static void init_logging(void)
* Lib constructor. Initialize torsocks here before the main execution of the
* binary we are preloading.
*/
-static void __attribute__((constructor)) init()
+static void __attribute__((constructor)) tsocks_init(void)
{
/* UID and effective UID MUST be the same or else we are SUID. */
is_suid = (getuid() != geteuid());
@@ -110,28 +122,30 @@ static void __attribute__((constructor)) init()
}
/*
- * Cleanup and exit with the given status.
+ * Lib destructor.
*/
-static void clean_exit(int status)
+static void __attribute__((destructor)) tsocks_exit(void)
{
- exit(status);
+ /* Clean up logging. */
+ log_destroy();
}
/*
- * Libc hijacked symbol connect(2).
+ * Torsocks call for connect(2).
*/
-int connect(LIBC_CONNECT_SIG)
+LIBC_CONNECT_RET_TYPE tsocks_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);
- }
- }
+ DBG("Connect catched on fd %d", __sockfd);
+ return tsocks_libc_connect(LIBC_CONNECT_ARGS);
+}
- return libc_connect(_sockfd, _addr, _addrlen);
+/*
+ * Libc hijacked symbol connect(2).
+ */
+LIBC_CONNECT_DECL
+{
+ /* Find symbol if not already set. Exit if not found. */
+ tsocks_libc_connect = find_libc_symbol(LIBC_CONNECT_NAME_STR,
+ TSOCKS_SYM_EXIT_NOT_FOUND);
+ return tsocks_connect(LIBC_CONNECT_ARGS);
}
diff --git a/src/lib/torsocks.h b/src/lib/torsocks.h
index 122d711..3d0b1b6 100644
--- a/src/lib/torsocks.h
+++ b/src/lib/torsocks.h
@@ -22,18 +22,38 @@
#include <common/compat.h>
+#define TSOCKS_LIBC_DECL(name, type, sig) \
+ type (*tsocks_libc_##name)(sig);
+
#if (defined(__linux__) || defined(__FreeBSD__) || defined(__darwin__))
#include <sys/types.h>
#include <sys/socket.h>
-#ifndef LIBC_CONNECT_SIG
+#define LIBC_CONNECT_NAME connect
+#define LIBC_CONNECT_NAME_STR XSTR(LIBC_CONNECT_NAME)
+#define LIBC_CONNECT_RET_TYPE int
#define LIBC_CONNECT_SIG \
- int _sockfd, const struct sockaddr *_addr, socklen_t _addrlen
-#endif /* LIBC_CONNECT_SIG */
+ int __sockfd, const struct sockaddr *__addr, socklen_t __addrlen
+#define LIBC_CONNECT_ARGS \
+ __sockfd, __addr, __addrlen
#else
#error "OS not supported."
#endif /* __linux__ , __FreeBSD__, __darwin__ */
+/*
+ * The following defines are libc function declarations using the macros
+ * defined above on a per OS basis.
+ */
+
+/* connect(2) */
+TSOCKS_LIBC_DECL(connect, LIBC_CONNECT_RET_TYPE, LIBC_CONNECT_SIG)
+#define LIBC_CONNECT_DECL \
+ LIBC_CONNECT_RET_TYPE LIBC_CONNECT_NAME(LIBC_CONNECT_SIG)
+
+enum tsocks_sym_action {
+ TSOCKS_SYM_EXIT_NOT_FOUND = 1,
+};
+
#endif /* TORSOCKS_H */
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits