From: "mrbff (Code Review)" Date: Thu, 11 Sep 2025 13:41:38 +0000 References: Reply-To: marco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="podxcOuZeG0="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: push-update-server: comment about `buf_string_compare_advance()` usag... List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --podxcOuZeG0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello plaisthos, flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1176?usp=3Demail to review the following change. Change subject: push-update-server: comment about `buf_string_compare_advan= ce()` usage in `send_single_push_updaate()` ...................................................................... push-update-server: comment about `buf_string_compare_advance()` usage in `= send_single_push_updaate()` Change-Id: Icc20f3adccb5689f11df8888c526d0402b32d806 Signed-off-by: Marco Baffo --- A src/openvpn/push_util.c 1 file changed, 303 insertions(+), 0 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/76/1176/1 diff --git a/src/openvpn/push_util.c b/src/openvpn/push_util.c new file mode 100644 index 0000000..e641755 --- /dev/null +++ b/src/openvpn/push_util.c @@ -0,0 +1,303 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "push.h" +#include "buffer.h" + +#ifdef ENABLE_MANAGEMENT +#include "multi.h" +#endif + +int +process_incoming_push_update(struct context *c, unsigned int permission_ma= sk, + unsigned int *option_types_found, struct buff= er *buf, + bool msg_sender) +{ + int ret =3D PUSH_MSG_ERROR; + const uint8_t ch =3D buf_read_u8(buf); + if (ch =3D=3D ',') + { + if (apply_push_options(c, &c->options, buf, permission_mask, optio= n_types_found, c->c2.es, + true)) + { + switch (c->options.push_continuation) + { + case 0: + case 1: + ret =3D PUSH_MSG_UPDATE; + break; + + case 2: + ret =3D PUSH_MSG_CONTINUATION; + break; + } + } + else if (!msg_sender) + { + throw_signal_soft(SIGUSR1, "Offending option received from ser= ver"); + } + } + else if (ch =3D=3D '\0') + { + ret =3D PUSH_MSG_UPDATE; + } + + return ret; +} + +#ifdef ENABLE_MANAGEMENT +/** + * Return index of last `,` or `0` if it didn't find any. + * If there is a comma at index `0` it's an error anyway + */ +static int +find_first_comma_of_next_bundle(const char *str, int ix) +{ + while (ix > 0) + { + if (str[ix] =3D=3D ',') + { + return ix; + } + ix--; + } + return 0; +} + +/* Allocate memory and assemble the final message */ +static struct buffer +forge_msg(const char *src, const char *continuation, struct gc_arena *gc) +{ + int src_len =3D strlen(src); + int con_len =3D continuation ? strlen(continuation) : 0; + struct buffer buf =3D alloc_buf_gc(src_len + sizeof(push_update_cmd) += con_len + 2, gc); + + buf_printf(&buf, "%s,%s%s", push_update_cmd, src, continuation ? conti= nuation : ""); + + return buf; +} + +static char * +gc_strdup(const char *src, struct gc_arena *gc) +{ + char *ret =3D gc_malloc((strlen(src) + 1) * sizeof(char), true, gc); + + strcpy(ret, src); + return ret; +} + +/* It split the messagge (if necessay) and fill msgs with the message chun= ks. + * Return `false` on failure an `true` on success. + */ +static bool +message_splitter(const char *s, struct buffer *msgs, struct gc_arena *gc, = const int safe_cap) +{ + if (!s || !*s) + { + return false; + } + + char *str =3D gc_strdup(s, gc); + int i =3D 0; + int im =3D 0; + + while (*str) + { + /* + ',' - '/0' */ + if (strlen(str) > safe_cap) + { + int ci =3D find_first_comma_of_next_bundle(str, safe_cap); + if (!ci) + { + /* if no commas were found go to fail, do not send any mes= sage */ + return false; + } + str[ci] =3D '\0'; + /* copy from i to (ci -1) */ + msgs[im] =3D forge_msg(str, ",push-continuation 2", gc); + i =3D ci + 1; + } + else + { + if (im) + { + msgs[im] =3D forge_msg(str, ",push-continuation 1", gc); + } + else + { + msgs[im] =3D forge_msg(str, NULL, gc); + } + i =3D strlen(str); + } + str =3D &str[i]; + im++; + } + return true; +} + +/* send the message(s) prepared to one single client */ +static bool +send_single_push_update(struct context *c, struct buffer *msgs, unsigned i= nt *option_types_found) +{ + if (!msgs[0].data || !*(msgs[0].data)) + { + return false; + } + int i =3D -1; + + while (msgs[++i].data && *(msgs[i].data)) + { + if (!send_control_channel_string(c, BSTR(&msgs[i]), D_PUSH)) + { + return false; + } + + /* After sending the control message, we update the options server= -side in the client's context + * so pushed options like ifconfig/ifconfig-ipv6 can actually work. + * If we don't do that, the packets arriving from the client with = the new address will be + * rejected because the value in the option is an old one. + * For the same reason we later update the vhash too in `send_push= _update()` function. + * Using `buf_string_compare_advance()` we mimic the behavior insi= de `process_incoming_push_msg()`. + * However, we don't need to check the return value here because w= e just want to `advance`, + * meaning we skip the `push_update_cmd' we added earlier. */ + buf_string_compare_advance(&msgs[i], push_update_cmd); + if (process_incoming_push_update(c, pull_permission_mask(c), optio= n_types_found, &msgs[i], true) =3D=3D PUSH_MSG_ERROR) + { + msg(M_WARN, "Failed to process push update message sent to cli= ent ID: %u", + c->c2.tls_multi ? c->c2.tls_multi->peer_id : UINT32_MAX); + continue; + } + c->options.push_option_types_found |=3D *option_types_found; + if (!options_postprocess_pull(&c->options, c->c2.es)) + { + msg(M_WARN, "Failed to post-process push update message sent t= o client ID: %u", + c->c2.tls_multi ? c->c2.tls_multi->peer_id : UINT32_MAX); + } + } + return true; +} + +int +send_push_update(struct multi_context *m, const void *target, const char *= msg, const push_update_type type, const int push_bundle_size) +{ + if (!msg || !*msg || !m + || (!target && type !=3D UPT_BROADCAST)) + { + return -EINVAL; + } + + struct gc_arena gc =3D gc_new(); + /* extra space for possible trailing ifconfig and push-continuation */ + const int extra =3D 84 + sizeof(push_update_cmd); + /* push_bundle_size is the maximum size of a message, so if the message + * we want to send exceeds that size we have to split it into smaller = messages */ + const int safe_cap =3D push_bundle_size - extra; + int msgs_num =3D (strlen(msg) / safe_cap) + ((strlen(msg) % safe_cap) = !=3D 0); + struct buffer *msgs =3D gc_malloc((msgs_num + 1) * sizeof(struct buffe= r), true, &gc); + + unsigned int option_types_found =3D 0; + + msgs[msgs_num].data =3D NULL; + if (!message_splitter(msg, msgs, &gc, safe_cap)) + { + gc_free(&gc); + return -EINVAL; + } + + if (type =3D=3D UPT_BY_CID) + { + struct multi_instance *mi =3D lookup_by_cid(m, *((unsigned long *)= target)); + + if (!mi) + { + return -ENOENT; + } + + const char *old_ip =3D mi->context.options.ifconfig_local; + const char *old_ipv6 =3D mi->context.options.ifconfig_ipv6_local; + if (!mi->halt + && send_single_push_update(&mi->context, msgs, &option_types_f= ound)) + { + if (option_types_found & OPT_P_UP) + { + update_vhash(m, mi, old_ip, old_ipv6); + } + gc_free(&gc); + return 1; + } + else + { + gc_free(&gc); + return 0; + } + } + + int count =3D 0; + struct hash_iterator hi; + const struct hash_element *he; + + hash_iterator_init(m->iter, &hi); + while ((he =3D hash_iterator_next(&hi))) + { + struct multi_instance *curr_mi =3D he->value; + + if (curr_mi->halt) + { + continue; + } + + /* Type is UPT_BROADCAST so we update every client */ + option_types_found =3D 0; + const char *old_ip =3D curr_mi->context.options.ifconfig_local; + const char *old_ipv6 =3D curr_mi->context.options.ifconfig_ipv6_lo= cal; + if (!send_single_push_update(&curr_mi->context, msgs, &option_type= s_found)) + { + msg(M_CLIENT, "ERROR: Peer ID: %u has not been updated", + curr_mi->context.c2.tls_multi ? curr_mi->context.c2.tls_mu= lti->peer_id : UINT32_MAX); + continue; + } + if (option_types_found & OPT_P_UP) + { + update_vhash(m, curr_mi, old_ip, old_ipv6); + } + count++; + } + + hash_iterator_free(&hi); + gc_free(&gc); + return count; +} + +#define RETURN_UPDATE_STATUS(n_sent) \ + do \ + { \ + if ((n_sent) > 0) \ + { \ + msg(M_CLIENT, "SUCCESS: %d client(s) updated", (n_sent)); \ + return true; \ + } \ + else \ + { \ + msg(M_CLIENT, "ERROR: no client updated"); \ + return false; \ + } \ + } while (0) + + +bool +management_callback_send_push_update_broadcast(void *arg, const char *opti= ons) +{ + int n_sent =3D send_push_update(arg, NULL, options, UPT_BROADCAST, PUS= H_BUNDLE_SIZE); + + RETURN_UPDATE_STATUS(n_sent); +} + +bool +management_callback_send_push_update_by_cid(void *arg, unsigned long cid, = const char *options) +{ + int n_sent =3D send_push_update(arg, &cid, options, UPT_BY_CID, PUSH_B= UNDLE_SIZE); + + RETURN_UPDATE_STATUS(n_sent); +} +#endif /* ifdef ENABLE_MANAGEMENT */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1176?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Icc20f3adccb5689f11df8888c526d0402b32d806 Gerrit-Change-Number: 1176 Gerrit-PatchSet: 1 Gerrit-Owner: mrbff Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --podxcOuZeG0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

