e3a5e1
From 471cd540e755d2a5e92b4e8cd2ab2027beaba449 Mon Sep 17 00:00:00 2001
e3a5e1
From: Simon Kelley <simon@thekelleys.org.uk>
e3a5e1
Date: Thu, 31 Mar 2022 21:35:20 +0100
e3a5e1
Subject: [PATCH] Fix write-after-free error in DHCPv6 code. CVE-2022-0934
e3a5e1
 refers.
61c145
61c145
---
e3a5e1
 src/rfc3315.c | 48 +++++++++++++++++++++++++++---------------------
e3a5e1
 1 file changed, 27 insertions(+), 21 deletions(-)
61c145
e3a5e1
diff --git a/src/rfc3315.c b/src/rfc3315.c
e3a5e1
index 554b1fe..bc77fbf 100644
e3a5e1
--- a/src/rfc3315.c
e3a5e1
+++ b/src/rfc3315.c
e3a5e1
@@ -36,9 +36,9 @@ struct state {
61c145
 #endif
e3a5e1
 };
61c145
 
e3a5e1
-static int dhcp6_maybe_relay(struct state *state, void *inbuff, size_t sz, 
e3a5e1
+static int dhcp6_maybe_relay(struct state *state, unsigned char *inbuff, size_t sz,
e3a5e1
 			     struct in6_addr *client_addr, int is_unicast, time_t now);
e3a5e1
-static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_t sz, int is_unicast, time_t now);
e3a5e1
+static int dhcp6_no_relay(struct state *state, int msg_type, unsigned char *inbuff, size_t sz, int is_unicast, time_t now);
e3a5e1
 static void log6_opts(int nest, unsigned int xid, void *start_opts, void *end_opts);
e3a5e1
 static void log6_packet(struct state *state, char *type, struct in6_addr *addr, char *string);
e3a5e1
 static void log6_quiet(struct state *state, char *type, struct in6_addr *addr, char *string);
e3a5e1
@@ -110,12 +110,12 @@ unsigned short dhcp6_reply(struct dhcp_context *context, int interface, char *if
61c145
 }
61c145
 
e3a5e1
 /* This cost me blood to write, it will probably cost you blood to understand - srk. */
