Attention is currently required from: Arne Schwabe.

Hello Arne Schwabe,

I'd like you to do a code review.
Please visit

    https://gerrit.openvpn.net/c/openvpn/+/2395?usp=email

to review the following change.


Change subject: probe: Move the probe TLV rules out of the generic TLV layer
......................................................................

probe: Move the probe TLV rules out of the generic TLV layer

ctrl_msg_find_tlv() walked a payload for one TLV type and rejected the
message on a second TLV of that type or on any TLV it did not understand
that was not marked optional. Both rules belong to the probe messages,
not to the TLV framing: the specification allows several TLVs of a type
unless that type forbids it, and the probe request and probe reply are
the ones that forbid it. A later message whose TLVs may repeat could not
have used the helper.

Move the walk into probe.c as probe_tlv_find(), where the rules it
applies are the rules of the messages it parses, and drop the generic
helper. ctrl_msg_tlv_next() remains the TLV layer's way of walking a
payload, which is what a message carrying several TLVs it knows about
will want.

No behaviour change: the tests that covered the rules now reach them
through probe_request_find(), and the ones that only needed a TLV value
decoded take it from ctrl_msg_tlv_next().

Change-Id: I00032bd06b32bbc18bce092e2ebd1ec0c5c7b907
Signed-off-by: Lev Stipakov <[email protected]>
---
M src/openvpn/control_msg.c
M src/openvpn/control_msg.h
M src/openvpn/probe.c
M src/openvpn/probe.h
M tests/unit_tests/openvpn/test_pkt.c
M tests/unit_tests/openvpn/test_probe.c
M tests/unit_tests/openvpn/test_probe.h
7 files changed, 87 insertions(+), 101 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/95/2395/1

diff --git a/src/openvpn/control_msg.c b/src/openvpn/control_msg.c
index a77413f..e14355c 100644
--- a/src/openvpn/control_msg.c
+++ b/src/openvpn/control_msg.c
@@ -91,32 +91,3 @@
     buf_set_read(value, v, hdr->value_len);
     return true;
 }
-
-bool
-ctrl_msg_find_tlv(struct buffer *payload, uint16_t wanted_type, struct buffer 
*value)
-{
-    bool found = false;
-    while (BLEN(payload) > 0)
-    {
-        struct ctrl_msg_tlv_header hdr;
-        struct buffer v;
-        if (!ctrl_msg_tlv_next(payload, &hdr, &v))
-        {
-            return false;
-        }
-        if (hdr.type == wanted_type)
-        {
-            if (found)
-            {
-                return false; /* the TLV may occur only once */
-            }
-            *value = v;
-            found = true;
-        }
-        else if (!hdr.optional)
-        {
-            return false; /* a mandatory TLV we do not understand, wherever it 
sits */
-        }
-    }
-    return found;
-}
diff --git a/src/openvpn/control_msg.h b/src/openvpn/control_msg.h
index bf3525f..307d1f0 100644
--- a/src/openvpn/control_msg.h
+++ b/src/openvpn/control_msg.h
@@ -107,32 +107,4 @@
  */
 bool ctrl_msg_tlv_next(struct buffer *buf, struct ctrl_msg_tlv_header *hdr, 
struct buffer *value);

-/**
- * Scan payload for the TLV of type wanted_type, skipping any other TLV type
- * that is marked optional.
- *
- * This is for messages that carry exactly one mandatory TLV, which is what the
- * probe messages are. Any other TLV that is not marked optional invalidates
- * the message wherever it appears, as does a second TLV of wanted_type, so the
- * whole sequence is walked even once the wanted TLV has been found. A message
- * carrying several mandatory TLVs walks them with ctrl_msg_tlv_next() instead.
- *
- * The optional flag is consulted only for the types the caller did not ask
- * for: it says whether a TLV may be ignored when its type is not understood,
- * so it carries nothing for wanted_type, which is matched on the type alone.
- *
- * On success value covers exactly the found TLV's value bytes; as in
- * ctrl_msg_tlv_next(), a header claiming more bytes than payload holds is
- * rejected rather than reported as found. payload is consumed as it is read.
- *
- * @param payload      buffer positioned at a TLV header
- * @param wanted_type  the TLV type to look for
- * @param value        set to a buffer covering the found TLV's value; it 
points
- *                     into payload and owns no storage
- * @return true if the TLV was found, false if it is absent or present more
- *         than once, if a TLV header or value is malformed or truncated, or if
- *         a TLV we do not understand is not marked optional.
- */
-bool ctrl_msg_find_tlv(struct buffer *payload, uint16_t wanted_type, struct 
buffer *value);
-
 #endif /* ifndef CONTROL_MSG_H */
diff --git a/src/openvpn/probe.c b/src/openvpn/probe.c
index d7211be..f63b142 100644
--- a/src/openvpn/probe.c
+++ b/src/openvpn/probe.c
@@ -93,11 +93,55 @@
     return true;
 }

