lists.osmocom.org
Sign In Sign Up
Manage this list Sign In Sign Up

Keyboard Shortcuts

Thread View

  • j: Next unread message
  • k: Previous unread message
  • j a: Jump to all threads
  • j l: Jump to MailingList overview

gerrit-log

Thread Start a new thread
Download
Threads by month
  • ----- 2026 -----
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2025 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2024 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2023 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2022 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
gerrit-log@lists.osmocom.org

August 2022

  • 3 participants
  • 2402 discussions
Change in pysim[master]: Add new pySim.ota library, implement SIM OTA crypto
by laforge 11 Aug '22

11 Aug '22
Attention is currently required from: laforge, pespin, Christian Amsüss. Hello Jenkins Builder, pespin, Christian Amsüss, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/c/pysim/+/29033 to look at the new patch set (#4). Change subject: Add new pySim.ota library, implement SIM OTA crypto ...................................................................... Add new pySim.ota library, implement SIM OTA crypto This introduces a hierarchy of classes implementing * ETS TS 102 225 (general command structure) * 3GPP TS 31.115 (dialects for SMS-PP) In this initial patch only the SMS "dialect" is supported, but it is foreseen that USSD/SMSCB/HTTPS dialects can be added at a later point. Change-Id: I193ff4712c8503279c017b4b1324f0c3d38b9f84 --- M contrib/jenkins.sh A pySim/ota.py A pySim/sms.py M requirements.txt M setup.py A tests/test_ota.py 6 files changed, 593 insertions(+), 1 deletion(-) git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/33/29033/4 -- To view, visit https://gerrit.osmocom.org/c/pysim/+/29033 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: pysim Gerrit-Branch: master Gerrit-Change-Id: I193ff4712c8503279c017b4b1324f0c3d38b9f84 Gerrit-Change-Number: 29033 Gerrit-PatchSet: 4 Gerrit-Owner: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: Christian Amsüss <chrysn(a)fsfe.org> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: laforge <laforge(a)osmocom.org> Gerrit-Attention: pespin <pespin(a)sysmocom.de> Gerrit-Attention: Christian Amsüss <chrysn(a)fsfe.org> Gerrit-MessageType: newpatchset
1 0
0 0
Change in libosmo-pfcp[master]: gtlv: check memory bounds 1/3: encoding TLV
by pespin 11 Aug '22

11 Aug '22
Attention is currently required from: neels. pespin has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmo-pfcp/+/29039 ) Change subject: gtlv: check memory bounds 1/3: encoding TLV ...................................................................... Patch Set 1: Code-Review+1 (3 comments) File src/libosmo-gtlv/gtlv_dec_enc.c: https://gerrit.osmocom.org/c/libosmo-pfcp/+/29039/comment/10eacd5c_e5fed231 PS1, Line 52: /* Reverse offsetof(): return the address of the struct member for a given osmo_gtlv_msg and member ofs_foo value. */ "ofs_foo" you mean memb_ofs here? https://gerrit.osmocom.org/c/libosmo-pfcp/+/29039/comment/bea708fc_aa20fdf5 PS1, Line 55: const char *p = parent; this extra p = parent here is only making thing smore difficult to understand. Just use parent or pass const uint8_t *parent. In any case s/char/uint8_t/ here, otherwise looks more like a string. https://gerrit.osmocom.org/c/libosmo-pfcp/+/29039/comment/5c449a51_ea4c686c PS1, Line 390: int osmo_gtlvs_encode(struct osmo_gtlv_put *gtlv, const void *decoded_struct, size_t decoded_struct_max_len, "max_len" afaict can be simply named "size", since it's the size of the decoded struct? -- To view, visit https://gerrit.osmocom.org/c/libosmo-pfcp/+/29039 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-pfcp Gerrit-Branch: master Gerrit-Change-Id: Id8d997c9d5e655ff1842ec69eab6c073875c6330 Gerrit-Change-Number: 29039 Gerrit-PatchSet: 1 Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: neels <nhofmeyr(a)sysmocom.de> Gerrit-Comment-Date: Thu, 11 Aug 2022 18:45:34 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: Yes Gerrit-MessageType: comment
1 0
0 0
Change in libosmo-pfcp[master]: pfcp_cp_peer: check use count correctness
by pespin 11 Aug '22

11 Aug '22
Attention is currently required from: neels. pespin has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmo-pfcp/+/29038 ) Change subject: pfcp_cp_peer: check use count correctness ...................................................................... Patch Set 1: Code-Review+1 -- To view, visit https://gerrit.osmocom.org/c/libosmo-pfcp/+/29038 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-pfcp Gerrit-Branch: master Gerrit-Change-Id: Id79a84312b3ff8d562e26a525866b8bb09f9d0bf Gerrit-Change-Number: 29038 Gerrit-PatchSet: 1 Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: neels <nhofmeyr(a)sysmocom.de> Gerrit-Comment-Date: Thu, 11 Aug 2022 18:38:33 +0000 Gerrit-HasComments: No Gerrit-Has-Labels: Yes Gerrit-MessageType: comment
1 0
0 0
Change in libosmo-netif[master]: rtp: Avoid memcpy(len=0)
by pespin 11 Aug '22

11 Aug '22
pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmo-netif/+/29044 ) Change subject: rtp: Avoid memcpy(len=0) ...................................................................... rtp: Avoid memcpy(len=0) Change-Id: I7618c6509b67465d21271ea632bccc8cf11e4852 --- M src/rtp.c 1 file changed, 2 insertions(+), 1 deletion(-) git pull ssh://gerrit.osmocom.org:29418/libosmo-netif refs/changes/44/29044/1 diff --git a/src/rtp.c b/src/rtp.c index 5c06c42..f4b0ada 100644 --- a/src/rtp.c +++ b/src/rtp.c @@ -199,7 +199,8 @@ rtph->timestamp = htonl(h->tx.timestamp); h->tx.timestamp += duration; rtph->ssrc = htonl(h->tx.ssrc); - memcpy(msg->data + sizeof(struct rtp_hdr), data, payload_len); + if (payload_len > 0) + memcpy(msg->data + sizeof(struct rtp_hdr), data, payload_len); msgb_put(msg, sizeof(struct rtp_hdr) + payload_len); return msg; -- To view, visit https://gerrit.osmocom.org/c/libosmo-netif/+/29044 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-netif Gerrit-Branch: master Gerrit-Change-Id: I7618c6509b67465d21271ea632bccc8cf11e4852 Gerrit-Change-Number: 29044 Gerrit-PatchSet: 1 Gerrit-Owner: pespin <pespin(a)sysmocom.de> Gerrit-MessageType: newchange
1 0
0 0
Change in libosmo-pfcp[master]: doc: minor fix in pfcp_cp_peer_fsm.dot
by pespin 11 Aug '22

11 Aug '22
Attention is currently required from: neels. pespin has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmo-pfcp/+/29037 ) Change subject: doc: minor fix in pfcp_cp_peer_fsm.dot ...................................................................... Patch Set 1: Code-Review+1 -- To view, visit https://gerrit.osmocom.org/c/libosmo-pfcp/+/29037 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-pfcp Gerrit-Branch: master Gerrit-Change-Id: I0de10d5331df128081d6b875e3ba9c0c3c32bd9f Gerrit-Change-Number: 29037 Gerrit-PatchSet: 1 Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: neels <nhofmeyr(a)sysmocom.de> Gerrit-Comment-Date: Thu, 11 Aug 2022 17:47:50 +0000 Gerrit-HasComments: No Gerrit-Has-Labels: Yes Gerrit-MessageType: comment
1 0
0 0
Change in pysim[master]: Add new pySim.ota library, implement SIM OTA crypto
by laforge 11 Aug '22

