aboutsummaryrefslogtreecommitdiffstats
path: root/main/quagga/0003-bgpd-fix-double-free-after-extcommunity-set-BZ-799.patch
blob: fbd226822f8b81b8805abd188a57b8efa83955f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
From 27bf90a14670283a899b96c56dd23f8413e0973e Mon Sep 17 00:00:00 2001
From: David Lamparter <equinox@opensourcerouting.org>
Date: Wed, 4 Jun 2014 00:59:01 +0200
Subject: [PATCH] bgpd: fix double free after extcommunity set (BZ#799)

The route-map extcommunity set code was incorrectly assuming that it
owns the intern'd struct ecommunity reference.  In reality, the intern'd
reference belongs to bgp_update_receive() and we're not supposed to
touch it in the route-map code.

Instead, like all the other set commands, we use a on-heap but
non-intern'd ecommunity to set the new value.  This is then either
intern'd in bgp_update_main/_rsclient() through bgp_attr_intern(), or
free'd through bgp_attr_flush().

This fixes Bugzilla #799, which is that bgpd otherwise crashes with a
double free.  The ecommunity got unintern'd first in the route-map set
command, then in bgp_update_receive().

Debugged-by: Milan Kocian <milon@wq.cz>
Reported-by: Florian S <florian@herrenlohe.de>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
---
 bgpd/bgp_routemap.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c
index eb5e80f..36d177d 100644
--- a/bgpd/bgp_routemap.c
+++ b/bgpd/bgp_routemap.c
@@ -1552,14 +1552,19 @@ route_set_ecommunity (void *rule, struct prefix *prefix,
       old_ecom = (bgp_attr_extra_get (bgp_info->attr))->ecommunity;
 
       if (old_ecom)
-	new_ecom = ecommunity_merge (ecommunity_dup (old_ecom), ecom);
+	{
+	  new_ecom = ecommunity_merge (ecommunity_dup (old_ecom), ecom);
+
+	  /* old_ecom->refcnt = 1 => owned elsewhere, e.g. bgp_update_receive()
+	   *         ->refcnt = 0 => set by a previous route-map statement */
+	  if (!old_ecom->refcnt)
+	    ecommunity_free (&old_ecom);
+	}
       else
 	new_ecom = ecommunity_dup (ecom);
 
-      bgp_info->attr->extra->ecommunity = ecommunity_intern (new_ecom);
-
-      if (old_ecom)
-	ecommunity_unintern (&old_ecom);
+      /* will be intern()'d or attr_flush()'d by bgp_update_main() */
+      bgp_info->attr->extra->ecommunity = new_ecom;
 
       bgp_info->attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES);
     }
-- 
2.0.1