From: Alan T. DeKok Date: Sat, 10 Apr 2021 18:50:16 +0000 (-0400) Subject: add (and use) "length" to fr_pair_list_afrom_str() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b35b77203272bdbd7e21daded65d0a1d08dbd23d;p=thirdparty%2Ffreeradius-server.git add (and use) "length" to fr_pair_list_afrom_str() ideally the input should either be sbuffs or valud_boxes, but this is at least a bit of a better start --- diff --git a/src/bin/radsniff.c b/src/bin/radsniff.c index b2018ea5a1d..c99e70380c4 100644 --- a/src/bin/radsniff.c +++ b/src/bin/radsniff.c @@ -2031,7 +2031,7 @@ static int rs_build_filter(fr_pair_list_t *out, char const *filter) fr_pair_t *vp; fr_token_t code; - code = fr_pair_list_afrom_str(conf, dict_radius, filter, out); + code = fr_pair_list_afrom_str(conf, dict_radius, filter, strlen(filter), out); if (code == T_INVALID) { fr_perror("Invalid RADIUS filter \"%s\"", filter); return -1; diff --git a/src/bin/unit_test_attribute.c b/src/bin/unit_test_attribute.c index 589b4cd162c..4259ade001a 100644 --- a/src/bin/unit_test_attribute.c +++ b/src/bin/unit_test_attribute.c @@ -1035,14 +1035,14 @@ static size_t command_allow_unresolved(command_result_t *result, command_file_ct * */ static size_t command_normalise_attribute(command_result_t *result, command_file_ctx_t *cc, - char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen) + char *data, UNUSED size_t data_used, char *in, size_t inlen) { fr_pair_list_t head; ssize_t slen; fr_pair_list_init(&head); - if (fr_pair_list_afrom_str(NULL, cc->tmpl_rules.dict_def ? cc->tmpl_rules.dict_def : cc->config->dict, in, &head) != T_EOL) { + if (fr_pair_list_afrom_str(NULL, cc->tmpl_rules.dict_def ? cc->tmpl_rules.dict_def : cc->config->dict, in, inlen, &head) != T_EOL) { RETURN_OK_WITH_ERROR(); } @@ -1587,7 +1587,7 @@ static size_t command_decode_dns_label(command_result_t *result, command_file_ct } static size_t command_encode_pair(command_result_t *result, command_file_ctx_t *cc, - char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen) + char *data, UNUSED size_t data_used, char *in, size_t inlen) { fr_test_point_pair_encode_t *tp = NULL; @@ -1637,7 +1637,7 @@ static size_t command_encode_pair(command_result_t *result, command_file_ctx_t * } if (fr_pair_list_afrom_str(cc->tmp_ctx, cc->tmpl_rules.dict_def ? cc->tmpl_rules.dict_def : cc->config->dict, - p, &head) != T_EOL) { + p, in + inlen - p, &head) != T_EOL) { CLEAR_TEST_POINT(cc); RETURN_OK_WITH_ERROR(); } @@ -1765,7 +1765,7 @@ static size_t command_returned(command_result_t *result, command_file_ctx_t *cc, } static size_t command_encode_proto(command_result_t *result, command_file_ctx_t *cc, - char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen) + char *data, UNUSED size_t data_used, char *in, size_t inlen) { fr_test_point_proto_encode_t *tp = NULL; @@ -1792,7 +1792,7 @@ static size_t command_encode_proto(command_result_t *result, command_file_ctx_t } if (fr_pair_list_afrom_str(cc->tmp_ctx, cc->tmpl_rules.dict_def ? cc->tmpl_rules.dict_def : cc->config->dict, - p, &head) != T_EOL) { + p, in + inlen - p, &head) != T_EOL) { CLEAR_TEST_POINT(cc); RETURN_OK_WITH_ERROR(); } diff --git a/src/lib/server/exec.c b/src/lib/server/exec.c index 61d1de9ef42..3a4e27ae9f8 100644 --- a/src/lib/server/exec.c +++ b/src/lib/server/exec.c @@ -610,7 +610,7 @@ int radius_exec_program(TALLOC_CTX *ctx, char *out, size_t outlen, fr_pair_list_ answer[--len] = '\0'; } - if (fr_pair_list_afrom_str(ctx, request->dict, answer, &vps) == T_INVALID) { + if (fr_pair_list_afrom_str(ctx, request->dict, answer, sizeof(answer), &vps) == T_INVALID) { RPERROR("Failed parsing output from: %s", cmd); if (out) strlcpy(out, answer, len); ret = -1; diff --git a/src/lib/util/pair.c b/src/lib/util/pair.c index 05ef888810c..b71fc366efa 100644 --- a/src/lib/util/pair.c +++ b/src/lib/util/pair.c @@ -2548,7 +2548,7 @@ void fr_pair_list_afrom_box(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_t cons */ if (last_comma) *last_comma = '\0'; - if (fr_pair_list_afrom_str(ctx, dict, box->vb_strvalue, out) == T_INVALID) { + if (fr_pair_list_afrom_str(ctx, dict, box->vb_strvalue, box->vb_length, out) == T_INVALID) { return; } diff --git a/src/lib/util/pair_legacy.c b/src/lib/util/pair_legacy.c index 60fb8cfa0cb..f43701cb0f9 100644 --- a/src/lib/util/pair_legacy.c +++ b/src/lib/util/pair_legacy.c @@ -269,6 +269,7 @@ fr_pair_t *fr_pair_make(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list_t * * @param[in] ctx for talloc * @param[in] parent parent to start referencing from * @param[in] buffer to read valuepairs from. + * @param[in] end end of the buffer * @param[in] list where the parsed fr_pair_ts will be appended. * @param[in,out] token The last token we parsed * @param[in] depth the nesting depth for FR_TYPE_GROUP @@ -277,7 +278,7 @@ fr_pair_t *fr_pair_make(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list_t * * - <= 0 on failure. * - The number of bytes of name consumed on success. */ -static ssize_t fr_pair_list_afrom_substr(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, char const *buffer, +static ssize_t fr_pair_list_afrom_substr(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, char const *buffer, char const *end, fr_pair_list_t *list, fr_token_t *token, int depth, fr_pair_t **relative_vp) { fr_pair_list_t tmp_list; @@ -342,7 +343,7 @@ static ssize_t fr_pair_list_afrom_substr(TALLOC_CTX *ctx, fr_dict_attr_t const * } slen = fr_dict_attr_by_oid_substr(NULL, &da, (*relative_vp)->da, - &FR_SBUFF_IN(p, strlen(p)), &bareword_terminals); + &FR_SBUFF_IN(p, (end - p)), &bareword_terminals); if (slen <= 0) goto error; my_list = &(*relative_vp)->vp_group; @@ -352,15 +353,15 @@ static ssize_t fr_pair_list_afrom_substr(TALLOC_CTX *ctx, fr_dict_attr_t const * * Parse the name. */ slen = fr_dict_attr_by_oid_substr(NULL, &da, parent, - &FR_SBUFF_IN(p, strlen(p)), &bareword_terminals); + &FR_SBUFF_IN(p, (end - p)), &bareword_terminals); if ((slen <= 0) && internal) { slen = fr_dict_attr_by_oid_substr(NULL, &da, internal, - &FR_SBUFF_IN(p, strlen(p)), &bareword_terminals); + &FR_SBUFF_IN(p, (end - p)), &bareword_terminals); } if (slen <= 0) { do_unknown: slen = fr_dict_unknown_afrom_oid_substr(ctx, NULL, &da_unknown, parent, - &FR_SBUFF_IN(p, strlen(p)), &bareword_terminals); + &FR_SBUFF_IN(p, (end - p)), &bareword_terminals); if (slen <= 0) { p += -slen; @@ -430,7 +431,7 @@ static ssize_t fr_pair_list_afrom_substr(TALLOC_CTX *ctx, fr_dict_attr_t const * * other, and not to our parent relative VP. */ my_relative_vp = NULL; - slen = fr_pair_list_afrom_substr(vp, vp->da, p, &vp->vp_group, &last_token, depth + 1, &my_relative_vp); + slen = fr_pair_list_afrom_substr(vp, vp->da, p, end, &vp->vp_group, &last_token, depth + 1, &my_relative_vp); if (slen <= 0) { talloc_free(vp); goto error; @@ -604,15 +605,16 @@ static ssize_t fr_pair_list_afrom_substr(TALLOC_CTX *ctx, fr_dict_attr_t const * * @param[in] ctx for talloc * @param[in] dict to resolve attributes in. * @param[in] buffer to read valuepairs from. + * @param[in] len length of the buffer * @param[in] list where the parsed fr_pair_ts will be appended. * @return the last token parsed, or #T_INVALID */ -fr_token_t fr_pair_list_afrom_str(TALLOC_CTX *ctx, fr_dict_t const *dict, char const *buffer, fr_pair_list_t *list) +fr_token_t fr_pair_list_afrom_str(TALLOC_CTX *ctx, fr_dict_t const *dict, char const *buffer, size_t len, fr_pair_list_t *list) { fr_token_t token; fr_pair_t *relative_vp = NULL; - (void) fr_pair_list_afrom_substr(ctx, fr_dict_root(dict), buffer, list, &token, 0, &relative_vp); + (void) fr_pair_list_afrom_substr(ctx, fr_dict_root(dict), buffer, buffer + len, list, &token, 0, &relative_vp); return token; } @@ -664,7 +666,7 @@ int fr_pair_list_afrom_file(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list /* * Call our internal function, instead of the public wrapper. */ - if (fr_pair_list_afrom_substr(ctx, fr_dict_root(dict), buf, &tmp_list, &last_token, 0, &relative_vp) < 0) { + if (fr_pair_list_afrom_substr(ctx, fr_dict_root(dict), buf, buf + sizeof(buf), &tmp_list, &last_token, 0, &relative_vp) < 0) { goto fail; } diff --git a/src/lib/util/pair_legacy.h b/src/lib/util/pair_legacy.h index 6db7437b992..db758a0aab2 100644 --- a/src/lib/util/pair_legacy.h +++ b/src/lib/util/pair_legacy.h @@ -40,7 +40,7 @@ fr_pair_t *fr_pair_make(TALLOC_CTX *ctx, fr_dict_t const *dict, int fr_pair_mark_xlat(fr_pair_t *vp, char const *value); fr_token_t fr_pair_list_afrom_str(TALLOC_CTX *ctx, fr_dict_t const *dict, - char const *buffer, fr_pair_list_t *head); + char const *buffer, size_t len, fr_pair_list_t *head); int fr_pair_list_afrom_file(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list_t *out, FILE *fp, bool *pfiledone); diff --git a/src/lib/util/pair_legacy_tests.c b/src/lib/util/pair_legacy_tests.c index 16fe3e80582..5367cceb7e2 100644 --- a/src/lib/util/pair_legacy_tests.c +++ b/src/lib/util/pair_legacy_tests.c @@ -249,7 +249,7 @@ static void test_fr_pair_list_afrom_str(void) fr_pair_list_init(&list); TEST_CASE("Create 'vp' using fr_pair_list_afrom_str()"); - TEST_CHECK(fr_pair_list_afrom_str(autofree, dict_test, buffer, &list) == T_EOL); + TEST_CHECK(fr_pair_list_afrom_str(autofree, dict_test, buffer, sizeof(buffer) - 1, &list) == T_EOL); TEST_CASE("Looking for Test-Integer"); TEST_CHECK((vp = fr_pair_find_by_da(&list, attr_test_integer)) != NULL); diff --git a/src/listen/detail/proto_detail.c b/src/listen/detail/proto_detail.c index cb297282dac..c4f00787ddd 100644 --- a/src/listen/detail/proto_detail.c +++ b/src/listen/detail/proto_detail.c @@ -295,7 +295,7 @@ static int mod_decode(void const *instance, request_t *request, uint8_t *const d * Ensure temporary list is empty before each use */ fr_pair_list_free(&tmp_list); - if ((fr_pair_list_afrom_str(request->request_ctx, request->dict, (char const *) p, &tmp_list) > 0) && !fr_pair_list_empty(&tmp_list)) { + if ((fr_pair_list_afrom_str(request->request_ctx, request->dict, (char const *) p, (data + data_len) - p, &tmp_list) > 0) && !fr_pair_list_empty(&tmp_list)) { vp = fr_pair_list_head(&tmp_list); fr_pair_list_append(&request->request_pairs, &tmp_list); } else {