+/* Walk the TLVs of a received message and return the value of the one of
+ * wanted_type. Both probe TLVs may appear only once in a CONTROL_V2 message --
+ * the specification says so for the request to rule out amplification, and for
+ * the reply because it answers a single request -- so a second one rejects the
+ * message, as does any TLV we do not understand that is not marked optional,
+ * wherever it sits. Hence the whole sequence is walked even once the wanted
+ * TLV has been found.
+ *
+ * These are rules of the probe messages, not of the TLV framing: the
+ * specification allows repeats of a type unless that type forbids them, so a
+ * message whose TLVs do not is parsed with ctrl_msg_tlv_next() directly.
+ */
+static bool
+probe_tlv_find(struct buffer *payload, uint16_t wanted_type, struct buffer 
*value)
+{
+    bool found = false;
+    while (BLEN(payload) > 0)
+    {
+        struct ctrl_msg_tlv_header hdr;
+        struct buffer v;
+        if (!ctrl_msg_tlv_next(payload, &hdr, &v))
+        {
+            return false;
+        }
+        if (hdr.type == wanted_type)
+        {
+            if (found)
+            {
+                return false;
+            }
+            *value = v;
+            found = true;
+        }
+        else if (!hdr.optional)
+        {
+            /* The optional flag is only consulted here, for a type we did not
+             * ask for: it says whether a TLV may be ignored when its type is
+             * not understood, so it carries nothing for wanted_type. */
+            return false;
+        }
+    }
+    return found;
+}
+
 bool
 probe_request_find(struct buffer *payload, struct probe_request *req)
 {
     struct buffer value;
-    return ctrl_msg_find_tlv(payload, TLV_TYPE_PROBE_REQUEST, &value)
+    return probe_tlv_find(payload, TLV_TYPE_PROBE_REQUEST, &value)
            && probe_request_read(&value, req);
 }

@@ -105,7 +149,7 @@
 probe_reply_find(struct buffer *payload, struct probe_reply *reply)
 {
     struct buffer value;
-    return ctrl_msg_find_tlv(payload, TLV_TYPE_PROBE_REPLY, &value)
+    return probe_tlv_find(payload, TLV_TYPE_PROBE_REPLY, &value)
            && probe_reply_read(&value, reply);
 }

diff --git a/src/openvpn/probe.h b/src/openvpn/probe.h
index cf600fc..ec15021 100644
--- a/src/openvpn/probe.h
+++ b/src/openvpn/probe.h
@@ -107,7 +107,7 @@
 /**
  * Read a probe request TLV value from buf.
  *
- * buf must cover exactly the TLV's value, as ctrl_msg_find_tlv() returns it.
+ * buf must cover exactly the TLV's value, as ctrl_msg_tlv_next() returns it.
  * Bytes beyond the fields understood here are ignored, so a longer value from
  * a future version still parses. Flag bits that are not known are kept in
  * flags rather than rejected, for the same reason.
@@ -143,8 +143,8 @@
  * message and parse it.
  *
  * A TLV of another type is skipped if it is marked optional and rejects the
- * whole message if it is not, as ctrl_msg_find_tlv() defines. payload is
- * consumed as it is read.
+ * whole message if it is not, and a second probe request rejects it too.
+ * payload is consumed as it is read.
  *
  * @param payload  the message payload, positioned at the first TLV
  * @param req      filled with the probe request on success
diff --git a/tests/unit_tests/openvpn/test_pkt.c 
b/tests/unit_tests/openvpn/test_pkt.c
index dc5589f..0be96c7 100644
--- a/tests/unit_tests/openvpn/test_pkt.c
+++ b/tests/unit_tests/openvpn/test_pkt.c
@@ -968,12 +968,12 @@
         cmocka_unit_test(test_probe_request_too_short),
         cmocka_unit_test(test_probe_reply_too_short),
         cmocka_unit_test(test_probe_reply_flags_contradict),
-        cmocka_unit_test(test_find_tlv_value_truncated),
-        cmocka_unit_test(test_find_tlv_trailing_bytes),
-        cmocka_unit_test(test_find_tlv_skips_optional_after_wanted),
-        cmocka_unit_test(test_find_tlv_rejects_unknown_mandatory),
-        cmocka_unit_test(test_find_tlv_rejects_duplicate),
-        cmocka_unit_test(test_find_tlv_empty_payload),
+        cmocka_unit_test(test_probe_find_value_truncated),
+        cmocka_unit_test(test_probe_find_trailing_bytes),
+        cmocka_unit_test(test_probe_find_skips_optional_after_wanted),
+        cmocka_unit_test(test_probe_find_rejects_unknown_mandatory),
+        cmocka_unit_test(test_probe_find_rejects_duplicate),
+        cmocka_unit_test(test_probe_find_empty_payload),
         cmocka_unit_test(test_probe_request_find),
         cmocka_unit_test(test_probe_request_find_skips_unknown),
         cmocka_unit_test(test_probe_request_find_rejects_unknown_mandatory),
diff --git a/tests/unit_tests/openvpn/test_probe.c 
b/tests/unit_tests/openvpn/test_probe.c
index 335f8d6..5cccbc7 100644
--- a/tests/unit_tests/openvpn/test_probe.c
+++ b/tests/unit_tests/openvpn/test_probe.c
@@ -59,7 +59,7 @@
     assert_int_equal(hdr.value_len, PROBE_REQUEST_LEN);

     struct buffer value;
-    assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
+    assert_true(ctrl_msg_tlv_next(&buf, &hdr, &value));
     assert_int_equal(BLEN(&value), PROBE_REQUEST_LEN);

     struct probe_request out = { 0 };
@@ -129,7 +129,7 @@
     assert_int_equal(hdr.value_len, PROBE_REPLY_LEN);

     struct buffer value;
-    assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REPLY, &value));
+    assert_true(ctrl_msg_tlv_next(&buf, &hdr, &value));

     struct probe_reply out = { 0 };
     assert_true(probe_reply_read(&value, &out));
@@ -198,8 +198,9 @@
     assert_true(buf_write_u32(&buf, 0x80000000));    /* flags, unknown bit set 
*/
     assert_true(buf_write_u32(&buf, 0x11223344));    /* unknown trailing field 
*/

