From: Alan T. DeKok Date: Thu, 24 Oct 2019 13:57:17 +0000 (-0400) Subject: check for fixed-size fields X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9fa01002178644fe0a6fbe19a99f71d174c574cc;p=thirdparty%2Ffreeradius-server.git check for fixed-size fields --- diff --git a/src/lib/util/struct.c b/src/lib/util/struct.c index cc7d6bd9a91..4bd40ed7bff 100644 --- a/src/lib/util/struct.c +++ b/src/lib/util/struct.c @@ -271,10 +271,30 @@ ssize_t fr_struct_to_network(uint8_t *out, size_t outlen, } /* - * Determine the nested type and call the appropriate encoder + * Encode fixed-size octets fields so that they + * are exactly the fixed size, UNLESS the entire + * output is truncated. */ - len = fr_value_box_to_network(NULL, p, outlen, &vp->data); - if (len <= 0) return -1; + if ((vp->da->type == FR_TYPE_OCTETS) && vp->da->flags.length) { + size_t mylen = vp->da->flags.length; + + if (mylen > outlen) mylen = outlen; + + if (vp->vp_length < mylen) { + memcpy(p, vp->vp_ptr, vp->vp_length); + memset(p + vp->vp_length, 0, mylen - vp->vp_length); + } else { + memcpy(p, vp->vp_ptr, mylen); + } + len = mylen; + + } else { + /* + * Determine the nested type and call the appropriate encoder + */ + len = fr_value_box_to_network(NULL, p, outlen, &vp->data); + if (len <= 0) return -1; + } if (child->flags.extra) { key_da = child; diff --git a/src/tests/unit/dhcpv6.txt b/src/tests/unit/dhcpv6.txt index 7588f808fce..a49a9a6cb6d 100644 --- a/src/tests/unit/dhcpv6.txt +++ b/src/tests/unit/dhcpv6.txt @@ -179,5 +179,17 @@ match 00 21 00 22 03 77 77 77 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 03 66 74 70 encode-pair BCMCS-Server-Domain-Name-List = "www.example.com", BCMCS-Server-Domain-Name-List = "ftp.example.com", BCMCS-Server-Domain-Name-List = "ns.example.com" match 00 21 00 32 03 77 77 77 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 03 66 74 70 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 02 6e 73 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 +encode-pair Client-ID-DUID = Client-ID-DUID-UUID, Client-ID-DUID-UUID-Value = 0x000102030405060708090a0b0c0d0e0f +match 00 01 00 12 00 04 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f + +# +# UUID-Value field is 16 octets. So we truncate if if it's too long, and pad it with zeros if it's too short. +# +encode-pair Client-ID-DUID = Client-ID-DUID-UUID, Client-ID-DUID-UUID-Value = 0x000102030405060708090a0b0c0d0e0f1112 +match 00 01 00 12 00 04 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f + +encode-pair Client-ID-DUID = Client-ID-DUID-UUID, Client-ID-DUID-UUID-Value = 0x000102030405060708090a0b0c0d +match 00 01 00 12 00 04 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 00 00 + count -match 80 +match 86