[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Implement "Dont-Care" from addresses to MapAddress control ...



Update of /home/or/cvsroot/tor/src/or
In directory moria.mit.edu:/tmp/cvs-serv25365/src/or

Modified Files:
	connection_edge.c control.c or.h 
Log Message:
Implement "Dont-Care" from addresses to MapAddress control message.  For safety, refuse to launch connections to unmapped addresses in the dont-care range.

Index: connection_edge.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/connection_edge.c,v
retrieving revision 1.288
retrieving revision 1.289
diff -u -d -r1.288 -r1.289
--- connection_edge.c	2 Mar 2005 03:13:05 -0000	1.288
+++ connection_edge.c	2 Mar 2005 19:26:46 -0000	1.289
@@ -534,6 +534,77 @@
                       time(NULL) + MAX_DNS_ENTRY_AGE);
 }
 
+/* Currently, we hand out 127.192.0.1 through 127.254.254.254.
+ * These addresses should map to localhost, so even if the
+ * application accidentally tried to connect to them directly (not
+ * via Tor), it wouldn't get too far astray.
+ *
+ * Eventually, we should probably make this configurable.
+ */
+#define MIN_UNUSED_IPV4 0x7fc00001u
+#define MAX_UNUSED_IPV4 0x7ffefefeu
+
+/**
+ * Return true iff <b>addr</b> is likely to have been returned by
+ * client_dns_get_unmapped_address.
+ **/
+static int
+address_is_in_unmapped_range(const char *addr)
+{
+  struct in_addr in;
+  tor_assert(addr);
+  if (!strcasecmpend(addr, ".virtual")) {
+    return 1;
+  } else if (tor_inet_aton(addr, &in)) {
+    uint32_t a = ntohl(in.s_addr);
+    if (a >= MIN_UNUSED_IPV4 && a <= MAX_UNUSED_IPV4)
+      return 1;
+  }
+  return 0;
+}
+
+/** Return a newly allocated string holding an address of <b>type</b>
+ * (one of RESOLVED_TYPE_{IPV4|HOSTNAME}) that has not yet been mapped,
+ * and that is very unlikely to be the address of any real host.
+ */
+char *
+client_dns_get_unmapped_address(int type)
+{
+  char buf[64];
+  static uint32_t next_ipv4 = MIN_UNUSED_IPV4;
+  struct in_addr in;
+
+  if (type == RESOLVED_TYPE_HOSTNAME) {
+    char rand[16];
+    do {
+      crypto_rand(rand, sizeof(rand));
+      base32_encode(buf,sizeof(buf),rand,sizeof(rand));
+      strlcat(buf, ".virtual", sizeof(buf));
+    } while (strmap_get(addressmap, buf));
+    return tor_strdup(buf);
+  } else if (type == RESOLVED_TYPE_IPV4) {
+    while (1) {
+      /* Don't hand out any .0 or .255 address. */
+      while ((next_ipv4 & 0xff) == 0 ||
+             (next_ipv4 & 0xff) == 0xff)
+        ++next_ipv4;
+      in.s_addr = htonl(next_ipv4);
+      tor_inet_ntoa(&in, buf, sizeof(buf));
+      if (!strmap_get(addressmap, buf))
+        break;
+
+      ++next_ipv4;
+      if (next_ipv4 > MAX_UNUSED_IPV4)
+        next_ipv4 = MIN_UNUSED_IPV4;
+    }
+    return tor_strdup(buf);
+  } else {
+    log_fn(LOG_WARN, "Called with unsupported address type (%d)",
+           type);
+    return NULL;
+  }
+}
+
 /** Return 1 if <b>address</b> has funny characters in it like
  * colons. Return 0 if it's fine.
  */
@@ -593,6 +664,17 @@
   /* For address map controls, remap the address */
   addressmap_rewrite(socks->address, sizeof(socks->address));
 
+  if (address_is_in_unmapped_range(socks->address)) {
+    /* This address was probably handed out by client_dns_get_unmapped_address,
+     * but the mapping was discarded for some reason.  We *don't* want to send
+     * the address through tor; that's likely to fail, and may leak
+     * information.
+     */
+    log_fn(LOG_WARN,"Missing mapping for virtual address '%s'. Refusing.",
+           socks->address);
+    return -1;
+  }
+
   /* Parse the address provided by SOCKS.  Modify it in-place if it
    * specifies a hidden-service (.onion) or particular exit node (.exit).
    */

Index: control.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/control.c,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -d -r1.46 -r1.47
--- control.c	1 Mar 2005 01:15:00 -0000	1.46
+++ control.c	2 Mar 2005 19:26:46 -0000	1.47
@@ -471,6 +471,18 @@
         log_fn(LOG_WARN,"Skipping invalid argument '%s' in MapAddress msg",from);
       } else if (!is_plausible_address(to)) {
         log_fn(LOG_WARN,"Skipping invalid argument '%s' in AddressMap msg",to);
+      } else if (!strcmp(from, ".") || !strcmp(from, "0.0.0.0")) {
+        char *addr = client_dns_get_unmapped_address(
+               strcmp(from,".") ? RESOLVED_TYPE_HOSTNAME : RESOLVED_TYPE_IPV4);
+        if (!addr) {
+          log_fn(LOG_WARN,
+                 "Unable to allocate address for '%s' in AdressMap msg", line);
+        } else {
+          char *ans = tor_malloc(strlen(addr)+strlen(to)+2);
+          tor_snprintf(ans, "%s %s", addr, to);
+          addressmap_register(addr, tor_strdup(to), 0);
+          smartlist_add(reply, ans);
+        }
       } else {
         addressmap_register(from, tor_strdup(to), 0);
         smartlist_add(reply, tor_strdup(line));

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/or.h,v
retrieving revision 1.547
retrieving revision 1.548
diff -u -d -r1.547 -r1.548
--- or.h	1 Mar 2005 22:42:31 -0000	1.547
+++ or.h	2 Mar 2005 19:26:46 -0000	1.548
@@ -427,6 +427,7 @@
 #define END_STREAM_REASON_CONNRESET 12
 #define END_STREAM_REASON_TORPROTOCOL 13
 
+#define RESOLVED_TYPE_HOSTNAME 0
 #define RESOLVED_TYPE_IPV4 4
 #define RESOLVED_TYPE_IPV6 6
 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
@@ -1318,6 +1319,7 @@
 void addressmap_register(const char *address, char *new_address, time_t expires);
 int client_dns_incr_failures(const char *address);
 void client_dns_set_addressmap(const char *address, uint32_t val, const char *exitname);
+char *client_dns_get_unmapped_address(int type);
 
 void parse_socks_policy(void);
 void free_socks_policy(void);