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