The unified diff between revisions [f14eeb09..] and [025095ab..] is displayed below. It can also be downloaded as a raw diff.

#
#
# add_file "fritz_client.cc"
#  content [05334b12c4cc7901d1565ef733bb7b7fd0e45b5d]
#
# add_file "fritz_server.cc"
#  content [d54d439d19fb718be381dd9fd92d7ca88aa07734]
#
# add_file "fritz_this.cc"
#  content [5899cbc88d4f237b849058bb09df347f5c0ccb72]
#
# patch "fritz.cc"
#  from [2d49a74e4937692d814a6a833af55df52f1af0f9]
#    to [fff88b4efeee29f139a96fc50eedb4b7c369a0ad]
#
# patch "fritz.hh"
#  from [0a09a9981868e04ed9e93bab0af8a66c79f87559]
#    to [52c3265868b60bb1a0bc6e75707ceee8c9389805]
#
============================================================
--- fritz_client.cc	05334b12c4cc7901d1565ef733bb7b7fd0e45b5d
+++ fritz_client.cc	05334b12c4cc7901d1565ef733bb7b7fd0e45b5d
@@ -0,0 +1,49 @@
+
+/* fritz.it
+ * A framework for intercepting application input, altering it,
+ * and checking for abormal changes in application behaviour.
+ *
+ * Copyright (C) 2007 Grahame Bowland.
+ * All rights reserved.
+ */
+
+#include <sys/errno.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <semaphore.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <iostream>
+#include <exception>
+
+#include "fritz.hh"
+
+static FritzClient client;
+
+ssize_t
+read (int d, void *buf, size_t nbytes)
+{
+    static int depth;
+    double (*cosine)(double);
+    static ssize_t (*real_read)(int, void *, size_t);
+    int rlen;
+
+    depth++;
+    if (depth != 1) {
+        throw fritz_exception ("Recursion in override detected.");
+    }
+    if (!real_read) {
+        real_read = (ssize_t (*)(int, void*, size_t))client.grab_symbol_from ("FRITZ_LIBC", "read");
+    }
+    rlen = (real_read) (d, buf, nbytes);
+    client.copy_data (rlen, buf);
+    client.pass_to_server ();
+    rlen = client.copy_back (nbytes, buf);
+    depth--;
+    return rlen;
+}
+
============================================================
--- fritz_server.cc	d54d439d19fb718be381dd9fd92d7ca88aa07734
+++ fritz_server.cc	d54d439d19fb718be381dd9fd92d7ca88aa07734
@@ -0,0 +1,10 @@
+
+#include <iostream>
+#include "fritz.hh"
+
+int
+main (int argc, char *argv[])
+{
+    FritzServer f;
+    f.run ();
+}
============================================================
--- fritz_this.cc	5899cbc88d4f237b849058bb09df347f5c0ccb72
+++ fritz_this.cc	5899cbc88d4f237b849058bb09df347f5c0ccb72
@@ -0,0 +1,23 @@
+
+#include <iostream>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int
+main (int argc, char *argv[])
+{
+    char buf[256];
+    int fd = open ("/etc/passwd", O_RDONLY);
+    for (;;) {
+        size_t nbytes = read (fd, buf, sizeof(buf) - 1);
+        if (nbytes <= 0) {
+            break;
+        } else {
+            buf[nbytes] = '\0';
+            std::cout << buf;
+        }
+    }
+}
============================================================
--- fritz.cc	2d49a74e4937692d814a6a833af55df52f1af0f9
+++ fritz.cc	fff88b4efeee29f139a96fc50eedb4b7c369a0ad
@@ -60,6 +60,7 @@ Fritzer::map_the_shm (void)
         throw fritz_exception("Unable to map shared memory.");
     }
     fritz_params = (struct fritz_params *)shm_mem;
+    fritz_contents = (unsigned char *)shm_mem + sizeof(struct fritz_params);
 }

 void
@@ -170,7 +171,7 @@ FritzServer::rewrite (void)
 FritzServer::rewrite (void)
 {
     for (size_t i=0;i<fritz_params->contents_size;i++) {
-        ((char *)&(fritz_params->contents))[i]++;
+        fritz_contents[i]++;
     }
 //    std::cerr << "rewrote " << fritz_params->contents_size << " bytes." << std::endl;
 }
@@ -268,12 +269,12 @@ FritzClient::copy_data (size_t nbytes, v
 {
     update_params ();
     int mbytes = fritz_params->buffer_size - \
-    			((char *)fritz_params - (char *)&(fritz_params->contents));
+    			((unsigned char *)fritz_params - fritz_contents);
     if (nbytes > mbytes) {
         throw fritz_exception ("buffer overrun; can't copy this many bytes!");
     }
     fritz_params->contents_size = nbytes;
-    memcpy (&(fritz_params->contents), from, nbytes);
+    memcpy (fritz_contents, from, nbytes);
 }

 size_t
@@ -282,7 +283,7 @@ FritzClient::copy_back (size_t maxbytes,
     if (fritz_params->contents_size > maxbytes)  {
         throw fritz_exception ("buffer overrun; can't copy this many bytes back!");
     }
-    memcpy (buf, &(fritz_params->contents), fritz_params->contents_size);
+    memcpy (buf, fritz_contents, fritz_params->contents_size);
     return fritz_params->contents_size;
 }

============================================================
--- fritz.hh	0a09a9981868e04ed9e93bab0af8a66c79f87559
+++ fritz.hh	52c3265868b60bb1a0bc6e75707ceee8c9389805
@@ -45,7 +45,6 @@ struct fritz_params {
     size_t              message_id;       /* monotonically increasing message ID */
     int                 fd;               /* file descriptor from which data was gathered */
     size_t              contents_size;    /* size of contents */
-    void                *contents;        /* contents */
 };

 class Fritzer {
@@ -62,6 +61,7 @@ protected:
     int shm_fd;
     void *shm_mem;
     struct fritz_params *fritz_params;
+    unsigned char *fritz_contents;

     void initialise_from_environ (void);
     sem_t *open_semaphore (const char *);