Skip to content

Commit b242e73

Browse files
committed
bgpd: Prevent use after free of peer structure
When changing the peers sockunion structure the bgp->peer list was not being updated properly. Since the peer's su is being used for a sorted insert then the change of it requires that the value be pulled out of the bgp->peer list and then put back into as well. Additionally ensure that the hash is always released on peer deletion. Lead to this from this decode in a address sanitizer run. ================================================================= ==30778==ERROR: AddressSanitizer: heap-use-after-free on address 0x62a0000d8440 at pc 0x7f48c9c5c547 bp 0x7ffcba272cb0 sp 0x7ffcba272ca8 READ of size 2 at 0x62a0000d8440 thread T0 #0 0x7f48c9c5c546 in sockunion_same lib/sockunion.c:425 sonic-net#1 0x55cfefe3000f in peer_hash_same bgpd/bgpd.c:890 sonic-net#2 0x7f48c9bde039 in hash_release lib/hash.c:209 sonic-net#3 0x55cfefe3373f in bgp_peer_conf_if_to_su_update bgpd/bgpd.c:1541 sonic-net#4 0x55cfefd0be7a in bgp_stop bgpd/bgp_fsm.c:1631 sonic-net#5 0x55cfefe4028f in peer_delete bgpd/bgpd.c:2362 sonic-net#6 0x55cfefdd5e97 in no_neighbor_interface_config bgpd/bgp_vty.c:4267 sonic-net#7 0x7f48c9b9d160 in cmd_execute_command_real lib/command.c:949 sonic-net#8 0x7f48c9ba1112 in cmd_execute_command lib/command.c:1009 sonic-net#9 0x7f48c9ba1573 in cmd_execute lib/command.c:1162 sonic-net#10 0x7f48c9c87402 in vty_command lib/vty.c:526 sonic-net#11 0x7f48c9c87832 in vty_execute lib/vty.c:1291 sonic-net#12 0x7f48c9c8e741 in vtysh_read lib/vty.c:2130 sonic-net#13 0x7f48c9c7a66d in thread_call lib/thread.c:1585 sonic-net#14 0x7f48c9bf64e7 in frr_run lib/libfrr.c:1123 sonic-net#15 0x55cfefc75a15 in main bgpd/bgp_main.c:540 sonic-net#16 0x7f48c96b009a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2409a) sonic-net#17 0x55cfefc787f9 in _start (/usr/lib/frr/bgpd+0xe27f9) 0x62a0000d8440 is located 576 bytes inside of 23376-byte region [0x62a0000d8200,0x62a0000ddd50) freed by thread T0 here: #0 0x7f48c9eb9fb0 in __interceptor_free (/lib/x86_64-linux-gnu/libasan.so.5+0xe8fb0) sonic-net#1 0x55cfefe3fe42 in peer_free bgpd/bgpd.c:1113 sonic-net#2 0x55cfefe3fe42 in peer_unlock_with_caller bgpd/bgpd.c:1144 sonic-net#3 0x55cfefe4092e in peer_delete bgpd/bgpd.c:2457 sonic-net#4 0x55cfefdd5e97 in no_neighbor_interface_config bgpd/bgp_vty.c:4267 sonic-net#5 0x7f48c9b9d160 in cmd_execute_command_real lib/command.c:949 sonic-net#6 0x7f48c9ba1112 in cmd_execute_command lib/command.c:1009 sonic-net#7 0x7f48c9ba1573 in cmd_execute lib/command.c:1162 sonic-net#8 0x7f48c9c87402 in vty_command lib/vty.c:526 sonic-net#9 0x7f48c9c87832 in vty_execute lib/vty.c:1291 sonic-net#10 0x7f48c9c8e741 in vtysh_read lib/vty.c:2130 sonic-net#11 0x7f48c9c7a66d in thread_call lib/thread.c:1585 sonic-net#12 0x7f48c9bf64e7 in frr_run lib/libfrr.c:1123 sonic-net#13 0x55cfefc75a15 in main bgpd/bgp_main.c:540 sonic-net#14 0x7f48c96b009a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2409a) Signed-off-by: Donald Sharp <[email protected]>
1 parent b8579ee commit b242e73

File tree

1 file changed

+61
-9
lines changed

1 file changed

+61
-9
lines changed

bgpd/bgpd.c

+61-9
Original file line numberDiff line numberDiff line change
@@ -1613,15 +1613,18 @@ void bgp_peer_conf_if_to_su_update(struct peer *peer)
16131613
struct interface *ifp;
16141614
int prev_family;
16151615
int peer_addr_updated = 0;
1616+
struct listnode *node;
1617+
union sockunion old_su;
16161618

