pjsip_transport_events: Fix possible use after free on transport
authorGeorge Joseph <gjoseph@digium.com>
Mon, 10 Oct 2022 14:35:54 +0000 (08:35 -0600)
committerFriendly Automation <jenkins2@gerrit.asterisk.org>
Sat, 3 Dec 2022 16:24:36 +0000 (10:24 -0600)
commit7684c9e907fb85f5c58b025d9e385ad2600f12a2
treee3ff53f3fc58b978758d38d282f56726978dbefc
parent81f10e847efdbe8ec264062ee234e1098c29b3f6
pjsip_transport_events: Fix possible use after free on transport

It was possible for a module that registered for transport monitor
events to pass in a pjsip_transport that had already been freed.
This caused pjsip_transport_events to crash when looking up the
monitor for the transport.  The fix is a two pronged approach.

1. We now increment the reference count on pjsip_transports when we
create monitors for them, then decrement the count when the
transport is going to be destroyed.

2. There are now APIs to register and unregister monitor callbacks
by "transport key" which is a string concatenation of the remote ip
address and port.  This way the module needing to monitor the
transport doesn't have to hold on to the transport object itself to
unregister.  It just has to save the transport_key.

* Added the pjsip_transport reference increment and decrement.

* Changed the internal transport monitor container key from the
  transport->obj_name (which may not be unique anyway) to the
  transport_key.

* Added a helper macro AST_SIP_MAKE_REMOTE_IPADDR_PORT_STR() that
  fills a buffer with the transport_key using a passed-in
  pjsip_transport.

* Added the following functions:
  ast_sip_transport_monitor_register_key
  ast_sip_transport_monitor_register_replace_key
  ast_sip_transport_monitor_unregister_key
  and marked their non-key counterparts as deprecated.

* Updated res_pjsip_pubsub and res_pjsip_outbound_register to use
  the new "key" monitor functions.

NOTE: res_pjsip_registrar also uses the transport monitor
functionality but doesn't have a persistent object other than
contact to store a transport key.  At this time, it continues to
use the non-key monitor functions.

ASTERISK-30244

Change-Id: I1a20baf2a8643c272dcf819871d6c395f148f00b
include/asterisk/res_pjsip.h
res/res_pjsip/pjsip_transport_events.c
res/res_pjsip_outbound_registration.c
res/res_pjsip_pubsub.c