11 Aug '22
Attention is currently required from: laforge, pespin, Christian Amsüss. Hello Jenkins Builder, pespin, Christian Amsüss, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/c/pysim/+/29033 to look at the new patch set (#3). Change subject: Add new pySim.ota library, implement SIM OTA crypto ...................................................................... Add new pySim.ota library, implement SIM OTA crypto This introduces a hierarchy of classes implementing * ETS TS 102 225 (general command structure) * 3GPP TS 31.115 (dialects for SMS-PP) In this initial patch only the SMS "dialect" is supported, but it is foreseen that USSD/SMSCB/HTTPS dialects can be added at a later point. Change-Id: I193ff4712c8503279c017b4b1324f0c3d38b9f84 --- M contrib/jenkins.sh A pySim/ota.py A pySim/sms.py M requirements.txt M setup.py A tests/test_ota.py 6 files changed, 593 insertions(+), 1 deletion(-) git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/33/29033/3 -- To view, visit https://gerrit.osmocom.org/c/pysim/+/29033 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: pysim Gerrit-Branch: master Gerrit-Change-Id: I193ff4712c8503279c017b4b1324f0c3d38b9f84 Gerrit-Change-Number: 29033 Gerrit-PatchSet: 3 Gerrit-Owner: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: Christian Amsüss <chrysn(a)fsfe.org> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: laforge <laforge(a)osmocom.org> Gerrit-Attention: pespin <pespin(a)sysmocom.de> Gerrit-Attention: Christian Amsüss <chrysn(a)fsfe.org> Gerrit-MessageType: newpatchset
1 0
0 0
Change in libosmo-pfcp[master]: gtlv: fix repeated IEIs to several struct members
by neels 11 Aug '22

11 Aug '22
neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmo-pfcp/+/29042 ) Change subject: gtlv: fix repeated IEIs to several struct members ...................................................................... gtlv: fix repeated IEIs to several struct members Coverity Scan has brought my attention to a problem with decoding repeated IEIs, where there are multiple struct members in the decoded struct that these are decoded to. Before this patch, gtlv aborts with an error as soon as the first struct member for a given tag is full, not parsing following IEIs into subsequent struct members. After this patch, gtlv continues to look whether subsequent entries in the message coding also decode the same tag, but to a different struct member. First commit without changing the gtlv regression test, to show that all current tests still succeed. The test for this particular issue follow in I994d0fb1f1435d2c27a8630a43fe106652ac6e41 Related: CID#275415 Related: SYS#5599 Change-Id: Ie37585178ff27306d425b75d8e407b71f92f1cdc --- M src/libosmo-gtlv/gtlv_dec_enc.c 1 file changed, 70 insertions(+), 20 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmo-pfcp refs/changes/42/29042/1 diff --git a/src/libosmo-gtlv/gtlv_dec_enc.c b/src/libosmo-gtlv/gtlv_dec_enc.c index 7e8d524..37b92a9 100644 --- a/src/libosmo-gtlv/gtlv_dec_enc.c +++ b/src/libosmo-gtlv/gtlv_dec_enc.c @@ -123,16 +123,19 @@ * that can store them are filled up. */ ie_max_allowed_count = 0; + /* Unordered: for every tag, look for matching IE definitions from the start. */ + iec = ie_coding; + /* There may be multiple subsequent occurences of the same tag, to be decoded into multiple members of + * the decoded struct. Iterate until encountering a still "free slot" for decoding. */ do { /* Find the IE coding for this tag */ - for (iec = ie_coding; - !osmo_gtlv_coding_end(iec) && osmo_gtlv_tag_inst_cmp(&iec->ti, &gtlv->ti); + for (;!osmo_gtlv_coding_end(iec) && osmo_gtlv_tag_inst_cmp(&iec->ti, &gtlv->ti); iec++); /* No such IE coding found. */ if (osmo_gtlv_coding_end(iec)) break; - /* Keep track how often this tag can occur */ + /* Keep track how often this tag can occur in total */ ie_max_allowed_count += iec->has_count ? iec->count_max : 1; /* Was this iec instance already decoded? Then skip to the next one, if any. */ @@ -141,23 +144,35 @@ multi_count_p = iec->has_count ? membof(obj, obj_maxlen, iec->count_ofs) : NULL; if ((presence_flag_p && *presence_flag_p) - || (multi_count_p && *multi_count_p >= iec->count_max)) + || (multi_count_p && *multi_count_p >= iec->count_max)) { + iec++; continue; + } + /* For IEs with a presence flag or a multi count, the decoded struct provides the information * whether the IE has already been decoded. Do the same for mandatory IEs, using local state in * seen_ie_coding_entries[]. */ - CHECK_SEEN(iec); - if (*seen_p) - continue; - } while (0); + if (!presence_flag_p && !multi_count_p) { + CHECK_SEEN(iec); + if (*seen_p) { + iec++; + continue; + } + } + + /* Found an IE coding that has not yet been decoded / still has unused struct members to decode + * to. */ + break; + } while (1); + if (osmo_gtlv_coding_end(iec)) { if (ie_max_allowed_count) { - /* There have been IE definitions for this IEI, but all slots to decode it are already - * filled. */ + /* This tag exists in the protocol definitions, but we've run out of struct members to + * decode the tag to. */ RETURN_ERROR(-ENOTSUP, gtlv->ti, "Only %u instances of this IE are supported per message", ie_max_allowed_count); } - /* No such IE defined in ie_coding, just skip the TLV. */ + /* This is an unknown tag, no such IEI defined in ie_coding, just skip the TLV. */ continue; } @@ -260,14 +275,22 @@ osmo_gtlv_load_start(gtlv); + /* Ordered: traverse the ie_coding exactly once, expecting tags to be decoded to appear in the same order. */ for (; !osmo_gtlv_coding_end(ie_coding); ie_coding++) { int rc; - bool *presence_flag = ie_coding->has_presence_flag ? - membof(obj, obj_maxlen, ie_coding->presence_flag_ofs) : NULL; - unsigned int *multi_count = ie_coding->has_count ? - membof(obj, obj_maxlen, ie_coding->count_ofs) : NULL; + bool *presence_flag; + unsigned int *multi_count; struct osmo_gtlv_tag_inst peek_ti; +#define UPDATE_STATE_FROM_IE_CODING() do { \ + presence_flag = ie_coding->has_presence_flag ? \ + membof(obj, obj_maxlen, ie_coding->presence_flag_ofs) : NULL; \ + multi_count = ie_coding->has_count ? \ + membof(obj, obj_maxlen, ie_coding->count_ofs) : NULL; \ + } while (0) + + UPDATE_STATE_FROM_IE_CODING(); + rc = osmo_gtlv_load_next_by_tag_inst(gtlv, &ie_coding->ti); switch (rc) { case 0: @@ -285,12 +308,39 @@ for (;;) { /* If this is a repeated IE, decode into the correct array index memb[idx], * next idx == (*multi_count) */ - unsigned int memb_next_array_idx = multi_count ? *multi_count : 0; - unsigned int memb_ofs = ie_coding->memb_ofs + memb_next_array_idx * ie_coding->memb_array_pitch; + unsigned int memb_next_array_idx; + unsigned int memb_ofs; - if (multi_count && memb_next_array_idx >= ie_coding->count_max) - RETURN_ERROR(-ENOTSUP, ie_coding->ti, "Only %u instances of this IE are supported per message", - ie_coding->count_max); +#define UPDATE_MEMB_IDX() do { \ + memb_next_array_idx = multi_count ? *multi_count : 0; \ + memb_ofs = ie_coding->memb_ofs + memb_next_array_idx * ie_coding->memb_array_pitch; \ + } while (0) + + UPDATE_MEMB_IDX(); + + if (multi_count && memb_next_array_idx >= ie_coding->count_max) { + /* This repeated IE is full. But the protocol may define this same IEI to follow again, + * with a different target struct member. See if this IEI appears again. */ + const struct osmo_gtlv_coding *iec = ie_coding; + bool iei_appears_again = false; + for (iec = ie_coding + 1; !osmo_gtlv_coding_end(iec); iec++) { + if (osmo_gtlv_tag_inst_cmp(&gtlv->ti, &iec->ti) == 0) { + iei_appears_again = true; + break; + } + } + + if (!iei_appears_again) + RETURN_ERROR(-ENOTSUP, ie_coding->ti, "Only %u instances of this IE are supported per message", + ie_coding->count_max); + + /* IEI does appear again. Skip forward to this ie_coding, and use that to decode the + * current TLV (do not call osmo_gtlv_load_next_by_tag_inst() again, or we'd skip the + * TLV in the message being decoded). */ + ie_coding = iec; + UPDATE_STATE_FROM_IE_CODING(); + UPDATE_MEMB_IDX(); + } /* Decode IE value part */ if (ie_coding->nested_ies) { -- To view, visit https://gerrit.osmocom.org/c/libosmo-pfcp/+/29042 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-pfcp Gerrit-Branch: master Gerrit-Change-Id: Ie37585178ff27306d425b75d8e407b71f92f1cdc Gerrit-Change-Number: 29042 Gerrit-PatchSet: 1 Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de> Gerrit-MessageType: newchange
1 0
0 0
Change in libosmo-pfcp[master]: gtlv: test repeated IEIs to several struct members
by neels 11 Aug '22