1619+
/*
1620+
* This function is only ever needed when FRR an interface
1621+
* based peering, so this simple test will tell us if
1622+
* we are in an interface based configuration or not
1623+
*/
16171624
if (!peer->conf_if)
16181625
return;
16191626

1620-
/*
1621-
* Our peer structure is stored in the bgp->peerhash
1622-
* release it before we modify anything.
1623-
*/
1624-
hash_release(peer->bgp->peerhash, peer);
1627+
old_su = peer->su;
16251628

16261629
prev_family = peer->su.sa.sa_family;
16271630
if ((ifp = if_lookup_by_name(peer->conf_if, peer->bgp->vrf_id))) {
@@ -1664,9 +1667,48 @@ void bgp_peer_conf_if_to_su_update(struct peer *peer)
16641667
}
16651668

16661669
/*
1667-
* Since our su changed we need to del/add peer to the peerhash
1670+
* If they are the same, nothing to do here, move along
16681671
*/
1669-
(void)hash_get(peer->bgp->peerhash, peer, hash_alloc_intern);
1672+
if (!sockunion_same(&old_su, &peer->su)) {
1673+
union sockunion new_su = peer->su;
1674+
struct bgp *bgp = peer->bgp;
1675+
1676+
/*
1677+
* Our peer structure is stored in the bgp->peerhash
1678+
* release it before we modify anything in both the
1679+
* hash and the list. But *only* if the peer
1680+
* is in the bgp->peerhash as that on deletion
1681+
* we call bgp_stop which calls this function :(
1682+
* so on deletion let's remove from the list first
1683+
* and then do the deletion preventing this from
1684+
* being added back on the list below when we
1685+
* fail to remove it up here.
1686+
*/
1687+
1688+
/*
1689+
* listnode_lookup just scans the list
1690+
* for the peer structure so it's safe
1691+
* to use without modifying the su
1692+
*/
1693+
node = listnode_lookup(bgp->peer, peer);
1694+
if (node) {
1695+
/*
1696+
* Let's reset the peer->su release and
1697+
* reset it and put it back. We have to
1698+
* do this because hash_release will
1699+
* scan through looking for a matching
1700+
* su if needed.
1701+
*/
1702+
peer->su = old_su;
1703+
hash_release(peer->bgp->peerhash, peer);
1704+
listnode_delete(peer->bgp->peer, peer);
1705+
1706+
peer->su = new_su;
1707+
(void)hash_get(peer->bgp->peerhash, peer,
1708+
hash_alloc_intern);
1709+
listnode_add_sort(peer->bgp->peer, peer);
1710+
}
1711+
}
16701712
}
16711713

16721714
void bgp_recalculate_afi_safi_bestpaths(struct bgp *bgp, afi_t afi, safi_t safi)
@@ -1816,6 +1858,7 @@ struct peer *peer_create_accept(struct bgp *bgp)
18161858

18171859
peer = peer_lock(peer); /* bgp peer list reference */
18181860
listnode_add_sort(bgp->peer, peer);
1861+
(void)hash_get(bgp->peerhash, peer, hash_alloc_intern);
18191862

18201863
return peer;
18211864
}
@@ -2509,9 +2552,16 @@ int peer_delete(struct peer *peer)
25092552
/* Delete from all peer list. */
25102553
if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)
25112554
&& (pn = listnode_lookup(bgp->peer, peer))) {
2512-
peer_unlock(peer); /* bgp peer list reference */
2555+
/*
2556+
* Removing from the list node first because
2557+
* peer_unlock *can* call peer_delete( I know,
2558+
* I know ). So let's remove it and in
2559+
* the su recalculate function we'll ensure
2560+
* it's in there or not.
2561+
*/
25132562
list_delete_node(bgp->peer, pn);
25142563
hash_release(bgp->peerhash, peer);
2564+
peer_unlock(peer); /* bgp peer list reference */
25152565
}
25162566

25172567
/* Buffers. */
@@ -3727,8 +3777,10 @@ int bgp_delete(struct bgp *bgp)
37273777
for (ALL_LIST_ELEMENTS(bgp->group, node, next, group))
37283778
peer_group_delete(group);
37293779

3730-
for (ALL_LIST_ELEMENTS(bgp->peer, node, next, peer))
3780+
while (listcount(bgp->peer)) {
3781+
peer = listnode_head(bgp->peer);
37313782
peer_delete(peer);
3783+
}
37323784

37333785
if (bgp->peer_self) {
37343786
peer_delete(bgp->peer_self);

0 commit comments

Comments
 (0)