Blame SOURCES/dnsmasq-2.87-CVE-2022-0934.patch

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