11 Aug '22
neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmo-pfcp/+/29043 ) Change subject: gtlv: test repeated IEIs to several struct members ...................................................................... gtlv: test repeated IEIs to several struct members Test the behavior fixed by Ie37585178ff27306d425b75d8e407b71f92f1cdc Related: CID#275415 Related: SYS#5599 Change-Id: I994d0fb1f1435d2c27a8630a43fe106652ac6e41 --- M tests/libosmo-gtlv/gtlv_dec_enc_test.c M tests/libosmo-gtlv/gtlv_dec_enc_test.ok 2 files changed, 380 insertions(+), 47 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmo-pfcp refs/changes/43/29043/1 diff --git a/tests/libosmo-gtlv/gtlv_dec_enc_test.c b/tests/libosmo-gtlv/gtlv_dec_enc_test.c index f0de7b0..848c0ef 100644 --- a/tests/libosmo-gtlv/gtlv_dec_enc_test.c +++ b/tests/libosmo-gtlv/gtlv_dec_enc_test.c @@ -94,7 +94,13 @@ struct baz baz; unsigned int repeat_int_count; - int repeat_int[32]; + int repeat_int[3]; + + unsigned int repeat_int2_count; + int repeat_int2[2]; + + bool repeat_int3_present; + unsigned int repeat_int3; unsigned int repeat_struct_count; struct repeat repeat_struct[32]; @@ -264,6 +270,26 @@ .count_max = ARRAY_SIZE(((struct decoded_msg *)0)->repeat_int), }, { + .ti = { TAG_REPEAT_INT }, + .dec_func = dec_u16, + .enc_func = enc_u16, + .enc_to_str_func = enc_to_str_u16, + .memb_ofs = offsetof(struct decoded_msg, repeat_int2), + .memb_array_pitch = OSMO_MEMB_ARRAY_PITCH(struct decoded_msg, repeat_int2), + .has_count = true, + .count_ofs = offsetof(struct decoded_msg, repeat_int2_count), + .count_max = ARRAY_SIZE(((struct decoded_msg *)0)->repeat_int2), + }, + { + .ti = { TAG_REPEAT_INT }, + .dec_func = dec_u16, + .enc_func = enc_u16, + .enc_to_str_func = enc_to_str_u16, + .memb_ofs = offsetof(struct decoded_msg, repeat_int3), + .has_presence_flag = true, + .presence_flag_ofs = offsetof(struct decoded_msg, repeat_int3_present), + }, + { .ti = { TAG_REPEAT_STRUCT }, .dec_func = dec_repeat_struct, .enc_func = enc_repeat_struct, @@ -284,11 +310,88 @@ {} }; -char *decoded_msg_to_str(const struct decoded_msg *m) -{ - return osmo_gtlvs_encode_to_str_c(ctx, m, sizeof(*m), 0, msg_ie_coding, tag_names); -} +/* Same as msg_ie_coding, but with different ordering of the REPEAT_INT IEIs: in msg_ie_coding, the three separate + * REPEAT_INT tags follow directly after each other, while in msg_ie_coding2, other tags appear in-between. */ +struct osmo_gtlv_coding msg_ie_coding2[] = { + { + .ti = { TAG_FOO }, + .dec_func = dec_u16, + .enc_func = enc_u16, + .enc_to_str_func = enc_to_str_u16, + .memb_ofs = offsetof(struct decoded_msg, foo), + }, + { + .ti = { TAG_BAR }, + .dec_func = dec_bar, + .enc_func = enc_bar, + .enc_to_str_func = enc_to_str_bar, + .memb_ofs = offsetof(struct decoded_msg, bar), + }, + { + .ti = { TAG_BAZ }, + .dec_func = dec_baz, + .enc_func = enc_baz, + .enc_to_str_func = enc_to_str_baz, + .memb_ofs = offsetof(struct decoded_msg, baz), + .has_presence_flag = true, + .presence_flag_ofs = offsetof(struct decoded_msg, baz_present), + }, + { + .ti = { TAG_REPEAT_INT }, + .dec_func = dec_u16, + .enc_func = enc_u16, + .enc_to_str_func = enc_to_str_u16, + .memb_ofs = offsetof(struct decoded_msg, repeat_int), + .memb_array_pitch = OSMO_MEMB_ARRAY_PITCH(struct decoded_msg, repeat_int), + .has_count = true, + .count_ofs = offsetof(struct decoded_msg, repeat_int_count), + .count_max = ARRAY_SIZE(((struct decoded_msg *)0)->repeat_int), + }, + { + .ti = { TAG_REPEAT_STRUCT }, + .dec_func = dec_repeat_struct, + .enc_func = enc_repeat_struct, + .enc_to_str_func = enc_to_str_repeat_struct, + .memb_ofs = offsetof(struct decoded_msg, repeat_struct), + .memb_array_pitch = OSMO_MEMB_ARRAY_PITCH(struct decoded_msg, repeat_struct), + .has_count = true, + .count_ofs = offsetof(struct decoded_msg, repeat_struct_count), + .count_max = ARRAY_SIZE(((struct decoded_msg *)0)->repeat_struct), + }, + { + .ti = { TAG_REPEAT_INT }, + .dec_func = dec_u16, + .enc_func = enc_u16, + .enc_to_str_func = enc_to_str_u16, + .memb_ofs = offsetof(struct decoded_msg, repeat_int2), + .memb_array_pitch = OSMO_MEMB_ARRAY_PITCH(struct decoded_msg, repeat_int2), + .has_count = true, + .count_ofs = offsetof(struct decoded_msg, repeat_int2_count), + .count_max = ARRAY_SIZE(((struct decoded_msg *)0)->repeat_int2), + }, + { + .ti = { TAG_NEST }, + .memb_ofs = offsetof(struct decoded_msg, nest), + .nested_ies = nested_inner_msg_ies, + .has_presence_flag = true, + .presence_flag_ofs = offsetof(struct decoded_msg, nest_present), + }, + { + .ti = { TAG_REPEAT_INT }, + .dec_func = dec_u16, + .enc_func = enc_u16, + .enc_to_str_func = enc_to_str_u16, + .memb_ofs = offsetof(struct decoded_msg, repeat_int3), + .has_presence_flag = true, + .presence_flag_ofs = offsetof(struct decoded_msg, repeat_int3_present), + }, + {} +}; +char *decoded_msg_to_str(const struct decoded_msg *m, const struct osmo_gtlv_coding *iec) +{ + return osmo_gtlvs_encode_to_str_c(ctx, m, sizeof(*m), 0, iec, tag_names); +} const struct decoded_msg enc_dec_tests[] = { { @@ -322,6 +425,38 @@ .foo = 23, .bar = { "twentythree" }, + .repeat_int_count = 3, + .repeat_int = { 1, 2, 0x7fff }, + + .repeat_struct_count = 1, + .repeat_struct = { + { + .v_int = 1001, + .v_bool = true, + .v_enum = R_A, + }, + }, + + .repeat_int2_count = 2, + .repeat_int2 = { 23, 42 }, + + .nest_present = true, + .nest = { + .foo = 42, + .bar = { "fortytwo" }, + .baz = { + .v_int = 4242, + .v_bool = false, + }, + }, + + .repeat_int3_present = true, + .repeat_int3 = 423, + }, + { + .foo = 23, + .bar = { "twentythree" }, + .baz_present = true, .baz = { .v_int = 2323, @@ -370,7 +505,7 @@ va_end(args); } -void test_enc_dec(const char *label, const struct osmo_gtlv_cfg *cfg, bool ordered) +void test_enc_dec(const char *label, const struct osmo_gtlv_cfg *cfg, const struct osmo_gtlv_coding *iec, bool ordered) { int i; for (i = 0; i < ARRAY_SIZE(enc_dec_tests); i++) { @@ -381,13 +516,13 @@ struct osmo_gtlv_put put; printf("\n=== start %s %s[%d]\n", label, __func__, i); - printf("encoded: %s\n", decoded_msg_to_str(orig)); + printf("encoded: %s\n", decoded_msg_to_str(orig, iec)); put = (struct osmo_gtlv_put){ .cfg = cfg, .dst = msgb_alloc(1024, __func__), }; - rc = osmo_gtlvs_encode(&put, (void *)orig, sizeof(*orig), 0, msg_ie_coding, + rc = osmo_gtlvs_encode(&put, (void *)orig, sizeof(*orig), 0, iec, err_cb, &verify_err_cb_data, tag_names); printf("osmo_gtlvs_encode() rc = %d\n", rc); printf("%s.\n", osmo_hexdump(put.dst->data, put.dst->len)); @@ -396,11 +531,11 @@ .cfg = cfg, .src = { put.dst->data, put.dst->len }, }; - rc = osmo_gtlvs_decode(&parsed, sizeof(parsed), 0, &load, ordered, msg_ie_coding, + rc = osmo_gtlvs_decode(&parsed, sizeof(parsed), 0, &load, ordered, iec, err_cb, &verify_err_cb_data, tag_names); printf("osmo_gtlvs_decode() rc = %d\n", rc); - printf("decoded: %s\n", decoded_msg_to_str(&parsed)); - if (strcmp(decoded_msg_to_str(orig), decoded_msg_to_str(&parsed))) { + printf("decoded: %s\n", decoded_msg_to_str(&parsed, iec)); + if (strcmp(decoded_msg_to_str(orig, iec), decoded_msg_to_str(&parsed, iec))) { printf(" ERROR: parsed != orig\n"); exit(1); } @@ -413,11 +548,17 @@ ctx = talloc_named_const(NULL, 0, "gtlv_test"); msgb_talloc_ctx_init(ctx, 0); - test_enc_dec("t8l8v ordered", &osmo_t8l8v_cfg, true); - test_enc_dec("t8l8v unordered", &osmo_t8l8v_cfg, false); + test_enc_dec("1: t8l8v ordered", &osmo_t8l8v_cfg, msg_ie_coding, true); + test_enc_dec("1: t8l8v unordered", &osmo_t8l8v_cfg, msg_ie_coding, false); - test_enc_dec("t16l16v ordered", &osmo_t16l16v_cfg, true); - test_enc_dec("t16l16v unordered", &osmo_t16l16v_cfg, false); + test_enc_dec("1: t16l16v ordered", &osmo_t16l16v_cfg, msg_ie_coding, true); + test_enc_dec("1: t16l16v unordered", &osmo_t16l16v_cfg, msg_ie_coding, false); + + test_enc_dec("2: t8l8v ordered", &osmo_t8l8v_cfg, msg_ie_coding2, true); + test_enc_dec("2: t8l8v unordered", &osmo_t8l8v_cfg, msg_ie_coding2, false); + + test_enc_dec("2: t16l16v ordered", &osmo_t16l16v_cfg, msg_ie_coding2, true); + test_enc_dec("2: t16l16v unordered", &osmo_t16l16v_cfg, msg_ie_coding2, false); talloc_free(ctx); return 0; diff --git a/tests/libosmo-gtlv/gtlv_dec_enc_test.ok b/tests/libosmo-gtlv/gtlv_dec_enc_test.ok index bd6df52..b6a2d7f 100644 --- a/tests/libosmo-gtlv/gtlv_dec_enc_test.ok +++ b/tests/libosmo-gtlv/gtlv_dec_enc_test.ok @@ -1,128 +1,320 @@ -=== start t8l8v ordered test_enc_dec[0] +=== start 1: t8l8v ordered test_enc_dec[0] encoded: 'FOO'=23 'BAR'="twentythree" osmo_gtlvs_encode() rc = 0 01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" -=== end t8l8v ordered test_enc_dec[0] +=== end 1: t8l8v ordered test_enc_dec[0] -=== start t8l8v ordered test_enc_dec[1] +=== start 1: t8l8v ordered test_enc_dec[1] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} osmo_gtlvs_encode() rc = 0 01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} -=== end t8l8v ordered test_enc_dec[1] +=== end 1: t8l8v ordered test_enc_dec[1] -=== start t8l8v ordered test_enc_dec[2] +=== start 1: t8l8v ordered test_enc_dec[2] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } osmo_gtlvs_encode() rc = 0 01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 04 02 00 01 04 02 00 02 04 02 7f ff . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } -=== end t8l8v ordered test_enc_dec[2] +=== end 1: t8l8v ordered test_enc_dec[2] -=== start t8l8v ordered test_enc_dec[3] +=== start 1: t8l8v ordered test_enc_dec[3] +encoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_INT'={ 23, 42 } 'REPEAT_INT'=423 'REPEAT_STRUCT'={ {1001,true,R_A} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 04 02 00 01 04 02 00 02 04 02 7f ff 04 02 00 17 04 02 00 2a 04 02 01 a7 05 03 03 e9 80 06 12 01 02 00 2a 02 08 66 6f 72 74 79 74 77 6f 03 02 10 92 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_INT'={ 23, 42 } 'REPEAT_INT'=423 'REPEAT_STRUCT'={ {1001,true,R_A} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +=== end 1: t8l8v ordered test_enc_dec[3] + +=== start 1: t8l8v ordered test_enc_dec[4] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } osmo_gtlvs_encode() rc = 0 01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 04 02 00 01 04 02 00 02 04 02 7f ff 05 03 03 e9 80 05 03 03 ea 01 06 12 01 02 00 2a 02 08 66 6f 72 74 79 74 77 6f 03 02 10 92 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } -=== end t8l8v ordered test_enc_dec[3] +=== end 1: t8l8v ordered test_enc_dec[4] -=== start t8l8v unordered test_enc_dec[0] +=== start 1: t8l8v unordered test_enc_dec[0] encoded: 'FOO'=23 'BAR'="twentythree" osmo_gtlvs_encode() rc = 0 01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" -=== end t8l8v unordered test_enc_dec[0] +=== end 1: t8l8v unordered test_enc_dec[0] -=== start t8l8v unordered test_enc_dec[1] +=== start 1: t8l8v unordered test_enc_dec[1] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} osmo_gtlvs_encode() rc = 0 01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} -=== end t8l8v unordered test_enc_dec[1] +=== end 1: t8l8v unordered test_enc_dec[1] -=== start t8l8v unordered test_enc_dec[2] +=== start 1: t8l8v unordered test_enc_dec[2] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } osmo_gtlvs_encode() rc = 0 01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 04 02 00 01 04 02 00 02 04 02 7f ff . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } -=== end t8l8v unordered test_enc_dec[2] +=== end 1: t8l8v unordered test_enc_dec[2] -=== start t8l8v unordered test_enc_dec[3] +=== start 1: t8l8v unordered test_enc_dec[3] +encoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_INT'={ 23, 42 } 'REPEAT_INT'=423 'REPEAT_STRUCT'={ {1001,true,R_A} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 04 02 00 01 04 02 00 02 04 02 7f ff 04 02 00 17 04 02 00 2a 04 02 01 a7 05 03 03 e9 80 06 12 01 02 00 2a 02 08 66 6f 72 74 79 74 77 6f 03 02 10 92 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_INT'={ 23, 42 } 'REPEAT_INT'=423 'REPEAT_STRUCT'={ {1001,true,R_A} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +=== end 1: t8l8v unordered test_enc_dec[3] + +=== start 1: t8l8v unordered test_enc_dec[4] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } osmo_gtlvs_encode() rc = 0 01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 04 02 00 01 04 02 00 02 04 02 7f ff 05 03 03 e9 80 05 03 03 ea 01 06 12 01 02 00 2a 02 08 66 6f 72 74 79 74 77 6f 03 02 10 92 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } -=== end t8l8v unordered test_enc_dec[3] +=== end 1: t8l8v unordered test_enc_dec[4] -=== start t16l16v ordered test_enc_dec[0] +=== start 1: t16l16v ordered test_enc_dec[0] encoded: 'FOO'=23 'BAR'="twentythree" osmo_gtlvs_encode() rc = 0 00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" -=== end t16l16v ordered test_enc_dec[0] +=== end 1: t16l16v ordered test_enc_dec[0] -=== start t16l16v ordered test_enc_dec[1] +=== start 1: t16l16v ordered test_enc_dec[1] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} osmo_gtlvs_encode() rc = 0 00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} -=== end t16l16v ordered test_enc_dec[1] +=== end 1: t16l16v ordered test_enc_dec[1] -=== start t16l16v ordered test_enc_dec[2] +=== start 1: t16l16v ordered test_enc_dec[2] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } osmo_gtlvs_encode() rc = 0 00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } -=== end t16l16v ordered test_enc_dec[2] +=== end 1: t16l16v ordered test_enc_dec[2] -=== start t16l16v ordered test_enc_dec[3] +=== start 1: t16l16v ordered test_enc_dec[3] +encoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_INT'={ 23, 42 } 'REPEAT_INT'=423 'REPEAT_STRUCT'={ {1001,true,R_A} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff 00 04 00 02 00 17 00 04 00 02 00 2a 00 04 00 02 01 a7 00 05 00 03 03 e9 80 00 06 00 18 00 01 00 02 00 2a 00 02 00 08 66 6f 72 74 79 74 77 6f 00 03 00 02 10 92 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_INT'={ 23, 42 } 'REPEAT_INT'=423 'REPEAT_STRUCT'={ {1001,true,R_A} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +=== end 1: t16l16v ordered test_enc_dec[3] + +=== start 1: t16l16v ordered test_enc_dec[4] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } osmo_gtlvs_encode() rc = 0 00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff 00 05 00 03 03 e9 80 00 05 00 03 03 ea 01 00 06 00 18 00 01 00 02 00 2a 00 02 00 08 66 6f 72 74 79 74 77 6f 00 03 00 02 10 92 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } -=== end t16l16v ordered test_enc_dec[3] +=== end 1: t16l16v ordered test_enc_dec[4] -=== start t16l16v unordered test_enc_dec[0] +=== start 1: t16l16v unordered test_enc_dec[0] encoded: 'FOO'=23 'BAR'="twentythree" osmo_gtlvs_encode() rc = 0 00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" -=== end t16l16v unordered test_enc_dec[0] +=== end 1: t16l16v unordered test_enc_dec[0] -=== start t16l16v unordered test_enc_dec[1] +=== start 1: t16l16v unordered test_enc_dec[1] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} osmo_gtlvs_encode() rc = 0 00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} -=== end t16l16v unordered test_enc_dec[1] +=== end 1: t16l16v unordered test_enc_dec[1] -=== start t16l16v unordered test_enc_dec[2] +=== start 1: t16l16v unordered test_enc_dec[2] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } osmo_gtlvs_encode() rc = 0 00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } -=== end t16l16v unordered test_enc_dec[2] +=== end 1: t16l16v unordered test_enc_dec[2] -=== start t16l16v unordered test_enc_dec[3] +=== start 1: t16l16v unordered test_enc_dec[3] +encoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_INT'={ 23, 42 } 'REPEAT_INT'=423 'REPEAT_STRUCT'={ {1001,true,R_A} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff 00 04 00 02 00 17 00 04 00 02 00 2a 00 04 00 02 01 a7 00 05 00 03 03 e9 80 00 06 00 18 00 01 00 02 00 2a 00 02 00 08 66 6f 72 74 79 74 77 6f 00 03 00 02 10 92 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_INT'={ 23, 42 } 'REPEAT_INT'=423 'REPEAT_STRUCT'={ {1001,true,R_A} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +=== end 1: t16l16v unordered test_enc_dec[3] + +=== start 1: t16l16v unordered test_enc_dec[4] encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } osmo_gtlvs_encode() rc = 0 00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff 00 05 00 03 03 e9 80 00 05 00 03 03 ea 01 00 06 00 18 00 01 00 02 00 2a 00 02 00 08 66 6f 72 74 79 74 77 6f 00 03 00 02 10 92 . osmo_gtlvs_decode() rc = 0 decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } -=== end t16l16v unordered test_enc_dec[3] +=== end 1: t16l16v unordered test_enc_dec[4] + +=== start 2: t8l8v ordered test_enc_dec[0] +encoded: 'FOO'=23 'BAR'="twentythree" +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" +=== end 2: t8l8v ordered test_enc_dec[0] + +=== start 2: t8l8v ordered test_enc_dec[1] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} +=== end 2: t8l8v ordered test_enc_dec[1] + +=== start 2: t8l8v ordered test_enc_dec[2] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 04 02 00 01 04 02 00 02 04 02 7f ff . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } +=== end 2: t8l8v ordered test_enc_dec[2] + +=== start 2: t8l8v ordered test_enc_dec[3] +encoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A} } 'REPEAT_INT'={ 23, 42 } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } 'REPEAT_INT'=423 +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 04 02 00 01 04 02 00 02 04 02 7f ff 05 03 03 e9 80 04 02 00 17 04 02 00 2a 06 12 01 02 00 2a 02 08 66 6f 72 74 79 74 77 6f 03 02 10 92 04 02 01 a7 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A} } 'REPEAT_INT'={ 23, 42 } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } 'REPEAT_INT'=423 +=== end 2: t8l8v ordered test_enc_dec[3] + +=== start 2: t8l8v ordered test_enc_dec[4] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 04 02 00 01 04 02 00 02 04 02 7f ff 05 03 03 e9 80 05 03 03 ea 01 06 12 01 02 00 2a 02 08 66 6f 72 74 79 74 77 6f 03 02 10 92 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +=== end 2: t8l8v ordered test_enc_dec[4] + +=== start 2: t8l8v unordered test_enc_dec[0] +encoded: 'FOO'=23 'BAR'="twentythree" +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" +=== end 2: t8l8v unordered test_enc_dec[0] + +=== start 2: t8l8v unordered test_enc_dec[1] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} +=== end 2: t8l8v unordered test_enc_dec[1] + +=== start 2: t8l8v unordered test_enc_dec[2] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 04 02 00 01 04 02 00 02 04 02 7f ff . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } +=== end 2: t8l8v unordered test_enc_dec[2] + +=== start 2: t8l8v unordered test_enc_dec[3] +encoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A} } 'REPEAT_INT'={ 23, 42 } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } 'REPEAT_INT'=423 +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 04 02 00 01 04 02 00 02 04 02 7f ff 05 03 03 e9 80 04 02 00 17 04 02 00 2a 06 12 01 02 00 2a 02 08 66 6f 72 74 79 74 77 6f 03 02 10 92 04 02 01 a7 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A} } 'REPEAT_INT'={ 23, 42 } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } 'REPEAT_INT'=423 +=== end 2: t8l8v unordered test_enc_dec[3] + +=== start 2: t8l8v unordered test_enc_dec[4] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +osmo_gtlvs_encode() rc = 0 +01 02 00 17 02 0b 74 77 65 6e 74 79 74 68 72 65 65 03 02 89 13 04 02 00 01 04 02 00 02 04 02 7f ff 05 03 03 e9 80 05 03 03 ea 01 06 12 01 02 00 2a 02 08 66 6f 72 74 79 74 77 6f 03 02 10 92 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +=== end 2: t8l8v unordered test_enc_dec[4] + +=== start 2: t16l16v ordered test_enc_dec[0] +encoded: 'FOO'=23 'BAR'="twentythree" +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" +=== end 2: t16l16v ordered test_enc_dec[0] + +=== start 2: t16l16v ordered test_enc_dec[1] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} +=== end 2: t16l16v ordered test_enc_dec[1] + +=== start 2: t16l16v ordered test_enc_dec[2] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } +=== end 2: t16l16v ordered test_enc_dec[2] + +=== start 2: t16l16v ordered test_enc_dec[3] +encoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A} } 'REPEAT_INT'={ 23, 42 } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } 'REPEAT_INT'=423 +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff 00 05 00 03 03 e9 80 00 04 00 02 00 17 00 04 00 02 00 2a 00 06 00 18 00 01 00 02 00 2a 00 02 00 08 66 6f 72 74 79 74 77 6f 00 03 00 02 10 92 00 04 00 02 01 a7 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A} } 'REPEAT_INT'={ 23, 42 } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } 'REPEAT_INT'=423 +=== end 2: t16l16v ordered test_enc_dec[3] + +=== start 2: t16l16v ordered test_enc_dec[4] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff 00 05 00 03 03 e9 80 00 05 00 03 03 ea 01 00 06 00 18 00 01 00 02 00 2a 00 02 00 08 66 6f 72 74 79 74 77 6f 00 03 00 02 10 92 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +=== end 2: t16l16v ordered test_enc_dec[4] + +=== start 2: t16l16v unordered test_enc_dec[0] +encoded: 'FOO'=23 'BAR'="twentythree" +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" +=== end 2: t16l16v unordered test_enc_dec[0] + +=== start 2: t16l16v unordered test_enc_dec[1] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} +=== end 2: t16l16v unordered test_enc_dec[1] + +=== start 2: t16l16v unordered test_enc_dec[2] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } +=== end 2: t16l16v unordered test_enc_dec[2] + +=== start 2: t16l16v unordered test_enc_dec[3] +encoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A} } 'REPEAT_INT'={ 23, 42 } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } 'REPEAT_INT'=423 +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff 00 05 00 03 03 e9 80 00 04 00 02 00 17 00 04 00 02 00 2a 00 06 00 18 00 01 00 02 00 2a 00 02 00 08 66 6f 72 74 79 74 77 6f 00 03 00 02 10 92 00 04 00 02 01 a7 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A} } 'REPEAT_INT'={ 23, 42 } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } 'REPEAT_INT'=423 +=== end 2: t16l16v unordered test_enc_dec[3] + +=== start 2: t16l16v unordered test_enc_dec[4] +encoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +osmo_gtlvs_encode() rc = 0 +00 01 00 02 00 17 00 02 00 0b 74 77 65 6e 74 79 74 68 72 65 65 00 03 00 02 89 13 00 04 00 02 00 01 00 04 00 02 00 02 00 04 00 02 7f ff 00 05 00 03 03 e9 80 00 05 00 03 03 ea 01 00 06 00 18 00 01 00 02 00 2a 00 02 00 08 66 6f 72 74 79 74 77 6f 00 03 00 02 10 92 . +osmo_gtlvs_decode() rc = 0 +decoded: 'FOO'=23 'BAR'="twentythree" 'BAZ'={2323,true} 'REPEAT_INT'={ 1, 2, 32767 } 'REPEAT_STRUCT'={ {1001,true,R_A}, {1002,false,R_B} } 'NEST'={ 'FOO'=42 'BAR'="fortytwo" 'BAZ'={4242,false} } +=== end 2: t16l16v unordered test_enc_dec[4] -- To view, visit https://gerrit.osmocom.org/c/libosmo-pfcp/+/29043 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-pfcp Gerrit-Branch: master Gerrit-Change-Id: I994d0fb1f1435d2c27a8630a43fe106652ac6e41 Gerrit-Change-Number: 29043 Gerrit-PatchSet: 1 Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de> Gerrit-MessageType: newchange
1 0
0 0
Change in libosmo-pfcp[master]: gtlv: check memory bounds 1/3: encoding TLV
by neels 11 Aug '22

