Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tipc: fix use-after-free in tipc_nametbl_stop

When we delete a service item in tipc_nametbl_stop() we loop over
all service ranges in the service's RB tree, and for each service
range we loop over its pertaining publications while calling
tipc_service_remove_publ() for each of them.

However, tipc_service_remove_publ() has the side effect that it also
removes the comprising service range item when there are no publications
left. This leads to a "use-after-free" access when the inner loop
continues to the next iteration, since the range item holding the list
we are looping no longer exists.

We fix this by moving the delete of the service range item outside
the said function. Instead, we now let the two functions calling it
test if the list is empty and perform the removal when that is the
case.

Reported-by: syzbot+d64b64afc55660106556@syzkaller.appspotmail.com
Signed-off-by: Jon Maloy <jon.maloy@ericsson.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Jon Maloy and committed by
David S. Miller
be47e41d 9c438d7a

+17 -12
+17 -12
net/tipc/name_table.c
··· 241 241 static struct publication *tipc_service_remove_publ(struct net *net, 242 242 struct tipc_service *sc, 243 243 u32 lower, u32 upper, 244 - u32 node, u32 key) 244 + u32 node, u32 key, 245 + struct service_range **rng) 245 246 { 246 247 struct tipc_subscription *sub, *tmp; 247 248 struct service_range *sr; ··· 276 275 277 276 list_del(&p->all_publ); 278 277 list_del(&p->local_publ); 279 - 280 - /* Remove service range item if this was its last publication */ 281 - if (list_empty(&sr->all_publ)) { 278 + if (list_empty(&sr->all_publ)) 282 279 last = true; 283 - rb_erase(&sr->tree_node, &sc->ranges); 284 - kfree(sr); 285 - } 286 280 287 281 /* Notify any waiting subscriptions */ 288 282 list_for_each_entry_safe(sub, tmp, &sc->subscriptions, service_list) { 289 283 tipc_sub_report_overlap(sub, p->lower, p->upper, TIPC_WITHDRAWN, 290 284 p->port, p->node, p->scope, last); 291 285 } 286 + *rng = sr; 292 287 return p; 293 288 } 294 289 ··· 376 379 u32 node, u32 key) 377 380 { 378 381 struct tipc_service *sc = tipc_service_find(net, type); 382 + struct service_range *sr = NULL; 379 383 struct publication *p = NULL; 380 384 381 385 if (!sc) 382 386 return NULL; 383 387 384 388 spin_lock_bh(&sc->lock); 385 - p = tipc_service_remove_publ(net, sc, lower, upper, node, key); 389 + p = tipc_service_remove_publ(net, sc, lower, upper, node, key, &sr); 390 + 391 + /* Remove service range item if this was its last publication */ 392 + if (sr && list_empty(&sr->all_publ)) { 393 + rb_erase(&sr->tree_node, &sc->ranges); 394 + kfree(sr); 395 + } 386 396 387 397 /* Delete service item if this no more publications and subscriptions */ 388 398 if (RB_EMPTY_ROOT(&sc->ranges) && list_empty(&sc->subscriptions)) { ··· 751 747 static void tipc_service_delete(struct net *net, struct tipc_service *sc) 752 748 { 753 749 struct service_range *sr, *tmpr; 754 - struct publication *p, *tmpb; 750 + struct publication *p, *tmp; 755 751 756 752 spin_lock_bh(&sc->lock); 757 753 rbtree_postorder_for_each_entry_safe(sr, tmpr, &sc->ranges, tree_node) { 758 - list_for_each_entry_safe(p, tmpb, 759 - &sr->all_publ, all_publ) { 754 + list_for_each_entry_safe(p, tmp, &sr->all_publ, all_publ) { 760 755 tipc_service_remove_publ(net, sc, p->lower, p->upper, 761 - p->node, p->key); 756 + p->node, p->key, &sr); 762 757 kfree_rcu(p, rcu); 763 758 } 759 + rb_erase(&sr->tree_node, &sc->ranges); 760 + kfree(sr); 764 761 } 765 762 hlist_del_init_rcu(&sc->service_list); 766 763 spin_unlock_bh(&sc->lock);