+    struct ctrl_msg_tlv_header hdr;
     struct buffer value;
-    assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
+    assert_true(ctrl_msg_tlv_next(&buf, &hdr, &value));
     assert_int_equal(BLEN(&value), extended_len);

     struct probe_request out = { 0 };
@@ -277,8 +278,9 @@
         const struct probe_reply in = { .request_id = 1, .flags = flags };
         assert_true(probe_reply_write(&buf, &in));

+        struct ctrl_msg_tlv_header hdr;
         struct buffer value;
-        assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REPLY, &value));
+        assert_true(ctrl_msg_tlv_next(&buf, &hdr, &value));

         struct probe_reply out = { 0 };
         assert_int_equal(probe_reply_read(&value, &out), expect_ok);
@@ -291,23 +293,23 @@
  * rejected by the scan rather than reported as found -- for the TLV being
  * looked for as much as for one that would merely be skipped. */
 void
-test_find_tlv_value_truncated(void **state)
+test_probe_find_value_truncated(void **state)
 {
     struct gc_arena gc = gc_new();
-    struct buffer value;
+    struct probe_request out = { 0 };

     /* the wanted TLV declares 20 value bytes, only 8 are present */
     struct buffer buf = alloc_buf_gc(128, &gc);
     assert_true(ctrl_msg_tlv_write_header(&buf, TLV_TYPE_PROBE_REQUEST, false, 
PROBE_REQUEST_LEN));
     assert_true(buf_write_u64(&buf, 0xdeadbeefULL));
-    assert_false(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
+    assert_false(probe_request_find(&buf, &out));

     /* same defect on a TLV that would be skipped: the scan must not walk past
      * the end of the payload looking for the next header */
     struct buffer buf2 = alloc_buf_gc(128, &gc);
     assert_true(ctrl_msg_tlv_write_header(&buf2, 0x7ff, false, 64));
     assert_true(buf_write_u32(&buf2, 0));
-    assert_false(ctrl_msg_find_tlv(&buf2, TLV_TYPE_PROBE_REQUEST, &value));
+    assert_false(probe_request_find(&buf2, &out));

     gc_free(&gc);
 }
@@ -315,10 +317,10 @@
 /* Bytes left over after the last TLV that are too few for a header make the
  * message malformed, even when the wanted TLV was already found. */
 void
-test_find_tlv_trailing_bytes(void **state)
+test_probe_find_trailing_bytes(void **state)
 {
     struct gc_arena gc = gc_new();
-    struct buffer value;
+    struct probe_request out = { 0 };

     /* 1 to 3 bytes: less than a 4-byte TLV header */
     for (int trailing = 1; trailing < CTRL_MSG_TLV_HEADER_LEN; trailing++)
@@ -330,7 +332,7 @@
         {
             assert_true(buf_write_u8(&buf, 0));
         }
-        assert_false(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
+        assert_false(probe_request_find(&buf, &out));
     }

     gc_free(&gc);
@@ -339,21 +341,18 @@
 /* An unknown TLV marked optional after the wanted one is skipped, and the
  * wanted value is still the one returned. */
 void
-test_find_tlv_skips_optional_after_wanted(void **state)
+test_probe_find_skips_optional_after_wanted(void **state)
 {
     struct gc_arena gc = gc_new();
     struct buffer buf = alloc_buf_gc(128, &gc);
-    struct buffer value;

     const struct probe_request in = { .request_id = 1, .timestamp = 7 };
     assert_true(probe_request_write(&buf, &in));
     assert_true(ctrl_msg_tlv_write_header(&buf, 0x7ff, true, 4));
     assert_true(buf_write_u32(&buf, 0));

-    assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
-    assert_int_equal(BLEN(&value), PROBE_REQUEST_LEN);
     struct probe_request out = { 0 };
-    assert_true(probe_request_read(&value, &out));
+    assert_true(probe_request_find(&buf, &out));
     assert_true(out.timestamp == 7);

     gc_free(&gc);
@@ -363,10 +362,10 @@
  * sender requires us to act on, so the scan rejects the message whether that
  * TLV comes before or after the wanted one. */
 void
-test_find_tlv_rejects_unknown_mandatory(void **state)
+test_probe_find_rejects_unknown_mandatory(void **state)
 {
     struct gc_arena gc = gc_new();
-    struct buffer value;
+    struct probe_request out = { 0 };
     const struct probe_request in = { .request_id = 1, .timestamp = 7 };

     struct buffer before = alloc_buf_gc(128, &gc);
@@ -379,8 +378,8 @@
     assert_true(ctrl_msg_tlv_write_header(&after, 0x7ff, false, 4));
     assert_true(buf_write_u32(&after, 0xcafef00d));

-    assert_false(ctrl_msg_find_tlv(&before, TLV_TYPE_PROBE_REQUEST, &value));
-    assert_false(ctrl_msg_find_tlv(&after, TLV_TYPE_PROBE_REQUEST, &value));
+    assert_false(probe_request_find(&before, &out));
+    assert_false(probe_request_find(&after, &out));

     gc_free(&gc);
 }
@@ -388,16 +387,16 @@
 /* The wanted TLV may occur only once: a second one rejects the message. 
Repeats
  * of an unknown TLV marked optional are skipped like any other. */
 void
-test_find_tlv_rejects_duplicate(void **state)
+test_probe_find_rejects_duplicate(void **state)
 {
     struct gc_arena gc = gc_new();
-    struct buffer value;
+    struct probe_request out = { 0 };
     const struct probe_request in = { .request_id = 1 };

     struct buffer buf = alloc_buf_gc(128, &gc);
     assert_true(probe_request_write(&buf, &in));
     assert_true(probe_request_write(&buf, &in));
-    assert_false(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
+    assert_false(probe_request_find(&buf, &out));

     buf = alloc_buf_gc(128, &gc);
     for (int i = 0; i < 2; i++)
@@ -406,20 +405,20 @@
         assert_true(buf_write_u32(&buf, 0));
     }
     assert_true(probe_request_write(&buf, &in));
-    assert_true(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
+    assert_true(probe_request_find(&buf, &out));

     gc_free(&gc);
 }

 /* An empty payload holds no TLV. */
 void
-test_find_tlv_empty_payload(void **state)
+test_probe_find_empty_payload(void **state)
 {
     struct gc_arena gc = gc_new();
     struct buffer buf = alloc_buf_gc(16, &gc);
-    struct buffer value;
+    struct probe_request out = { 0 };

-    assert_false(ctrl_msg_find_tlv(&buf, TLV_TYPE_PROBE_REQUEST, &value));
+    assert_false(probe_request_find(&buf, &out));

     gc_free(&gc);
 }
diff --git a/tests/unit_tests/openvpn/test_probe.h 
b/tests/unit_tests/openvpn/test_probe.h
index ee61754..04f661e 100644
--- a/tests/unit_tests/openvpn/test_probe.h
+++ b/tests/unit_tests/openvpn/test_probe.h
@@ -39,17 +39,17 @@

 void test_probe_reply_flags_contradict(void **state);

-void test_find_tlv_value_truncated(void **state);
+void test_probe_find_value_truncated(void **state);

-void test_find_tlv_trailing_bytes(void **state);
+void test_probe_find_trailing_bytes(void **state);

-void test_find_tlv_skips_optional_after_wanted(void **state);
+void test_probe_find_skips_optional_after_wanted(void **state);

-void test_find_tlv_rejects_unknown_mandatory(void **state);
+void test_probe_find_rejects_unknown_mandatory(void **state);

-void test_find_tlv_rejects_duplicate(void **state);
+void test_probe_find_rejects_duplicate(void **state);

-void test_find_tlv_empty_payload(void **state);
+void test_probe_find_empty_payload(void **state);

 void test_probe_request_find(void **state);


--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2395?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I00032bd06b32bbc18bce092e2ebd1ec0c5c7b907
Gerrit-Change-Number: 2395
Gerrit-PatchSet: 1
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to