From: "mrbff (Code Review)" Attention is cur=
rently required from: mrbff, plaisthos. mrbff uploaded patch set #2 to this change.PUSH_UPDATE server: invalid read bug-fix and unit-tests=
improvements
The number of messages calculated before the call to m=
essage_splitter(), used
in the memory allocation in the buffer array, co=
uld in certain cases be less
than one than the actual number of messages=
, thus causing an override of the
sentinel buffer in message_splitter an=
d therefore an invalid read in
send_single_push_update(). The case in qu=
estion would be, for example, a
sequence of three options "A, B, C&=
quot; with the size of B equal to safe_cap - 1
and the sum of the sizes =
of A and C less than safe_cap - 2.
The buffer array was therefore replac=
ed with a list of buffers to completely
avoid calculating the number of =
messages before it was actually computed.
The test case in question has =
been added to the unit tests.
The unit tests have been improved using cm=
ocka macros.
Change-Id: Idba419681fe3ccc4e6e2f6ce7592332dcff62cd9
Signed-off-by: Marco Baffo <marco@...2726...>
---
M src/openvp=
n/push_util.c
M tests/unit_tests/openvpn/test_push_update_msg.c
2 fil=
es changed, 145 insertions(+), 32 deletions(-)g=
it pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/16/1316/2
=
index 552679a..51c7b5f 100644
--- a/src/openvpn/push=
_util.c
+++ b/src/openvpn/push_util.c
@@ -9=
1,7 +91,7 @@
* Return `false` on failure an `true` on suc=
cess.
*/
static bool
-message_splitter(const char *s, struct bu=
ffer *msgs, struct gc_arena *gc, const size_t safe_cap)
+message_splitter(const char *s, struct =
buffer_list *msgs, struct gc_arena *gc, const size_t safe_cap) {
if (!s || !*s)
{
@@ -100,7 +100,6 @@
char *str=
=3D gc_strdup(s, gc);
size_t i =3D 0;
- int im =3D 0;
=
span>
while (*str)
{
@@ =
-115,37 +114,38 @@
}
=
str[ci] =3D '\0';
/* copy from i t=
o (ci -1) */
- =
msgs[im] =3D forge_msg(str, ",push-continuation 2", gc);
+ struct buffer=
tmp =3D forge_msg(str, ",push-continuation 2", gc);+ buffer_list_push(msg=
s, BSTR(&tmp));
i =3D ci + 1; }
else
{
- if (im)
+ if (msgs->=
head)
{
- msgs[im] =3D forge_msg(str, ",push-cont=
inuation 1", gc);
+ struct buffer tmp =3D forge_msg(str, ",push-continu=
ation 1", gc);
+=
buffer_list_push(msgs, BSTR(&tmp));
=
}
else
=
{
- msgs=
[im] =3D forge_msg(str, NULL, gc);
+ struct buffer tmp =3D forge_msg(str, NULL, g=
c);
+ =
buffer_list_push(msgs, BSTR(&tmp));
}
i =3D strlen(str);
}
str =3D &str[i];
- im++;
}
=
return true;
}
/* se=
nd the message(s) prepared to one single client */
static =
bool
-send_single_push_=
update(struct multi_context *m, struct multi_instance *mi, struct buffer *m=
sgs)
+send_single_pus=
h_update(struct multi_context *m, struct multi_instance *mi, struct buffer_=
list *msgs)
{
- if (!msgs[0].data || !*(msgs[0].data))
+ if (!msgs->head)
=
{
return false;
}<=
br>
- int i =
=3D -1;
unsigned int option_types_found =3D 0;<=
br> struct context *c =3D &mi->context;
=
struct options o;
@@ -160,9 +160,10 @@
=
o.ifconfig_local =3D canary;
o.ifconfig_ipv6_local=
=3D canary;
- while (msgs[++i].data && *(msgs[i].data))+ struct buffer_entry *e =3D m=
sgs->head;
+ wh=
ile (e)
{
- if (!send_control_channel_string(c, BSTR(&msgs[i]), D_=
PUSH))
+ if (!=
send_control_channel_string(c, BSTR(&e->buf), D_PUSH))
return false;
=
}
@@ -182,13 +183,14 @@
* =
Also we need to make a temporary copy so we can buf_advance()
=
*/
- struct buf=
fer tmp_msg =3D msgs[i];
+ struct buffer tmp_msg =3D e->buf;
=
buf_string_compare_advance(&tmp_msg, push_update_cmd);
=
unsigned int permission_mask =3D pull_permission_mask(c); if (process_push_update(c, &o, permission_mask, &o=
ption_types_found, &tmp_msg, true) =3D=3D PUSH_MSG_ERROR)
msg(M_WARN, "Failed to proc=
ess push update message sent to client ID: %u", c->c2.tls_multi->=
;peer_id);
}
+ e =3D e->next;
}
if (option_types_found & OPT_P_UP)
@@ -270,12 +272,11 @@
* we want to send ex=
ceeds that size we have to split it into smaller messages */
const size=
_t safe_cap =3D push_bundle_size - extra;
- size_t msgs_num =3D (strlen(msg) / safe_cap) + ((st=
rlen(msg) % safe_cap) !=3D 0);
- struct buffer *msgs =3D gc_malloc((msgs_num + 1) * sizeof(stru=
ct buffer), true, &gc);
+ struct buffer_list *msgs =3D buffer_list_new();
- msgs[msgs_num].=
data =3D NULL;
if (!message_splitter(msg, msgs, &g=
c, safe_cap))
{
+ buffer_list_free(msgs);
g=
c_free(&gc);
return -EINVAL;
=
}
@@ -286,6 +287,7 @@
if (!mi)
{
+ buffer_list_free(msgs);
return -ENOE=
NT;
}
@@ -293,6 +295,7 @@ if (!support_push_update(mi))
{=
span>
msg(M_CLIENT, "PUSH_UPDATE: not sending me=
ssage to unsupported peer with ID: %u", mi->context.c2.tls_multi-&g=
t;peer_id);
+ =
buffer_list_free(msgs);
gc_free(&gc);<=
/span>
return 0;
}
@@ -300,11 +303,13 @@
if (!mi->halt
&& send_single_push_update(m, mi, msgs))=
span>
{
+ buffer_list_free(msgs);
gc_f=
ree(&gc);
return 1;
=
}
else
{
gc_free(&gc);
=
return 0;
}
@@ -334,6 +339,7 @=
@
}
hash_iterat=
or_free(&hi);
+ =
buffer_list_free(msgs);
gc_free(&gc);
=
return count;
}
diff --git a/te=
sts/unit_tests/openvpn/test_push_update_msg.c b/tests/unit_tests/openvpn/te=
st_push_update_msg.c
index 516e94c..e450a31 100644--- a/tests/unit_tests/openvpn/test_push_update_msg.c
@@=
-136,11 +136,7 @@
bool
send_control_chan=
nel_string(struct context *c, const char *str, msglvl_t msglevel)
{
- if (res =
&& res[i] && strcmp(res[i], str))
- {
- printf("\n\nexpected: %s\n\n actual: %s\n\n"=
, res[i], str);
- =
return false;
- }<=
/span>
+ check_expected(s=
tr);
i++;
return true;
=
}
@@ -301,44 +297,75 @@
=
#ifdef ENABLE_MANAGEMENT
char *r0[] =3D {
- "PUSH_UPDATE,redirect-=
gateway local,route 192.168.1.0 255.255.255.0"
+ "PUSH_UPDATE,redirect-gateway lo=
cal,route 192.168.1.0 255.255.255.0",
+ NULL
};
char *=
r1[] =3D {
"PUSH_UPDATE,-dhcp-option,blablalalala=
lalalalalalalalalf, lalalalalalalalalalalalalalaf,push-continuation 2"=
,
"PUSH_UPDATE, akakakakakakakakakakakaf, dhcp-op=
tion DNS 8.8.8.8,redirect-gateway local,push-continuation 2",- "PUSH_UPDATE,route 19=
2.168.1.0 255.255.255.0,push-continuation 1"
+ "PUSH_UPDATE,route 192.168.1.0 255.2=
55.255.0,push-continuation 1",
+ NULL
};
char *r3[] =
=3D {
- "PUSH_U=
PDATE,,,"
+ &=
quot;PUSH_UPDATE,,,",
+ NULL
};
char *r4[] =3D {
"PUSH_UPDATE,-dhcp-option, blablalalalalalalalalalalal=
alf, lalalalalalalalalalalalalalaf,push-continuation 2",
- "PUSH_UPDATE, route 192.168.1.0 25=
5.255.255.0,,push-continuation 1"
+ "PUSH_UPDATE, route 192.168.1.0 255.255.255.0,,=
push-continuation 1",
+ NULL
};
char *r5[] =3D {
"PUSH_UPDATE,,-dhcp-option, blablalalalalalalalalalala=
lalf, lalalalalalalalalalalalalalaf,push-continuation 2", "PUSH_UPDATE, akakakakakakakakakakakaf,dhcp-option DNS 8.8.8.=
8, redirect-gateway local,push-continuation 2",
- "PUSH_UPDATE, route 192.168.1.0 25=
5.255.255.0,push-continuation 1"
+ "PUSH_UPDATE, route 192.168.1.0 255.255.255.0,pu=
sh-continuation 1",
+ NULL
};
char *r6[] =3D {<=
br> "PUSH_UPDATE,-dhcp-option,blablalalalalalalalalalalalalf=
, lalalalalalalalalalalalalalaf,push-continuation 2",
=
"PUSH_UPDATE, akakakakakakakakakakakaf, dhcp-option DNS 8.8.8.8, =
redirect-gateway 10.10.10.10,,push-continuation 2",
- "PUSH_UPDATE, route 192.168.1.0=
255.255.255.0,,push-continuation 1"
+ "PUSH_UPDATE, route 192.168.1.0 255.255.255.=
0,,push-continuation 1",
+ NULL
};
char *r7[] =3D {
"PUSH_UPDATE,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,=
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,push-continuation 2"=
;,
- "PUSH_UPDA=
TE,,,,,,,,,,,,,,,,,,,push-continuation 1"
+ "PUSH_UPDATE,,,,,,,,,,,,,,,,,,,push-con=
tinuation 1",
+ =
NULL
};
char *r8[] =3D {
&=
quot;PUSH_UPDATE, akakakakakakakakakakakaf, dhcp-option DNS 8.8.8.8,redirec=
t-gateway\n local,push-continuation 2",
- "PUSH_UPDATE,route 192.168.1.0 255.255.255.=
0\n\n\n,push-continuation 1"
+ "PUSH_UPDATE,route 192.168.1.0 255.255.255.0\n\n\n,p=
ush-continuation 1",
+ NULL
};
char *r9[] =3D {=
- "PUSH_UPDATE,,"=
;
+ "PUSH_UPD=
ATE,,",
+ NUL=
L
};
-=
+char *r11[] =3D {=
span>
+ "PUSH_UPDATE=
,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,=
a,a,a,a,a,a,a,push-continuation 2",
+ "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,=
a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,push-continuation 2=
",
+ "PU=
SH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a=
,a,a,a,a,a,a,a,a,a,a,a,push-continuation 2",
+ "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a=
,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,push-conti=
nuation 2",
+ =
"PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,push-con=
tinuation 1",
+ =
NULL
+};
+char *r12[] =3D {
<=
span style=3D"color: hsl(120, 100%, 40%);">+ "PUSH_UPDATE,a,a,a,a,a=
,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,,,,,=
,a,push-continuation 2",
+ "PUSH_UPDATE,abc,push-continuation 1",+ NULL
+};
+char *r13[] =3D {
+ "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,=
a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,,,,,,a,",
+ NULL
+};
+char *r14[] =3D {
+ "PUSH_UPDATE,a,push-continuation 2",
+ "PUSH_UPDATE,aaaaaaaaaaaaaaa=
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,pu=
sh-continuation 2",
+ "PUSH_UPDATE,a,push-continuation 1",
+ NULL
+};
const char *=
msg0 =3D "redirect-gateway local,route 192.168.1.0 255.255.255.0"=
;
const char *msg1 =3D "-dhcp-option,blablalalalalala=
lalalalalalalf, lalalalalalalalalalalalalalaf,"
@@ -36=
5,8 +392,29 @@
"daisy damage damp=
dance danger daring dash daughter dawn day deal debate debris decade decem=
ber decide decline"
"decorat=
e decrease deer defense define defy degree delay deliver demand demise deni=
al";
+const char *msg11 =3D "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a=
,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,"
+ "a,a,a,a,a,a,a,a,a,a,a=
,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,"
+ "a,a=
,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,=
a,"
+ =
"a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,=
a,a,a,a,a,a,a,a,a,a,"
+ "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,=
a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a";
+
+const char *msg12 =3D "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a=
,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,,,,,,a,abc";
+
+const char *msg13 =3D "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a=
,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,,,,,,a,";
+
+const char *msg14 =3D "a,aaaaaaaaaaaaaaaaaa=
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,a&quo=
t;;
+
#define PUSH_BUNDLE_SIZE_TEST 184
+#define expect_control_channel_strings() =
\
+ do =
\
+ { =
\
+ for (int j =3D 0; res[j] !=3D =
NULL; j++) \
+ { =
\
+ =
expect_string(send_control_channel_string, str, res[j]); \
<=
span style=3D"color: hsl(120, 100%, 40%);">+ } =
\
+ } while (0)
+
static void
test_send_pu=
sh_msg0(void **state)
{
@@ -374,8 +422,10 =
@@
res =3D r0;
struct multi_conte=
xt *m =3D *state;
const unsigned long cid =3D 0;
+ expect_control_channe=
l_strings();
assert_int_equal(send_push_update(m, &=
;cid, msg0, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);
}
+
static=
void
test_send_push_msg1(void **state)
{=
@@ -383,6 +433,7 @@
res =3D r1;
struct multi_context *m =3D *state;
co=
nst unsigned long cid =3D 0;
+ expect_control_channel_strings();
assert_=
int_equal(send_push_update(m, &cid, msg1, UPT_BY_CID, PUSH_BUNDLE_SIZE_=
TEST), 1);
}
@@ -403,6 +=
454,7 @@
res =3D r3;
struct multi=
_context *m =3D *state;
const unsigned long cid =3D 0;=
+ expect_control_=
channel_strings();
assert_int_equal(send_push_update(m=
, &cid, msg3, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);
=
}
@@ -413,6 +465,7 @@
=
res =3D r4;
struct multi_context *m =3D *state;
const unsigned long cid =3D 0;
+ expect_control_channel_strings();
assert_int_equal(send_push_update(m, &cid, msg4, UPT_BY_CID=
, PUSH_BUNDLE_SIZE_TEST), 1);
}
@@ -423,6 +476,7 @@
res =3D r5;
const unsig=
ned long cid =3D 0;
+=
expect_control_channel_strings();
assert_int_equal=
(send_push_update(m, &cid, msg5, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1)=
;
}
@@ -433,6 +487,7 @@<=
/span>
res =3D r6;
struct multi_context =
*m =3D *state;
const unsigned long cid =3D 0;+ expect_control_channel_s=
trings();
assert_int_equal(send_push_update(m, &ci=
d, msg6, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);
}<=
br>
@@ -443,6 +498,7 @@
res =3D=
r7;
struct multi_context *m =3D *state;
+ expect_control_channel_strings();
=
assert_int_equal(send_push_update(m, &cid, msg7, UPT_BY_CID, PUSH_BU=
NDLE_SIZE_TEST), 1);
}
@=
@ -453,6 +509,7 @@
res =3D r8;
st=
ruct multi_context *m =3D *state;
const unsigned long =
cid =3D 0;
+ expec=
t_control_channel_strings();
assert_int_equal(send_pus=
h_update(m, &cid, msg8, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);<=
br> }
@@ -463,6 +520,7 @@
res =3D r9;
struct multi_context *m =3D *s=
tate;
const unsigned long cid =3D 0;
+ expect_control_channel_strings();=
assert_int_equal(send_push_update(m, &cid, msg9, =
UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);
}
=
@@ -476,6 +534,50 @@
assert_int_equa=
l(send_push_update(m, &cid, msg10, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), =
-EINVAL);
}
+static void
+test_send_push_msg11(void **state)
+{
+ i =3D 0;
+ res =3D r11;
+ struct multi_context *m =3D *state;
+ const unsigned long cid =3D 0;
+ assert_int_e=
qual(send_push_update(m, &cid, msg11, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST=
), 1);
+}
<=
span style=3D"color: hsl(120, 100%, 40%);">+
+static void
+test_send_push_msg12(void **state)
+{
+ i =3D 0;
+ res =3D r12;
+ struct multi_context *m =3D *state;
+ const unsigned long cid =3D 0;
+ assert_int_e=
qual(send_push_update(m, &cid, msg12, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST=
), 1);
+}
<=
span style=3D"color: hsl(120, 100%, 40%);">+
+static void
+test_send_push_msg13(void **state)
+{
+ i =3D 0;
+ res =3D r13;
+ struct multi_context *m =3D *state;
+ const unsigned long cid =3D 0;
+ assert_int_e=
qual(send_push_update(m, &cid, msg13, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST=
), 1);
+}
<=
span style=3D"color: hsl(120, 100%, 40%);">+
+static void
+test_send_push_msg14(void **state)
+{
+ i =3D 0;
+ res =3D r14;
+ struct multi_context *m =3D *state;
+ const unsigned long cid =3D 0;
+ assert_int_e=
qual(send_push_update(m, &cid, msg14, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST=
), 1);
+}
<=
span style=3D"color: hsl(120, 100%, 40%);">+
#undef PUSH_B=
UNDLE_SIZE_TEST
static int
cmocka_unit_test_setup_tear=
down(test_incoming_push_message_mix, setup, teardown),
=
cmocka_unit_test_setup_teardown(test_incoming_push_message_mix2, setup=
, teardown),
#ifdef ENABLE_MANAGEMENT
+
cmocka_unit_te=
st_setup_teardown(test_send_push_msg0, setup2, teardown2),
=
cmocka_unit_test_setup_teardown(test_send_push_msg1, setup2, teard=
own2),
cmocka_unit_test_setup_teardown(test_send_p=
ush_msg2, setup2, teardown2),
@@ -545,7 +648,11 @@ cmocka_unit_test_setup_teardown(test_send_push_msg7, setup=
2, teardown2),
cmocka_unit_test_setup_teardown(tes=
t_send_push_msg8, setup2, teardown2),
cmocka_unit_=
test_setup_teardown(test_send_push_msg9, setup2, teardown2),
+ cmocka_unit_test_setup_teardown(test_send_=
push_msg10, setup2, teardown2),
+ cmocka_unit_test_setup_teardown(test_send_push_msg11, s=
etup2, teardown2),
+ =
cmocka_unit_test_setup_teardown(test_send_push_msg12, setup2, teardo=
wn2),
+ cmocka=
_unit_test_setup_teardown(test_send_push_msg13, setup2, teardown2),<=
br>+ cmocka_unit_test_se=
tup_teardown(test_send_push_msg14, setup2, teardown2)
#end=
if
};
To view, visit change 1316. To unsubscribe, or for help writing mail filt= ers, visit setti= ngs.