<p>laforge <strong>submitted</strong> this change.</p><p><a href="https://gerrit.osmocom.org/c/osmo-mgw/+/15838">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, but someone else must approve
  pespin: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">client: endp fsm: allow cancelling a notify event<br><br>There is a use-after-free problem if a 'notify' FSM as passed to<br>osmo_mgcpc_ep_ci_request() deallocates before the notify event has been<br>dispatched. To avoid that, add API to allow cancelling a notify.<br><br>Change-Id: I41687d7f3a808587ab7f7520f46dcc3c29cff92d<br>---<br>M include/osmocom/mgcp_client/mgcp_client_endpoint_fsm.h<br>M src/libosmo-mgcp-client/mgcp_client_endpoint_fsm.c<br>2 files changed, 42 insertions(+), 1 deletion(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/osmocom/mgcp_client/mgcp_client_endpoint_fsm.h b/include/osmocom/mgcp_client/mgcp_client_endpoint_fsm.h</span><br><span>index d77371a..9ae2039 100644</span><br><span>--- a/include/osmocom/mgcp_client/mgcp_client_endpoint_fsm.h</span><br><span>+++ b/include/osmocom/mgcp_client/mgcp_client_endpoint_fsm.h</span><br><span>@@ -29,6 +29,8 @@</span><br><span>                          uint32_t event_success, uint32_t event_failure,</span><br><span>                              void *notify_data);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+void osmo_mgcpc_ep_cancel_notify(struct osmo_mgcpc_ep *ep, struct osmo_fsm_inst *notify);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! Dispatch a DLCX for the given connection.</span><br><span>  * \param ci  Connection identifier as obtained from osmo_mgcpc_ep_ci_add().</span><br><span>  */</span><br><span>diff --git a/src/libosmo-mgcp-client/mgcp_client_endpoint_fsm.c b/src/libosmo-mgcp-client/mgcp_client_endpoint_fsm.c</span><br><span>index 30ad2d3..34717e9 100644</span><br><span>--- a/src/libosmo-mgcp-client/mgcp_client_endpoint_fsm.c</span><br><span>+++ b/src/libosmo-mgcp-client/mgcp_client_endpoint_fsm.c</span><br><span>@@ -74,6 +74,7 @@</span><br><span> static struct osmo_fsm osmo_mgcpc_ep_fsm;</span><br><span> </span><br><span> struct fsm_notify {</span><br><span style="color: hsl(120, 100%, 40%);">+   struct llist_head entry;</span><br><span>     struct osmo_fsm_inst *fi;</span><br><span>    uint32_t success;</span><br><span>    uint32_t failure;</span><br><span>@@ -122,6 +123,10 @@</span><br><span>     /*! Endpoint connection slots. Note that each connection has its own set of FSM event numbers to signal success</span><br><span>       * and failure, depending on its index within this array. See CI_EV_SUCCESS and CI_EV_FAILURE. */</span><br><span>    struct osmo_mgcpc_ep_ci ci[USABLE_CI];</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /*! Internal use: if a function keeps an fsm_notify for later dispatch while already clearing or re-using the</span><br><span style="color: hsl(120, 100%, 40%);">+  * ci[], the fsm_notify should be kept here to also get canceled by osmo_mgcpc_ep_cancel_notify(). */</span><br><span style="color: hsl(120, 100%, 40%);">+ struct llist_head background_notify;</span><br><span> };</span><br><span> </span><br><span> const struct value_string osmo_mgcp_verb_names[] = {</span><br><span>@@ -300,6 +305,7 @@</span><br><span>                 .fi = fi,</span><br><span>            .T_defs = T_defs,</span><br><span>    };</span><br><span style="color: hsl(120, 100%, 40%);">+    INIT_LLIST_HEAD(&ep->background_notify);</span><br><span>      fi->priv = ep;</span><br><span> </span><br><span>        va_start(ap, endpoint_str_fmt);</span><br><span>@@ -368,6 +374,9 @@</span><br><span>        /* When dispatching an event for this CI, the user may decide to trigger the next request for this conn right</span><br><span>         * away. So we must be ready with a cleared *ci. Store the notify separately and clear before dispatching. */</span><br><span>        notify = ci->notify;</span><br><span style="color: hsl(120, 100%, 40%);">+       /* Register the planned notification in ep->background_notify so we also catch any osmo_mgcpc_ep_cancel_notify()</span><br><span style="color: hsl(120, 100%, 40%);">+    * that might be triggered between clearing the ci and actually dispatching the event. */</span><br><span style="color: hsl(120, 100%, 40%);">+     llist_add(&notify.entry, &ep->background_notify);</span><br><span> </span><br><span>     *ci = (struct osmo_mgcpc_ep_ci){</span><br><span>             .ep = ci->ep,</span><br><span>@@ -393,11 +402,15 @@</span><br><span> </span><br><span>         /* If this check has terminated the FSM instance, don't fire any more events to prevent use-after-free problems.</span><br><span>          * The endpoint FSM does dispatch a term event to its parent, and everything should be cleaned like that. */</span><br><span style="color: hsl(0, 100%, 40%);">-    if (!osmo_mgcpc_ep_fsm_check_state_chg_after_response(ci->ep->fi))</span><br><span style="color: hsl(120, 100%, 40%);">+      if (!osmo_mgcpc_ep_fsm_check_state_chg_after_response(ep->fi)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           /* The ep has deallocated, no need to llist_del(&notify.entry) here. */</span><br><span>          return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span> </span><br><span>        if (notify.fi)</span><br><span>               osmo_fsm_inst_dispatch(notify.fi, notify.failure, notify.data);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     llist_del(&notify.entry);</span><br><span> }</span><br><span> </span><br><span> static int update_endpoint_name(struct osmo_mgcpc_ep_ci *ci, const char *new_endpoint_name)</span><br><span>@@ -542,6 +555,11 @@</span><br><span>                                      ((struct osmo_mgcpc_ep*)fi->priv)->T_defs, 5)</span><br><span> </span><br><span> /*! Dispatch an actual CRCX/MDCX/DLCX message for this connection.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If the 'notify' instance deallocates before it received a notification of event_success or event_failure,</span><br><span style="color: hsl(120, 100%, 40%);">+ * osmo_mgcpc_ep_ci_cancel_notify() or osmo_mgcpc_ep_cancel_notify() must be called. It is not harmful to cancel</span><br><span style="color: hsl(120, 100%, 40%);">+ * notification after an event has been received.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span>  * \param ci  Connection identifier as obtained from osmo_mgcpc_ep_ci_add().</span><br><span>  * \param verb  MGCP operation to dispatch.</span><br><span>  * \param verb_info  Parameters for the MGCP operation.</span><br><span>@@ -659,6 +677,26 @@</span><br><span>               osmo_fsm_inst_dispatch(notify, event_failure, notify_data);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! No longer notify for any state changes for any conns of this endpoint.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Useful if the notify instance passed to osmo_mgcpc_ep_ci_request() is about to deallocate.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param ep  The endpoint FSM instance.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param notify  Which target to cancel notification for, if NULL cancel all notifications. */</span><br><span style="color: hsl(120, 100%, 40%);">+void osmo_mgcpc_ep_cancel_notify(struct osmo_mgcpc_ep *ep, struct osmo_fsm_inst *notify)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct fsm_notify *n;</span><br><span style="color: hsl(120, 100%, 40%);">+ int i;</span><br><span style="color: hsl(120, 100%, 40%);">+        for (i = 0; i < ARRAY_SIZE(ep->ci); i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+              struct osmo_mgcpc_ep_ci *ci = &ep->ci[i];</span><br><span style="color: hsl(120, 100%, 40%);">+              if (!notify || ci->notify.fi == notify)</span><br><span style="color: hsl(120, 100%, 40%);">+                    ci->notify.fi = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+      }</span><br><span style="color: hsl(120, 100%, 40%);">+     llist_for_each_entry(n, &ep->background_notify, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+               if (!notify || n->fi == notify)</span><br><span style="color: hsl(120, 100%, 40%);">+                    n->fi = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+      }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static int send_verb(struct osmo_mgcpc_ep_ci *ci)</span><br><span> {</span><br><span>   int rc;</span><br><span>@@ -724,6 +762,7 @@</span><br><span> {</span><br><span>   if (!ep)</span><br><span>             return;</span><br><span style="color: hsl(120, 100%, 40%);">+       osmo_mgcpc_ep_cancel_notify(ep, NULL);</span><br><span>       osmo_fsm_inst_term(ep->fi, OSMO_FSM_TERM_REGULAR, 0);</span><br><span> }</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/osmo-mgw/+/15838">change 15838</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/osmo-mgw/+/15838"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-mgw </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I41687d7f3a808587ab7f7520f46dcc3c29cff92d </div>
<div style="display:none"> Gerrit-Change-Number: 15838 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: neels <nhofmeyr@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder </div>
<div style="display:none"> Gerrit-Reviewer: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-Reviewer: neels <nhofmeyr@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: pespin <pespin@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>