11 Aug '22
neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmo-pfcp/+/29039 ) Change subject: gtlv: check memory bounds 1/3: encoding TLV ...................................................................... gtlv: check memory bounds 1/3: encoding TLV Introduce a maximum bound of memory access to the osmo_gtlv API. Properly pass const-ness within the gtlv implementation. This patch adds membof_const(). The following patch will add the non-const membof() equivalent, which is not needed in this patch, yet. Coverity CID#275417 drew my attention to the fact that the gtlv decoding and encoding does not actually guard against access past the end of the decoded struct. We have not yet officially released libosmo-gtlv; also, osmo-upf and osmo-hnbgw so far only use the libosmo-pfcp API, which "hides" the gtlv API. Hence just change the API without a backwards compat shim. Related: CID#275417 Change-Id: Id8d997c9d5e655ff1842ec69eab6c073875c6330 --- M include/osmocom/gtlv/gtlv_dec_enc.h M src/libosmo-gtlv/gtlv_dec_enc.c M src/libosmo-gtlv/gtlv_gen.c M tests/libosmo-gtlv/gtlv_dec_enc_test.c 4 files changed, 29 insertions(+), 14 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmo-pfcp refs/changes/39/29039/1 diff --git a/include/osmocom/gtlv/gtlv_dec_enc.h b/include/osmocom/gtlv/gtlv_dec_enc.h index 132239f..df8d161 100644 --- a/include/osmocom/gtlv/gtlv_dec_enc.h +++ b/include/osmocom/gtlv/gtlv_dec_enc.h @@ -186,9 +186,9 @@ const struct osmo_gtlv_coding *ie_coding, osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs); -int osmo_gtlvs_encode(struct osmo_gtlv_put *gtlv, const void *decoded_struct, unsigned int obj_ofs, - const struct osmo_gtlv_coding *ie_coding, - osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs); +int osmo_gtlvs_encode(struct osmo_gtlv_put *gtlv, const void *decoded_struct, size_t decoded_struct_max_len, + unsigned int obj_ofs, const struct osmo_gtlv_coding *ie_coding, osmo_gtlv_err_cb err_cb, + void *err_cb_data, const struct value_string *iei_strs); int osmo_gtlvs_encode_to_str_buf(char *buf, size_t buflen, const void *decoded_struct, unsigned int obj_ofs, const struct osmo_gtlv_coding *ie_coding, const struct value_string *iei_strs); diff --git a/src/libosmo-gtlv/gtlv_dec_enc.c b/src/libosmo-gtlv/gtlv_dec_enc.c index 2e5509a..f60ea94 100644 --- a/src/libosmo-gtlv/gtlv_dec_enc.c +++ b/src/libosmo-gtlv/gtlv_dec_enc.c @@ -49,6 +49,15 @@ return RC; \ } while (0) +/* Reverse offsetof(): return the address of the struct member for a given osmo_gtlv_msg and member ofs_foo value. */ +static const void *membof_const(const void *parent, size_t parent_max_len, unsigned int memb_ofs) +{ + const char *p = parent; + const char *end = p + parent_max_len; + const char *memb = p + memb_ofs; + OSMO_ASSERT(memb < end); + return memb; +} /*! Decode a TLV structure from raw data to a decoded struct, for unordered TLV IEs. * How to decode IE values and where to place them in the decoded struct, is defined by ie_coding, an array terminated @@ -378,19 +387,22 @@ * \param[in] iei_strs value_string array to give IEI names in error messages passed to err_cb(), or NULL. * \return 0 on success, negative on error. */ -int osmo_gtlvs_encode(struct osmo_gtlv_put *gtlv, const void *decoded_struct, unsigned int obj_ofs, - const struct osmo_gtlv_coding *ie_coding, - osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs) +int osmo_gtlvs_encode(struct osmo_gtlv_put *gtlv, const void *decoded_struct, size_t decoded_struct_max_len, + unsigned int obj_ofs, const struct osmo_gtlv_coding *ie_coding, osmo_gtlv_err_cb err_cb, + void *err_cb_data, const struct value_string *iei_strs) { - void *obj = MEMB(decoded_struct, obj_ofs); + const void *obj = membof_const(decoded_struct, decoded_struct_max_len, obj_ofs); + size_t obj_maxlen = decoded_struct_max_len - obj_ofs; if (!ie_coding) return -ENOTSUP; for (; !osmo_gtlv_coding_end(ie_coding); ie_coding++) { int rc; - bool *presence_flag_p = ie_coding->has_presence_flag ? MEMB(obj, ie_coding->presence_flag_ofs) : NULL; - unsigned int *multi_count_p = ie_coding->has_count ? MEMB(obj, ie_coding->count_ofs) : NULL; + const bool *presence_flag_p = ie_coding->has_presence_flag ? + membof_const(obj, obj_maxlen, ie_coding->presence_flag_ofs) : NULL; + const unsigned int *multi_count_p = ie_coding->has_count ? + membof_const(obj, obj_maxlen, ie_coding->count_ofs) : NULL; unsigned int n; unsigned int i; @@ -423,13 +435,14 @@ .cfg = ie_coding->nested_ies_cfg ? : gtlv->cfg, .dst = gtlv->dst, }; - rc = osmo_gtlvs_encode(&nested_tlv, decoded_struct, obj_ofs + memb_ofs, - ie_coding->nested_ies, err_cb, err_cb_data, iei_strs); + rc = osmo_gtlvs_encode(&nested_tlv, decoded_struct, decoded_struct_max_len, + obj_ofs + memb_ofs, ie_coding->nested_ies, + err_cb, err_cb_data, iei_strs); if (rc) RETURN_ERROR(rc, ie_coding->ti, "Error while encoding TLV structure nested inside this IE"); } else { - rc = ie_coding->enc_func(gtlv, decoded_struct, MEMB(obj, memb_ofs)); + rc = ie_coding->enc_func(gtlv, decoded_struct, membof_const(obj, obj_maxlen, memb_ofs)); if (rc) RETURN_ERROR(rc, ie_coding->ti, "Error while encoding this IE"); } diff --git a/src/libosmo-gtlv/gtlv_gen.c b/src/libosmo-gtlv/gtlv_gen.c index fd3fbd9..9fe4b0c 100644 --- a/src/libosmo-gtlv/gtlv_gen.c +++ b/src/libosmo-gtlv/gtlv_gen.c @@ -388,7 +388,8 @@ "int %s_ies_encode(struct osmo_gtlv_put *gtlv, const union %s_ies *src,\n" " %s message_type, osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs)\n" "{\n" - " return osmo_gtlvs_encode(gtlv, src, 0, %s_get_msg_coding(message_type), err_cb, err_cb_data, iei_strs);\n" + " return osmo_gtlvs_encode(gtlv, src, sizeof(*src), 0, %s_get_msg_coding(message_type),\n" + " err_cb, err_cb_data, iei_strs);\n" "}\n", g_cfg->proto_name, g_cfg->proto_name, g_cfg->message_type_enum ? : "int", g_cfg->proto_name); printf("\n" diff --git a/tests/libosmo-gtlv/gtlv_dec_enc_test.c b/tests/libosmo-gtlv/gtlv_dec_enc_test.c index e02a2e5..63de266 100644 --- a/tests/libosmo-gtlv/gtlv_dec_enc_test.c +++ b/tests/libosmo-gtlv/gtlv_dec_enc_test.c @@ -387,7 +387,8 @@ .cfg = cfg, .dst = msgb_alloc(1024, __func__), }; - rc = osmo_gtlvs_encode(&put, (void *)orig, 0, msg_ie_coding, err_cb, &verify_err_cb_data, tag_names); + rc = osmo_gtlvs_encode(&put, (void *)orig, sizeof(*orig), 0, msg_ie_coding, + err_cb, &verify_err_cb_data, tag_names); printf("osmo_gtlvs_encode() rc = %d\n", rc); printf("%s.\n", osmo_hexdump(put.dst->data, put.dst->len)); -- To view, visit https://gerrit.osmocom.org/c/libosmo-pfcp/+/29039 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-pfcp Gerrit-Branch: master Gerrit-Change-Id: Id8d997c9d5e655ff1842ec69eab6c073875c6330 Gerrit-Change-Number: 29039 Gerrit-PatchSet: 1 Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de> Gerrit-MessageType: newchange
1 0
0 0
Change in libosmo-pfcp[master]: gtlv: check memory bounds 2/3: decoding TLV
by neels 11 Aug '22