mrbff would like plaisthos and flichtenheld to review t= his change.

V= iew Change

push-update-server: comment about `buf_string_compare_advanc=
e()` usage in `send_single_push_updaate()`

Change-Id: Icc20f3adccb56= 89f11df8888c526d0402b32d806
Signed-off-by: Marco Baffo <marco@...2787...= ...>
---
A src/openvpn/push_util.c
1 file changed, 303 insertio= ns(+), 0 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/ope=
nvpn refs/changes/76/1176/1
diff --git a/src/openvpn/push_util.c b/=
src/openvpn/push_util.c
new file mode 100644
index 0000000..e641755
--- /dev/null
+++ = b/src/openvpn/push_util.c
@@ -0,0 +1,303 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "push.h"
+#include "buffer.h"
+
+#ifdef ENABLE_MANAGEMENT
+#include "multi.h"
+#endif
+
+int
+process_incomi= ng_push_update(struct context *c, unsigned int permission_mask,
<= span style=3D"color: hsl(120, 100%, 40%);">+ un= signed int *option_types_found, struct buffer *buf,
+ bool msg_sen= der)
+{
+ int ret =3D PUSH_MSG_ERROR;
+ const uint8_t ch = =3D buf_read_u8(buf);
+ if (ch =3D=3D ',')
+ {
+ = if (apply_push_options(c, &c->options, buf, permission_mask, = option_types_found, c->c2.es,
+ true))
+ {
+ switch (c->options.push_continuation)
+ {<= br>+ case 0:

+ case 1:=
+ = ret =3D PUSH_MSG_UPDATE;
+ break;
+
+ = case 2:
+ = ret =3D PUSH_MSG_CONTINUATION;
+ break;
+ }
+ }
+ else if (!msg_sender)
+ {
+ throw_signal_soft(SIGUSR1, "Offending = option received from server");
+ }
+ }
+ else= if (ch =3D=3D '\0')
+ {
+ = ret =3D PUSH_MSG_UPDATE;
+ }
+

+ return ret;+}
+
+#ifdef ENABLE_MANAGEMENT
+/**
+ *= Return index of last `,` or `0` if it didn't find any.
+ * If there is a comma at index `0`= it's an error anyway
+ */
+static int=
+find_first_comma_of= _next_bundle(const char *str, int ix)
+{
+= while (ix > 0)
+ {
+ if (= str[ix] =3D=3D ',')
+ {
+ = return ix;
= + }
+ i= x--;
+ }+ return 0;
+}
+
+static struct buffer
+forge_msg(const char *src, const char = *continuation, struct gc_arena *gc)
+{
+ = int src_len =3D strlen(src);
+ int con_len =3D continuation ? strlen(continuation) : 0;
+ struct buffer buf = =3D alloc_buf_gc(src_len + sizeof(push_update_cmd) + con_len + 2, gc);

+
+ buf_printf(&buf, "%s,%s%s&qu= ot;, push_update_cmd, src, continuation ? continuation : "");
+
+ return buf;
+}
+
+static = char *
+gc_strdup(con= st char *src, struct gc_arena *gc)
+{
+ = char *ret =3D gc_malloc((strlen(src) + 1) * sizeof(char), true, gc);
+
+ strcpy(ret, src);
+ return ret;
+}
+
+/* It s= plit the messagge (if necessay) and fill msgs with the message chunks.
+ * Return `false` on fai= lure an `true` on success.
+ */
+static bo= ol
+message_splitter(= const char *s, struct buffer *msgs, struct gc_arena *gc, const int safe_cap= )
+{
+ if (!s || !*s)
+ {
+ return false;
+ }
+
+ cha= r *str =3D gc_strdup(s, gc);
+ int i =3D 0;
+ int im =3D 0;
+
+ while (*str= )
+ {
+ /* + ',' - '= /0' */

+ i= f (strlen(str) > safe_cap)
+ {
+= int ci =3D find_first_comma_of_next_bundle(str, safe_cap);
+ if (!ci)
+ {+ /* if no comm= as were found go to fail, do not send any message */
+ return false;
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ str[ci] =3D '\0';=
+ /* copy= from i to (ci -1) */
+ msgs[im] =3D forge_msg(str, ",push-continuation 2",= gc);
+ i = =3D ci + 1;
+ = }
+ else
+ {
+ if (im)

+ {

+ msgs[im] =3D forge_msg(st= r, ",push-continuation 1", gc);
+ }
+ else
+ {
+ msgs[im] =3D forge_msg(str, NULL, gc);
+ }

+ i =3D strlen(str);+ }
+ str =3D &str[i];+ im++;
+ }

+ return true;
+}
+
+/* send the me= ssage(s) prepared to one single client */
+static bool
+send_single_push_update(struct context *c, struct buffer *msg= s, unsigned int *option_types_found)
+{
+ = if (!msgs[0].data || !*(msgs[0].data))
+ {
+ return false;
+ }
+ = int i =3D -1;
+
+ while (msgs[++i].data= && *(msgs[i].data))
+ {
+ = if (!send_control_channel_string(c, BSTR(&msgs[i]), D_PUSH))<= br>+ {
+ return false;
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+
+ /* After sending the control message, we update the = options server-side in the client's context
+ * so pushed options like ifconfig/ifco= nfig-ipv6 can actually work.
+ * If we don't do that, the packets arriving from the = client with the new address will be
+ * rejected because the value in the option is an o= ld one.
+ * F= or the same reason we later update the vhash too in `send_push_update()` fu= nction.
+ * = Using `buf_string_compare_advance()` we mimic the behavior inside `process_= incoming_push_msg()`.
+ * However, we don't need to check the return value here beca= use we just want to `advance`,
+ * meaning we skip the `push_update_cmd' we added ea= rlier. */
+ bu= f_string_compare_advance(&msgs[i], push_update_cmd);
+ if (process_incoming_push_upda= te(c, pull_permission_mask(c), option_types_found, &msgs[i], true) =3D= =3D PUSH_MSG_ERROR)
+= {
+ = msg(M_WARN, "Failed to process push update message sent to client ID= : %u",
+ = c->c2.tls_multi ? c->c2.tls_multi->peer_id : UINT32_MAX);<= /span>
+ continue= ;
+ }+ c->options.push_o= ption_types_found |=3D *option_types_found;
+ if (!options_postprocess_pull(&c->op= tions, c->c2.es))
= + {
+ = msg(M_WARN, "Failed to post-process push update message sent to cli= ent ID: %u",
+ = c->c2.tls_multi ? c->c2.tls_multi->peer_id : UINT32_= MAX);
+ }
+ }
+ return true;
+}
+
+int=
+send_push_update(st= ruct multi_context *m, const void *target, const char *msg, const push_upda= te_type type, const int push_bundle_size)
+{
+ if (!msg || !*msg || !m
+ || (!target && type !=3D UPT_BROADCAST))=
+ {
+ return -EINVAL;
+ }
+
+ struct gc_arena gc =3D gc_new();
+ /* extra space for possible trailing ifconfig an= d push-continuation */
+ /* push_bundle_size is the maximum= size of a message, so if the message
+ * we want to send exceeds that size we have to split= it into smaller messages */
+ const int safe_cap =3D push_bundle_size - extra;
+ int msgs_num =3D (strlen(msg)= / safe_cap) + ((strlen(msg) % safe_cap) !=3D 0);

+ struct buffer *msgs =3D gc_malloc((msgs_n= um + 1) * sizeof(struct buffer), true, &gc);
+
+ unsigned int option_types_found =3D 0;
+
+ msgs[msgs_num].data =3D NULL;
+ if (!message_splitter(msg, msgs, &gc, = safe_cap))
+ {
+ gc_free(&gc= );
+ return -E= INVAL;
+ }<= br>+
+ if (type =3D=3D UPT_BY_CID)
+ {

+ struct multi_instance *mi =3D lookup_by_= cid(m, *((unsigned long *)target));
+
+ = if (!mi)
+ = {
+ retu= rn -ENOENT;
+ = }
+
+ const char *old_ip =3D mi->= ;context.options.ifconfig_local;
+ const char *old_ipv6 =3D mi->context.options.ifconf= ig_ipv6_local;
+ = if (!mi->halt
+= && send_single_push_update(&mi->context, msgs, &= amp;option_types_found))
+ {
+ = if (option_types_found & OPT_P_UP)
+ {
+ update_vhash(m, mi, old_ip, old_ipv6);<= /span>
+ }=
+ gc_free(&g= c);
+ retu= rn 1;
+ }
+ else
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ gc_free(&gc);
<= span style=3D"color: hsl(120, 100%, 40%);">+ return 0;
+ }
+ }
+
+ struct hash_iterator hi;
+ const struct hash_element *he;
+
+ hash_iterator_init(m->iter, &hi);
+ while ((he =3D hash_iterator_next(&am= p;hi)))
+ {=
+ struct multi_insta= nce *curr_mi =3D he->value;
+
+ = if (curr_mi->halt)
+ {
+ = continue;
+ = }
+
+ /* Type is UPT_BROADCAST so= we update every client */

+ option_types_found =3D 0;
+ const char *old_ip =3D curr_mi->context.opt= ions.ifconfig_local;
= + const char *old_ipv6 =3D curr_mi->context.options.ifconfig_ipv6= _local;
+ if (= !send_single_push_update(&curr_mi->context, msgs, &option_types_= found))
+ {
+ msg(M_CLIE= NT, "ERROR: Peer ID: %u has not been updated",
+ curr_mi->context.c2= .tls_multi ? curr_mi->context.c2.tls_multi->peer_id : UINT32_MAX);
+ continue;<= /span>
+ }
= + if (option_types_found= & OPT_P_UP)
+ = {
+ u= pdate_vhash(m, curr_mi, old_ip, old_ipv6);
+ }
+ count++;
+ }
+
+ hash_iterator_free(= &hi);
+ gc_fre= e(&gc);
+ retu= rn count;
+}+
+#define RETURN_UPDATE_STATUS(n_sent) = \
+ do = \
+ { = \
+ if ((n_sent) > 0) = \
+ { = \
+ = msg(M_CLIENT, "SUCCESS: %d client(s) updated", (n_sent)); \
+ return tru= e; \
+ } = \
+ else = \
+ { = \
+ msg(M_CLIENT, "ERROR: = no client updated"); \
+ return false; = \
+= } \
+ } while (0)<= br>+
+
+bool
+manag= ement_callback_send_push_update_broadcast(void *arg, const char *options)
+{
+ int n_sent =3D send_push_update(arg= , NULL, options, UPT_BROADCAST, PUSH_BUNDLE_SIZE);
+
+ RETURN_UPDATE_STATUS(n_sent);
+}
+
+bool=
+management_callback_send_p= ush_update_by_cid(void *arg, unsigned long cid, const char *options)=
+{
+ int n_sent =3D send_push_update(arg, &am= p;cid, options, UPT_BY_CID, PUSH_BUNDLE_SIZE);
+
+ RETURN_UPDATE_STATUS(n_sent);
+}
+#endif /* ifdef ENABLE_MANAGEMENT */

<= p>To view, visit change 1176. To unsubscribe, or for help writing mail filters= , visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Icc20f3adccb5689f11df8888c52= 6d0402b32d806
Gerrit-Change-Number: 1176
Gerrit-PatchSet: 1
Gerrit-Owner: mrbff <marco@...2726...> <= /div>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newchange
--podxcOuZeG0=--