From 3ade181f69fe2cf76420bbbca5d70d79c32662db Mon Sep 17 00:00:00 2001
From: whh8b <whh8b@git.zephyr-software.com>
Date: Thu, 3 Sep 2015 17:33:37 +0000
Subject: [PATCH] Initial checkin of dynamic hook callbacks.

---
 .gitattributes                       |   2 +
 hookdynamic/Makefile.in              |  28 +++++++
 hookdynamic/hook_dynamic_callbacks.c | 113 +++++++++++++++++++++++++++
 3 files changed, 143 insertions(+)
 create mode 100644 hookdynamic/Makefile.in
 create mode 100644 hookdynamic/hook_dynamic_callbacks.c

diff --git a/.gitattributes b/.gitattributes
index a63e585..65ac26a 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -10,6 +10,8 @@ canaries/debug_canary_callbacks.c -text
 /configure_for_rss -text
 example/Makefile.in -text
 example/callback.c -text
+hookdynamic/Makefile.in -text
+hookdynamic/hook_dynamic_callbacks.c -text
 inferfn/Makefile.in -text
 inferfn/infer.c -text
 inferfn/infer.h -text
diff --git a/hookdynamic/Makefile.in b/hookdynamic/Makefile.in
new file mode 100644
index 0000000..0bb4c53
--- /dev/null
+++ b/hookdynamic/Makefile.in
@@ -0,0 +1,28 @@
+
+
+CC=@CC@
+CFLAGS=@CFLAGS@
+LIB=@LIB@
+AR=@AR@
+AS=@AS@
+ASFLAGS=@ASFLAGS@
+
+OBJS=hook_dynamic_callbacks.o
+
+
+.SUFFIXES: .o .s .c
+
+.c.o:
+	$(CC) $(CFLAGS) $(INCLUDE) -D__$(ARCH) -c $<
+
+.s.o:
+	$(AS) $(ASFLAGS) $<
+
+
+all: 	$(OBJS)
+	$(AR) -r $(LIB) $(OBJS)
+
+clean:
+	rm *.o
+	
+
diff --git a/hookdynamic/hook_dynamic_callbacks.c b/hookdynamic/hook_dynamic_callbacks.c
new file mode 100644
index 0000000..16044a7
--- /dev/null
+++ b/hookdynamic/hook_dynamic_callbacks.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2014 - Zephyr Software LLC
+ *
+ * This file may be used and modified for non-commercial purposes as long as
+ * all copyright, permission, and nonwarranty notices are preserved.
+ * Redistribution is prohibited without prior written consent from Zephyr
+ * Software.
+ *
+ * Please contact the authors for restrictions applying to commercial use.
+ *
+ * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author: Zephyr Software
+ * e-mail: jwd@zephyr-software.com
+ * URL   : http://www.zephyr-software.com/
+ *
+ */
+
+#ifdef DEBUG
+#include "malloc.h"
+#include "itox.h"
+#include "strlen.h"
+#include "null.h"
+
+#define DEBUG_ADD
+#else
+
+typedef unsigned uintptr_t;
+#define NULL 0L
+
+#endif
+
+
+#ifdef DEBUG
+	#define print_str_debug print_str
+	#define print_int_debug print_int
+#else
+	#define print_str_debug(a) 
+	#define print_int_debug(a) 
+	#define assert(a)
+#endif
+
+
+#ifdef DEBUG
+#define write_fd 2
+void print_str(char *s)
+{
+	write(write_fd,s,strlen(s));
+}
+void print_int(int x)
+{
+	char buf[100];
+	itox(x,buf);
+	write(write_fd,buf,strlen(buf));
+}
+#endif
+
+unsigned long long read_random64(void) {
+	unsigned long long random_value = -1;
+	int fd = -1;
+	fd = open("/dev/urandom", 0x00);
+	if (fd != -1) {
+		read(fd, (void*)&random_value, sizeof(unsigned long long));
+		close(fd);
+	}
+	return random_value;
+}
+
+unsigned long read_random32(void) {
+	unsigned long random_value = -1;
+	int fd = -1;
+	fd = open("/dev/urandom", 0x00);
+	if (fd != -1) {
+		read(fd, (void*)&random_value, sizeof(unsigned long));
+		close(fd);
+	}
+	return random_value;
+}
+void set_canary64(unsigned long long new_canary) {
+	asm volatile ("mov %0, %%fs:0x28\n"
+	    : 
+	    : "r" (new_canary)
+	    :);
+}
+
+void set_canary32(unsigned long new_canary) {
+	asm volatile ("mov %0, %%fs:0x28\n"
+	    : 
+	    : "r" (new_canary)
+	    :);
+}
+
+/*
+ */
+#define FORK 57
+void zipr_hook_dynamic_callback(unsigned int id, unsigned int rax)
+{
+#ifdef DEBUG
+	print_str_debug("In zipr_hook_dynamic_callback, id=");
+	print_int_debug(id);
+	print_str_debug(", rax=");
+	print_int_debug(rax);
+	print_str_debug("\n");
+#endif
+	if (id == FORK && rax!=0) {
+		/*
+		 * Child!
+		 */
+		set_canary32(read_random32());
+	}
+}
-- 
GitLab