e3a5e1
-static int dhcp6_maybe_relay(struct state *state, void *inbuff, size_t sz, 
e3a5e1
+static int dhcp6_maybe_relay(struct state *state, unsigned char *inbuff, size_t sz, 
e3a5e1
 			     struct in6_addr *client_addr, int is_unicast, time_t now)
e3a5e1
 {
61c145
   void *end = inbuff + sz;
61c145
   void *opts = inbuff + 34;
e3a5e1
-  int msg_type = *((unsigned char *)inbuff);
e3a5e1
+  int msg_type = *inbuff;
e3a5e1
   unsigned char *outmsgtypep;
61c145
   void *opt;
61c145
   struct dhcp_vendor *vendor;
e3a5e1
@@ -241,15 +241,15 @@ static int dhcp6_maybe_relay(struct state *state, void *inbuff, size_t sz,
e3a5e1
   return 1;
e3a5e1
 }
61c145
 
e3a5e1
-static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_t sz, int is_unicast, time_t now)
e3a5e1
+static int dhcp6_no_relay(struct state *state, int msg_type, unsigned char *inbuff, size_t sz, int is_unicast, time_t now)
e3a5e1
 {
e3a5e1
   void *opt;
e3a5e1
-  int i, o, o1, start_opts;
e3a5e1
+  int i, o, o1, start_opts, start_msg;
e3a5e1
   struct dhcp_opt *opt_cfg;
61c145
   struct dhcp_netid *tagif;
61c145
   struct dhcp_config *config = NULL;
61c145
   struct dhcp_netid known_id, iface_id, v6_id;
61c145
-  unsigned char *outmsgtypep;
e3a5e1
+  unsigned char outmsgtype;
61c145
   struct dhcp_vendor *vendor;
61c145
   struct dhcp_context *context_tmp;
61c145
   struct dhcp_mac *mac_opt;
e3a5e1
@@ -285,12 +285,13 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
e3a5e1
   v6_id.next = state->tags;
61c145
   state->tags = &v6_id;
61c145
 
e3a5e1
-  /* copy over transaction-id, and save pointer to message type */
61c145
-  if (!(outmsgtypep = put_opt6(inbuff, 4)))
e3a5e1
+  start_msg = save_counter(-1);
e3a5e1
+  /* copy over transaction-id */
e3a5e1
+  if (!put_opt6(inbuff, 4))
61c145
     return 0;
61c145
   start_opts = save_counter(-1);
61c145
-  state->xid = outmsgtypep[3] | outmsgtypep[2] << 8 | outmsgtypep[1] << 16;
e3a5e1
-   
e3a5e1
+  state->xid = inbuff[3] | inbuff[2] << 8 | inbuff[1] << 16;
e3a5e1
+    
61c145
   /* We're going to be linking tags from all context we use. 
61c145
      mark them as unused so we don't link one twice and break the list */
e3a5e1
   for (context_tmp = state->context; context_tmp; context_tmp = context_tmp->current)
e3a5e1
@@ -336,7 +337,7 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
61c145
       (msg_type == DHCP6REQUEST || msg_type == DHCP6RENEW || msg_type == DHCP6RELEASE || msg_type == DHCP6DECLINE))
61c145
     
61c145
     {  
61c145
-      *outmsgtypep = DHCP6REPLY;
e3a5e1
+      outmsgtype = DHCP6REPLY;
61c145
       o1 = new_opt6(OPTION6_STATUS_CODE);
61c145
       put_opt6_short(DHCP6USEMULTI);
61c145
       put_opt6_string("Use multicast");
e3a5e1
@@ -600,11 +601,11 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
61c145
 	struct dhcp_netid *solicit_tags;
61c145
 	struct dhcp_context *c;
61c145
 	
61c145
-	*outmsgtypep = DHCP6ADVERTISE;
e3a5e1
+	outmsgtype = DHCP6ADVERTISE;
61c145
 	
61c145
 	if (opt6_find(state->packet_options, state->end, OPTION6_RAPID_COMMIT, 0))
61c145
 	  {
61c145
-	    *outmsgtypep = DHCP6REPLY;
e3a5e1
+	    outmsgtype = DHCP6REPLY;
61c145
 	    state->lease_allocate = 1;
61c145
 	    o = new_opt6(OPTION6_RAPID_COMMIT);
61c145
 	    end_opt6(o);
e3a5e1
@@ -876,7 +877,7 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
61c145
 	int start = save_counter(-1);
61c145
 
61c145
 	/* set reply message type */
61c145
-	*outmsgtypep = DHCP6REPLY;
e3a5e1
+	outmsgtype = DHCP6REPLY;
61c145
 	state->lease_allocate = 1;
61c145
 
61c145
 	log6_quiet(state, "DHCPREQUEST", NULL, ignore ? _("ignored") : NULL);
e3a5e1
@@ -992,7 +993,7 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
61c145
     case DHCP6RENEW:
61c145
       {
61c145
 	/* set reply message type */
61c145
-	*outmsgtypep = DHCP6REPLY;
e3a5e1
+	outmsgtype = DHCP6REPLY;
61c145
 	
61c145
 	log6_quiet(state, "DHCPRENEW", NULL, NULL);
61c145
 
e3a5e1
@@ -1104,7 +1105,7 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
61c145
 	int good_addr = 0;
61c145
 
61c145
 	/* set reply message type */
61c145
-	*outmsgtypep = DHCP6REPLY;
e3a5e1
+	outmsgtype = DHCP6REPLY;
61c145
 	
61c145
 	log6_quiet(state, "DHCPCONFIRM", NULL, NULL);
61c145
 	
e3a5e1
@@ -1168,7 +1169,7 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
61c145
 	log6_quiet(state, "DHCPINFORMATION-REQUEST", NULL, ignore ? _("ignored") : state->hostname);
61c145
 	if (ignore)
61c145
 	  return 0;
61c145
-	*outmsgtypep = DHCP6REPLY;
e3a5e1
+	outmsgtype = DHCP6REPLY;
61c145
 	tagif = add_options(state, 1);
61c145
 	break;
61c145
       }
e3a5e1
@@ -1177,7 +1178,7 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
61c145
     case DHCP6RELEASE:
61c145
       {
61c145
 	/* set reply message type */
61c145
-	*outmsgtypep = DHCP6REPLY;
e3a5e1
+	outmsgtype = DHCP6REPLY;
61c145
 
61c145
 	log6_quiet(state, "DHCPRELEASE", NULL, NULL);
61c145
 
e3a5e1
@@ -1242,7 +1243,7 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
61c145
     case DHCP6DECLINE:
61c145
       {
61c145
 	/* set reply message type */
61c145
-	*outmsgtypep = DHCP6REPLY;
e3a5e1
+	outmsgtype = DHCP6REPLY;
61c145
 	
61c145
 	log6_quiet(state, "DHCPDECLINE", NULL, NULL);
61c145
 
e3a5e1
@@ -1321,7 +1322,12 @@ static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_
e3a5e1
       }
e3a5e1
 
e3a5e1
     }
e3a5e1
-  
e3a5e1
+
e3a5e1
+  /* Fill in the message type. Note that we store the offset,
e3a5e1
+     not a direct pointer, since the packet memory may have been 
e3a5e1
+     reallocated. */
e3a5e1
+  ((unsigned char *)(daemon->outpacket.iov_base))[start_msg] = outmsgtype;
e3a5e1
+
e3a5e1
   log_tags(tagif, state->xid);
e3a5e1
   log6_opts(0, state->xid, daemon->outpacket.iov_base + start_opts, daemon->outpacket.iov_base + save_counter(-1));
e3a5e1
   
61c145
-- 
e3a5e1
2.39.1
61c145