11 Aug '22
neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmo-pfcp/+/29040 ) Change subject: gtlv: check memory bounds 2/3: decoding TLV ...................................................................... gtlv: check memory bounds 2/3: decoding TLV See Id8d997c9d5e655ff1842ec69eab6c073875c6330 Related: CID#275417 Change-Id: I841da89112ccf70fcd0f60eb902445fb1712eb48 --- M include/osmocom/gtlv/gtlv_dec_enc.h M src/libosmo-gtlv/gtlv_dec_enc.c M src/libosmo-gtlv/gtlv_gen.c M tests/libosmo-gtlv/gtlv_dec_enc_test.c 4 files changed, 50 insertions(+), 30 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmo-pfcp refs/changes/40/29040/1 diff --git a/include/osmocom/gtlv/gtlv_dec_enc.h b/include/osmocom/gtlv/gtlv_dec_enc.h index df8d161..243ad96 100644 --- a/include/osmocom/gtlv/gtlv_dec_enc.h +++ b/include/osmocom/gtlv/gtlv_dec_enc.h @@ -182,9 +182,10 @@ */ typedef void (*osmo_gtlv_err_cb)(void *data, void *decoded_struct, const char *file, int line, const char *fmt, ...); -int osmo_gtlvs_decode(void *decoded_struct, unsigned int obj_ofs, struct osmo_gtlv_load *gtlv, bool tlv_ordered, - const struct osmo_gtlv_coding *ie_coding, - osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs); +int osmo_gtlvs_decode(void *decoded_struct, size_t decoded_struct_max_len, + unsigned int obj_ofs, struct osmo_gtlv_load *gtlv, bool tlv_ordered, + const struct osmo_gtlv_coding *ie_coding, + osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs); int osmo_gtlvs_encode(struct osmo_gtlv_put *gtlv, const void *decoded_struct, size_t decoded_struct_max_len, unsigned int obj_ofs, const struct osmo_gtlv_coding *ie_coding, osmo_gtlv_err_cb err_cb, diff --git a/src/libosmo-gtlv/gtlv_dec_enc.c b/src/libosmo-gtlv/gtlv_dec_enc.c index f60ea94..e442baa 100644 --- a/src/libosmo-gtlv/gtlv_dec_enc.c +++ b/src/libosmo-gtlv/gtlv_dec_enc.c @@ -59,6 +59,11 @@ return memb; } +static void *membof(void *parent, size_t parent_max_len, unsigned int memb_ofs) +{ + return (void*)membof_const((const void *)parent, parent_max_len, memb_ofs); +} + /*! Decode a TLV structure from raw data to a decoded struct, for unordered TLV IEs. * How to decode IE values and where to place them in the decoded struct, is defined by ie_coding, an array terminated * by a '{}' entry. @@ -74,11 +79,13 @@ * \param[in] iei_strs value_string array to give IEI names in error messages passed to err_cb(), or NULL. * \return 0 on success, negative on error. */ -static int osmo_gtlvs_decode_unordered(void *decoded_struct, unsigned int obj_ofs, struct osmo_gtlv_load *gtlv, - const struct osmo_gtlv_coding *ie_coding, - osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs) +static int osmo_gtlvs_decode_unordered(void *decoded_struct, size_t decoded_struct_max_len, + unsigned int obj_ofs, struct osmo_gtlv_load *gtlv, + const struct osmo_gtlv_coding *ie_coding, + osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs) { - void *obj = MEMB(decoded_struct, obj_ofs); + void *obj = membof(decoded_struct, decoded_struct_max_len, obj_ofs); + size_t obj_maxlen = decoded_struct_max_len - obj_ofs; const struct osmo_gtlv_coding *iec; unsigned int *multi_count_p; @@ -132,8 +139,10 @@ ie_max_allowed_count += iec->has_count ? iec->count_max : 1; /* Was this iec instance already decoded? Then skip to the next one, if any. */ - presence_flag_p = iec->has_presence_flag ? MEMB(obj, iec->presence_flag_ofs) : NULL; - multi_count_p = iec->has_count ? MEMB(obj, iec->count_ofs) : NULL; + presence_flag_p = iec->has_presence_flag ? + membof(obj, obj_maxlen, iec->presence_flag_ofs) : NULL; + multi_count_p = iec->has_count ? + membof(obj, obj_maxlen, iec->count_ofs) : NULL; if ((presence_flag_p && *presence_flag_p) || (multi_count_p && *multi_count_p >= iec->count_max)) continue; @@ -183,15 +192,16 @@ default: OSMO_ASSERT(0); } - rc = osmo_gtlvs_decode(decoded_struct, obj_ofs + memb_ofs, &inner_tlv, ordered, iec->nested_ies, - err_cb, err_cb_data, iei_strs); + rc = osmo_gtlvs_decode(decoded_struct, decoded_struct_max_len, + obj_ofs + memb_ofs, &inner_tlv, ordered, iec->nested_ies, + err_cb, err_cb_data, iei_strs); if (rc) RETURN_ERROR(rc, gtlv->ti, "Error while decoding TLV structure nested inside this IE"); } else { /* Normal IE, decode the specific IE data. */ if (!iec->dec_func) RETURN_ERROR(-EIO, gtlv->ti, "IE definition lacks a dec_func()"); - rc = iec->dec_func(decoded_struct, MEMB(obj, memb_ofs), gtlv); + rc = iec->dec_func(decoded_struct, membof(obj, obj_maxlen, memb_ofs), gtlv); if (rc) RETURN_ERROR(rc, gtlv->ti, "Error while decoding this IE"); } @@ -214,7 +224,7 @@ for (iec = ie_coding; !osmo_gtlv_coding_end(iec); iec++) { if (iec->has_presence_flag) continue; - multi_count_p = iec->has_count ? MEMB(obj, iec->count_ofs) : NULL; + multi_count_p = iec->has_count ? membof(obj, obj_maxlen, iec->count_ofs) : NULL; if (multi_count_p) { if (*multi_count_p < iec->count_mandatory) RETURN_ERROR(-EINVAL, iec->ti, "%u instances of this IE are mandatory, got %u", @@ -243,18 +253,22 @@ * \param[in] iei_strs value_string array to give IEI names in error messages passed to err_cb(), or NULL. * \return 0 on success, negative on error. */ -static int osmo_gtlvs_decode_ordered(void *decoded_struct, unsigned int obj_ofs, struct osmo_gtlv_load *gtlv, - const struct osmo_gtlv_coding *ie_coding, - osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs) +static int osmo_gtlvs_decode_ordered(void *decoded_struct, size_t decoded_struct_max_len, + unsigned int obj_ofs, struct osmo_gtlv_load *gtlv, + const struct osmo_gtlv_coding *ie_coding, + osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs) { - void *obj = MEMB(decoded_struct, obj_ofs); + void *obj = membof(decoded_struct, decoded_struct_max_len, obj_ofs); + size_t obj_maxlen = decoded_struct_max_len - obj_ofs; osmo_gtlv_load_start(gtlv); for (; !osmo_gtlv_coding_end(ie_coding); ie_coding++) { int rc; - bool *presence_flag = ie_coding->has_presence_flag ? MEMB(obj, ie_coding->presence_flag_ofs) : NULL; - unsigned int *multi_count = ie_coding->has_count ? MEMB(obj, ie_coding->count_ofs) : NULL; + bool *presence_flag = ie_coding->has_presence_flag ? + membof(obj, obj_maxlen, ie_coding->presence_flag_ofs) : NULL; + unsigned int *multi_count = ie_coding->has_count ? + membof(obj, obj_maxlen, ie_coding->count_ofs) : NULL; struct osmo_gtlv_tag_inst peek_ti; rc = osmo_gtlv_load_next_by_tag_inst(gtlv, &ie_coding->ti); @@ -304,8 +318,9 @@ default: OSMO_ASSERT(0); } - rc = osmo_gtlvs_decode(decoded_struct, obj_ofs + memb_ofs, &inner_tlv, ordered, - ie_coding->nested_ies, err_cb, err_cb_data, iei_strs); + rc = osmo_gtlvs_decode(decoded_struct, decoded_struct_max_len, + obj_ofs + memb_ofs, &inner_tlv, ordered, ie_coding->nested_ies, + err_cb, err_cb_data, iei_strs); if (rc) RETURN_ERROR(rc, ie_coding->ti, "Error while decoding TLV structure nested inside this IE"); @@ -313,7 +328,7 @@ /* Normal IE, decode the specific IE data. */ if (!ie_coding->dec_func) RETURN_ERROR(-EIO, ie_coding->ti, "IE definition lacks a dec_func()"); - rc = ie_coding->dec_func(decoded_struct, MEMB(obj, memb_ofs), gtlv); + rc = ie_coding->dec_func(decoded_struct, membof(obj, obj_maxlen, memb_ofs), gtlv); if (rc) RETURN_ERROR(rc, ie_coding->ti, "Error while decoding this IE"); } @@ -361,17 +376,19 @@ * \param[in] iei_strs value_string array to give IEI names in error messages passed to err_cb(), or NULL. * \return 0 on success, negative on error. */ -int osmo_gtlvs_decode(void *decoded_struct, unsigned int obj_ofs, struct osmo_gtlv_load *gtlv, bool tlv_ordered, - const struct osmo_gtlv_coding *ie_coding, - osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs) +int osmo_gtlvs_decode(void *decoded_struct, size_t decoded_struct_max_len, + unsigned int obj_ofs, struct osmo_gtlv_load *gtlv, bool tlv_ordered, + const struct osmo_gtlv_coding *ie_coding, + osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs) { if (!ie_coding) return -ENOTSUP; if (tlv_ordered) - return osmo_gtlvs_decode_ordered(decoded_struct, obj_ofs, gtlv, ie_coding, err_cb, err_cb_data, iei_strs); + return osmo_gtlvs_decode_ordered(decoded_struct, decoded_struct_max_len, obj_ofs, gtlv, ie_coding, + err_cb, err_cb_data, iei_strs); else - return osmo_gtlvs_decode_unordered(decoded_struct, obj_ofs, gtlv, ie_coding, err_cb, err_cb_data, - iei_strs); + return osmo_gtlvs_decode_unordered(decoded_struct, decoded_struct_max_len, obj_ofs, gtlv, ie_coding, + err_cb, err_cb_data, iei_strs); } /*! Encode a TLV structure from decoded struct to raw data. diff --git a/src/libosmo-gtlv/gtlv_gen.c b/src/libosmo-gtlv/gtlv_gen.c index 9fe4b0c..3b2b1f0 100644 --- a/src/libosmo-gtlv/gtlv_gen.c +++ b/src/libosmo-gtlv/gtlv_gen.c @@ -381,7 +381,8 @@ " %s message_type,\n" " osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs)\n" "{\n" - " return osmo_gtlvs_decode(dst, 0, gtlv, tlv_ordered, %s_get_msg_coding(message_type), err_cb, err_cb_data, iei_strs);\n" + " return osmo_gtlvs_decode(dst, sizeof(*dst), 0, gtlv, tlv_ordered, %s_get_msg_coding(message_type),\n" + " err_cb, err_cb_data, iei_strs);\n" "}\n", g_cfg->proto_name, g_cfg->proto_name, g_cfg->message_type_enum ? : "int", g_cfg->proto_name); printf("\n" diff --git a/tests/libosmo-gtlv/gtlv_dec_enc_test.c b/tests/libosmo-gtlv/gtlv_dec_enc_test.c index 63de266..a542cfa 100644 --- a/tests/libosmo-gtlv/gtlv_dec_enc_test.c +++ b/tests/libosmo-gtlv/gtlv_dec_enc_test.c @@ -396,7 +396,8 @@ .cfg = cfg, .src = { put.dst->data, put.dst->len }, }; - rc = osmo_gtlvs_decode(&parsed, 0, &load, ordered, msg_ie_coding, err_cb, &verify_err_cb_data, tag_names); + rc = osmo_gtlvs_decode(&parsed, sizeof(parsed), 0, &load, ordered, msg_ie_coding, + err_cb, &verify_err_cb_data, tag_names); printf("osmo_gtlvs_decode() rc = %d\n", rc); printf("decoded: %s\n", decoded_msg_to_str(&parsed)); if (strcmp(decoded_msg_to_str(orig), decoded_msg_to_str(&parsed))) { -- To view, visit https://gerrit.osmocom.org/c/libosmo-pfcp/+/29040 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-pfcp Gerrit-Branch: master Gerrit-Change-Id: I841da89112ccf70fcd0f60eb902445fb1712eb48 Gerrit-Change-Number: 29040 Gerrit-PatchSet: 1 Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de> Gerrit-MessageType: newchange
1 0
0 0
  • ← Newer
  • 1
  • ...
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • ...
  • 241
  • Older →

HyperKitty Powered by HyperKitty version 1.3.12.