<p>Neels Hofmeyr has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/12716">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">osmo_fsm_inst_state_chg(): clamp timeout_secs to <= ~68 years<br><br>During testing of the upcoming tdef API, it became apparent that passing very<br>large timeout values to osmo_fsm_inst_state_chg() wraps back in the number<br>range, and might actually result in effectively very short timeouts instead.<br><br>Since time_t's range is not well defined across platforms, use a reasonable<br>maximum value of signed 64 bit integer. Hence this will be safe at least on<br>systems with an int64_t for struct timeval.tv_sec and larger.<br><br>Clamp the osmo_fsm_inst_state_chg() timeout_secs argument to a maximum of<br>0x7fffffff, which amounts to just above 68 years:<br>float(0x7fffffff) / (60. * 60 * 24 * 365.25) = 68.04965038532715<br><br>(In upcoming patch Ibd6b1ed7f1bd6e1f2e0fde53352055a4468f23e5, this can be<br>verified to work by invoking tdef_test manually with a cmdline argument passed<br>to enable the range check.)<br><br>Change-Id: I35ec4654467b1d6040c8aa215049766089e5e64a<br>---<br>M src/fsm.c<br>1 file changed, 13 insertions(+), 1 deletion(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/16/12716/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/fsm.c b/src/fsm.c</span><br><span>index ae7c0f5..33a5bbd 100644</span><br><span>--- a/src/fsm.c</span><br><span>+++ b/src/fsm.c</span><br><span>@@ -437,6 +437,11 @@</span><br><span>     uint32_t old_state = fi->state;</span><br><span>   const struct osmo_fsm_state *st = &fsm->states[fi->state];</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+      /* Limit to 0x7fffffff seconds as explained by</span><br><span style="color: hsl(120, 100%, 40%);">+         * _osmo_fsm_inst_state_chg()'s API doc. */</span><br><span style="color: hsl(120, 100%, 40%);">+       if (timeout_secs > 0x7fffffff)</span><br><span style="color: hsl(120, 100%, 40%);">+             timeout_secs = 0x7fffffff;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>         /* validate if new_state is a valid state */</span><br><span>         if (!(st->out_state_mask & (1 << new_state))) {</span><br><span>                 LOGPFSMLSRC(fi, LOGL_ERROR, file, line,</span><br><span>@@ -493,9 +498,16 @@</span><br><span>  *  timer_cb. If passing timeout_secs == 0, it is recommended to also pass T ==</span><br><span>  *  0, so that fi->T is reset to 0 when no timeout is invoked.</span><br><span>  *</span><br><span style="color: hsl(120, 100%, 40%);">+ *  Range: since time_t's maximum value is not well defined in a cross platform</span><br><span style="color: hsl(120, 100%, 40%);">+ *  way, clamp timeout_secs to the maximum of the signed 64bit range, or roughly</span><br><span style="color: hsl(120, 100%, 40%);">+ *  68 years (float(0x7fffffff) / (60. * 60 * 24 * 365.25) = 68.0497). Thus</span><br><span style="color: hsl(120, 100%, 40%);">+ *  ensure that very large timeouts do not wrap around to become very small</span><br><span style="color: hsl(120, 100%, 40%);">+ *  ones. Note though that this might still be unsafe on systems with a time_t</span><br><span style="color: hsl(120, 100%, 40%);">+ *  range below 64 bits.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span>  *  \param[in] fi FSM instance whose state is to change</span><br><span>  *  \param[in] new_state The new state into which we should change</span><br><span style="color: hsl(0, 100%, 40%);">- *  \param[in] timeout_secs Timeout in seconds (if !=0)</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] timeout_secs Timeout in seconds (if !=0), maximum-clamped to 2147483647 seconds.</span><br><span>  *  \param[in] T Timer number (if \ref timeout_secs != 0)</span><br><span>  *  \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro)</span><br><span>  *  \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro)</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/12716">change 12716</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/12716"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I35ec4654467b1d6040c8aa215049766089e5e64a </div>
<div style="display:none"> Gerrit-Change-Number: 12716 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Neels Hofmeyr <nhofmeyr@sysmocom.de> </div>