2e72422
From e69b535f92eafb599329bf725d9b4c6fd5d7fded Mon Sep 17 00:00:00 2001
2e72422
From: Paul Jakma <paul@jakma.org>
2e72422
Date: Sat, 6 Jan 2018 19:52:10 +0000
2e72422
Subject: [PATCH] bgpd/security: Fix double free of unknown attribute
2e72422
2e72422
Security issue: Quagga-2018-1114
2e72422
See: https://www.quagga.net/security/Quagga-2018-1114.txt
2e72422
2e72422
It is possible for bgpd to double-free an unknown attribute. This can happen
2e72422
via bgp_update_receive receiving an UPDATE with an invalid unknown attribute.
2e72422
bgp_update_receive then will call bgp_attr_unintern_sub and bgp_attr_flush,
2e72422
and the latter may try free an already freed unknown attr.
2e72422
2e72422
* bgpd/bgp_attr.c: (transit_unintern) Take a pointer to the caller's storage
2e72422
  for the (struct transit *), so that transit_unintern can NULL out the
2e72422
  caller's reference if the (struct transit) is freed.
2e72422
  (cluster_unintern) By inspection, appears to have a similar issue.
2e72422
  (bgp_attr_unintern_sub) adjust for above.
2e72422
---
2e72422
 bgpd/bgp_attr.c | 33 +++++++++++++++++++--------------
2e72422
 bgpd/bgp_attr.h |  4 ++--
2e72422
 2 files changed, 21 insertions(+), 16 deletions(-)
2e72422
2e72422
diff --git a/bgpd/bgp_attr.c b/bgpd/bgp_attr.c
2e72422
index 9564637e..0c2806b5 100644
2e72422
--- a/bgpd/bgp_attr.c
2e72422
+++ b/bgpd/bgp_attr.c
2e72422
@@ -199,15 +199,17 @@ cluster_intern (struct cluster_list *cluster)
2e72422
 }
2e72422
 
2e72422
 void
2e72422
-cluster_unintern (struct cluster_list *cluster)
2e72422
+cluster_unintern (struct cluster_list **cluster)
2e72422
 {
2e72422
-  if (cluster->refcnt)
2e72422
-    cluster->refcnt--;
2e72422
+  struct cluster_list *c = *cluster;
2e72422
+  if (c->refcnt)
2e72422
+    c->refcnt--;
2e72422
 
2e72422
-  if (cluster->refcnt == 0)
2e72422
+  if (c->refcnt == 0)
2e72422
     {
2e72422
-      hash_release (cluster_hash, cluster);
2e72422
-      cluster_free (cluster);
2e72422
+      hash_release (cluster_hash, c);
2e72422
+      cluster_free (c);
2e72422
+      *cluster = NULL;
2e72422
     }
2e72422
 }
2e72422
 
2e72422
@@ -357,15 +359,18 @@ transit_intern (struct transit *transit)
2e72422
 }
2e72422
 
2e72422
 void
2e72422
-transit_unintern (struct transit *transit)
2e72422
+transit_unintern (struct transit **transit)
2e72422
 {
2e72422
-  if (transit->refcnt)
2e72422
-    transit->refcnt--;
2e72422
+  struct transit *t = *transit;
2e72422
+  
2e72422
+  if (t->refcnt)
2e72422
+    t->refcnt--;
2e72422
 
2e72422
-  if (transit->refcnt == 0)
2e72422
+  if (t->refcnt == 0)
2e72422
     {
2e72422
-      hash_release (transit_hash, transit);
2e72422
-      transit_free (transit);
2e72422
+      hash_release (transit_hash, t);
2e72422
+      transit_free (t);
2e72422
+      *transit = NULL;
2e72422
     }
2e72422
 }
2e72422
 
2e72422
@@ -820,11 +825,11 @@ bgp_attr_unintern_sub (struct attr *attr)
2e72422
       UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_LARGE_COMMUNITIES));
2e72422
       
2e72422
       if (attr->extra->cluster)
2e72422
-        cluster_unintern (attr->extra->cluster);
2e72422
+        cluster_unintern (&attr->extra->cluster);
2e72422
       UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST));
2e72422
       
2e72422
       if (attr->extra->transit)
2e72422
-        transit_unintern (attr->extra->transit);
2e72422
+        transit_unintern (&attr->extra->transit);
2e72422
     }
2e72422
 }
2e72422
 
2e72422
diff --git a/bgpd/bgp_attr.h b/bgpd/bgp_attr.h
2e72422
index 9ff074b2..052acc7d 100644
2e72422
--- a/bgpd/bgp_attr.h
2e72422
+++ b/bgpd/bgp_attr.h
2e72422
@@ -187,10 +187,10 @@ extern unsigned long int attr_unknown_count (void);
2e72422
 
2e72422
 /* Cluster list prototypes. */
2e72422
 extern int cluster_loop_check (struct cluster_list *, struct in_addr);
2e72422
-extern void cluster_unintern (struct cluster_list *);
2e72422
+extern void cluster_unintern (struct cluster_list **);
2e72422
 
2e72422
 /* Transit attribute prototypes. */
2e72422
-void transit_unintern (struct transit *);
2e72422
+void transit_unintern (struct transit **);
2e72422
 
2e72422
 /* Below exported for unit-test purposes only */
2e72422
 struct bgp_attr_parser_args {
2e72422
-- 
2e72422
2.14.3
2e72422